C++ api add Rescale

pull/7235/head
shenwei41 5 years ago
parent b80fa15918
commit 87b108f4e0

@ -39,6 +39,7 @@
#include "minddata/dataset/kernels/image/random_sharpness_op.h" #include "minddata/dataset/kernels/image/random_sharpness_op.h"
#include "minddata/dataset/kernels/image/random_solarize_op.h" #include "minddata/dataset/kernels/image/random_solarize_op.h"
#include "minddata/dataset/kernels/image/random_vertical_flip_op.h" #include "minddata/dataset/kernels/image/random_vertical_flip_op.h"
#include "minddata/dataset/kernels/image/rescale_op.h"
#include "minddata/dataset/kernels/image/resize_op.h" #include "minddata/dataset/kernels/image/resize_op.h"
#include "minddata/dataset/kernels/image/rgba_to_bgr_op.h" #include "minddata/dataset/kernels/image/rgba_to_bgr_op.h"
#include "minddata/dataset/kernels/image/rgba_to_rgb_op.h" #include "minddata/dataset/kernels/image/rgba_to_rgb_op.h"
@ -277,6 +278,16 @@ std::shared_ptr<RandomVerticalFlipOperation> RandomVerticalFlip(float prob) {
return op; return op;
} }
// Function to create RescaleOperation.
std::shared_ptr<RescaleOperation> Rescale(float rescale, float shift) {
auto op = std::make_shared<RescaleOperation>(rescale, shift);
// Input validation
if (!op->ValidateParams()) {
return nullptr;
}
return op;
}
// Function to create ResizeOperation. // Function to create ResizeOperation.
std::shared_ptr<ResizeOperation> Resize(std::vector<int32_t> size, InterpolationMode interpolation) { std::shared_ptr<ResizeOperation> Resize(std::vector<int32_t> size, InterpolationMode interpolation) {
auto op = std::make_shared<ResizeOperation>(size, interpolation); auto op = std::make_shared<ResizeOperation>(size, interpolation);
@ -977,6 +988,22 @@ std::shared_ptr<TensorOp> RandomVerticalFlipOperation::Build() {
return tensor_op; return tensor_op;
} }
// RescaleOperation
RescaleOperation::RescaleOperation(float rescale, float shift) : rescale_(rescale), shift_(shift) {}
bool RescaleOperation::ValidateParams() {
if (rescale_ < 0) {
MS_LOG(ERROR) << "Rescale: rescale must be greater than or equal to 0, got: rescale = " << rescale_;
return false;
}
return true;
}
std::shared_ptr<TensorOp> RescaleOperation::Build() {
std::shared_ptr<RescaleOp> tensor_op = std::make_shared<RescaleOp>(rescale_, shift_);
return tensor_op;
}
// ResizeOperation // ResizeOperation
ResizeOperation::ResizeOperation(std::vector<int32_t> size, InterpolationMode interpolation) ResizeOperation::ResizeOperation(std::vector<int32_t> size, InterpolationMode interpolation)
: size_(size), interpolation_(interpolation) {} : size_(size), interpolation_(interpolation) {}

@ -50,6 +50,7 @@ class RandomRotationOperation;
class RandomSharpnessOperation; class RandomSharpnessOperation;
class RandomSolarizeOperation; class RandomSolarizeOperation;
class RandomVerticalFlipOperation; class RandomVerticalFlipOperation;
class RescaleOperation;
class ResizeOperation; class ResizeOperation;
class RgbaToBgrOperation; class RgbaToBgrOperation;
class RgbaToRgbOperation; class RgbaToRgbOperation;
@ -257,6 +258,13 @@ std::shared_ptr<RandomSolarizeOperation> RandomSolarize(std::vector<uint8_t> thr
/// \return Shared pointer to the current TensorOperation. /// \return Shared pointer to the current TensorOperation.
std::shared_ptr<RandomVerticalFlipOperation> RandomVerticalFlip(float prob = 0.5); std::shared_ptr<RandomVerticalFlipOperation> RandomVerticalFlip(float prob = 0.5);
/// \brief Function to create a RescaleOperation TensorOperation.
/// \notes Tensor operation to rescale the input image.
/// \param[in] rescale Rescale factor.
/// \param[in] shift Shift factor.
/// \return Shared pointer to the current TensorOperation.
std::shared_ptr<RescaleOperation> Rescale(float rescale, float shift);
/// \brief Function to create a Resize TensorOperation. /// \brief Function to create a Resize TensorOperation.
/// \notes Resize the input image to the given size. /// \notes Resize the input image to the given size.
/// \param[in] size - a vector representing the output size of the resized image. /// \param[in] size - a vector representing the output size of the resized image.
@ -606,6 +614,21 @@ class RandomVerticalFlipOperation : public TensorOperation {
float probability_; float probability_;
}; };
class RescaleOperation : public TensorOperation {
public:
explicit RescaleOperation(float rescale, float shift);
~RescaleOperation() = default;
std::shared_ptr<TensorOp> Build() override;
bool ValidateParams() override;
private:
float rescale_;
float shift_;
};
class ResizeOperation : public TensorOperation { class ResizeOperation : public TensorOperation {
public: public:
explicit ResizeOperation(std::vector<int32_t> size, explicit ResizeOperation(std::vector<int32_t> size,

@ -1427,3 +1427,93 @@ TEST_F(MindDataTestPipeline, TestRandomCropDecodeResizeFail) {
mindspore::dataset::InterpolationMode::kLinear, 0); mindspore::dataset::InterpolationMode::kLinear, 0);
EXPECT_EQ(random_crop_decode_resize_6, nullptr); EXPECT_EQ(random_crop_decode_resize_6, nullptr);
} }
TEST_F(MindDataTestPipeline, TestRescaleSucess1) {
MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRescaleSucess1.";
// Create an ImageFolder Dataset
std::string folder_path = datasets_root_path_ + "/testPK/data/";
std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, SequentialSampler(0, 1));
EXPECT_NE(ds, nullptr);
// Create an iterator over the result of the above dataset
// This will trigger the creation of the Execution Tree and launch it.
std::shared_ptr<Iterator> iter = ds->CreateIterator();
EXPECT_NE(iter, nullptr);
// Iterate the dataset and get each row
std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
iter->GetNextRow(&row);
auto image = row["image"];
// Create objects for the tensor ops
std::shared_ptr<TensorOperation> rescale = mindspore::dataset::api::vision::Rescale(1.0, 0.0);
EXPECT_NE(rescale, nullptr);
// Convert to the same type
std::shared_ptr<TensorOperation> type_cast = transforms::TypeCast("uint8");
EXPECT_NE(type_cast, nullptr);
ds = ds->Map({rescale, type_cast}, {"image"});
EXPECT_NE(ds, nullptr);
// Create an iterator over the result of the above dataset
// This will trigger the creation of the Execution Tree and launch it.
std::shared_ptr<Iterator> iter1 = ds->CreateIterator();
EXPECT_NE(iter1, nullptr);
// Iterate the dataset and get each row1
std::unordered_map<std::string, std::shared_ptr<Tensor>> row1;
iter1->GetNextRow(&row1);
auto image1 = row1["image"];
EXPECT_EQ(*image, *image1);
// Manually terminate the pipeline
iter1->Stop();
}
TEST_F(MindDataTestPipeline, TestRescaleSucess2) {
MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRescaleSucess2 with different params.";
// Create an ImageFolder Dataset
std::string folder_path = datasets_root_path_ + "/testPK/data/";
std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 1));
EXPECT_NE(ds, nullptr);
// Create objects for the tensor ops
std::shared_ptr<TensorOperation> rescale = mindspore::dataset::api::vision::Rescale(1.0 / 255, 1.0);
EXPECT_NE(rescale, nullptr);
ds = ds->Map({rescale}, {"image"});
EXPECT_NE(ds, nullptr);
// Create an iterator over the result of the above dataset
// This will trigger the creation of the Execution Tree and launch it.
std::shared_ptr<Iterator> iter = ds->CreateIterator();
EXPECT_NE(iter, nullptr);
// Iterate the dataset and get each row
std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
iter->GetNextRow(&row);
uint64_t i = 0;
while (row.size() != 0) {
i++;
auto image = row["image"];
MS_LOG(INFO) << "Tensor image shape: " << image->shape();
iter->GetNextRow(&row);
}
EXPECT_EQ(i, 1);
// Manually terminate the pipeline
iter->Stop();
}
TEST_F(MindDataTestPipeline, TestRescaleFail) {
MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRescaleSucess3 with invalid params.";
// incorrect negative rescale parameter
std::shared_ptr<TensorOperation> rescale = mindspore::dataset::api::vision::Rescale(-1.0, 0.0);
EXPECT_EQ(rescale, nullptr);
}
Loading…
Cancel
Save