diff --git a/mindspore/ccsrc/minddata/dataset/api/transforms.cc b/mindspore/ccsrc/minddata/dataset/api/transforms.cc index d46ec291a8..906b510296 100644 --- a/mindspore/ccsrc/minddata/dataset/api/transforms.cc +++ b/mindspore/ccsrc/minddata/dataset/api/transforms.cc @@ -321,6 +321,16 @@ std::shared_ptr UniformAugment(std::vector &size) { + for (int i = 0; i < size.size(); ++i) { + if (size[i] <= 0) return false; + } + return true; +} + +bool CmpFloat(const float &a, const float &b, float epsilon = 0.0000000001f) { return (std::fabs(a - b) < epsilon); } + /* ####################################### Derived TensorOperation classes ################################# */ // CenterCropOperation @@ -331,6 +341,13 @@ bool CenterCropOperation::ValidateParams() { MS_LOG(ERROR) << "CenterCrop: size vector has incorrect size."; return false; } + // We have to limit crop size due to library restrictions, optimized to only iterate over size_ once + for (int i = 0; i < size_.size(); ++i) { + if (size_[i] <= 0 || size_[i] == INT_MAX) { + MS_LOG(ERROR) << "Crop: invalid size, size must be greater than zero, got: " << size_[i]; + return false; + } + } return true; } @@ -353,14 +370,22 @@ CropOperation::CropOperation(std::vector coordinates, std::vector 2) { - MS_LOG(ERROR) << "Crop: coordinates must be a vector of one or two values"; + if (coordinates_.size() != 2) { + MS_LOG(ERROR) << "Crop: coordinates must be a vector of two values"; return false; } + // we don't check the coordinates here because we don't have access to image dimensions if (size_.empty() || size_.size() > 2) { MS_LOG(ERROR) << "Crop: size must be a vector of one or two values"; return false; } + // We have to limit crop size due to library restrictions, optimized to only iterate over size_ once + for (int i = 0; i < size_.size(); ++i) { + if (size_[i] <= 0 || size_[i] == INT_MAX) { + MS_LOG(ERROR) << "Crop: invalid size, size must be greater than zero, got: " << size_[i]; + return false; + } + } return true; } @@ -452,12 +477,24 @@ bool NormalizeOperation::ValidateParams() { MS_LOG(ERROR) << "Normalize: mean vector has incorrect size: " << mean_.size(); return false; } - + // check mean value + for (int i = 0; i < mean_.size(); ++i) { + if (mean_[i] < 0.0f || mean_[i] > 255.0f || CmpFloat(mean_[i], 0.0f)) { + MS_LOG(ERROR) << "Normalize: mean vector has incorrect value: " << mean_[i]; + return false; + } + } if (std_.size() != 3) { MS_LOG(ERROR) << "Normalize: std vector has incorrect size: " << std_.size(); return false; } - + // check std value + for (int i = 0; i < std_.size(); ++i) { + if (std_[i] < 0.0f || mean_[i] > 255.0f || CmpFloat(std_[i], 0.0f)) { + MS_LOG(ERROR) << "Normalize: std vector has incorrect value: " << std_[i]; + return false; + } + } return true; } @@ -886,6 +923,9 @@ bool ResizeOperation::ValidateParams() { MS_LOG(ERROR) << "Resize: size vector has incorrect size: " << size_.size(); return false; } + if (!CheckVectorPositive(size_)) { + return false; + } return true; } diff --git a/tests/ut/cpp/dataset/c_api_transforms_test.cc b/tests/ut/cpp/dataset/c_api_transforms_test.cc index 97873efeb6..16bcb39887 100644 --- a/tests/ut/cpp/dataset/c_api_transforms_test.cc +++ b/tests/ut/cpp/dataset/c_api_transforms_test.cc @@ -1262,3 +1262,53 @@ TEST_F(MindDataTestPipeline, TestRandomSolarizeFail) { random_solarize = mindspore::dataset::api::vision::RandomSolarize(threshold); EXPECT_EQ(random_solarize, nullptr); } + +TEST_F(MindDataTestPipeline, TestResizeFail) { + MS_LOG(INFO) << "Doing MindDataTestPipeline-TestResize with invalid params."; + // negative resize value + std::shared_ptr resize = mindspore::dataset::api::vision::Resize({30, -30}); + EXPECT_EQ(resize, nullptr); + // zero resize value + resize = mindspore::dataset::api::vision::Resize({0, 30}); + EXPECT_EQ(resize, nullptr); +} + +TEST_F(MindDataTestPipeline, TestCropFail) { + MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCrop with invalid params."; + // wrong width + std::shared_ptr crop = mindspore::dataset::api::vision::Crop({0, 0}, {32, -32}); + EXPECT_EQ(crop, nullptr); + // wrong height + crop = mindspore::dataset::api::vision::Crop({0, 0}, {-32, -32}); + EXPECT_EQ(crop, nullptr); + // zero height + crop = mindspore::dataset::api::vision::Crop({0, 0}, {0, 32}); + EXPECT_EQ(crop, nullptr); +} + +TEST_F(MindDataTestPipeline, TestCenterCropFail) { + MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCenterCrop with invalid params."; + // center crop height value negative + std::shared_ptr center_crop = mindspore::dataset::api::vision::CenterCrop({-32, 32}); + EXPECT_EQ(center_crop, nullptr); + // center crop width value negative + center_crop = mindspore::dataset::api::vision::CenterCrop({32, -32}); + EXPECT_EQ(center_crop, nullptr); + // 0 value would result in nullptr + center_crop = mindspore::dataset::api::vision::CenterCrop({0, 32}); + EXPECT_EQ(center_crop, nullptr); +} + +TEST_F(MindDataTestPipeline, TestNormalizeFail) { + MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalize with invalid params."; + // mean value 0.0 + std::shared_ptr normalize = mindspore::dataset::api::vision::Normalize({0.0, 115.0, 100.0}, + {70.0, 68.0, 71.0}); + EXPECT_EQ(normalize, nullptr); + // std value at 0.0 + normalize = mindspore::dataset::api::vision::Normalize({121.0, 115.0, 100.0}, {0.0, 68.0, 71.0}); + EXPECT_EQ(normalize, nullptr); + // mean value 300.0 greater than 255.0 + normalize = mindspore::dataset::api::vision::Normalize({300.0, 115.0, 100.0}, {70.0, 68.0, 71.0}); + EXPECT_EQ(normalize, nullptr); +}