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.

vision.cc 30 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /**
  2. * Copyright 2020-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 "minddata/dataset/include/vision.h"
  17. #include "minddata/dataset/include/transforms.h"
  18. #include "minddata/dataset/kernels/ir/vision/vision_ir.h"
  19. #ifndef ENABLE_ANDROID
  20. #include "minddata/dataset/kernels/image/image_utils.h"
  21. #endif
  22. #include "minddata/dataset/kernels/ir/validators.h"
  23. // Kernel image headers (in alphabetical order)
  24. #ifndef ENABLE_ANDROID
  25. #include "minddata/dataset/kernels/image/auto_contrast_op.h"
  26. #include "minddata/dataset/kernels/image/bounding_box_augment_op.h"
  27. #endif
  28. #include "minddata/dataset/kernels/image/center_crop_op.h"
  29. #include "minddata/dataset/kernels/image/crop_op.h"
  30. #ifndef ENABLE_ANDROID
  31. #include "minddata/dataset/kernels/image/cutmix_batch_op.h"
  32. #include "minddata/dataset/kernels/image/cut_out_op.h"
  33. #endif
  34. #include "minddata/dataset/kernels/image/decode_op.h"
  35. #ifdef ENABLE_ACL
  36. #include "minddata/dataset/include/vision_ascend.h"
  37. #include "minddata/dataset/kernels/image/dvpp/dvpp_crop_jpeg_op.h"
  38. #include "minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_jpeg_op.h"
  39. #include "minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_crop_jpeg_op.h"
  40. #include "minddata/dataset/kernels/image/dvpp/dvpp_decode_jpeg_op.h"
  41. #include "minddata/dataset/kernels/image/dvpp/dvpp_decode_png_op.h"
  42. #include "minddata/dataset/kernels/image/dvpp/dvpp_resize_jpeg_op.h"
  43. #endif
  44. #ifndef ENABLE_ANDROID
  45. #include "minddata/dataset/kernels/image/equalize_op.h"
  46. #include "minddata/dataset/kernels/image/hwc_to_chw_op.h"
  47. #include "minddata/dataset/kernels/image/invert_op.h"
  48. #include "minddata/dataset/kernels/image/mixup_batch_op.h"
  49. #endif
  50. #include "minddata/dataset/kernels/image/normalize_op.h"
  51. #include "minddata/dataset/kernels/image/normalize_pad_op.h"
  52. #ifndef ENABLE_ANDROID
  53. #include "minddata/dataset/kernels/image/pad_op.h"
  54. #include "minddata/dataset/kernels/image/random_affine_op.h"
  55. #include "minddata/dataset/kernels/image/random_color_op.h"
  56. #include "minddata/dataset/kernels/image/random_color_adjust_op.h"
  57. #include "minddata/dataset/kernels/image/random_crop_and_resize_op.h"
  58. #include "minddata/dataset/kernels/image/random_crop_op.h"
  59. #include "minddata/dataset/kernels/image/random_crop_decode_resize_op.h"
  60. #include "minddata/dataset/kernels/image/random_crop_with_bbox_op.h"
  61. #include "minddata/dataset/kernels/image/random_crop_and_resize_with_bbox_op.h"
  62. #include "minddata/dataset/kernels/image/random_horizontal_flip_op.h"
  63. #include "minddata/dataset/kernels/image/random_horizontal_flip_with_bbox_op.h"
  64. #include "minddata/dataset/kernels/image/random_posterize_op.h"
  65. #include "minddata/dataset/kernels/image/random_resize_op.h"
  66. #include "minddata/dataset/kernels/image/random_resize_with_bbox_op.h"
  67. #include "minddata/dataset/kernels/image/random_rotation_op.h"
  68. #include "minddata/dataset/kernels/image/random_select_subpolicy_op.h"
  69. #include "minddata/dataset/kernels/image/random_sharpness_op.h"
  70. #include "minddata/dataset/kernels/image/random_solarize_op.h"
  71. #include "minddata/dataset/kernels/image/random_vertical_flip_op.h"
  72. #include "minddata/dataset/kernels/image/random_vertical_flip_with_bbox_op.h"
  73. #include "minddata/dataset/kernels/image/rescale_op.h"
  74. #endif
  75. #include "minddata/dataset/kernels/image/resize_op.h"
  76. #ifndef ENABLE_ANDROID
  77. #include "minddata/dataset/kernels/image/resize_with_bbox_op.h"
  78. #include "minddata/dataset/kernels/image/rgba_to_bgr_op.h"
  79. #include "minddata/dataset/kernels/image/rgba_to_rgb_op.h"
  80. #include "minddata/dataset/kernels/image/soft_dvpp/soft_dvpp_decode_random_crop_resize_jpeg_op.h"
  81. #include "minddata/dataset/kernels/image/soft_dvpp/soft_dvpp_decode_resize_jpeg_op.h"
  82. #include "minddata/dataset/kernels/image/swap_red_blue_op.h"
  83. #include "minddata/dataset/kernels/image/uniform_aug_op.h"
  84. #endif
  85. #include "minddata/dataset/kernels/image/rotate_op.h"
  86. namespace mindspore {
  87. namespace dataset {
  88. // Transform operations for computer vision.
  89. namespace vision {
  90. #ifndef ENABLE_ANDROID
  91. // FUNCTIONS TO CREATE VISION TRANSFORM OPERATIONS
  92. // (In alphabetical order)
  93. // Function to create AutoContrastOperation.
  94. std::shared_ptr<AutoContrastOperation> AutoContrast(float cutoff, std::vector<uint32_t> ignore) {
  95. auto op = std::make_shared<AutoContrastOperation>(cutoff, ignore);
  96. // Input validation
  97. return op->ValidateParams() ? op : nullptr;
  98. }
  99. // Function to create BoundingBoxAugmentOperation.
  100. std::shared_ptr<BoundingBoxAugmentOperation> BoundingBoxAugment(std::shared_ptr<TensorOperation> transform,
  101. float ratio) {
  102. auto op = std::make_shared<BoundingBoxAugmentOperation>(transform, ratio);
  103. // Input validation
  104. return op->ValidateParams() ? op : nullptr;
  105. }
  106. #endif
  107. // Function to create CenterCropOperation.
  108. std::shared_ptr<CenterCropOperation> CenterCrop(std::vector<int32_t> size) {
  109. auto op = std::make_shared<CenterCropOperation>(size);
  110. // Input validation
  111. return op->ValidateParams() ? op : nullptr;
  112. }
  113. // Function to create CropOperation.
  114. std::shared_ptr<CropOperation> Crop(std::vector<int32_t> coordinates, std::vector<int32_t> size) {
  115. auto op = std::make_shared<CropOperation>(coordinates, size);
  116. // Input validation
  117. return op->ValidateParams() ? op : nullptr;
  118. }
  119. #ifndef ENABLE_ANDROID
  120. // Function to create CutMixBatchOperation.
  121. std::shared_ptr<CutMixBatchOperation> CutMixBatch(ImageBatchFormat image_batch_format, float alpha, float prob) {
  122. auto op = std::make_shared<CutMixBatchOperation>(image_batch_format, alpha, prob);
  123. // Input validation
  124. return op->ValidateParams() ? op : nullptr;
  125. }
  126. // Function to create CutOutOp.
  127. std::shared_ptr<CutOutOperation> CutOut(int32_t length, int32_t num_patches) {
  128. auto op = std::make_shared<CutOutOperation>(length, num_patches);
  129. // Input validation
  130. return op->ValidateParams() ? op : nullptr;
  131. }
  132. // Function to create DecodeOperation.
  133. std::shared_ptr<DecodeOperation> Decode(bool rgb) {
  134. auto op = std::make_shared<DecodeOperation>(rgb);
  135. // Input validation
  136. return op->ValidateParams() ? op : nullptr;
  137. }
  138. #ifdef ENABLE_ACL
  139. // Function to create DvppResizeOperation.
  140. std::shared_ptr<DvppCropJpegOperation> DvppCropJpeg(std::vector<uint32_t> crop) {
  141. auto op = std::make_shared<DvppCropJpegOperation>(crop);
  142. // Input validation
  143. return op->ValidateParams() ? op : nullptr;
  144. }
  145. // Function to create DvppDecodeResizeOperation.
  146. std::shared_ptr<DvppDecodeResizeOperation> DvppDecodeResizeJpeg(std::vector<uint32_t> resize) {
  147. auto op = std::make_shared<DvppDecodeResizeOperation>(resize);
  148. // Input validation
  149. return op->ValidateParams() ? op : nullptr;
  150. }
  151. // Function to create DvppDecodeResizeCropOperation.
  152. std::shared_ptr<DvppDecodeResizeCropOperation> DvppDecodeResizeCropJpeg(std::vector<uint32_t> crop,
  153. std::vector<uint32_t> resize) {
  154. auto op = std::make_shared<DvppDecodeResizeCropOperation>(crop, resize);
  155. // Input validation
  156. return op->ValidateParams() ? op : nullptr;
  157. }
  158. // Function to create DvppDecodeJpegOperation.
  159. std::shared_ptr<DvppDecodeJpegOperation> DvppDecodeJpeg() {
  160. auto op = std::make_shared<DvppDecodeJpegOperation>();
  161. // Input validation
  162. return op->ValidateParams() ? op : nullptr;
  163. }
  164. // Function to create DvppDecodePngOperation.
  165. std::shared_ptr<DvppDecodePngOperation> DvppDecodePng() {
  166. auto op = std::make_shared<DvppDecodePngOperation>();
  167. // Input validation
  168. return op->ValidateParams() ? op : nullptr;
  169. }
  170. // Function to create DvppResizeOperation.
  171. std::shared_ptr<DvppResizeJpegOperation> DvppResizeJpeg(std::vector<uint32_t> resize) {
  172. auto op = std::make_shared<DvppResizeJpegOperation>(resize);
  173. // Input validation
  174. return op->ValidateParams() ? op : nullptr;
  175. }
  176. #endif
  177. // Function to create EqualizeOperation.
  178. std::shared_ptr<EqualizeOperation> Equalize() {
  179. auto op = std::make_shared<EqualizeOperation>();
  180. // Input validation
  181. return op->ValidateParams() ? op : nullptr;
  182. }
  183. // Function to create HwcToChwOperation.
  184. std::shared_ptr<HwcToChwOperation> HWC2CHW() {
  185. auto op = std::make_shared<HwcToChwOperation>();
  186. // Input validation
  187. return op->ValidateParams() ? op : nullptr;
  188. }
  189. // Function to create InvertOperation.
  190. std::shared_ptr<InvertOperation> Invert() {
  191. auto op = std::make_shared<InvertOperation>();
  192. // Input validation
  193. return op->ValidateParams() ? op : nullptr;
  194. }
  195. // Function to create MixUpBatchOperation.
  196. std::shared_ptr<MixUpBatchOperation> MixUpBatch(float alpha) {
  197. auto op = std::make_shared<MixUpBatchOperation>(alpha);
  198. // Input validation
  199. return op->ValidateParams() ? op : nullptr;
  200. }
  201. #endif
  202. // Function to create NormalizeOperation.
  203. std::shared_ptr<NormalizeOperation> Normalize(std::vector<float> mean, std::vector<float> std) {
  204. auto op = std::make_shared<NormalizeOperation>(mean, std);
  205. // Input validation
  206. return op->ValidateParams() ? op : nullptr;
  207. }
  208. #ifndef ENABLE_ANDROID
  209. // Function to create NormalizePadOperation.
  210. std::shared_ptr<NormalizePadOperation> NormalizePad(const std::vector<float> &mean, const std::vector<float> &std,
  211. const std::string &dtype) {
  212. auto op = std::make_shared<NormalizePadOperation>(mean, std, dtype);
  213. // Input validation
  214. return op->ValidateParams() ? op : nullptr;
  215. }
  216. // Function to create PadOperation.
  217. std::shared_ptr<PadOperation> Pad(std::vector<int32_t> padding, std::vector<uint8_t> fill_value,
  218. BorderType padding_mode) {
  219. auto op = std::make_shared<PadOperation>(padding, fill_value, padding_mode);
  220. // Input validation
  221. return op->ValidateParams() ? op : nullptr;
  222. }
  223. // Function to create RandomAffineOperation.
  224. std::shared_ptr<RandomAffineOperation> RandomAffine(const std::vector<float_t> &degrees,
  225. const std::vector<float_t> &translate_range,
  226. const std::vector<float_t> &scale_range,
  227. const std::vector<float_t> &shear_ranges,
  228. InterpolationMode interpolation,
  229. const std::vector<uint8_t> &fill_value) {
  230. auto op = std::make_shared<RandomAffineOperation>(degrees, translate_range, scale_range, shear_ranges, interpolation,
  231. fill_value);
  232. // Input validation
  233. return op->ValidateParams() ? op : nullptr;
  234. }
  235. // Function to create RandomColorOperation.
  236. std::shared_ptr<RandomColorOperation> RandomColor(float t_lb, float t_ub) {
  237. auto op = std::make_shared<RandomColorOperation>(t_lb, t_ub);
  238. // Input validation
  239. return op->ValidateParams() ? op : nullptr;
  240. }
  241. std::shared_ptr<TensorOp> RandomColorOperation::Build() {
  242. std::shared_ptr<RandomColorOp> tensor_op = std::make_shared<RandomColorOp>(t_lb_, t_ub_);
  243. return tensor_op;
  244. }
  245. // Function to create RandomColorAdjustOperation.
  246. std::shared_ptr<RandomColorAdjustOperation> RandomColorAdjust(std::vector<float> brightness,
  247. std::vector<float> contrast,
  248. std::vector<float> saturation, std::vector<float> hue) {
  249. auto op = std::make_shared<RandomColorAdjustOperation>(brightness, contrast, saturation, hue);
  250. // Input validation
  251. return op->ValidateParams() ? op : nullptr;
  252. }
  253. // Function to create RandomCropOperation.
  254. std::shared_ptr<RandomCropOperation> RandomCrop(std::vector<int32_t> size, std::vector<int32_t> padding,
  255. bool pad_if_needed, std::vector<uint8_t> fill_value,
  256. BorderType padding_mode) {
  257. auto op = std::make_shared<RandomCropOperation>(size, padding, pad_if_needed, fill_value, padding_mode);
  258. // Input validation
  259. return op->ValidateParams() ? op : nullptr;
  260. }
  261. // Function to create RandomCropDecodeResizeOperation.
  262. std::shared_ptr<RandomCropDecodeResizeOperation> RandomCropDecodeResize(std::vector<int32_t> size,
  263. std::vector<float> scale,
  264. std::vector<float> ratio,
  265. InterpolationMode interpolation,
  266. int32_t max_attempts) {
  267. auto op = std::make_shared<RandomCropDecodeResizeOperation>(size, scale, ratio, interpolation, max_attempts);
  268. // Input validation
  269. return op->ValidateParams() ? op : nullptr;
  270. }
  271. // Function to create RandomCropWithBBoxOperation.
  272. std::shared_ptr<RandomCropWithBBoxOperation> RandomCropWithBBox(std::vector<int32_t> size, std::vector<int32_t> padding,
  273. bool pad_if_needed, std::vector<uint8_t> fill_value,
  274. BorderType padding_mode) {
  275. auto op = std::make_shared<RandomCropWithBBoxOperation>(size, padding, pad_if_needed, fill_value, padding_mode);
  276. // Input validation
  277. return op->ValidateParams() ? op : nullptr;
  278. }
  279. // Function to create RandomHorizontalFlipOperation.
  280. std::shared_ptr<RandomHorizontalFlipOperation> RandomHorizontalFlip(float prob) {
  281. auto op = std::make_shared<RandomHorizontalFlipOperation>(prob);
  282. // Input validation
  283. return op->ValidateParams() ? op : nullptr;
  284. }
  285. // Function to create RandomHorizontalFlipOperation.
  286. std::shared_ptr<RandomHorizontalFlipWithBBoxOperation> RandomHorizontalFlipWithBBox(float prob) {
  287. auto op = std::make_shared<RandomHorizontalFlipWithBBoxOperation>(prob);
  288. // Input validation
  289. return op->ValidateParams() ? op : nullptr;
  290. }
  291. // Function to create RandomPosterizeOperation.
  292. std::shared_ptr<RandomPosterizeOperation> RandomPosterize(const std::vector<uint8_t> &bit_range) {
  293. auto op = std::make_shared<RandomPosterizeOperation>(bit_range);
  294. // Input validation
  295. return op->ValidateParams() ? op : nullptr;
  296. }
  297. // Function to create RandomResizeOperation.
  298. std::shared_ptr<RandomResizeOperation> RandomResize(std::vector<int32_t> size) {
  299. auto op = std::make_shared<RandomResizeOperation>(size);
  300. // Input validation
  301. return op->ValidateParams() ? op : nullptr;
  302. }
  303. // Function to create RandomResizeWithBBoxOperation.
  304. std::shared_ptr<RandomResizeWithBBoxOperation> RandomResizeWithBBox(std::vector<int32_t> size) {
  305. auto op = std::make_shared<RandomResizeWithBBoxOperation>(size);
  306. // Input validation
  307. return op->ValidateParams() ? op : nullptr;
  308. }
  309. // Function to create RandomResizedCropOperation.
  310. std::shared_ptr<RandomResizedCropOperation> RandomResizedCrop(std::vector<int32_t> size, std::vector<float> scale,
  311. std::vector<float> ratio, InterpolationMode interpolation,
  312. int32_t max_attempts) {
  313. auto op = std::make_shared<RandomResizedCropOperation>(size, scale, ratio, interpolation, max_attempts);
  314. // Input validation
  315. return op->ValidateParams() ? op : nullptr;
  316. }
  317. // Function to create RandomResizedCropOperation.
  318. std::shared_ptr<RandomResizedCropWithBBoxOperation> RandomResizedCropWithBBox(std::vector<int32_t> size,
  319. std::vector<float> scale,
  320. std::vector<float> ratio,
  321. InterpolationMode interpolation,
  322. int32_t max_attempts) {
  323. auto op = std::make_shared<RandomResizedCropWithBBoxOperation>(size, scale, ratio, interpolation, max_attempts);
  324. // Input validation
  325. return op->ValidateParams() ? op : nullptr;
  326. }
  327. // Function to create RandomRotationOperation.
  328. std::shared_ptr<RandomRotationOperation> RandomRotation(std::vector<float> degrees, InterpolationMode resample,
  329. bool expand, std::vector<float> center,
  330. std::vector<uint8_t> fill_value) {
  331. auto op = std::make_shared<RandomRotationOperation>(degrees, resample, expand, center, fill_value);
  332. // Input validation
  333. return op->ValidateParams() ? op : nullptr;
  334. }
  335. // Function to create RandomSharpnessOperation.
  336. std::shared_ptr<RandomSharpnessOperation> RandomSharpness(std::vector<float> degrees) {
  337. auto op = std::make_shared<RandomSharpnessOperation>(degrees);
  338. // Input validation
  339. return op->ValidateParams() ? op : nullptr;
  340. }
  341. // Function to create RandomSolarizeOperation.
  342. std::shared_ptr<RandomSolarizeOperation> RandomSolarize(std::vector<uint8_t> threshold) {
  343. auto op = std::make_shared<RandomSolarizeOperation>(threshold);
  344. // Input validation
  345. return op->ValidateParams() ? op : nullptr;
  346. }
  347. // Function to create RandomSelectSubpolicyOperation.
  348. std::shared_ptr<RandomSelectSubpolicyOperation> RandomSelectSubpolicy(
  349. std::vector<std::vector<std::pair<std::shared_ptr<TensorOperation>, double>>> policy) {
  350. auto op = std::make_shared<RandomSelectSubpolicyOperation>(policy);
  351. // Input validation
  352. return op->ValidateParams() ? op : nullptr;
  353. }
  354. // Function to create RandomVerticalFlipOperation.
  355. std::shared_ptr<RandomVerticalFlipOperation> RandomVerticalFlip(float prob) {
  356. auto op = std::make_shared<RandomVerticalFlipOperation>(prob);
  357. // Input validation
  358. return op->ValidateParams() ? op : nullptr;
  359. }
  360. // Function to create RandomVerticalFlipWithBBoxOperation.
  361. std::shared_ptr<RandomVerticalFlipWithBBoxOperation> RandomVerticalFlipWithBBox(float prob) {
  362. auto op = std::make_shared<RandomVerticalFlipWithBBoxOperation>(prob);
  363. // Input validation
  364. return op->ValidateParams() ? op : nullptr;
  365. }
  366. // Function to create RescaleOperation.
  367. std::shared_ptr<RescaleOperation> Rescale(float rescale, float shift) {
  368. auto op = std::make_shared<RescaleOperation>(rescale, shift);
  369. // Input validation
  370. return op->ValidateParams() ? op : nullptr;
  371. }
  372. #endif
  373. // Function to create ResizeOperation.
  374. std::shared_ptr<ResizeOperation> Resize(std::vector<int32_t> size, InterpolationMode interpolation) {
  375. auto op = std::make_shared<ResizeOperation>(size, interpolation);
  376. // Input validation
  377. return op->ValidateParams() ? op : nullptr;
  378. }
  379. #ifdef ENABLE_ANDROID
  380. // Function to create RotateOperation.
  381. std::shared_ptr<RotateOperation> Rotate() {
  382. auto op = std::make_shared<RotateOperation>();
  383. // Input validation
  384. return op->ValidateParams() ? op : nullptr;
  385. }
  386. #endif
  387. #ifndef ENABLE_ANDROID
  388. // Function to create ResizeWithBBoxOperation.
  389. std::shared_ptr<ResizeWithBBoxOperation> ResizeWithBBox(std::vector<int32_t> size, InterpolationMode interpolation) {
  390. auto op = std::make_shared<ResizeWithBBoxOperation>(size, interpolation);
  391. // Input validation
  392. return op->ValidateParams() ? op : nullptr;
  393. }
  394. // Function to create RgbaToBgrOperation.
  395. std::shared_ptr<RgbaToBgrOperation> RGBA2BGR() {
  396. auto op = std::make_shared<RgbaToBgrOperation>();
  397. // Input validation
  398. return op->ValidateParams() ? op : nullptr;
  399. }
  400. // Function to create RgbaToRgbOperation.
  401. std::shared_ptr<RgbaToRgbOperation> RGBA2RGB() {
  402. auto op = std::make_shared<RgbaToRgbOperation>();
  403. // Input validation
  404. return op->ValidateParams() ? op : nullptr;
  405. }
  406. // Function to create SoftDvppDecodeRandomCropResizeJpegOperation.
  407. std::shared_ptr<SoftDvppDecodeRandomCropResizeJpegOperation> SoftDvppDecodeRandomCropResizeJpeg(
  408. std::vector<int32_t> size, std::vector<float> scale, std::vector<float> ratio, int32_t max_attempts) {
  409. auto op = std::make_shared<SoftDvppDecodeRandomCropResizeJpegOperation>(size, scale, ratio, max_attempts);
  410. // Input validation
  411. return op->ValidateParams() ? op : nullptr;
  412. }
  413. // Function to create SoftDvppDecodeResizeJpegOperation.
  414. std::shared_ptr<SoftDvppDecodeResizeJpegOperation> SoftDvppDecodeResizeJpeg(std::vector<int32_t> size) {
  415. auto op = std::make_shared<SoftDvppDecodeResizeJpegOperation>(size);
  416. // Input validation
  417. return op->ValidateParams() ? op : nullptr;
  418. }
  419. // Function to create SwapRedBlueOperation.
  420. std::shared_ptr<SwapRedBlueOperation> SwapRedBlue() {
  421. auto op = std::make_shared<SwapRedBlueOperation>();
  422. // Input validation
  423. return op->ValidateParams() ? op : nullptr;
  424. }
  425. // Function to create UniformAugOperation.
  426. std::shared_ptr<UniformAugOperation> UniformAugment(std::vector<std::shared_ptr<TensorOperation>> transforms,
  427. int32_t num_ops) {
  428. auto op = std::make_shared<UniformAugOperation>(transforms, num_ops);
  429. // Input validation
  430. return op->ValidateParams() ? op : nullptr;
  431. }
  432. #endif
  433. #ifdef ENABLE_ACL
  434. // DvppCropOperation
  435. DvppCropJpegOperation::DvppCropJpegOperation(const std::vector<uint32_t> &crop) : crop_(crop) {}
  436. Status DvppCropJpegOperation::ValidateParams() {
  437. // size
  438. if (crop_.empty() || crop_.size() > 2) {
  439. std::string err_msg =
  440. "DvppCropJpeg: Crop resolution must be a vector of one or two elements, got: " + std::to_string(crop_.size());
  441. MS_LOG(ERROR) << err_msg;
  442. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  443. }
  444. if (*min_element(crop_.begin(), crop_.end()) < 32 || *max_element(crop_.begin(), crop_.end()) > 2048) {
  445. std::string err_msg = "Dvpp module supports crop image with resolution in range [32, 2048], got crop Parameters: ";
  446. if (crop_.size() == 2) {
  447. MS_LOG(ERROR) << err_msg << "[" << crop_[0] << ", " << crop_[1] << "]";
  448. } else {
  449. MS_LOG(ERROR) << err_msg << "[" << crop_[0] << ", " << crop_[0] << "]";
  450. }
  451. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  452. }
  453. return Status::OK();
  454. }
  455. std::shared_ptr<TensorOp> DvppCropJpegOperation::Build() {
  456. // If size is a single value, the smaller edge of the image will be
  457. // resized to this value with the same image aspect ratio.
  458. uint32_t cropHeight, cropWidth;
  459. // User specified the width value.
  460. if (crop_.size() == 1) {
  461. cropHeight = crop_[0];
  462. cropWidth = crop_[0];
  463. } else {
  464. cropHeight = crop_[0];
  465. cropWidth = crop_[1];
  466. }
  467. std::shared_ptr<DvppCropJpegOp> tensor_op = std::make_shared<DvppCropJpegOp>(cropHeight, cropWidth);
  468. return tensor_op;
  469. }
  470. // DvppDecodeResizeOperation
  471. DvppDecodeResizeOperation::DvppDecodeResizeOperation(const std::vector<uint32_t> &resize) : resize_(resize) {}
  472. Status DvppDecodeResizeOperation::ValidateParams() {
  473. // size
  474. if (resize_.empty() || resize_.size() > 2) {
  475. std::string err_msg = "DvppDecodeResizeJpeg: resize resolution must be a vector of one or two elements, got: " +
  476. std::to_string(resize_.size());
  477. MS_LOG(ERROR) << err_msg;
  478. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  479. }
  480. if (*min_element(resize_.begin(), resize_.end()) < 32 || *max_element(resize_.begin(), resize_.end()) > 2048) {
  481. std::string err_msg =
  482. "Dvpp module supports resize image with resolution in range [32, 2048], got resize Parameters: ";
  483. if (resize_.size() == 2) {
  484. MS_LOG(ERROR) << err_msg << "[" << resize_[0] << ", " << resize_[1] << "]";
  485. } else {
  486. MS_LOG(ERROR) << err_msg << "[" << resize_[0] << ", " << resize_[0] << "]";
  487. }
  488. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  489. }
  490. return Status::OK();
  491. }
  492. std::shared_ptr<TensorOp> DvppDecodeResizeOperation::Build() {
  493. // If size is a single value, the smaller edge of the image will be
  494. // resized to this value with the same image aspect ratio.
  495. uint32_t resizeHeight, resizeWidth;
  496. // User specified the width value.
  497. if (resize_.size() == 1) {
  498. resizeHeight = resize_[0];
  499. resizeWidth = 0;
  500. } else {
  501. resizeHeight = resize_[0];
  502. resizeWidth = resize_[1];
  503. }
  504. std::shared_ptr<DvppDecodeResizeJpegOp> tensor_op =
  505. std::make_shared<DvppDecodeResizeJpegOp>(resizeHeight, resizeWidth);
  506. return tensor_op;
  507. }
  508. // DvppDecodeResizeCropOperation
  509. DvppDecodeResizeCropOperation::DvppDecodeResizeCropOperation(const std::vector<uint32_t> &crop,
  510. const std::vector<uint32_t> &resize)
  511. : crop_(crop), resize_(resize) {}
  512. Status DvppDecodeResizeCropOperation::ValidateParams() {
  513. // size
  514. if (crop_.empty() || crop_.size() > 2) {
  515. std::string err_msg = "DvppDecodeResizeCropJpeg: crop resolution must be a vector of one or two elements, got: " +
  516. std::to_string(crop_.size());
  517. MS_LOG(ERROR) << err_msg;
  518. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  519. }
  520. if (resize_.empty() || resize_.size() > 2) {
  521. std::string err_msg = "DvppDecodeResizeCropJpeg: resize resolution must be a vector of one or two elements, got: " +
  522. std::to_string(resize_.size());
  523. MS_LOG(ERROR) << err_msg;
  524. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  525. }
  526. if (*min_element(crop_.begin(), crop_.end()) < 32 || *max_element(crop_.begin(), crop_.end()) > 2048) {
  527. std::string err_msg = "Dvpp module supports crop image with resolution in range [32, 2048], got Crop Parameters: ";
  528. if (crop_.size() == 2) {
  529. MS_LOG(ERROR) << err_msg << "[" << crop_[0] << ", " << crop_[1] << "]";
  530. } else {
  531. MS_LOG(ERROR) << err_msg << "[" << crop_[0] << ", " << crop_[0] << "]";
  532. }
  533. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  534. }
  535. if (*min_element(resize_.begin(), resize_.end()) < 32 || *max_element(resize_.begin(), resize_.end()) > 2048) {
  536. std::string err_msg =
  537. "Dvpp module supports resize image with resolution in range [32, 2048], got Crop Parameters: ";
  538. if (resize_.size() == 2) {
  539. MS_LOG(ERROR) << err_msg << "[" << resize_[0] << ", " << resize_[1] << "]";
  540. } else {
  541. MS_LOG(ERROR) << err_msg << "[" << resize_[0] << "]";
  542. }
  543. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  544. }
  545. if (crop_.size() < resize_.size()) {
  546. if (crop_[0] > MIN(resize_[0], resize_[1])) {
  547. std::string err_msg =
  548. "Each value of crop parameter must be smaller than corresponding resize parameter, for example: x[0] <= "
  549. "y[0], and x[1] <= y[1], please verify your input parameters.";
  550. MS_LOG(ERROR) << err_msg;
  551. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  552. }
  553. }
  554. if (crop_.size() > resize_.size()) {
  555. if (MAX(crop_[0], crop_[1]) > resize_[0]) {
  556. std::string err_msg =
  557. "Each value of crop parameter must be smaller than corresponding resize parameter, for example: x[0] <= "
  558. "y[0], and x[1] <= y[1], please verify your input parameters.";
  559. MS_LOG(ERROR) << err_msg;
  560. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  561. }
  562. }
  563. if (crop_.size() == resize_.size()) {
  564. for (int32_t i = 0; i < crop_.size(); ++i) {
  565. if (crop_[i] > resize_[i]) {
  566. std::string err_msg =
  567. "Each value of crop parameter must be smaller than corresponding resize parameter, for example: x[0] <= "
  568. "y[0], and x[1] <= y[1], please verify your input parameters.";
  569. MS_LOG(ERROR) << err_msg;
  570. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  571. }
  572. }
  573. }
  574. return Status::OK();
  575. }
  576. std::shared_ptr<TensorOp> DvppDecodeResizeCropOperation::Build() {
  577. // If size is a single value, the smaller edge of the image will be
  578. // resized to this value with the same image aspect ratio.
  579. uint32_t cropHeight, cropWidth, resizeHeight, resizeWidth;
  580. if (crop_.size() == 1) {
  581. cropHeight = crop_[0];
  582. cropWidth = crop_[0];
  583. } else {
  584. cropHeight = crop_[0];
  585. cropWidth = crop_[1];
  586. }
  587. // User specified the width value.
  588. if (resize_.size() == 1) {
  589. resizeHeight = resize_[0];
  590. resizeWidth = 0;
  591. } else {
  592. resizeHeight = resize_[0];
  593. resizeWidth = resize_[1];
  594. }
  595. std::shared_ptr<DvppDecodeResizeCropJpegOp> tensor_op =
  596. std::make_shared<DvppDecodeResizeCropJpegOp>(cropHeight, cropWidth, resizeHeight, resizeWidth);
  597. return tensor_op;
  598. }
  599. // DvppDecodeJPEG
  600. Status DvppDecodeJpegOperation::ValidateParams() { return Status::OK(); }
  601. std::shared_ptr<TensorOp> DvppDecodeJpegOperation::Build() { return std::make_shared<DvppDecodeJpegOp>(); }
  602. // DvppDecodePNG
  603. Status DvppDecodePngOperation::ValidateParams() { return Status::OK(); }
  604. std::shared_ptr<TensorOp> DvppDecodePngOperation::Build() { return std::make_shared<DvppDecodePngOp>(); }
  605. // DvppResizeOperation
  606. DvppResizeJpegOperation::DvppResizeJpegOperation(const std::vector<uint32_t> &resize) : resize_(resize) {}
  607. Status DvppResizeJpegOperation::ValidateParams() {
  608. // size
  609. if (resize_.empty() || resize_.size() > 2) {
  610. std::string err_msg = "DvppResizeJpeg: resize resolution must be a vector of one or two elements, got: " +
  611. std::to_string(resize_.size());
  612. MS_LOG(ERROR) << err_msg;
  613. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  614. }
  615. if (*min_element(resize_.begin(), resize_.end()) < 32 || *max_element(resize_.begin(), resize_.end()) > 2048) {
  616. std::string err_msg =
  617. "Dvpp module supports resize image with resolution in range [32, 2048], got resize Parameters: ";
  618. if (resize_.size() == 2) {
  619. MS_LOG(ERROR) << err_msg << "[" << resize_[0] << ", " << resize_[1] << "]";
  620. } else {
  621. MS_LOG(ERROR) << err_msg << "[" << resize_[0] << ", " << resize_[0] << "]";
  622. }
  623. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  624. }
  625. return Status::OK();
  626. }
  627. std::shared_ptr<TensorOp> DvppResizeJpegOperation::Build() {
  628. // If size is a single value, the smaller edge of the image will be
  629. // resized to this value with the same image aspect ratio.
  630. uint32_t resizeHeight, resizeWidth;
  631. // User specified the width value.
  632. if (resize_.size() == 1) {
  633. resizeHeight = resize_[0];
  634. resizeWidth = 0;
  635. } else {
  636. resizeHeight = resize_[0];
  637. resizeWidth = resize_[1];
  638. }
  639. std::shared_ptr<DvppResizeJpegOp> tensor_op = std::make_shared<DvppResizeJpegOp>(resizeHeight, resizeWidth);
  640. return tensor_op;
  641. }
  642. #endif
  643. } // namespace vision
  644. } // namespace dataset
  645. } // namespace mindspore