diff --git a/mindspore/ccsrc/minddata/dataset/api/transforms.cc b/mindspore/ccsrc/minddata/dataset/api/transforms.cc index 093559776c..1be01ecab8 100644 --- a/mindspore/ccsrc/minddata/dataset/api/transforms.cc +++ b/mindspore/ccsrc/minddata/dataset/api/transforms.cc @@ -31,85 +31,169 @@ namespace mindspore { namespace dataset { /* ####################################### Validator Functions ############################################ */ -Status ValidateVectorFillvalue(const std::string &transform_name, const std::vector &fill_value) { +Status ValidateProbability(const std::string &op_name, const float probability) { + if (probability < 0.0 || probability > 1.0) { + std::string err_msg = op_name + ": probability must be between 0.0 and 1.0, got: " + std::to_string(probability); + MS_LOG(ERROR) << err_msg; + RETURN_STATUS_SYNTAX_ERROR(err_msg); + } + + return Status::OK(); +} + +Status ValidateIntScalarPositive(const std::string &op_name, const std::string &scalar_name, int32_t scalar) { + RETURN_IF_NOT_OK(ValidateScalar(op_name, scalar_name, scalar, {0}, true)); + return Status::OK(); +} + +Status ValidateFloatScalarPositive(const std::string &op_name, const std::string &scalar_name, float scalar) { + RETURN_IF_NOT_OK(ValidateScalar(op_name, scalar_name, scalar, {0}, true)); + return Status::OK(); +} + +Status ValidateVectorFillvalue(const std::string &op_name, const std::vector &fill_value) { if (fill_value.empty() || (fill_value.size() != 1 && fill_value.size() != 3)) { std::string err_msg = - transform_name + ": fill_value vector has incorrect size: " + std::to_string(fill_value.size()); + op_name + ": fill_value expecting size 1 or 3, got fill_value.size(): " + std::to_string(fill_value.size()); MS_LOG(ERROR) << err_msg; RETURN_STATUS_SYNTAX_ERROR(err_msg); } - for (uint8_t single_fill_value : fill_value) { - if (single_fill_value > 255) { - std::string err_msg = - transform_name + ": fill_value has to be between 0 and 255, got:" + std::to_string(single_fill_value); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } + // Note that fill_value need to be in range [0, 255], + // but we omit the check since its type is uint8_t + return Status::OK(); +} + +Status ValidateVectorColorAttribute(const std::string &op_name, const std::string &attr_name, + const std::vector &attr, const std::vector &range) { + if (attr.empty() || attr.size() > 2) { + std::string err_msg = op_name + ":" + attr_name + " expecting size 1 or 2, but got: " + std::to_string(attr.size()); + MS_LOG(ERROR) << err_msg; + RETURN_STATUS_SYNTAX_ERROR(err_msg); + } + for (auto &attr_val : attr) { + RETURN_IF_NOT_OK(ValidateScalar(op_name, attr_name, attr_val, range, false, false)); + } + if (attr.size() == 2 && (attr[0] > attr[1])) { + std::string err_msg = op_name + ":" + attr_name + + " lower bound must be less or equal to upper bound, got lb: " + std::to_string(attr[0]) + + ", ub: " + std::to_string(attr[1]); + MS_LOG(ERROR) << err_msg; + RETURN_STATUS_SYNTAX_ERROR(err_msg); } return Status::OK(); } -Status ValidateProbability(const std::string &transform_name, const float &probability) { - if (probability < 0.0 || probability > 1.0) { - std::string err_msg = - transform_name + ": probability must be between 0.0 and 1.0, got: " + std::to_string(probability); +Status ValidateVectorMeanStd(const std::string &op_name, const std::vector &mean, + const std::vector &std) { + if (mean.size() != 3) { + std::string err_msg = op_name + ": mean expecting size 3, got size: " + std::to_string(mean.size()); MS_LOG(ERROR) << err_msg; RETURN_STATUS_SYNTAX_ERROR(err_msg); } + if (std.size() != 3) { + std::string err_msg = op_name + ": std expecting size 3, got size: " + std::to_string(std.size()); + MS_LOG(ERROR) << err_msg; + RETURN_STATUS_SYNTAX_ERROR(err_msg); + } + // check std/mean value + for (int32_t i = 0; i < std.size(); ++i) { + RETURN_IF_NOT_OK(ValidateScalar(op_name, "mean", mean[i], {0.0, 255.0}, false, false)); + RETURN_IF_NOT_OK(ValidateScalar(op_name, "std", std[i], {0.0, 255.0}, true, false)); + } return Status::OK(); } -Status ValidateVectorPadding(const std::string &transform_name, const std::vector &padding) { +Status ValidateVectorPadding(const std::string &op_name, const std::vector &padding) { if (padding.empty() || padding.size() == 3 || padding.size() > 4) { - std::string err_msg = transform_name + ": padding vector has incorrect size: " + std::to_string(padding.size()); + std::string err_msg = op_name + ": padding expecting size 1, 2 or 4, got size: " + std::to_string(padding.size()); MS_LOG(ERROR) << err_msg; RETURN_STATUS_SYNTAX_ERROR(err_msg); } - for (int32_t i = 0; i < padding.size(); ++i) { - if (padding[i] < 0) { - std::string err_msg = - transform_name + - ": invalid padding, padding value must be greater than or equal to 0, got: " + std::to_string(padding[i]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (padding[i] == INT_MAX) { - std::string err_msg = - transform_name + ": invalid padding, padding value too large, got: " + std::to_string(padding[i]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } + for (const auto &pad_val : padding) { + RETURN_IF_NOT_OK(ValidateScalar(op_name, "padding", pad_val, {0, INT_MAX}, false, false)); } return Status::OK(); } -Status ValidateVectorPositive(const std::string &transform_name, const std::vector &size) { - for (int32_t i = 0; i < size.size(); ++i) { - if (size[i] <= 0) { - std::string err_msg = - transform_name + ": Non-positive size value: " + std::to_string(size[i]) + " at element: " + std::to_string(i); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } +Status ValidateVectorPositive(const std::string &op_name, const std::string &vec_name, + const std::vector &vec) { + for (const auto &vec_val : vec) { + RETURN_IF_NOT_OK(ValidateScalar(op_name, vec_name, vec_val, {0}, true)); + } + + return Status::OK(); +} + +Status ValidateVectorNonNegative(const std::string &op_name, const std::string &vec_name, + const std::vector &vec) { + for (const auto &vec_val : vec) { + RETURN_IF_NOT_OK(ValidateScalar(op_name, vec_name, vec_val, {0}, false)); + } + + return Status::OK(); +} + +Status ValidateVectorSize(const std::string &op_name, const std::vector &size) { + if (size.empty() || size.size() > 2) { + std::string err_msg = op_name + ": size expecting size 2, got size.size(): " + std::to_string(size.size()); + MS_LOG(ERROR) << err_msg; + RETURN_STATUS_SYNTAX_ERROR(err_msg); + } + for (const auto &size_val : size) { + RETURN_IF_NOT_OK(ValidateScalar(op_name, "size", size_val, {0, INT_MAX}, true, false)); + } + + return Status::OK(); +} + +Status ValidateVectorScale(const std::string &op_name, const std::vector &scale) { + if (scale.size() != 2) { + std::string err_msg = op_name + ": scale expecting size 2, got scale.size(): " + std::to_string(scale.size()); + MS_LOG(ERROR) << err_msg; + RETURN_STATUS_SYNTAX_ERROR(err_msg); + } + RETURN_IF_NOT_OK(ValidateScalar(op_name, "scale", scale[0], {0}, false)); + RETURN_IF_NOT_OK(ValidateScalar(op_name, "scale", scale[1], {0}, true)); + if (scale[1] < scale[0]) { + std::string err_msg = op_name + ": scale must be in the format of (min, max)."; + MS_LOG(ERROR) << op_name + ": scale must be in the format of (min, max), but got: " << scale; + RETURN_STATUS_SYNTAX_ERROR(err_msg); + } + + return Status::OK(); +} + +Status ValidateVectorRatio(const std::string &op_name, const std::vector &ratio) { + if (ratio.size() != 2) { + std::string err_msg = op_name + ": ratio expecting size 2, got ratio.size(): " + std::to_string(ratio.size()); + MS_LOG(ERROR) << err_msg; + RETURN_STATUS_SYNTAX_ERROR(err_msg); + } + RETURN_IF_NOT_OK(ValidateScalar(op_name, "scale", ratio[0], {0}, true)); + RETURN_IF_NOT_OK(ValidateScalar(op_name, "scale", ratio[1], {0}, true)); + if (ratio[1] < ratio[0]) { + std::string err_msg = op_name + ": ratio must be in the format of (min, max)."; + MS_LOG(ERROR) << op_name + ": ratio must be in the format of (min, max), but got: " << ratio; + RETURN_STATUS_SYNTAX_ERROR(err_msg); } return Status::OK(); } -Status ValidateVectorTransforms(const std::string &transform_name, +Status ValidateVectorTransforms(const std::string &op_name, const std::vector> &transforms) { if (transforms.empty()) { - std::string err_msg = transform_name + ": transform list must not be empty."; + std::string err_msg = op_name + ": transform list must not be empty."; MS_LOG(ERROR) << err_msg; RETURN_STATUS_SYNTAX_ERROR(err_msg); } for (int32_t i = 0; i < transforms.size(); ++i) { if (transforms[i] == nullptr) { std::string err_msg = - transform_name + ": transform ops must not be null, got transform[" + std::to_string(i) + "] == nullptr."; + op_name + ": transform ops must not be null, got transform[" + std::to_string(i) + "] == nullptr."; MS_LOG(ERROR) << err_msg; RETURN_STATUS_SYNTAX_ERROR(err_msg); } @@ -118,7 +202,7 @@ Status ValidateVectorTransforms(const std::string &transform_name, return Status::OK(); } -bool CmpFloat(const float &a, const float &b, float epsilon) { return (std::fabs(a - b) < epsilon); } +bool CmpFloat(const float a, const float b, float epsilon) { return (std::fabs(a - b) < epsilon); } // Transform operations for data. namespace transforms { diff --git a/mindspore/ccsrc/minddata/dataset/api/vision.cc b/mindspore/ccsrc/minddata/dataset/api/vision.cc index 2400eb5344..7f43c44e94 100644 --- a/mindspore/ccsrc/minddata/dataset/api/vision.cc +++ b/mindspore/ccsrc/minddata/dataset/api/vision.cc @@ -492,24 +492,7 @@ std::shared_ptr BoundingBoxAugmentOperation::Build() { CenterCropOperation::CenterCropOperation(std::vector size) : size_(size) {} Status CenterCropOperation::ValidateParams() { - if (size_.empty() || size_.size() > 2) { - std::string err_msg = "CenterCrop: size vector has incorrect size."; - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - // We have to limit crop size due to library restrictions, optimized to only iterate over size_ once - for (int32_t i = 0; i < size_.size(); ++i) { - if (size_[i] <= 0) { - std::string err_msg = "CenterCrop: invalid size, size must be greater than 0, got: " + std::to_string(size_[i]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (size_[i] == INT_MAX) { - std::string err_msg = "CenterCrop: invalid size, size too large, got: " + std::to_string(size_[i]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - } + RETURN_IF_NOT_OK(ValidateVectorSize("CenterCrop", size_)); return Status::OK(); } @@ -536,39 +519,16 @@ CropOperation::CropOperation(std::vector coordinates, std::vector 2) { - std::string err_msg = "Crop: size must be a vector of one or two values"; - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - // We have to limit crop size due to library restrictions, optimized to only iterate over size_ once - for (int32_t i = 0; i < size_.size(); ++i) { - if (size_[i] <= 0) { - std::string err_msg = "Crop: invalid size, size must be greater than 0, got: " + std::to_string(size_[i]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (size_[i] == INT_MAX) { - std::string err_msg = "Crop: invalid size, size too large, got: " + std::to_string(size_[i]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - } - for (int32_t j = 0; j < coordinates_.size(); ++j) { - if (coordinates_[j] < 0) { - std::string err_msg = - "Crop: invalid coordinates, coordinates must be greater than 0, got: " + std::to_string(coordinates_[j]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - } + RETURN_IF_NOT_OK(ValidateVectorNonNegative("Crop", "coordinates", coordinates_)); return Status::OK(); } @@ -595,17 +555,8 @@ CutMixBatchOperation::CutMixBatchOperation(ImageBatchFormat image_batch_format, : image_batch_format_(image_batch_format), alpha_(alpha), prob_(prob) {} Status CutMixBatchOperation::ValidateParams() { - if (alpha_ <= 0) { - std::string err_msg = - "CutMixBatch: alpha must be a positive floating value however it is: " + std::to_string(alpha_); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (prob_ < 0 || prob_ > 1) { - std::string err_msg = "CutMixBatch: Probability has to be between 0 and 1."; - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } + RETURN_IF_NOT_OK(ValidateFloatScalarPositive("CutMixBatch", "alpha", alpha_)); + RETURN_IF_NOT_OK(ValidateProbability("CutMixBatch", prob_)); return Status::OK(); } @@ -618,16 +569,8 @@ std::shared_ptr CutMixBatchOperation::Build() { CutOutOperation::CutOutOperation(int32_t length, int32_t num_patches) : length_(length), num_patches_(num_patches) {} Status CutOutOperation::ValidateParams() { - if (length_ <= 0) { - std::string err_msg = "CutOut: length must be positive, got: " + std::to_string(length_); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (num_patches_ <= 0) { - std::string err_msg = "CutOut: number of patches must be positive, got: " + std::to_string(num_patches_); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } + RETURN_IF_NOT_OK(ValidateIntScalarPositive("CutOut", "length", length_)); + RETURN_IF_NOT_OK(ValidateIntScalarPositive("CutOut", "num_patches", num_patches_)); return Status::OK(); } @@ -667,13 +610,7 @@ std::shared_ptr InvertOperation::Build() { return std::make_shared DvppDecodeResizeCropOperation::Build() { NormalizeOperation::NormalizeOperation(std::vector mean, std::vector std) : mean_(mean), std_(std) {} Status NormalizeOperation::ValidateParams() { - if (mean_.size() != 3) { - std::string err_msg = "Normalize: mean vector has incorrect size: " + std::to_string(mean_.size()); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (std_.size() != 3) { - std::string err_msg = "Normalize: std vector has incorrect size: " + std::to_string(std_.size()); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - // check std/mean value - for (int32_t i = 0; i < std_.size(); ++i) { - if (std_[i] < 0.0f || std_[i] > 255.0f || CmpFloat(std_[i], 0.0f)) { - std::string err_msg = "Normalize: std vector has incorrect value: " + std::to_string(std_[i]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (mean_[i] < 0.0f || mean_[i] > 255.0f) { - std::string err_msg = "Normalize: mean vector has incorrect value: " + std::to_string(mean_[i]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - } + RETURN_IF_NOT_OK(ValidateVectorMeanStd("Normalize", mean_, std_)); return Status::OK(); } @@ -826,29 +741,7 @@ NormalizePadOperation::NormalizePadOperation(const std::vector &mean, con : mean_(mean), std_(std), dtype_(dtype) {} Status NormalizePadOperation::ValidateParams() { - if (mean_.size() != 3) { - std::string err_msg = "NormalizePad: mean vector has incorrect size: " + std::to_string(mean_.size()); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (std_.size() != 3) { - std::string err_msg = "NormalizePad: std vector has incorrect size: " + std::to_string(std_.size()); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - // check std/mean value - for (int32_t i = 0; i < std_.size(); ++i) { - if (std_[i] < 0.0f || std_[i] > 255.0f || CmpFloat(std_[i], 0.0f)) { - std::string err_msg = "NormalizePad: std vector has incorrect value: " + std::to_string(std_[i]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (mean_[i] < 0.0f || mean_[i] > 255.0f) { - std::string err_msg = "NormalizePad: mean vector has incorrect value: " + std::to_string(mean_[i]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - } + RETURN_IF_NOT_OK(ValidateVectorMeanStd("NormalizePad", mean_, std_)); if (dtype_ != "float32" && dtype_ != "float16") { std::string err_msg = "NormalizePad: dtype must be float32 or float16, but got: " + dtype_; MS_LOG(ERROR) << err_msg; @@ -963,18 +856,8 @@ Status RandomAffineOperation::ValidateParams() { MS_LOG(ERROR) << err_msg; RETURN_STATUS_SYNTAX_ERROR(err_msg); } - if (translate_range_[0] < -1 || translate_range_[0] > 1) { - std::string err_msg = "RandomAffine: minimum of translate range on x is out of range of [-1, 1], value = " + - std::to_string(translate_range_[0]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (translate_range_[1] < -1 || translate_range_[1] > 1) { - std::string err_msg = "RandomAffine: maximum of translate range on x is out of range of [-1, 1], value = " + - std::to_string(translate_range_[1]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } + RETURN_IF_NOT_OK(ValidateScalar("RandomAffine", "translate", translate_range_[0], {-1, 1}, false, false)); + RETURN_IF_NOT_OK(ValidateScalar("RandomAffine", "translate", translate_range_[1], {-1, 1}, false, false)); if (translate_range_.size() == 4) { if (translate_range_[2] > translate_range_[3]) { std::string err_msg = "RandomAffine: minimum of translate range on y is greater than maximum: min = " + @@ -982,45 +865,11 @@ Status RandomAffineOperation::ValidateParams() { MS_LOG(ERROR) << err_msg; RETURN_STATUS_SYNTAX_ERROR(err_msg); } - if (translate_range_[2] < -1 || translate_range_[2] > 1) { - std::string err_msg = "RandomAffine: minimum of translate range on y is out of range of [-1, 1], value = " + - std::to_string(translate_range_[2]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (translate_range_[3] < -1 || translate_range_[3] > 1) { - std::string err_msg = "RandomAffine: maximum of translate range on y is out of range of [-1, 1], value = " + - std::to_string(translate_range_[3]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } + RETURN_IF_NOT_OK(ValidateScalar("RandomAffine", "translate", translate_range_[2], {-1, 1}, false, false)); + RETURN_IF_NOT_OK(ValidateScalar("RandomAffine", "translate", translate_range_[3], {-1, 1}, false, false)); } // Scale - if (scale_range_.size() != 2) { - std::string err_msg = "RandomAffine: scale_range vector has incorrect size: scale_range.size() = " + - std::to_string(scale_range_.size()); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (scale_range_[0] < 0) { - std::string err_msg = - "RandomAffine: min scale range must be greater than or equal to 0, got: " + std::to_string(scale_range_[0]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (scale_range_[1] <= 0) { - std::string err_msg = - "RandomAffine: max scale range must be greater than 0, got: " + std::to_string(scale_range_[1]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (scale_range_[0] > scale_range_[1]) { - std::string err_msg = - "RandomAffine: minimum of scale range is greater than maximum: min = " + std::to_string(scale_range_[0]) + - ", max = " + std::to_string(scale_range_[1]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } + RETURN_IF_NOT_OK(ValidateVectorScale("RandomAffine", scale_range_)); // Shear if (shear_ranges_.size() != 2 && shear_ranges_.size() != 4) { std::string err_msg = "RandomAffine: shear_ranges expecting size 2 or 4, got: shear_ranges.size() = " + @@ -1041,20 +890,7 @@ Status RandomAffineOperation::ValidateParams() { RETURN_STATUS_SYNTAX_ERROR(err_msg); } // Fill Value - if (fill_value_.size() != 3) { - std::string err_msg = - "RandomAffine: fill_value vector has incorrect size: fill_value.size() = " + std::to_string(fill_value_.size()); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - for (int32_t i = 0; i < fill_value_.size(); ++i) { - if (fill_value_[i] < 0 || fill_value_[i] > 255) { - std::string err_msg = - "RandomAffine: fill_value has to be between 0 and 255, got:" + std::to_string(fill_value_[i]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - } + RETURN_IF_NOT_OK(ValidateVectorFillvalue("RandomAffine", fill_value_)); return Status::OK(); } @@ -1074,7 +910,6 @@ std::shared_ptr RandomAffineOperation::Build() { RandomColorOperation::RandomColorOperation(float t_lb, float t_ub) : t_lb_(t_lb), t_ub_(t_ub) { random_op_ = true; } Status RandomColorOperation::ValidateParams() { - // Do some input validation. if (t_lb_ < 0 || t_ub_ < 0) { std::string err_msg = "RandomColor: lower bound or upper bound must be greater than or equal to 0, got t_lb: " + std::to_string(t_lb_) + @@ -1101,89 +936,13 @@ RandomColorAdjustOperation::RandomColorAdjustOperation(std::vector bright Status RandomColorAdjustOperation::ValidateParams() { // brightness - if (brightness_.empty() || brightness_.size() > 2) { - std::string err_msg = - "RandomColorAdjust: brightness must be a vector of one or two values, got: " + std::to_string(brightness_.size()); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - for (int32_t i = 0; i < brightness_.size(); ++i) { - if (brightness_[i] < 0) { - std::string err_msg = - "RandomColorAdjust: brightness must be greater than or equal to 0, got: " + std::to_string(brightness_[i]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - } - if (brightness_.size() == 2 && (brightness_[0] > brightness_[1])) { - std::string err_msg = "RandomColorAdjust: brightness lower bound must be less or equal to upper bound, got lb: " + - std::to_string(brightness_[0]) + ", ub: " + std::to_string(brightness_[1]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } + RETURN_IF_NOT_OK(ValidateVectorColorAttribute("RandomColorAdjust", "brightness", brightness_, {0})); // contrast - if (contrast_.empty() || contrast_.size() > 2) { - std::string err_msg = - "RandomColorAdjust: contrast must be a vector of one or two values, got: " + std::to_string(contrast_.size()); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - for (int32_t i = 0; i < contrast_.size(); ++i) { - if (contrast_[i] < 0) { - std::string err_msg = - "RandomColorAdjust: contrast must be greater than or equal to 0, got: " + std::to_string(contrast_[i]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - } - if (contrast_.size() == 2 && (contrast_[0] > contrast_[1])) { - std::string err_msg = "RandomColorAdjust: contrast lower bound must be less or equal to upper bound, got lb: " + - std::to_string(contrast_[0]) + ", ub: " + std::to_string(contrast_[1]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } + RETURN_IF_NOT_OK(ValidateVectorColorAttribute("RandomColorAdjust", "contrast", contrast_, {0})); // saturation - if (saturation_.empty() || saturation_.size() > 2) { - std::string err_msg = - "RandomColorAdjust: saturation must be a vector of one or two values, got: " + std::to_string(saturation_.size()); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - for (int32_t i = 0; i < saturation_.size(); ++i) { - if (saturation_[i] < 0) { - std::string err_msg = - "RandomColorAdjust: saturation must be greater than or equal to 0, got: " + std::to_string(saturation_[i]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - } - if (saturation_.size() == 2 && (saturation_[0] > saturation_[1])) { - std::string err_msg = "RandomColorAdjust: saturation lower bound must be less or equal to upper bound, got lb: " + - std::to_string(saturation_[0]) + ", ub: " + std::to_string(saturation_[1]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } + RETURN_IF_NOT_OK(ValidateVectorColorAttribute("RandomColorAdjust", "saturation", saturation_, {0})); // hue - if (hue_.empty() || hue_.size() > 2) { - std::string err_msg = - "RandomColorAdjust: hue must be a vector of one or two values, got: " + std::to_string(hue_.size()); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - for (int32_t i = 0; i < hue_.size(); ++i) { - if (hue_[i] < -0.5 || hue_[i] > 0.5) { - std::string err_msg = "RandomColorAdjust: hue has to be between -0.5 and 0.5, got: " + std::to_string(hue_[i]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - } - if (hue_.size() == 2 && (hue_[0] > hue_[1])) { - std::string err_msg = - "RandomColorAdjust: hue lower bound must be less or equal to upper bound, got lb: " + std::to_string(hue_[0]) + - ", ub: " + std::to_string(hue_[1]); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } + RETURN_IF_NOT_OK(ValidateVectorColorAttribute("RandomColorAdjust", "hue", hue_, {-0.5, 0.5})); return Status::OK(); } @@ -1239,12 +998,7 @@ RandomCropOperation::RandomCropOperation(std::vector size, std::vector< Status RandomCropOperation::ValidateParams() { // size - if (size_.empty() || size_.size() > 2) { - std::string err_msg = "RandomCrop: size must be a vector of one or two values"; - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - RETURN_IF_NOT_OK(ValidateVectorPositive("RandomCrop", size_)); + RETURN_IF_NOT_OK(ValidateVectorSize("RandomCrop", size_)); // padding RETURN_IF_NOT_OK(ValidateVectorPadding("RandomCrop", padding_)); // fill_value @@ -1352,12 +1106,7 @@ RandomCropWithBBoxOperation::RandomCropWithBBoxOperation(std::vector si Status RandomCropWithBBoxOperation::ValidateParams() { // size - if (size_.empty() || size_.size() > 2) { - std::string err_msg = "RandomCropWithBBox: size must be a vector of one or two values"; - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - RETURN_IF_NOT_OK(ValidateVectorPositive("RandomCropWithBBox", size_)); + RETURN_IF_NOT_OK(ValidateVectorSize("RandomCropWithBBox", size_)); // padding RETURN_IF_NOT_OK(ValidateVectorPadding("RandomCropWithBBox", padding_)); // fill_value @@ -1418,7 +1167,6 @@ RandomHorizontalFlipOperation::RandomHorizontalFlipOperation(float probability) Status RandomHorizontalFlipOperation::ValidateParams() { RETURN_IF_NOT_OK(ValidateProbability("RandomHorizontalFlip", probability_)); - return Status::OK(); } @@ -1433,7 +1181,6 @@ RandomHorizontalFlipWithBBoxOperation::RandomHorizontalFlipWithBBoxOperation(flo Status RandomHorizontalFlipWithBBoxOperation::ValidateParams() { RETURN_IF_NOT_OK(ValidateProbability("RandomHorizontalFlipWithBBox", probability_)); - return Status::OK(); } @@ -1482,18 +1229,7 @@ std::shared_ptr RandomPosterizeOperation::Build() { RandomResizeOperation::RandomResizeOperation(std::vector size) : TensorOperation(true), size_(size) {} Status RandomResizeOperation::ValidateParams() { - // size - if (size_.size() != 2 && size_.size() != 1) { - std::string err_msg = - "RandomResize: size must be a vector of one or two values, got: " + std::to_string(size_.size()); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (size_[0] <= 0 || (size_.size() == 2 && size_[1] <= 0)) { - std::string err_msg = "RandomResize: size must only contain positive integers."; - MS_LOG(ERROR) << "RandomResize: size must only contain positive integers, got: " << size_; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } + RETURN_IF_NOT_OK(ValidateVectorSize("RandomResize", size_)); return Status::OK(); } @@ -1517,18 +1253,7 @@ RandomResizeWithBBoxOperation::RandomResizeWithBBoxOperation(std::vector size Status RandomResizedCropOperation::ValidateParams() { // size - if (size_.size() != 2 && size_.size() != 1) { - std::string err_msg = Name() + ": size must be a vector of one or two values, got: " + std::to_string(size_.size()); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (size_[0] <= 0 || (size_.size() == 2 && size_[1] <= 0)) { - std::string err_msg = Name() + ": size must only contain positive integers."; - MS_LOG(ERROR) << Name() + ": size must only contain positive integers, got: " << size_; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } + RETURN_IF_NOT_OK(ValidateVectorSize(Name(), size_)); // scale - if (scale_.size() != 2) { - std::string err_msg = Name() + ": scale must be a vector of two values, got: " + std::to_string(scale_.size()); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (scale_[0] < 0) { - std::string err_msg = Name() + ": min scale must be greater than or equal to 0."; - MS_LOG(ERROR) << Name() + ": min scale must be greater than or equal to 0, got: " + std::to_string(scale_[0]); - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (scale_[1] <= 0) { - std::string err_msg = Name() + ": max scale must be greater than 0."; - MS_LOG(ERROR) << Name() + ": max scale must be greater than 0, got: " + std::to_string(scale_[1]); - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (scale_[1] < scale_[0]) { - std::string err_msg = Name() + ": scale must have a size of two in the format of (min, max)."; - MS_LOG(ERROR) << Name() + ": scale must have a size of two in the format of (min, max), but got: " << scale_; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } + RETURN_IF_NOT_OK(ValidateVectorScale(Name(), scale_)); // ratio - if (ratio_.size() != 2) { - std::string err_msg = Name() + ": ratio must be a vector of two values, got: " + std::to_string(ratio_.size()); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (ratio_[0] <= 0 || ratio_[1] <= 0) { - std::string err_msg = Name() + ": ratio must be greater than 0."; - MS_LOG(ERROR) << Name() + ": ratio must be greater than 0, got: " << ratio_; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (ratio_[1] < ratio_[0]) { - std::string err_msg = Name() + ": ratio must have a size of two in the format of (min, max)."; - MS_LOG(ERROR) << Name() + ": ratio must have a size of two in the format of (min, max), but got: " << ratio_; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } + RETURN_IF_NOT_OK(ValidateVectorRatio(Name(), ratio_)); // max_attempts if (max_attempts_ < 1) { std::string err_msg = @@ -1639,59 +1322,11 @@ RandomResizedCropWithBBoxOperation::RandomResizedCropWithBBoxOperation(std::vect Status RandomResizedCropWithBBoxOperation::ValidateParams() { // size - if (size_.size() != 2 && size_.size() != 1) { - std::string err_msg = - "RandomResizedCropWithBBox: size must be a vector of one or two values, got: " + std::to_string(size_.size()); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (size_[0] <= 0 || (size_.size() == 2 && size_[1] <= 0)) { - std::string err_msg = "RandomResizedCropWithBBox: size must only contain positive integers."; - MS_LOG(ERROR) << "RandomResizedCropWithBBox: size must only contain positive integers, got: " << size_; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } + RETURN_IF_NOT_OK(ValidateVectorSize("RandomResizedCropWithBBox", size_)); // scale - if (scale_.size() != 2) { - std::string err_msg = - "RandomResizedCropWithBBox: scale must be a vector of two values, got: " + std::to_string(scale_.size()); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (scale_[0] < 0) { - std::string err_msg = "RandomResizedCropWithBBox: min scale must be greater than or equal to 0."; - MS_LOG(ERROR) << "RandomResizedCropWithBBox: min scale must be greater than or equal to 0, got: " + - std::to_string(scale_[0]); - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (scale_[1] <= 0) { - std::string err_msg = "RandomResizedCropWithBBox: max scale must be greater than 0."; - MS_LOG(ERROR) << "RandomResizedCropWithBBox: max scale must be greater than 0, got: " + std::to_string(scale_[1]); - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (scale_[1] < scale_[0]) { - std::string err_msg = "RandomResizedCropWithBBox: scale must have a size of two in the format of (min, max)."; - MS_LOG(ERROR) << "RandomResizedCropWithBBox: scale must have a size of two in the format of (min, max), but got: " - << scale_; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } + RETURN_IF_NOT_OK(ValidateVectorScale("RandomResizedCropWithBBox", scale_)); // ratio - if (ratio_.size() != 2) { - std::string err_msg = - "RandomResizedCropWithBBox: ratio must be a vector of two values, got: " + std::to_string(ratio_.size()); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (ratio_[0] <= 0 || ratio_[1] <= 0) { - std::string err_msg = "RandomResizedCropWithBBox: ratio must be greater than 0."; - MS_LOG(ERROR) << "RandomResizedCropWithBBox: ratio must be greater than 0, got: " << ratio_; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (ratio_[1] < ratio_[0]) { - std::string err_msg = "RandomResizedCropWithBBox: ratio must have a size of two in the format of (min, max)."; - MS_LOG(ERROR) << "RandomResizedCropWithBBox: ratio must have a size of two in the format of (min, max), but got: " - << ratio_; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } + RETURN_IF_NOT_OK(ValidateVectorRatio("RandomResizedCropWithBBox", ratio_)); // max_attempts if (max_attempts_ < 1) { std::string err_msg = "RandomResizedCropWithBBox: max_attempts must be greater than or equal to 1, got: " + @@ -1859,7 +1494,6 @@ Status RandomSharpnessOperation::ValidateParams() { MS_LOG(ERROR) << "RandomSharpness: degrees must be in the format of (min, max), got: " << degrees_; RETURN_STATUS_SYNTAX_ERROR(err_msg); } - return Status::OK(); } @@ -1962,14 +1596,7 @@ ResizeOperation::ResizeOperation(std::vector size, InterpolationMode in : size_(size), interpolation_(interpolation) {} Status ResizeOperation::ValidateParams() { - // size - if (size_.empty() || size_.size() > 2) { - std::string err_msg = "Resize: size must be a vector of one or two values, got: " + std::to_string(size_.size()); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - RETURN_IF_NOT_OK(ValidateVectorPositive("Resize", size_)); - + RETURN_IF_NOT_OK(ValidateVectorSize("Resize", size_)); return Status::OK(); } @@ -2012,15 +1639,7 @@ ResizeWithBBoxOperation::ResizeWithBBoxOperation(std::vector size, Inte : size_(size), interpolation_(interpolation) {} Status ResizeWithBBoxOperation::ValidateParams() { - // size - if (size_.empty() || size_.size() > 2) { - std::string err_msg = - "ResizeWithBBox: size must be a vector of one or two values, got: " + std::to_string(size_.size()); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - RETURN_IF_NOT_OK(ValidateVectorPositive("Resize", size_)); - + RETURN_IF_NOT_OK(ValidateVectorSize("ResizeWithBBox", size_)); return Status::OK(); } @@ -2065,60 +1684,11 @@ SoftDvppDecodeRandomCropResizeJpegOperation::SoftDvppDecodeRandomCropResizeJpegO Status SoftDvppDecodeRandomCropResizeJpegOperation::ValidateParams() { // size - if (size_.size() != 2 && size_.size() != 1) { - std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: size must be a vector of one or two values, got: " + - std::to_string(size_.size()); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (size_[0] <= 0 || (size_.size() == 2 && size_[1] <= 0)) { - std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: size must only contain positive integers."; - MS_LOG(ERROR) << "SoftDvppDecodeRandomCropResizeJpeg: size must only contain positive integers, got: " << size_; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } + RETURN_IF_NOT_OK(ValidateVectorSize("SoftDvppDecodeRandomCropResizeJpeg", size_)); // scale - if (scale_.size() != 2) { - std::string err_msg = - "SoftDvppDecodeRandomCropResizeJpeg: scale must be a vector of two values, got: " + std::to_string(scale_.size()); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (scale_[0] < 0) { - std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: min scale must be greater than or equal to 0."; - MS_LOG(ERROR) << "SoftDvppDecodeRandomCropResizeJpeg: min scale must be greater than or equal to 0, got: " + - std::to_string(scale_[0]); - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (scale_[1] <= 0) { - std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: max scale must be greater than 0."; - MS_LOG(ERROR) << "SoftDvppDecodeRandomCropResizeJpeg: max scale must be greater than 0, got: " + - std::to_string(scale_[1]); - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (scale_[1] < scale_[0]) { - std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: scale must be in the format of (min, max)."; - MS_LOG(ERROR) << "SoftDvppDecodeRandomCropResizeJpeg: scale must be in the format of (min, max), but got: " - << scale_; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } + RETURN_IF_NOT_OK(ValidateVectorScale("SoftDvppDecodeRandomCropResizeJpeg", scale_)); // ratio - if (ratio_.size() != 2) { - std::string err_msg = - "SoftDvppDecodeRandomCropResizeJpeg: ratio must be a vector of two values, got: " + std::to_string(ratio_.size()); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (ratio_[0] <= 0 || ratio_[1] <= 0) { - std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: ratio must be greater than 0."; - MS_LOG(ERROR) << "SoftDvppDecodeRandomCropResizeJpeg: ratio must be greater than 0, got: " << ratio_; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - if (ratio_[1] < ratio_[0]) { - std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: ratio must be in the format of (min, max)."; - MS_LOG(ERROR) << "SoftDvppDecodeRandomCropResizeJpeg: ratio must be in the format of (min, max), but got: " - << ratio_; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } + RETURN_IF_NOT_OK(ValidateVectorRatio("SoftDvppDecodeRandomCropResizeJpeg", ratio_)); // max_attempts if (max_attempts_ < 1) { std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: max_attempts must be greater than or equal to 1, got: " + @@ -2146,15 +1716,7 @@ std::shared_ptr SoftDvppDecodeRandomCropResizeJpegOperation::Build() { SoftDvppDecodeResizeJpegOperation::SoftDvppDecodeResizeJpegOperation(std::vector size) : size_(size) {} Status SoftDvppDecodeResizeJpegOperation::ValidateParams() { - // size - if (size_.empty() || size_.size() > 2) { - std::string err_msg = - "SoftDvppDecodeResizeJpeg: size must be a vector of one or two values, got: " + std::to_string(size_.size()); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } - RETURN_IF_NOT_OK(ValidateVectorPositive("SoftDvppDecodeResizeJpeg", size_)); - + RETURN_IF_NOT_OK(ValidateVectorSize("SoftDvppDecodeResizeJpeg", size_)); return Status::OK(); } @@ -2195,11 +1757,7 @@ Status UniformAugOperation::ValidateParams() { RETURN_STATUS_SYNTAX_ERROR(err_msg); } // num_ops - if (num_ops_ <= 0) { - std::string err_msg = "UniformAug: num_ops must be greater than 0, but got: " + std::to_string(num_ops_); - MS_LOG(ERROR) << err_msg; - RETURN_STATUS_SYNTAX_ERROR(err_msg); - } + RETURN_IF_NOT_OK(ValidateIntScalarPositive("UniformAug", "num_ops", num_ops_)); return Status::OK(); } diff --git a/mindspore/ccsrc/minddata/dataset/include/transforms.h b/mindspore/ccsrc/minddata/dataset/include/transforms.h index c2448782fb..08db9b045a 100644 --- a/mindspore/ccsrc/minddata/dataset/include/transforms.h +++ b/mindspore/ccsrc/minddata/dataset/include/transforms.h @@ -24,6 +24,7 @@ #include "minddata/dataset/include/constants.h" #include "minddata/dataset/include/status.h" +#include "minddata/dataset/include/tensor.h" #ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_ #define INCLUDE_NLOHMANN_JSON_FWD_HPP_ @@ -88,24 +89,80 @@ class TensorOperation : public std::enable_shared_from_this { bool random_op_; }; +// Helper function to validate probability +Status ValidateProbability(const std::string &op_name, const float probability); + +// Helper function to positive int scalar +Status ValidateIntScalarPositive(const std::string &op_name, const std::string &scalar_name, int32_t scalar); + +// Helper function to positive float scalar +Status ValidateFloatScalarPositive(const std::string &op_name, const std::string &scalar_name, float scalar); + +// Helper function to validate scalar +template +Status ValidateScalar(const std::string &op_name, const std::string &scalar_name, const T scalar, + const std::vector &range, bool left_open_interval = false, bool right_open_interval = false) { + if (range.empty() || range.size() > 2) { + std::string err_msg = "Range check expecting size 1 or 2, but got: " + std::to_string(range.size()); + MS_LOG(ERROR) << err_msg; + RETURN_STATUS_SYNTAX_ERROR(err_msg); + } + if ((left_open_interval && scalar <= range[0]) || (!left_open_interval && scalar < range[0])) { + std::string interval_description = left_open_interval ? " greater than " : " greater than or equal to "; + std::string err_msg = op_name + ":" + scalar_name + " must be" + interval_description + std::to_string(range[0]) + + ", got: " + std::to_string(scalar); + MS_LOG(ERROR) << err_msg; + RETURN_STATUS_SYNTAX_ERROR(err_msg); + } + if (range.size() == 2) { + if ((right_open_interval && scalar >= range[1]) || (!right_open_interval && scalar > range[1])) { + std::string left_bracket = left_open_interval ? "(" : "["; + std::string right_bracket = right_open_interval ? ")" : "]"; + std::string err_msg = op_name + ":" + scalar_name + " is out of range " + left_bracket + + std::to_string(range[0]) + ", " + std::to_string(range[1]) + right_bracket + + ", got: " + std::to_string(scalar); + MS_LOG(ERROR) << err_msg; + RETURN_STATUS_SYNTAX_ERROR(err_msg); + } + } + return Status::OK(); +} + +// Helper function to validate color attribute +Status ValidateVectorColorAttribute(const std::string &op_name, const std::string &attr_name, + const std::vector &attr, const std::vector &range); + // Helper function to validate fill value -Status ValidateVectorFillvalue(const std::string &transform_name, const std::vector &fill_value); +Status ValidateVectorFillvalue(const std::string &op_name, const std::vector &fill_value); -// Helper function to validate probability -Status ValidateProbability(const std::string &transform_name, const float &probability); +// Helper function to validate mean/std value +Status ValidateVectorMeanStd(const std::string &op_name, const std::vector &mean, const std::vector &std); // Helper function to validate padding -Status ValidateVectorPadding(const std::string &transform_name, const std::vector &padding); +Status ValidateVectorPadding(const std::string &op_name, const std::vector &padding); + +// Helper function to validate positive value +Status ValidateVectorPositive(const std::string &op_name, const std::string &vec_name, const std::vector &vec); + +// Helper function to validate non-negative value +Status ValidateVectorNonNegative(const std::string &op_name, const std::string &vec_name, + const std::vector &vec); + +// Helper function to validate size of size +Status ValidateVectorSize(const std::string &op_name, const std::vector &size); + +// Helper function to validate scale +Status ValidateVectorScale(const std::string &op_name, const std::vector &scale); -// Helper function to validate size -Status ValidateVectorPositive(const std::string &transform_name, const std::vector &size); +// Helper function to validate ratio +Status ValidateVectorRatio(const std::string &op_name, const std::vector &ratio); // Helper function to validate transforms -Status ValidateVectorTransforms(const std::string &transform_name, +Status ValidateVectorTransforms(const std::string &op_name, const std::vector> &transforms); // Helper function to compare float value -bool CmpFloat(const float &a, const float &b, float epsilon = 0.0000000001f); +bool CmpFloat(const float a, const float b, float epsilon = 0.0000000001f); // Transform operations for performing data transformation. namespace transforms { diff --git a/tests/ut/cpp/dataset/c_api_vision_test.cc b/tests/ut/cpp/dataset/c_api_vision_test.cc index 5160862b91..46c2843038 100644 --- a/tests/ut/cpp/dataset/c_api_vision_test.cc +++ b/tests/ut/cpp/dataset/c_api_vision_test.cc @@ -265,6 +265,9 @@ TEST_F(MindDataTestPipeline, TestCropFail) { // zero height crop = mindspore::dataset::vision::Crop({0, 0}, {0, 32}); EXPECT_EQ(crop, nullptr); + // negative coordinates + crop = mindspore::dataset::vision::Crop({-1, 0}, {32, 32}); + EXPECT_EQ(crop, nullptr); } TEST_F(MindDataTestPipeline, TestCutMixBatchSuccess1) { @@ -882,7 +885,7 @@ TEST_F(MindDataTestPipeline, TestNormalize) { EXPECT_NE(ds, nullptr); // Create objects for the tensor ops - std::shared_ptr normalize = vision::Normalize({121.0, 115.0, 100.0}, {70.0, 68.0, 71.0}); + std::shared_ptr normalize = vision::Normalize({121.0, 115.0, 0.0}, {70.0, 68.0, 71.0}); EXPECT_NE(normalize, nullptr); // Create a Map operation on ds @@ -924,6 +927,15 @@ TEST_F(MindDataTestPipeline, TestNormalizeFail) { std::shared_ptr normalize = mindspore::dataset::vision::Normalize({121.0, 115.0, 100.0}, {0.0, 68.0, 71.0}); EXPECT_EQ(normalize, nullptr); + // mean out of range + normalize = mindspore::dataset::vision::Normalize({121.0, 0.0, 100.0}, {256.0, 68.0, 71.0}); + EXPECT_EQ(normalize, nullptr); + // mean out of range + normalize = mindspore::dataset::vision::Normalize({256.0, 0.0, 100.0}, {70.0, 68.0, 71.0}); + EXPECT_EQ(normalize, nullptr); + // mean out of range + normalize = mindspore::dataset::vision::Normalize({-1.0, 0.0, 100.0}, {70.0, 68.0, 71.0}); + EXPECT_EQ(normalize, nullptr); // normalize with 2 values (not 3 values) for mean normalize = mindspore::dataset::vision::Normalize({121.0, 115.0}, {70.0, 68.0, 71.0}); EXPECT_EQ(normalize, nullptr); @@ -1258,7 +1270,7 @@ TEST_F(MindDataTestPipeline, TestRandomColorAdjust) { std::shared_ptr random_color_adjust4 = vision::RandomColorAdjust(); EXPECT_NE(random_color_adjust4, nullptr); - // Use subset of explictly set parameters + // Use subset of explicitly set parameters std::shared_ptr random_color_adjust5 = vision::RandomColorAdjust({0.0, 0.5}, {0.25}); EXPECT_NE(random_color_adjust5, nullptr); @@ -1295,6 +1307,31 @@ TEST_F(MindDataTestPipeline, TestRandomColorAdjust) { iter->Stop(); } +TEST_F(MindDataTestPipeline, TestRandomColorAdjustFail) { + MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomColorAdjustFail."; + // brightness out of range + std::shared_ptr random_color_adjust1 = vision::RandomColorAdjust({-1.0}); + EXPECT_EQ(random_color_adjust1, nullptr); + + // contrast out of range + std::shared_ptr random_color_adjust2 = vision::RandomColorAdjust({1.0}, {-0.1}); + EXPECT_EQ(random_color_adjust2, nullptr); + + // saturation out of range + std::shared_ptr random_color_adjust3 = vision::RandomColorAdjust({0.0}, {0.0}, {-0.2}); + EXPECT_EQ(random_color_adjust3, nullptr); + + // hue out of range + std::shared_ptr random_color_adjust4 = vision::RandomColorAdjust({0.0}, {0.0}, {0.0}, {-0.6}); + EXPECT_EQ(random_color_adjust4, nullptr); + + std::shared_ptr random_color_adjust5 = vision::RandomColorAdjust({0.0}, {0.0}, {0.0}, {-0.5, 0.6}); + EXPECT_EQ(random_color_adjust5, nullptr); + + std::shared_ptr random_color_adjust6 = vision::RandomColorAdjust({0.0}, {0.0}, {0.0}, {0.51}); + EXPECT_EQ(random_color_adjust6, nullptr); +} + TEST_F(MindDataTestPipeline, TestRandomCropSuccess) { MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomCropSuccess."; // Create an VOC Dataset @@ -1796,13 +1833,17 @@ TEST_F(MindDataTestPipeline, TestRandomResizeFail) { std::shared_ptr random_resize1 = vision::RandomResize({-66, 77}); EXPECT_EQ(random_resize1, nullptr); - // RandomResize : size must be a vector of one or two values - std::shared_ptr random_resize2 = vision::RandomResize({1, 2, 3}); + // RandomResize : size must only contain positive integers + std::shared_ptr random_resize2 = vision::RandomResize({0, 77}); EXPECT_EQ(random_resize2, nullptr); // RandomResize : size must be a vector of one or two values - std::shared_ptr random_resize3 = vision::RandomResize({}); + std::shared_ptr random_resize3 = vision::RandomResize({1, 2, 3}); EXPECT_EQ(random_resize3, nullptr); + + // RandomResize : size must be a vector of one or two values + std::shared_ptr random_resize4 = vision::RandomResize({}); + EXPECT_EQ(random_resize4, nullptr); } TEST_F(MindDataTestPipeline, TestRandomResizeWithBBoxSuccess1) {