1 /** 2 * Copyright 2021 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include <memory> 18 #include <string> 19 #include "common/common.h" 20 #include "minddata/dataset/kernels/ir/vision/affine_ir.h" 21 #include "minddata/dataset/kernels/ir/vision/auto_contrast_ir.h" 22 #include "minddata/dataset/kernels/ir/vision/bounding_box_augment_ir.h" 23 #include "minddata/dataset/kernels/ir/vision/center_crop_ir.h" 24 #include "minddata/dataset/kernels/ir/vision/crop_ir.h" 25 #include "minddata/dataset/kernels/ir/vision/cutmix_batch_ir.h" 26 #include "minddata/dataset/kernels/ir/vision/cutout_ir.h" 27 #include "minddata/dataset/kernels/ir/vision/decode_ir.h" 28 #include "minddata/dataset/kernels/ir/vision/equalize_ir.h" 29 #include "minddata/dataset/kernels/ir/vision/hwc_to_chw_ir.h" 30 #include "minddata/dataset/kernels/ir/vision/invert_ir.h" 31 #include "minddata/dataset/kernels/ir/vision/mixup_batch_ir.h" 32 #include "minddata/dataset/kernels/ir/vision/normalize_ir.h" 33 #include "minddata/dataset/kernels/ir/vision/normalize_pad_ir.h" 34 #include "minddata/dataset/kernels/ir/vision/pad_ir.h" 35 #include "minddata/dataset/kernels/ir/vision/random_affine_ir.h" 36 #include "minddata/dataset/kernels/ir/vision/random_color_adjust_ir.h" 37 #include "minddata/dataset/kernels/ir/vision/random_color_ir.h" 38 #include "minddata/dataset/kernels/ir/vision/random_crop_decode_resize_ir.h" 39 #include "minddata/dataset/kernels/ir/vision/random_crop_ir.h" 40 #include "minddata/dataset/kernels/ir/vision/random_crop_with_bbox_ir.h" 41 #include "minddata/dataset/kernels/ir/vision/random_horizontal_flip_ir.h" 42 #include "minddata/dataset/kernels/ir/vision/random_horizontal_flip_with_bbox_ir.h" 43 #include "minddata/dataset/kernels/ir/vision/random_posterize_ir.h" 44 #include "minddata/dataset/kernels/ir/vision/random_resized_crop_ir.h" 45 #include "minddata/dataset/kernels/ir/vision/random_resized_crop_with_bbox_ir.h" 46 #include "minddata/dataset/kernels/ir/vision/random_resize_ir.h" 47 #include "minddata/dataset/kernels/ir/vision/random_resize_with_bbox_ir.h" 48 #include "minddata/dataset/kernels/ir/vision/random_rotation_ir.h" 49 #include "minddata/dataset/kernels/ir/vision/random_select_subpolicy_ir.h" 50 #include "minddata/dataset/kernels/ir/vision/random_sharpness_ir.h" 51 #include "minddata/dataset/kernels/ir/vision/random_solarize_ir.h" 52 #include "minddata/dataset/kernels/ir/vision/random_vertical_flip_ir.h" 53 #include "minddata/dataset/kernels/ir/vision/random_vertical_flip_with_bbox_ir.h" 54 #include "minddata/dataset/kernels/ir/vision/rescale_ir.h" 55 #include "minddata/dataset/kernels/ir/vision/resize_ir.h" 56 #include "minddata/dataset/kernels/ir/vision/resize_preserve_ar_ir.h" 57 #include "minddata/dataset/kernels/ir/vision/resize_with_bbox_ir.h" 58 #include "minddata/dataset/kernels/ir/vision/rgba_to_bgr_ir.h" 59 #include "minddata/dataset/kernels/ir/vision/rgba_to_rgb_ir.h" 60 #include "minddata/dataset/kernels/ir/vision/rgb_to_gray_ir.h" 61 #include "minddata/dataset/kernels/ir/vision/rotate_ir.h" 62 #include "minddata/dataset/kernels/ir/vision/softdvpp_decode_random_crop_resize_jpeg_ir.h" 63 #include "minddata/dataset/kernels/ir/vision/softdvpp_decode_resize_jpeg_ir.h" 64 #include "minddata/dataset/kernels/ir/vision/swap_red_blue_ir.h" 65 #include "minddata/dataset/kernels/ir/vision/uniform_aug_ir.h" 66 67 using namespace mindspore::dataset; 68 69 class MindDataTestIRVision : public UT::DatasetOpTesting { 70 public: 71 MindDataTestIRVision() = default; 72 }; 73 74 TEST_F(MindDataTestIRVision, TestAutoContrastFail1) { 75 MS_LOG(INFO) << "Doing MindDataTestIRVision-TestAutoContrastFail1."; 76 77 // Testing invalid cutoff < 0 78 std::shared_ptr<TensorOperation> auto_contrast1(new vision::AutoContrastOperation(-1.0, {})); 79 Status rc1 = auto_contrast1->ValidateParams(); 80 EXPECT_ERROR(rc1); 81 82 // Testing invalid cutoff > 100 83 std::shared_ptr<TensorOperation> auto_contrast2(new vision::AutoContrastOperation(110.0, {10, 20})); 84 Status rc2 = auto_contrast2->ValidateParams(); 85 EXPECT_ERROR(rc2); 86 } 87 88 TEST_F(MindDataTestIRVision, TestCenterCropFail) { 89 MS_LOG(INFO) << "Doing MindDataTestIRVision-TestCenterCrop with invalid parameters."; 90 91 Status rc; 92 93 // center crop height value negative 94 std::shared_ptr<TensorOperation> center_crop1(new vision::CenterCropOperation({-32, 32})); 95 rc = center_crop1->ValidateParams(); 96 EXPECT_ERROR(rc); 97 98 // center crop width value negative 99 std::shared_ptr<TensorOperation> center_crop2(new vision::CenterCropOperation({32, -32})); 100 rc = center_crop2->ValidateParams(); 101 EXPECT_ERROR(rc); 102 103 // 0 value would result in nullptr 104 std::shared_ptr<TensorOperation> center_crop3(new vision::CenterCropOperation({0, 32})); 105 rc = center_crop3->ValidateParams(); 106 EXPECT_ERROR(rc); 107 108 // center crop with 3 values 109 std::shared_ptr<TensorOperation> center_crop4(new vision::CenterCropOperation({10, 20, 30})); 110 rc = center_crop4->ValidateParams(); 111 EXPECT_ERROR(rc); 112 } 113 114 TEST_F(MindDataTestIRVision, TestCropFail) { 115 MS_LOG(INFO) << "Doing MindDataTestIRVision-TestCrop with invalid parameters."; 116 117 Status rc; 118 119 // wrong width 120 std::shared_ptr<TensorOperation> crop1(new vision::CropOperation({0, 0}, {32, -32})); 121 rc = crop1->ValidateParams(); 122 EXPECT_ERROR(rc); 123 124 // wrong height 125 std::shared_ptr<TensorOperation> crop2(new vision::CropOperation({0, 0}, {-32, -32})); 126 rc = crop2->ValidateParams(); 127 EXPECT_ERROR(rc); 128 129 // zero height 130 std::shared_ptr<TensorOperation> crop3(new vision::CropOperation({0, 0}, {0, 32})); 131 rc = crop3->ValidateParams(); 132 EXPECT_ERROR(rc); 133 134 // negative coordinates 135 std::shared_ptr<TensorOperation> crop4(new vision::CropOperation({-1, 0}, {32, 32})); 136 rc = crop4->ValidateParams(); 137 EXPECT_ERROR(rc); 138 } 139 140 TEST_F(MindDataTestIRVision, TestCutOutFail1) { 141 MS_LOG(INFO) << "Doing MindDataTestIRVision-TestCutOutFail1 with invalid parameters."; 142 143 Status rc; 144 145 // Create object for the tensor op 146 // Invalid negative length 147 std::shared_ptr<TensorOperation> cutout_op = std::make_shared<vision::CutOutOperation>(-10, 1); 148 rc = cutout_op->ValidateParams(); 149 EXPECT_ERROR(rc); 150 151 // Invalid negative number of patches 152 cutout_op = std::make_shared<vision::CutOutOperation>(10, -1); 153 rc = cutout_op->ValidateParams(); 154 EXPECT_ERROR(rc); 155 } 156 157 TEST_F(MindDataTestIRVision, TestCutOutFail2) { 158 MS_LOG(INFO) << "Doing MindDataTestIRVision-TestCutOutFail2 with invalid params, boundary cases."; 159 160 Status rc; 161 162 // Create object for the tensor op 163 // Invalid zero length 164 std::shared_ptr<TensorOperation> cutout_op = std::make_shared<vision::CutOutOperation>(0, 1); 165 rc = cutout_op->ValidateParams(); 166 EXPECT_ERROR(rc); 167 168 // Invalid zero number of patches 169 cutout_op = std::make_shared<vision::CutOutOperation>(10, 0); 170 rc = cutout_op->ValidateParams(); 171 EXPECT_ERROR(rc); 172 } 173 174 TEST_F(MindDataTestIRVision, TestNormalizeFail) { 175 MS_LOG(INFO) << "Doing MindDataTestIRVision-TestNormalizeFail with invalid parameters."; 176 177 Status rc; 178 179 // std value 0.0 out of range 180 std::shared_ptr<TensorOperation> normalize1(new vision::NormalizeOperation({121.0, 115.0, 100.0}, {0.0, 68.0, 71.0})); 181 rc = normalize1->ValidateParams(); 182 EXPECT_ERROR(rc); 183 184 // std value 256.0 out of range 185 std::shared_ptr<TensorOperation> normalize2( 186 new vision::NormalizeOperation({121.0, 10.0, 100.0}, {256.0, 68.0, 71.0})); 187 rc = normalize2->ValidateParams(); 188 EXPECT_ERROR(rc); 189 190 // mean value 256.0 out of range 191 std::shared_ptr<TensorOperation> normalize3(new vision::NormalizeOperation({256.0, 0.0, 100.0}, {70.0, 68.0, 71.0})); 192 rc = normalize3->ValidateParams(); 193 EXPECT_ERROR(rc); 194 195 // mean value 0.0 out of range 196 std::shared_ptr<TensorOperation> normalize4(new vision::NormalizeOperation({-1.0, 0.0, 100.0}, {70.0, 68.0, 71.0})); 197 rc = normalize4->ValidateParams(); 198 EXPECT_ERROR(rc); 199 200 // normalize with 2 values (not 3 values) for mean 201 std::shared_ptr<TensorOperation> normalize5(new vision::NormalizeOperation({121.0, 115.0}, {70.0, 68.0, 71.0})); 202 rc = normalize5->ValidateParams(); 203 EXPECT_ERROR(rc); 204 205 // normalize with 2 values (not 3 values) for standard deviation 206 std::shared_ptr<TensorOperation> normalize6(new vision::NormalizeOperation({121.0, 115.0, 100.0}, {68.0, 71.0})); 207 rc = normalize6->ValidateParams(); 208 EXPECT_ERROR(rc); 209 } 210 211 TEST_F(MindDataTestIRVision, TestNormalizePadFail) { 212 MS_LOG(INFO) << "Doing MindDataTestIRVision-TestNormalizePadFail with invalid parameters."; 213 214 Status rc; 215 216 // std value at 0.0 217 std::shared_ptr<TensorOperation> normalizepad1( 218 new vision::NormalizePadOperation({121.0, 115.0, 100.0}, {0.0, 68.0, 71.0}, "float32")); 219 rc = normalizepad1->ValidateParams(); 220 EXPECT_ERROR(rc); 221 222 // normalizepad with 2 values (not 3 values) for mean 223 std::shared_ptr<TensorOperation> normalizepad2( 224 new vision::NormalizePadOperation({121.0, 115.0}, {70.0, 68.0, 71.0}, "float32")); 225 rc = normalizepad2->ValidateParams(); 226 EXPECT_ERROR(rc); 227 228 // normalizepad with 2 values (not 3 values) for standard deviation 229 std::shared_ptr<TensorOperation> normalizepad3( 230 new vision::NormalizePadOperation({121.0, 115.0, 100.0}, {68.0, 71.0}, "float32")); 231 rc = normalizepad3->ValidateParams(); 232 EXPECT_ERROR(rc); 233 234 // normalizepad with invalid dtype 235 std::shared_ptr<TensorOperation> normalizepad4( 236 new vision::NormalizePadOperation({121.0, 115.0, 100.0}, {68.0, 71.0, 71.0}, "123")); 237 rc = normalizepad4->ValidateParams(); 238 EXPECT_ERROR(rc); 239 } 240 241 TEST_F(MindDataTestIRVision, TestRescaleFail) { 242 MS_LOG(INFO) << "Doing MindDataTestIRVision-TestRescaleFail with invalid params."; 243 244 Status rc; 245 246 // incorrect negative rescale parameter 247 std::shared_ptr<TensorOperation> rescale(new vision::RescaleOperation(-1.0, 0.0)); 248 rc = rescale->ValidateParams(); 249 EXPECT_ERROR(rc); 250 } 251 252 TEST_F(MindDataTestIRVision, TestResizeFail) { 253 MS_LOG(INFO) << "Doing MindDataTestIRVision-TestResize with invalid parameters."; 254 255 Status rc; 256 257 // negative resize value 258 std::shared_ptr<TensorOperation> resize_op1(new vision::ResizeOperation({30, -30}, InterpolationMode::kLinear)); 259 rc = resize_op1->ValidateParams(); 260 EXPECT_ERROR(rc); 261 262 // zero resize value 263 std::shared_ptr<TensorOperation> resize_op2(new vision::ResizeOperation({0, 30}, InterpolationMode::kLinear)); 264 rc = resize_op2->ValidateParams(); 265 EXPECT_ERROR(rc); 266 267 // resize with 3 values 268 std::shared_ptr<TensorOperation> resize_op3(new vision::ResizeOperation({30, 20, 10}, InterpolationMode::kLinear)); 269 rc = resize_op3->ValidateParams(); 270 EXPECT_ERROR(rc); 271 } 272 273 TEST_F(MindDataTestIRVision, TestResizeWithBBoxFail) { 274 MS_LOG(INFO) << "Doing MindDataTestIRVision-TestResizeWithBBoxFail with invalid parameters."; 275 276 Status rc; 277 278 // Testing negative resize value 279 std::shared_ptr<TensorOperation> resize_with_bbox_op( 280 new vision::ResizeWithBBoxOperation({10, -10}, InterpolationMode::kLinear)); 281 EXPECT_NE(resize_with_bbox_op, nullptr); 282 rc = resize_with_bbox_op->ValidateParams(); 283 EXPECT_ERROR(rc); 284 285 // Testing negative resize value 286 std::shared_ptr<TensorOperation> resize_with_bbox_op1( 287 new vision::ResizeWithBBoxOperation({-10}, InterpolationMode::kLinear)); 288 EXPECT_NE(resize_with_bbox_op1, nullptr); 289 rc = resize_with_bbox_op1->ValidateParams(); 290 EXPECT_ERROR(rc); 291 292 // Testing zero resize value 293 std::shared_ptr<TensorOperation> resize_with_bbox_op2( 294 new vision::ResizeWithBBoxOperation({0, 10}, InterpolationMode::kLinear)); 295 EXPECT_NE(resize_with_bbox_op2, nullptr); 296 rc = resize_with_bbox_op2->ValidateParams(); 297 EXPECT_ERROR(rc); 298 299 // Testing resize with 3 values 300 std::shared_ptr<TensorOperation> resize_with_bbox_op3( 301 new vision::ResizeWithBBoxOperation({10, 10, 10}, InterpolationMode::kLinear)); 302 EXPECT_NE(resize_with_bbox_op3, nullptr); 303 rc = resize_with_bbox_op3->ValidateParams(); 304 EXPECT_ERROR(rc); 305 } 306 307 TEST_F(MindDataTestIRVision, TestSoftDvppDecodeRandomCropResizeJpegFail) { 308 MS_LOG(INFO) << "Doing MindDataTestIRVision-TestSoftDvppDecodeRandomCropResizeJpegFail with incorrect parameters."; 309 310 Status rc; 311 312 // SoftDvppDecodeRandomCropResizeJpeg: size must only contain positive integers 313 std::shared_ptr<TensorOperation> soft_dvpp_decode_random_crop_resize_jpeg1( 314 new vision::SoftDvppDecodeRandomCropResizeJpegOperation({-500, 600}, {0.08, 1.0}, {3. / 4., 4. / 3.}, 10)); 315 rc = soft_dvpp_decode_random_crop_resize_jpeg1->ValidateParams(); 316 EXPECT_ERROR(rc); 317 318 // SoftDvppDecodeRandomCropResizeJpeg: size must only contain positive integers 319 std::shared_ptr<TensorOperation> soft_dvpp_decode_random_crop_resize_jpeg2( 320 new vision::SoftDvppDecodeRandomCropResizeJpegOperation({-500}, {0.08, 1.0}, {3. / 4., 4. / 3.}, 10)); 321 rc = soft_dvpp_decode_random_crop_resize_jpeg2->ValidateParams(); 322 EXPECT_ERROR(rc); 323 324 // SoftDvppDecodeRandomCropResizeJpeg: size must be a vector of one or two values 325 std::shared_ptr<TensorOperation> soft_dvpp_decode_random_crop_resize_jpeg3( 326 new vision::SoftDvppDecodeRandomCropResizeJpegOperation({500, 600, 700}, {0.08, 1.0}, {3. / 4., 4. / 3.}, 10)); 327 rc = soft_dvpp_decode_random_crop_resize_jpeg3->ValidateParams(); 328 EXPECT_ERROR(rc); 329 330 // SoftDvppDecodeRandomCropResizeJpeg: scale must be greater than or equal to 0 331 std::shared_ptr<TensorOperation> soft_dvpp_decode_random_crop_resize_jpeg4( 332 new vision::SoftDvppDecodeRandomCropResizeJpegOperation({500}, {-0.1, 0.9}, {3. / 4., 4. / 3.}, 1)); 333 rc = soft_dvpp_decode_random_crop_resize_jpeg4->ValidateParams(); 334 EXPECT_ERROR(rc); 335 336 // SoftDvppDecodeRandomCropResizeJpeg: scale must be in the format of (min, max) 337 std::shared_ptr<TensorOperation> soft_dvpp_decode_random_crop_resize_jpeg5( 338 new vision::SoftDvppDecodeRandomCropResizeJpegOperation({500}, {0.6, 0.2}, {3. / 4., 4. / 3.}, 1)); 339 rc = soft_dvpp_decode_random_crop_resize_jpeg5->ValidateParams(); 340 EXPECT_ERROR(rc); 341 342 // SoftDvppDecodeRandomCropResizeJpeg: scale must be a vector of two values 343 std::shared_ptr<TensorOperation> soft_dvpp_decode_random_crop_resize_jpeg6( 344 new vision::SoftDvppDecodeRandomCropResizeJpegOperation({500}, {0.5, 0.6, 0.7}, {3. / 4., 4. / 3.}, 1)); 345 rc = soft_dvpp_decode_random_crop_resize_jpeg6->ValidateParams(); 346 EXPECT_ERROR(rc); 347 348 // SoftDvppDecodeRandomCropResizeJpeg: ratio must be greater than or equal to 0 349 std::shared_ptr<TensorOperation> soft_dvpp_decode_random_crop_resize_jpeg7( 350 new vision::SoftDvppDecodeRandomCropResizeJpegOperation({500}, {0.5, 0.9}, {-0.2, 0.4}, 5)); 351 rc = soft_dvpp_decode_random_crop_resize_jpeg7->ValidateParams(); 352 EXPECT_ERROR(rc); 353 354 // SoftDvppDecodeRandomCropResizeJpeg: ratio must be in the format of (min, max) 355 std::shared_ptr<TensorOperation> soft_dvpp_decode_random_crop_resize_jpeg8( 356 new vision::SoftDvppDecodeRandomCropResizeJpegOperation({500}, {0.5, 0.9}, {0.4, 0.2}, 5)); 357 rc = soft_dvpp_decode_random_crop_resize_jpeg8->ValidateParams(); 358 EXPECT_ERROR(rc); 359 // SoftDvppDecodeRandomCropResizeJpeg: ratio must be a vector of two values 360 std::shared_ptr<TensorOperation> soft_dvpp_decode_random_crop_resize_jpeg9( 361 new vision::SoftDvppDecodeRandomCropResizeJpegOperation({500}, {0.5, 0.9}, {0.1, 0.2, 0.3}, 5)); 362 rc = soft_dvpp_decode_random_crop_resize_jpeg9->ValidateParams(); 363 EXPECT_ERROR(rc); 364 365 // SoftDvppDecodeRandomCropResizeJpeg: max_attempts must be greater than or equal to 1 366 std::shared_ptr<TensorOperation> soft_dvpp_decode_random_crop_resize_jpeg10( 367 new vision::SoftDvppDecodeRandomCropResizeJpegOperation({500}, {0.5, 0.9}, {0.1, 0.2}, 0)); 368 rc = soft_dvpp_decode_random_crop_resize_jpeg10->ValidateParams(); 369 EXPECT_ERROR(rc); 370 } 371 372 TEST_F(MindDataTestIRVision, TestSoftDvppDecodeResizeJpegFail) { 373 MS_LOG(INFO) << "Doing MindDataTestIRVision-TestSoftDvppDecodeResizeJpegFail with incorrect size."; 374 375 Status rc; 376 377 // SoftDvppDecodeResizeJpeg: size must be a vector of one or two values 378 std::shared_ptr<TensorOperation> soft_dvpp_decode_resize_jpeg_op1(new vision::SoftDvppDecodeResizeJpegOperation({})); 379 rc = soft_dvpp_decode_resize_jpeg_op1->ValidateParams(); 380 EXPECT_ERROR(rc); 381 382 // SoftDvppDecodeResizeJpeg: size must be a vector of one or two values 383 std::shared_ptr<TensorOperation> soft_dvpp_decode_resize_jpeg_op2( 384 new vision::SoftDvppDecodeResizeJpegOperation({1, 2, 3})); 385 rc = soft_dvpp_decode_resize_jpeg_op2->ValidateParams(); 386 EXPECT_ERROR(rc); 387 388 // SoftDvppDecodeResizeJpeg: size must only contain positive integers 389 std::shared_ptr<TensorOperation> soft_dvpp_decode_resize_jpeg_op3( 390 new vision::SoftDvppDecodeResizeJpegOperation({20, -20})); 391 rc = soft_dvpp_decode_resize_jpeg_op3->ValidateParams(); 392 EXPECT_ERROR(rc); 393 394 // SoftDvppDecodeResizeJpeg: size must only contain positive integers 395 std::shared_ptr<TensorOperation> soft_dvpp_decode_resize_jpeg_op4(new vision::SoftDvppDecodeResizeJpegOperation({0})); 396 rc = soft_dvpp_decode_resize_jpeg_op4->ValidateParams(); 397 EXPECT_ERROR(rc); 398 } 399 400 TEST_F(MindDataTestIRVision, TestVisionOperationName) { 401 MS_LOG(INFO) << "Doing MindDataTestIRVision-TestVisionOperationName."; 402 403 std::string correct_name; 404 405 // Create object for the tensor op, and check the name 406 std::shared_ptr<TensorOperation> random_vertical_flip_op = std::make_shared<vision::RandomVerticalFlipOperation>(0.5); 407 correct_name = "RandomVerticalFlip"; 408 EXPECT_EQ(correct_name, random_vertical_flip_op->Name()); 409 410 // Create object for the tensor op, and check the name 411 std::shared_ptr<TensorOperation> softDvpp_decode_resize_jpeg_op( 412 new vision::SoftDvppDecodeResizeJpegOperation({1, 1})); 413 correct_name = "SoftDvppDecodeResizeJpeg"; 414 EXPECT_EQ(correct_name, softDvpp_decode_resize_jpeg_op->Name()); 415 } 416