You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ir_vision_test.cc 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. #include <memory>
  17. #include <string>
  18. #include "common/common.h"
  19. #include "minddata/dataset/kernels/ir/vision/vision_ir.h"
  20. using namespace mindspore::dataset;
  21. class MindDataTestIRVision : public UT::DatasetOpTesting {
  22. public:
  23. MindDataTestIRVision() = default;
  24. };
  25. TEST_F(MindDataTestIRVision, TestAutoContrastFail1) {
  26. MS_LOG(INFO) << "Doing MindDataTestIRVision-TestAutoContrastFail1.";
  27. // Testing invalid cutoff < 0
  28. std::shared_ptr<TensorOperation> auto_contrast1(new vision::AutoContrastOperation(-1.0, {}));
  29. Status rc1 = auto_contrast1->ValidateParams();
  30. EXPECT_ERROR(rc1);
  31. // Testing invalid cutoff > 100
  32. std::shared_ptr<TensorOperation> auto_contrast2(new vision::AutoContrastOperation(110.0, {10, 20}));
  33. Status rc2 = auto_contrast2->ValidateParams();
  34. EXPECT_ERROR(rc2);
  35. }
  36. TEST_F(MindDataTestIRVision, TestCenterCropFail) {
  37. MS_LOG(INFO) << "Doing MindDataTestIRVision-TestCenterCrop with invalid parameters.";
  38. Status rc;
  39. // center crop height value negative
  40. std::shared_ptr<TensorOperation> center_crop1(new vision::CenterCropOperation({-32, 32}));
  41. rc = center_crop1->ValidateParams();
  42. EXPECT_ERROR(rc);
  43. // center crop width value negative
  44. std::shared_ptr<TensorOperation> center_crop2(new vision::CenterCropOperation({32, -32}));
  45. rc = center_crop2->ValidateParams();
  46. EXPECT_ERROR(rc);
  47. // 0 value would result in nullptr
  48. std::shared_ptr<TensorOperation> center_crop3(new vision::CenterCropOperation({0, 32}));
  49. rc = center_crop3->ValidateParams();
  50. EXPECT_ERROR(rc);
  51. // center crop with 3 values
  52. std::shared_ptr<TensorOperation> center_crop4(new vision::CenterCropOperation({10, 20, 30}));
  53. rc = center_crop4->ValidateParams();
  54. EXPECT_ERROR(rc);
  55. }
  56. TEST_F(MindDataTestIRVision, TestCropFail) {
  57. MS_LOG(INFO) << "Doing MindDataTestIRVision-TestCrop with invalid parameters.";
  58. Status rc;
  59. // wrong width
  60. std::shared_ptr<TensorOperation> crop1(new vision::CropOperation({0, 0}, {32, -32}));
  61. rc = crop1->ValidateParams();
  62. EXPECT_ERROR(rc);
  63. // wrong height
  64. std::shared_ptr<TensorOperation> crop2(new vision::CropOperation({0, 0}, {-32, -32}));
  65. rc = crop2->ValidateParams();
  66. EXPECT_ERROR(rc);
  67. // zero height
  68. std::shared_ptr<TensorOperation> crop3(new vision::CropOperation({0, 0}, {0, 32}));
  69. rc = crop3->ValidateParams();
  70. EXPECT_ERROR(rc);
  71. // negative coordinates
  72. std::shared_ptr<TensorOperation> crop4(new vision::CropOperation({-1, 0}, {32, 32}));
  73. rc = crop4->ValidateParams();
  74. EXPECT_ERROR(rc);
  75. }
  76. TEST_F(MindDataTestIRVision, TestCutOutFail1) {
  77. MS_LOG(INFO) << "Doing MindDataTestIRVision-TestCutOutFail1 with invalid parameters.";
  78. Status rc;
  79. // Create object for the tensor op
  80. // Invalid negative length
  81. std::shared_ptr<TensorOperation> cutout_op = std::make_shared<vision::CutOutOperation>(-10, 1);
  82. rc = cutout_op->ValidateParams();
  83. EXPECT_ERROR(rc);
  84. // Invalid negative number of patches
  85. cutout_op = std::make_shared<vision::CutOutOperation>(10, -1);
  86. rc = cutout_op->ValidateParams();
  87. EXPECT_ERROR(rc);
  88. }
  89. TEST_F(MindDataTestIRVision, TestCutOutFail2) {
  90. MS_LOG(INFO) << "Doing MindDataTestIRVision-TestCutOutFail2 with invalid params, boundary cases.";
  91. Status rc;
  92. // Create object for the tensor op
  93. // Invalid zero length
  94. std::shared_ptr<TensorOperation> cutout_op = std::make_shared<vision::CutOutOperation>(0, 1);
  95. rc = cutout_op->ValidateParams();
  96. EXPECT_ERROR(rc);
  97. // Invalid zero number of patches
  98. cutout_op = std::make_shared<vision::CutOutOperation>(10, 0);
  99. rc = cutout_op->ValidateParams();
  100. EXPECT_ERROR(rc);
  101. }
  102. TEST_F(MindDataTestIRVision, TestNormalizeFail) {
  103. MS_LOG(INFO) << "Doing MindDataTestIRVision-TestNormalizeFail with invalid parameters.";
  104. Status rc;
  105. // std value 0.0 out of range
  106. std::shared_ptr<TensorOperation> normalize1(new vision::NormalizeOperation({121.0, 115.0, 100.0}, {0.0, 68.0, 71.0}));
  107. rc = normalize1->ValidateParams();
  108. EXPECT_ERROR(rc);
  109. // std value 256.0 out of range
  110. std::shared_ptr<TensorOperation> normalize2(
  111. new vision::NormalizeOperation({121.0, 10.0, 100.0}, {256.0, 68.0, 71.0}));
  112. rc = normalize2->ValidateParams();
  113. EXPECT_ERROR(rc);
  114. // mean value 256.0 out of range
  115. std::shared_ptr<TensorOperation> normalize3(new vision::NormalizeOperation({256.0, 0.0, 100.0}, {70.0, 68.0, 71.0}));
  116. rc = normalize3->ValidateParams();
  117. EXPECT_ERROR(rc);
  118. // mean value 0.0 out of range
  119. std::shared_ptr<TensorOperation> normalize4(new vision::NormalizeOperation({-1.0, 0.0, 100.0}, {70.0, 68.0, 71.0}));
  120. rc = normalize4->ValidateParams();
  121. EXPECT_ERROR(rc);
  122. // normalize with 2 values (not 3 values) for mean
  123. std::shared_ptr<TensorOperation> normalize5(new vision::NormalizeOperation({121.0, 115.0}, {70.0, 68.0, 71.0}));
  124. rc = normalize5->ValidateParams();
  125. EXPECT_ERROR(rc);
  126. // normalize with 2 values (not 3 values) for standard deviation
  127. std::shared_ptr<TensorOperation> normalize6(new vision::NormalizeOperation({121.0, 115.0, 100.0}, {68.0, 71.0}));
  128. rc = normalize6->ValidateParams();
  129. EXPECT_ERROR(rc);
  130. }
  131. TEST_F(MindDataTestIRVision, TestNormalizePadFail) {
  132. MS_LOG(INFO) << "Doing MindDataTestIRVision-TestNormalizePadFail with invalid parameters.";
  133. Status rc;
  134. // std value at 0.0
  135. std::shared_ptr<TensorOperation> normalizepad1(
  136. new vision::NormalizePadOperation({121.0, 115.0, 100.0}, {0.0, 68.0, 71.0}, "float32"));
  137. rc = normalizepad1->ValidateParams();
  138. EXPECT_ERROR(rc);
  139. // normalizepad with 2 values (not 3 values) for mean
  140. std::shared_ptr<TensorOperation> normalizepad2(
  141. new vision::NormalizePadOperation({121.0, 115.0}, {70.0, 68.0, 71.0}, "float32"));
  142. rc = normalizepad2->ValidateParams();
  143. EXPECT_ERROR(rc);
  144. // normalizepad with 2 values (not 3 values) for standard deviation
  145. std::shared_ptr<TensorOperation> normalizepad3(
  146. new vision::NormalizePadOperation({121.0, 115.0, 100.0}, {68.0, 71.0}, "float32"));
  147. rc = normalizepad3->ValidateParams();
  148. EXPECT_ERROR(rc);
  149. // normalizepad with invalid dtype
  150. std::shared_ptr<TensorOperation> normalizepad4(
  151. new vision::NormalizePadOperation({121.0, 115.0, 100.0}, {68.0, 71.0, 71.0}, "123"));
  152. rc = normalizepad4->ValidateParams();
  153. EXPECT_ERROR(rc);
  154. }
  155. TEST_F(MindDataTestIRVision, TestRescaleFail) {
  156. MS_LOG(INFO) << "Doing MindDataTestIRVision-TestRescaleFail with invalid params.";
  157. Status rc;
  158. // incorrect negative rescale parameter
  159. std::shared_ptr<TensorOperation> rescale(new vision::RescaleOperation(-1.0, 0.0));
  160. rc = rescale->ValidateParams();
  161. EXPECT_ERROR(rc);
  162. }
  163. TEST_F(MindDataTestIRVision, TestResizeFail) {
  164. MS_LOG(INFO) << "Doing MindDataTestIRVision-TestResize with invalid parameters.";
  165. Status rc;
  166. // negative resize value
  167. std::shared_ptr<TensorOperation> resize_op1(new vision::ResizeOperation({30, -30}, InterpolationMode::kLinear));
  168. rc = resize_op1->ValidateParams();
  169. EXPECT_ERROR(rc);
  170. // zero resize value
  171. std::shared_ptr<TensorOperation> resize_op2(new vision::ResizeOperation({0, 30}, InterpolationMode::kLinear));
  172. rc = resize_op2->ValidateParams();
  173. EXPECT_ERROR(rc);
  174. // resize with 3 values
  175. std::shared_ptr<TensorOperation> resize_op3(new vision::ResizeOperation({30, 20, 10}, InterpolationMode::kLinear));
  176. rc = resize_op3->ValidateParams();
  177. EXPECT_ERROR(rc);
  178. }
  179. TEST_F(MindDataTestIRVision, TestResizeWithBBoxFail) {
  180. MS_LOG(INFO) << "Doing MindDataTestIRVision-TestResizeWithBBoxFail with invalid parameters.";
  181. Status rc;
  182. // Testing negative resize value
  183. std::shared_ptr<TensorOperation> resize_with_bbox_op(
  184. new vision::ResizeWithBBoxOperation({10, -10}, InterpolationMode::kLinear));
  185. EXPECT_NE(resize_with_bbox_op, nullptr);
  186. rc = resize_with_bbox_op->ValidateParams();
  187. EXPECT_ERROR(rc);
  188. // Testing negative resize value
  189. std::shared_ptr<TensorOperation> resize_with_bbox_op1(
  190. new vision::ResizeWithBBoxOperation({-10}, InterpolationMode::kLinear));
  191. EXPECT_NE(resize_with_bbox_op1, nullptr);
  192. rc = resize_with_bbox_op1->ValidateParams();
  193. EXPECT_ERROR(rc);
  194. // Testing zero resize value
  195. std::shared_ptr<TensorOperation> resize_with_bbox_op2(
  196. new vision::ResizeWithBBoxOperation({0, 10}, InterpolationMode::kLinear));
  197. EXPECT_NE(resize_with_bbox_op2, nullptr);
  198. rc = resize_with_bbox_op2->ValidateParams();
  199. EXPECT_ERROR(rc);
  200. // Testing resize with 3 values
  201. std::shared_ptr<TensorOperation> resize_with_bbox_op3(
  202. new vision::ResizeWithBBoxOperation({10, 10, 10}, InterpolationMode::kLinear));
  203. EXPECT_NE(resize_with_bbox_op3, nullptr);
  204. rc = resize_with_bbox_op3->ValidateParams();
  205. EXPECT_ERROR(rc);
  206. }
  207. TEST_F(MindDataTestIRVision, TestSoftDvppDecodeRandomCropResizeJpegFail) {
  208. MS_LOG(INFO) << "Doing MindDataTestIRVision-TestSoftDvppDecodeRandomCropResizeJpegFail with incorrect parameters.";
  209. Status rc;
  210. // SoftDvppDecodeRandomCropResizeJpeg: size must only contain positive integers
  211. std::shared_ptr<TensorOperation> soft_dvpp_decode_random_crop_resize_jpeg1(
  212. new vision::SoftDvppDecodeRandomCropResizeJpegOperation({-500, 600}, {0.08, 1.0}, {3. / 4., 4. / 3.}, 10));
  213. rc = soft_dvpp_decode_random_crop_resize_jpeg1->ValidateParams();
  214. EXPECT_ERROR(rc);
  215. // SoftDvppDecodeRandomCropResizeJpeg: size must only contain positive integers
  216. std::shared_ptr<TensorOperation> soft_dvpp_decode_random_crop_resize_jpeg2(
  217. new vision::SoftDvppDecodeRandomCropResizeJpegOperation({-500}, {0.08, 1.0}, {3. / 4., 4. / 3.}, 10));
  218. rc = soft_dvpp_decode_random_crop_resize_jpeg2->ValidateParams();
  219. EXPECT_ERROR(rc);
  220. // SoftDvppDecodeRandomCropResizeJpeg: size must be a vector of one or two values
  221. std::shared_ptr<TensorOperation> soft_dvpp_decode_random_crop_resize_jpeg3(
  222. new vision::SoftDvppDecodeRandomCropResizeJpegOperation({500, 600, 700}, {0.08, 1.0}, {3. / 4., 4. / 3.}, 10));
  223. rc = soft_dvpp_decode_random_crop_resize_jpeg3->ValidateParams();
  224. EXPECT_ERROR(rc);
  225. // SoftDvppDecodeRandomCropResizeJpeg: scale must be greater than or equal to 0
  226. std::shared_ptr<TensorOperation> soft_dvpp_decode_random_crop_resize_jpeg4(
  227. new vision::SoftDvppDecodeRandomCropResizeJpegOperation({500}, {-0.1, 0.9}, {3. / 4., 4. / 3.}, 1));
  228. rc = soft_dvpp_decode_random_crop_resize_jpeg4->ValidateParams();
  229. EXPECT_ERROR(rc);
  230. // SoftDvppDecodeRandomCropResizeJpeg: scale must be in the format of (min, max)
  231. std::shared_ptr<TensorOperation> soft_dvpp_decode_random_crop_resize_jpeg5(
  232. new vision::SoftDvppDecodeRandomCropResizeJpegOperation({500}, {0.6, 0.2}, {3. / 4., 4. / 3.}, 1));
  233. rc = soft_dvpp_decode_random_crop_resize_jpeg5->ValidateParams();
  234. EXPECT_ERROR(rc);
  235. // SoftDvppDecodeRandomCropResizeJpeg: scale must be a vector of two values
  236. std::shared_ptr<TensorOperation> soft_dvpp_decode_random_crop_resize_jpeg6(
  237. new vision::SoftDvppDecodeRandomCropResizeJpegOperation({500}, {0.5, 0.6, 0.7}, {3. / 4., 4. / 3.}, 1));
  238. rc = soft_dvpp_decode_random_crop_resize_jpeg6->ValidateParams();
  239. EXPECT_ERROR(rc);
  240. // SoftDvppDecodeRandomCropResizeJpeg: ratio must be greater than or equal to 0
  241. std::shared_ptr<TensorOperation> soft_dvpp_decode_random_crop_resize_jpeg7(
  242. new vision::SoftDvppDecodeRandomCropResizeJpegOperation({500}, {0.5, 0.9}, {-0.2, 0.4}, 5));
  243. rc = soft_dvpp_decode_random_crop_resize_jpeg7->ValidateParams();
  244. EXPECT_ERROR(rc);
  245. // SoftDvppDecodeRandomCropResizeJpeg: ratio must be in the format of (min, max)
  246. std::shared_ptr<TensorOperation> soft_dvpp_decode_random_crop_resize_jpeg8(
  247. new vision::SoftDvppDecodeRandomCropResizeJpegOperation({500}, {0.5, 0.9}, {0.4, 0.2}, 5));
  248. rc = soft_dvpp_decode_random_crop_resize_jpeg8->ValidateParams();
  249. EXPECT_ERROR(rc);
  250. // SoftDvppDecodeRandomCropResizeJpeg: ratio must be a vector of two values
  251. std::shared_ptr<TensorOperation> soft_dvpp_decode_random_crop_resize_jpeg9(
  252. new vision::SoftDvppDecodeRandomCropResizeJpegOperation({500}, {0.5, 0.9}, {0.1, 0.2, 0.3}, 5));
  253. rc = soft_dvpp_decode_random_crop_resize_jpeg9->ValidateParams();
  254. EXPECT_ERROR(rc);
  255. // SoftDvppDecodeRandomCropResizeJpeg: max_attempts must be greater than or equal to 1
  256. std::shared_ptr<TensorOperation> soft_dvpp_decode_random_crop_resize_jpeg10(
  257. new vision::SoftDvppDecodeRandomCropResizeJpegOperation({500}, {0.5, 0.9}, {0.1, 0.2}, 0));
  258. rc = soft_dvpp_decode_random_crop_resize_jpeg10->ValidateParams();
  259. EXPECT_ERROR(rc);
  260. }
  261. TEST_F(MindDataTestIRVision, TestSoftDvppDecodeResizeJpegFail) {
  262. MS_LOG(INFO) << "Doing MindDataTestIRVision-TestSoftDvppDecodeResizeJpegFail with incorrect size.";
  263. Status rc;
  264. // SoftDvppDecodeResizeJpeg: size must be a vector of one or two values
  265. std::shared_ptr<TensorOperation> soft_dvpp_decode_resize_jpeg_op1(new vision::SoftDvppDecodeResizeJpegOperation({}));
  266. rc = soft_dvpp_decode_resize_jpeg_op1->ValidateParams();
  267. EXPECT_ERROR(rc);
  268. // SoftDvppDecodeResizeJpeg: size must be a vector of one or two values
  269. std::shared_ptr<TensorOperation> soft_dvpp_decode_resize_jpeg_op2(
  270. new vision::SoftDvppDecodeResizeJpegOperation({1, 2, 3}));
  271. rc = soft_dvpp_decode_resize_jpeg_op2->ValidateParams();
  272. EXPECT_ERROR(rc);
  273. // SoftDvppDecodeResizeJpeg: size must only contain positive integers
  274. std::shared_ptr<TensorOperation> soft_dvpp_decode_resize_jpeg_op3(
  275. new vision::SoftDvppDecodeResizeJpegOperation({20, -20}));
  276. rc = soft_dvpp_decode_resize_jpeg_op3->ValidateParams();
  277. EXPECT_ERROR(rc);
  278. // SoftDvppDecodeResizeJpeg: size must only contain positive integers
  279. std::shared_ptr<TensorOperation> soft_dvpp_decode_resize_jpeg_op4(new vision::SoftDvppDecodeResizeJpegOperation({0}));
  280. rc = soft_dvpp_decode_resize_jpeg_op4->ValidateParams();
  281. EXPECT_ERROR(rc);
  282. }
  283. TEST_F(MindDataTestIRVision, TestVisionOperationName) {
  284. MS_LOG(INFO) << "Doing MindDataTestIRVision-TestVisionOperationName.";
  285. std::string correct_name;
  286. // Create object for the tensor op, and check the name
  287. std::shared_ptr<TensorOperation> random_vertical_flip_op = std::make_shared<vision::RandomVerticalFlipOperation>(0.5);
  288. correct_name = "RandomVerticalFlip";
  289. EXPECT_EQ(correct_name, random_vertical_flip_op->Name());
  290. // Create object for the tensor op, and check the name
  291. std::shared_ptr<TensorOperation> softDvpp_decode_resize_jpeg_op(
  292. new vision::SoftDvppDecodeResizeJpegOperation({1, 1}));
  293. correct_name = "SoftDvppDecodeResizeJpeg";
  294. EXPECT_EQ(correct_name, softDvpp_decode_resize_jpeg_op->Name());
  295. }