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 81 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029
  1. /**
  2. * Copyright 2020 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/transforms.h"
  17. #include "minddata/dataset/include/vision.h"
  18. #ifndef ENABLE_ANDROID
  19. #include "minddata/dataset/kernels/image/image_utils.h"
  20. #endif
  21. // Kernel image headers (in alphabetical order)
  22. #ifndef ENABLE_ANDROID
  23. #include "minddata/dataset/kernels/image/auto_contrast_op.h"
  24. #include "minddata/dataset/kernels/image/bounding_box_augment_op.h"
  25. #endif
  26. #include "minddata/dataset/kernels/image/center_crop_op.h"
  27. #include "minddata/dataset/kernels/image/crop_op.h"
  28. #ifndef ENABLE_ANDROID
  29. #include "minddata/dataset/kernels/image/cutmix_batch_op.h"
  30. #include "minddata/dataset/kernels/image/cut_out_op.h"
  31. #endif
  32. #include "minddata/dataset/kernels/image/decode_op.h"
  33. #ifndef ENABLE_ANDROID
  34. #include "minddata/dataset/kernels/image/equalize_op.h"
  35. #include "minddata/dataset/kernels/image/hwc_to_chw_op.h"
  36. #include "minddata/dataset/kernels/image/invert_op.h"
  37. #include "minddata/dataset/kernels/image/mixup_batch_op.h"
  38. #endif
  39. #include "minddata/dataset/kernels/image/normalize_op.h"
  40. #ifndef ENABLE_ANDROID
  41. #include "minddata/dataset/kernels/image/pad_op.h"
  42. #include "minddata/dataset/kernels/image/random_affine_op.h"
  43. #include "minddata/dataset/kernels/image/random_color_op.h"
  44. #include "minddata/dataset/kernels/image/random_color_adjust_op.h"
  45. #include "minddata/dataset/kernels/image/random_crop_and_resize_op.h"
  46. #include "minddata/dataset/kernels/image/random_crop_op.h"
  47. #include "minddata/dataset/kernels/image/random_crop_decode_resize_op.h"
  48. #include "minddata/dataset/kernels/image/random_crop_with_bbox_op.h"
  49. #include "minddata/dataset/kernels/image/random_crop_and_resize_with_bbox_op.h"
  50. #include "minddata/dataset/kernels/image/random_horizontal_flip_op.h"
  51. #include "minddata/dataset/kernels/image/random_horizontal_flip_with_bbox_op.h"
  52. #include "minddata/dataset/kernels/image/random_posterize_op.h"
  53. #include "minddata/dataset/kernels/image/random_resize_op.h"
  54. #include "minddata/dataset/kernels/image/random_resize_with_bbox_op.h"
  55. #include "minddata/dataset/kernels/image/random_rotation_op.h"
  56. #include "minddata/dataset/kernels/image/random_select_subpolicy_op.h"
  57. #include "minddata/dataset/kernels/image/random_sharpness_op.h"
  58. #include "minddata/dataset/kernels/image/random_solarize_op.h"
  59. #include "minddata/dataset/kernels/image/random_vertical_flip_op.h"
  60. #include "minddata/dataset/kernels/image/random_vertical_flip_with_bbox_op.h"
  61. #include "minddata/dataset/kernels/image/rescale_op.h"
  62. #endif
  63. #include "minddata/dataset/kernels/image/resize_op.h"
  64. #ifndef ENABLE_ANDROID
  65. #include "minddata/dataset/kernels/image/resize_with_bbox_op.h"
  66. #include "minddata/dataset/kernels/image/rgba_to_bgr_op.h"
  67. #include "minddata/dataset/kernels/image/rgba_to_rgb_op.h"
  68. #include "minddata/dataset/kernels/image/soft_dvpp/soft_dvpp_decode_random_crop_resize_jpeg_op.h"
  69. #include "minddata/dataset/kernels/image/soft_dvpp/soft_dvpp_decode_resize_jpeg_op.h"
  70. #include "minddata/dataset/kernels/image/swap_red_blue_op.h"
  71. #include "minddata/dataset/kernels/image/uniform_aug_op.h"
  72. #endif
  73. namespace mindspore {
  74. namespace dataset {
  75. // Transform operations for computer vision.
  76. namespace vision {
  77. #ifndef ENABLE_ANDROID
  78. // FUNCTIONS TO CREATE VISION TRANSFORM OPERATIONS
  79. // (In alphabetical order)
  80. // Function to create AutoContrastOperation.
  81. std::shared_ptr<AutoContrastOperation> AutoContrast(float cutoff, std::vector<uint32_t> ignore) {
  82. auto op = std::make_shared<AutoContrastOperation>(cutoff, ignore);
  83. // Input validation
  84. return op->ValidateParams() ? op : nullptr;
  85. }
  86. // Function to create BoundingBoxAugmentOperation.
  87. std::shared_ptr<BoundingBoxAugmentOperation> BoundingBoxAugment(std::shared_ptr<TensorOperation> transform,
  88. float ratio) {
  89. auto op = std::make_shared<BoundingBoxAugmentOperation>(transform, ratio);
  90. // Input validation
  91. return op->ValidateParams() ? op : nullptr;
  92. }
  93. #endif
  94. // Function to create CenterCropOperation.
  95. std::shared_ptr<CenterCropOperation> CenterCrop(std::vector<int32_t> size) {
  96. auto op = std::make_shared<CenterCropOperation>(size);
  97. // Input validation
  98. return op->ValidateParams() ? op : nullptr;
  99. }
  100. // Function to create CropOperation.
  101. std::shared_ptr<CropOperation> Crop(std::vector<int32_t> coordinates, std::vector<int32_t> size) {
  102. auto op = std::make_shared<CropOperation>(coordinates, size);
  103. // Input validation
  104. return op->ValidateParams() ? op : nullptr;
  105. }
  106. #ifndef ENABLE_ANDROID
  107. // Function to create CutMixBatchOperation.
  108. std::shared_ptr<CutMixBatchOperation> CutMixBatch(ImageBatchFormat image_batch_format, float alpha, float prob) {
  109. auto op = std::make_shared<CutMixBatchOperation>(image_batch_format, alpha, prob);
  110. // Input validation
  111. return op->ValidateParams() ? op : nullptr;
  112. }
  113. // Function to create CutOutOp.
  114. std::shared_ptr<CutOutOperation> CutOut(int32_t length, int32_t num_patches) {
  115. auto op = std::make_shared<CutOutOperation>(length, num_patches);
  116. // Input validation
  117. return op->ValidateParams() ? op : nullptr;
  118. }
  119. // Function to create DecodeOperation.
  120. std::shared_ptr<DecodeOperation> Decode(bool rgb) {
  121. auto op = std::make_shared<DecodeOperation>(rgb);
  122. // Input validation
  123. return op->ValidateParams() ? op : nullptr;
  124. }
  125. // Function to create EqualizeOperation.
  126. std::shared_ptr<EqualizeOperation> Equalize() {
  127. auto op = std::make_shared<EqualizeOperation>();
  128. // Input validation
  129. return op->ValidateParams() ? op : nullptr;
  130. }
  131. // Function to create HwcToChwOperation.
  132. std::shared_ptr<HwcToChwOperation> HWC2CHW() {
  133. auto op = std::make_shared<HwcToChwOperation>();
  134. // Input validation
  135. return op->ValidateParams() ? op : nullptr;
  136. }
  137. // Function to create InvertOperation.
  138. std::shared_ptr<InvertOperation> Invert() {
  139. auto op = std::make_shared<InvertOperation>();
  140. // Input validation
  141. return op->ValidateParams() ? op : nullptr;
  142. }
  143. // Function to create MixUpBatchOperation.
  144. std::shared_ptr<MixUpBatchOperation> MixUpBatch(float alpha) {
  145. auto op = std::make_shared<MixUpBatchOperation>(alpha);
  146. // Input validation
  147. return op->ValidateParams() ? op : nullptr;
  148. }
  149. #endif
  150. // Function to create NormalizeOperation.
  151. std::shared_ptr<NormalizeOperation> Normalize(std::vector<float> mean, std::vector<float> std) {
  152. auto op = std::make_shared<NormalizeOperation>(mean, std);
  153. // Input validation
  154. return op->ValidateParams() ? op : nullptr;
  155. }
  156. #ifndef ENABLE_ANDROID
  157. // Function to create PadOperation.
  158. std::shared_ptr<PadOperation> Pad(std::vector<int32_t> padding, std::vector<uint8_t> fill_value,
  159. BorderType padding_mode) {
  160. auto op = std::make_shared<PadOperation>(padding, fill_value, padding_mode);
  161. // Input validation
  162. return op->ValidateParams() ? op : nullptr;
  163. }
  164. // Function to create RandomAffineOperation.
  165. std::shared_ptr<RandomAffineOperation> RandomAffine(const std::vector<float_t> &degrees,
  166. const std::vector<float_t> &translate_range,
  167. const std::vector<float_t> &scale_range,
  168. const std::vector<float_t> &shear_ranges,
  169. InterpolationMode interpolation,
  170. const std::vector<uint8_t> &fill_value) {
  171. auto op = std::make_shared<RandomAffineOperation>(degrees, translate_range, scale_range, shear_ranges, interpolation,
  172. fill_value);
  173. // Input validation
  174. return op->ValidateParams() ? op : nullptr;
  175. }
  176. // Function to create RandomColorOperation.
  177. std::shared_ptr<RandomColorOperation> RandomColor(float t_lb, float t_ub) {
  178. auto op = std::make_shared<RandomColorOperation>(t_lb, t_ub);
  179. // Input validation
  180. return op->ValidateParams() ? op : nullptr;
  181. }
  182. std::shared_ptr<TensorOp> RandomColorOperation::Build() {
  183. std::shared_ptr<RandomColorOp> tensor_op = std::make_shared<RandomColorOp>(t_lb_, t_ub_);
  184. return tensor_op;
  185. }
  186. // Function to create RandomColorAdjustOperation.
  187. std::shared_ptr<RandomColorAdjustOperation> RandomColorAdjust(std::vector<float> brightness,
  188. std::vector<float> contrast,
  189. std::vector<float> saturation, std::vector<float> hue) {
  190. auto op = std::make_shared<RandomColorAdjustOperation>(brightness, contrast, saturation, hue);
  191. // Input validation
  192. return op->ValidateParams() ? op : nullptr;
  193. }
  194. // Function to create RandomCropOperation.
  195. std::shared_ptr<RandomCropOperation> RandomCrop(std::vector<int32_t> size, std::vector<int32_t> padding,
  196. bool pad_if_needed, std::vector<uint8_t> fill_value,
  197. BorderType padding_mode) {
  198. auto op = std::make_shared<RandomCropOperation>(size, padding, pad_if_needed, fill_value, padding_mode);
  199. // Input validation
  200. return op->ValidateParams() ? op : nullptr;
  201. }
  202. // Function to create RandomCropDecodeResizeOperation.
  203. std::shared_ptr<RandomCropDecodeResizeOperation> RandomCropDecodeResize(std::vector<int32_t> size,
  204. std::vector<float> scale,
  205. std::vector<float> ratio,
  206. InterpolationMode interpolation,
  207. int32_t max_attempts) {
  208. auto op = std::make_shared<RandomCropDecodeResizeOperation>(size, scale, ratio, interpolation, max_attempts);
  209. // Input validation
  210. return op->ValidateParams() ? op : nullptr;
  211. }
  212. // Function to create RandomCropWithBBoxOperation.
  213. std::shared_ptr<RandomCropWithBBoxOperation> RandomCropWithBBox(std::vector<int32_t> size, std::vector<int32_t> padding,
  214. bool pad_if_needed, std::vector<uint8_t> fill_value,
  215. BorderType padding_mode) {
  216. auto op = std::make_shared<RandomCropWithBBoxOperation>(size, padding, pad_if_needed, fill_value, padding_mode);
  217. // Input validation
  218. return op->ValidateParams() ? op : nullptr;
  219. }
  220. // Function to create RandomHorizontalFlipOperation.
  221. std::shared_ptr<RandomHorizontalFlipOperation> RandomHorizontalFlip(float prob) {
  222. auto op = std::make_shared<RandomHorizontalFlipOperation>(prob);
  223. // Input validation
  224. return op->ValidateParams() ? op : nullptr;
  225. }
  226. // Function to create RandomHorizontalFlipOperation.
  227. std::shared_ptr<RandomHorizontalFlipWithBBoxOperation> RandomHorizontalFlipWithBBox(float prob) {
  228. auto op = std::make_shared<RandomHorizontalFlipWithBBoxOperation>(prob);
  229. // Input validation
  230. return op->ValidateParams() ? op : nullptr;
  231. }
  232. // Function to create RandomPosterizeOperation.
  233. std::shared_ptr<RandomPosterizeOperation> RandomPosterize(const std::vector<uint8_t> &bit_range) {
  234. auto op = std::make_shared<RandomPosterizeOperation>(bit_range);
  235. // Input validation
  236. return op->ValidateParams() ? op : nullptr;
  237. }
  238. // Function to create RandomResizeOperation.
  239. std::shared_ptr<RandomResizeOperation> RandomResize(std::vector<int32_t> size) {
  240. auto op = std::make_shared<RandomResizeOperation>(size);
  241. // Input validation
  242. return op->ValidateParams() ? op : nullptr;
  243. }
  244. // Function to create RandomResizeWithBBoxOperation.
  245. std::shared_ptr<RandomResizeWithBBoxOperation> RandomResizeWithBBox(std::vector<int32_t> size) {
  246. auto op = std::make_shared<RandomResizeWithBBoxOperation>(size);
  247. // Input validation
  248. return op->ValidateParams() ? op : nullptr;
  249. }
  250. // Function to create RandomResizedCropOperation.
  251. std::shared_ptr<RandomResizedCropOperation> RandomResizedCrop(std::vector<int32_t> size, std::vector<float> scale,
  252. std::vector<float> ratio, InterpolationMode interpolation,
  253. int32_t max_attempts) {
  254. auto op = std::make_shared<RandomResizedCropOperation>(size, scale, ratio, interpolation, max_attempts);
  255. // Input validation
  256. return op->ValidateParams() ? op : nullptr;
  257. }
  258. // Function to create RandomResizedCropOperation.
  259. std::shared_ptr<RandomResizedCropWithBBoxOperation> RandomResizedCropWithBBox(std::vector<int32_t> size,
  260. std::vector<float> scale,
  261. std::vector<float> ratio,
  262. InterpolationMode interpolation,
  263. int32_t max_attempts) {
  264. auto op = std::make_shared<RandomResizedCropWithBBoxOperation>(size, scale, ratio, interpolation, max_attempts);
  265. // Input validation
  266. return op->ValidateParams() ? op : nullptr;
  267. }
  268. // Function to create RandomRotationOperation.
  269. std::shared_ptr<RandomRotationOperation> RandomRotation(std::vector<float> degrees, InterpolationMode resample,
  270. bool expand, std::vector<float> center,
  271. std::vector<uint8_t> fill_value) {
  272. auto op = std::make_shared<RandomRotationOperation>(degrees, resample, expand, center, fill_value);
  273. // Input validation
  274. return op->ValidateParams() ? op : nullptr;
  275. }
  276. // Function to create RandomSharpnessOperation.
  277. std::shared_ptr<RandomSharpnessOperation> RandomSharpness(std::vector<float> degrees) {
  278. auto op = std::make_shared<RandomSharpnessOperation>(degrees);
  279. // Input validation
  280. return op->ValidateParams() ? op : nullptr;
  281. }
  282. // Function to create RandomSolarizeOperation.
  283. std::shared_ptr<RandomSolarizeOperation> RandomSolarize(std::vector<uint8_t> threshold) {
  284. auto op = std::make_shared<RandomSolarizeOperation>(threshold);
  285. // Input validation
  286. return op->ValidateParams() ? op : nullptr;
  287. }
  288. // Function to create RandomSelectSubpolicyOperation.
  289. std::shared_ptr<RandomSelectSubpolicyOperation> RandomSelectSubpolicy(
  290. std::vector<std::vector<std::pair<std::shared_ptr<TensorOperation>, double>>> policy) {
  291. auto op = std::make_shared<RandomSelectSubpolicyOperation>(policy);
  292. // Input validation
  293. return op->ValidateParams() ? op : nullptr;
  294. }
  295. // Function to create RandomVerticalFlipOperation.
  296. std::shared_ptr<RandomVerticalFlipOperation> RandomVerticalFlip(float prob) {
  297. auto op = std::make_shared<RandomVerticalFlipOperation>(prob);
  298. // Input validation
  299. return op->ValidateParams() ? op : nullptr;
  300. }
  301. // Function to create RandomVerticalFlipWithBBoxOperation.
  302. std::shared_ptr<RandomVerticalFlipWithBBoxOperation> RandomVerticalFlipWithBBox(float prob) {
  303. auto op = std::make_shared<RandomVerticalFlipWithBBoxOperation>(prob);
  304. // Input validation
  305. return op->ValidateParams() ? op : nullptr;
  306. }
  307. // Function to create RescaleOperation.
  308. std::shared_ptr<RescaleOperation> Rescale(float rescale, float shift) {
  309. auto op = std::make_shared<RescaleOperation>(rescale, shift);
  310. // Input validation
  311. return op->ValidateParams() ? op : nullptr;
  312. }
  313. #endif
  314. // Function to create ResizeOperation.
  315. std::shared_ptr<ResizeOperation> Resize(std::vector<int32_t> size, InterpolationMode interpolation) {
  316. auto op = std::make_shared<ResizeOperation>(size, interpolation);
  317. // Input validation
  318. return op->ValidateParams() ? op : nullptr;
  319. }
  320. #ifndef ENABLE_ANDROID
  321. // Function to create ResizeWithBBoxOperation.
  322. std::shared_ptr<ResizeWithBBoxOperation> ResizeWithBBox(std::vector<int32_t> size, InterpolationMode interpolation) {
  323. auto op = std::make_shared<ResizeWithBBoxOperation>(size, interpolation);
  324. // Input validation
  325. return op->ValidateParams() ? op : nullptr;
  326. }
  327. // Function to create RgbaToBgrOperation.
  328. std::shared_ptr<RgbaToBgrOperation> RGBA2BGR() {
  329. auto op = std::make_shared<RgbaToBgrOperation>();
  330. // Input validation
  331. return op->ValidateParams() ? op : nullptr;
  332. }
  333. // Function to create RgbaToRgbOperation.
  334. std::shared_ptr<RgbaToRgbOperation> RGBA2RGB() {
  335. auto op = std::make_shared<RgbaToRgbOperation>();
  336. // Input validation
  337. return op->ValidateParams() ? op : nullptr;
  338. }
  339. // Function to create SoftDvppDecodeRandomCropResizeJpegOperation.
  340. std::shared_ptr<SoftDvppDecodeRandomCropResizeJpegOperation> SoftDvppDecodeRandomCropResizeJpeg(
  341. std::vector<int32_t> size, std::vector<float> scale, std::vector<float> ratio, int32_t max_attempts) {
  342. auto op = std::make_shared<SoftDvppDecodeRandomCropResizeJpegOperation>(size, scale, ratio, max_attempts);
  343. // Input validation
  344. return op->ValidateParams() ? op : nullptr;
  345. }
  346. // Function to create SoftDvppDecodeResizeJpegOperation.
  347. std::shared_ptr<SoftDvppDecodeResizeJpegOperation> SoftDvppDecodeResizeJpeg(std::vector<int32_t> size) {
  348. auto op = std::make_shared<SoftDvppDecodeResizeJpegOperation>(size);
  349. // Input validation
  350. return op->ValidateParams() ? op : nullptr;
  351. }
  352. // Function to create SwapRedBlueOperation.
  353. std::shared_ptr<SwapRedBlueOperation> SwapRedBlue() {
  354. auto op = std::make_shared<SwapRedBlueOperation>();
  355. // Input validation
  356. return op->ValidateParams() ? op : nullptr;
  357. }
  358. // Function to create UniformAugOperation.
  359. std::shared_ptr<UniformAugOperation> UniformAugment(std::vector<std::shared_ptr<TensorOperation>> transforms,
  360. int32_t num_ops) {
  361. auto op = std::make_shared<UniformAugOperation>(transforms, num_ops);
  362. // Input validation
  363. return op->ValidateParams() ? op : nullptr;
  364. }
  365. #endif
  366. /* ####################################### Derived TensorOperation classes ################################# */
  367. // (In alphabetical order)
  368. #ifndef ENABLE_ANDROID
  369. // AutoContrastOperation
  370. AutoContrastOperation::AutoContrastOperation(float cutoff, std::vector<uint32_t> ignore)
  371. : cutoff_(cutoff), ignore_(ignore) {}
  372. Status AutoContrastOperation::ValidateParams() {
  373. if (cutoff_ < 0 || cutoff_ > 100) {
  374. std::string err_msg = "AutoContrast: cutoff has to be between 0 and 100, got: " + std::to_string(cutoff_);
  375. MS_LOG(ERROR) << err_msg;
  376. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  377. }
  378. for (uint32_t single_ignore : ignore_) {
  379. if (single_ignore > 255) {
  380. std::string err_msg =
  381. "AutoContrast: invalid size, ignore has to be between 0 and 255, got: " + std::to_string(single_ignore);
  382. MS_LOG(ERROR) << err_msg;
  383. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  384. }
  385. }
  386. return Status::OK();
  387. }
  388. std::shared_ptr<TensorOp> AutoContrastOperation::Build() {
  389. std::shared_ptr<AutoContrastOp> tensor_op = std::make_shared<AutoContrastOp>(cutoff_, ignore_);
  390. return tensor_op;
  391. }
  392. // BoundingBoxAugmentOperation
  393. BoundingBoxAugmentOperation::BoundingBoxAugmentOperation(std::shared_ptr<TensorOperation> transform, float ratio)
  394. : transform_(transform), ratio_(ratio) {}
  395. Status BoundingBoxAugmentOperation::ValidateParams() {
  396. RETURN_IF_NOT_OK(ValidateVectorTransforms("BoundingBoxAugment", {transform_}));
  397. RETURN_IF_NOT_OK(ValidateProbability("BoundingBoxAugment", ratio_));
  398. return Status::OK();
  399. }
  400. std::shared_ptr<TensorOp> BoundingBoxAugmentOperation::Build() {
  401. std::shared_ptr<BoundingBoxAugmentOp> tensor_op = std::make_shared<BoundingBoxAugmentOp>(transform_->Build(), ratio_);
  402. return tensor_op;
  403. }
  404. #endif
  405. // CenterCropOperation
  406. CenterCropOperation::CenterCropOperation(std::vector<int32_t> size) : size_(size) {}
  407. Status CenterCropOperation::ValidateParams() {
  408. if (size_.empty() || size_.size() > 2) {
  409. std::string err_msg = "CenterCrop: size vector has incorrect size.";
  410. MS_LOG(ERROR) << err_msg;
  411. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  412. }
  413. // We have to limit crop size due to library restrictions, optimized to only iterate over size_ once
  414. for (int32_t i = 0; i < size_.size(); ++i) {
  415. if (size_[i] <= 0) {
  416. std::string err_msg = "CenterCrop: invalid size, size must be greater than 0, got: " + std::to_string(size_[i]);
  417. MS_LOG(ERROR) << err_msg;
  418. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  419. }
  420. if (size_[i] == INT_MAX) {
  421. std::string err_msg = "CenterCrop: invalid size, size too large, got: " + std::to_string(size_[i]);
  422. MS_LOG(ERROR) << err_msg;
  423. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  424. }
  425. }
  426. return Status::OK();
  427. }
  428. std::shared_ptr<TensorOp> CenterCropOperation::Build() {
  429. int32_t crop_height = size_[0];
  430. int32_t crop_width = size_[0];
  431. // User has specified crop_width.
  432. if (size_.size() == 2) {
  433. crop_width = size_[1];
  434. }
  435. std::shared_ptr<CenterCropOp> tensor_op = std::make_shared<CenterCropOp>(crop_height, crop_width);
  436. return tensor_op;
  437. }
  438. // CropOperation.
  439. CropOperation::CropOperation(std::vector<int32_t> coordinates, std::vector<int32_t> size)
  440. : coordinates_(coordinates), size_(size) {}
  441. Status CropOperation::ValidateParams() {
  442. // Do some input validation.
  443. if (coordinates_.size() != 2) {
  444. std::string err_msg = "Crop: coordinates must be a vector of two values";
  445. MS_LOG(ERROR) << err_msg;
  446. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  447. }
  448. // we don't check the coordinates here because we don't have access to image dimensions
  449. if (size_.empty() || size_.size() > 2) {
  450. std::string err_msg = "Crop: size must be a vector of one or two values";
  451. MS_LOG(ERROR) << err_msg;
  452. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  453. }
  454. // We have to limit crop size due to library restrictions, optimized to only iterate over size_ once
  455. for (int32_t i = 0; i < size_.size(); ++i) {
  456. if (size_[i] <= 0) {
  457. std::string err_msg = "Crop: invalid size, size must be greater than 0, got: " + std::to_string(size_[i]);
  458. MS_LOG(ERROR) << err_msg;
  459. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  460. }
  461. if (size_[i] == INT_MAX) {
  462. std::string err_msg = "Crop: invalid size, size too large, got: " + std::to_string(size_[i]);
  463. MS_LOG(ERROR) << err_msg;
  464. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  465. }
  466. }
  467. for (int32_t j = 0; j < coordinates_.size(); ++j) {
  468. if (coordinates_[j] < 0) {
  469. std::string err_msg =
  470. "Crop: invalid coordinates, coordinates must be greater than 0, got: " + std::to_string(coordinates_[j]);
  471. MS_LOG(ERROR) << err_msg;
  472. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  473. }
  474. }
  475. return Status::OK();
  476. }
  477. std::shared_ptr<TensorOp> CropOperation::Build() {
  478. int32_t x, y, height, width;
  479. x = coordinates_[0];
  480. y = coordinates_[1];
  481. height = size_[0];
  482. width = size_[0];
  483. // User has specified crop_width.
  484. if (size_.size() == 2) {
  485. width = size_[1];
  486. }
  487. std::shared_ptr<CropOp> tensor_op = std::make_shared<CropOp>(x, y, height, width);
  488. return tensor_op;
  489. }
  490. #ifndef ENABLE_ANDROID
  491. // CutMixBatchOperation
  492. CutMixBatchOperation::CutMixBatchOperation(ImageBatchFormat image_batch_format, float alpha, float prob)
  493. : image_batch_format_(image_batch_format), alpha_(alpha), prob_(prob) {}
  494. Status CutMixBatchOperation::ValidateParams() {
  495. if (alpha_ <= 0) {
  496. std::string err_msg =
  497. "CutMixBatch: alpha must be a positive floating value however it is: " + std::to_string(alpha_);
  498. MS_LOG(ERROR) << err_msg;
  499. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  500. }
  501. if (prob_ < 0 || prob_ > 1) {
  502. std::string err_msg = "CutMixBatch: Probability has to be between 0 and 1.";
  503. MS_LOG(ERROR) << err_msg;
  504. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  505. }
  506. return Status::OK();
  507. }
  508. std::shared_ptr<TensorOp> CutMixBatchOperation::Build() {
  509. std::shared_ptr<CutMixBatchOp> tensor_op = std::make_shared<CutMixBatchOp>(image_batch_format_, alpha_, prob_);
  510. return tensor_op;
  511. }
  512. // CutOutOperation
  513. CutOutOperation::CutOutOperation(int32_t length, int32_t num_patches) : length_(length), num_patches_(num_patches) {}
  514. Status CutOutOperation::ValidateParams() {
  515. if (length_ <= 0) {
  516. std::string err_msg = "CutOut: length must be positive, got: " + std::to_string(length_);
  517. MS_LOG(ERROR) << err_msg;
  518. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  519. }
  520. if (num_patches_ <= 0) {
  521. std::string err_msg = "CutOut: number of patches must be positive, got: " + std::to_string(num_patches_);
  522. MS_LOG(ERROR) << err_msg;
  523. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  524. }
  525. return Status::OK();
  526. }
  527. std::shared_ptr<TensorOp> CutOutOperation::Build() {
  528. std::shared_ptr<CutOutOp> tensor_op = std::make_shared<CutOutOp>(length_, length_, num_patches_, false, 0, 0, 0);
  529. return tensor_op;
  530. }
  531. // DecodeOperation
  532. DecodeOperation::DecodeOperation(bool rgb) : rgb_(rgb) {}
  533. Status DecodeOperation::ValidateParams() { return Status::OK(); }
  534. std::shared_ptr<TensorOp> DecodeOperation::Build() { return std::make_shared<DecodeOp>(rgb_); }
  535. // EqualizeOperation
  536. Status EqualizeOperation::ValidateParams() { return Status::OK(); }
  537. std::shared_ptr<TensorOp> EqualizeOperation::Build() { return std::make_shared<EqualizeOp>(); }
  538. // HwcToChwOperation
  539. Status HwcToChwOperation::ValidateParams() { return Status::OK(); }
  540. std::shared_ptr<TensorOp> HwcToChwOperation::Build() { return std::make_shared<HwcToChwOp>(); }
  541. // InvertOperation
  542. Status InvertOperation::ValidateParams() { return Status::OK(); }
  543. std::shared_ptr<TensorOp> InvertOperation::Build() { return std::make_shared<InvertOp>(); }
  544. // MixUpOperation
  545. MixUpBatchOperation::MixUpBatchOperation(float alpha) : alpha_(alpha) {}
  546. Status MixUpBatchOperation::ValidateParams() {
  547. if (alpha_ <= 0) {
  548. std::string err_msg =
  549. "MixUpBatch: alpha must be a positive floating value however it is: " + std::to_string(alpha_);
  550. MS_LOG(ERROR) << err_msg;
  551. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  552. }
  553. return Status::OK();
  554. }
  555. std::shared_ptr<TensorOp> MixUpBatchOperation::Build() { return std::make_shared<MixUpBatchOp>(alpha_); }
  556. #endif
  557. // NormalizeOperation
  558. NormalizeOperation::NormalizeOperation(std::vector<float> mean, std::vector<float> std) : mean_(mean), std_(std) {}
  559. Status NormalizeOperation::ValidateParams() {
  560. if (mean_.size() != 3) {
  561. std::string err_msg = "Normalize: mean vector has incorrect size: " + std::to_string(mean_.size());
  562. MS_LOG(ERROR) << err_msg;
  563. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  564. }
  565. if (std_.size() != 3) {
  566. std::string err_msg = "Normalize: std vector has incorrect size: " + std::to_string(std_.size());
  567. MS_LOG(ERROR) << err_msg;
  568. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  569. }
  570. // check std/mean value
  571. for (int32_t i = 0; i < std_.size(); ++i) {
  572. if (std_[i] < 0.0f || std_[i] > 255.0f || CmpFloat(std_[i], 0.0f)) {
  573. std::string err_msg = "Normalize: std vector has incorrect value: " + std::to_string(std_[i]);
  574. MS_LOG(ERROR) << err_msg;
  575. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  576. }
  577. if (mean_[i] < 0.0f || mean_[i] > 255.0f || CmpFloat(mean_[i], 0.0f)) {
  578. std::string err_msg = "Normalize: mean vector has incorrect value: " + std::to_string(mean_[i]);
  579. MS_LOG(ERROR) << err_msg;
  580. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  581. }
  582. }
  583. return Status::OK();
  584. }
  585. std::shared_ptr<TensorOp> NormalizeOperation::Build() {
  586. return std::make_shared<NormalizeOp>(mean_[0], mean_[1], mean_[2], std_[0], std_[1], std_[2]);
  587. }
  588. #ifndef ENABLE_ANDROID
  589. // PadOperation
  590. PadOperation::PadOperation(std::vector<int32_t> padding, std::vector<uint8_t> fill_value, BorderType padding_mode)
  591. : padding_(padding), fill_value_(fill_value), padding_mode_(padding_mode) {}
  592. Status PadOperation::ValidateParams() {
  593. // padding
  594. RETURN_IF_NOT_OK(ValidateVectorPadding("Pad", padding_));
  595. // fill_value
  596. RETURN_IF_NOT_OK(ValidateVectorFillvalue("Pad", fill_value_));
  597. return Status::OK();
  598. }
  599. std::shared_ptr<TensorOp> PadOperation::Build() {
  600. int32_t pad_top, pad_bottom, pad_left, pad_right;
  601. switch (padding_.size()) {
  602. case 1:
  603. pad_left = padding_[0];
  604. pad_top = padding_[0];
  605. pad_right = padding_[0];
  606. pad_bottom = padding_[0];
  607. break;
  608. case 2:
  609. pad_left = padding_[0];
  610. pad_top = padding_[1];
  611. pad_right = padding_[0];
  612. pad_bottom = padding_[1];
  613. break;
  614. default:
  615. pad_left = padding_[0];
  616. pad_top = padding_[1];
  617. pad_right = padding_[2];
  618. pad_bottom = padding_[3];
  619. }
  620. uint8_t fill_r, fill_g, fill_b;
  621. fill_r = fill_value_[0];
  622. fill_g = fill_value_[0];
  623. fill_b = fill_value_[0];
  624. if (fill_value_.size() == 3) {
  625. fill_r = fill_value_[0];
  626. fill_g = fill_value_[1];
  627. fill_b = fill_value_[2];
  628. }
  629. std::shared_ptr<PadOp> tensor_op =
  630. std::make_shared<PadOp>(pad_top, pad_bottom, pad_left, pad_right, padding_mode_, fill_r, fill_g, fill_b);
  631. return tensor_op;
  632. }
  633. // RandomAffineOperation
  634. RandomAffineOperation::RandomAffineOperation(const std::vector<float_t> &degrees,
  635. const std::vector<float_t> &translate_range,
  636. const std::vector<float_t> &scale_range,
  637. const std::vector<float_t> &shear_ranges, InterpolationMode interpolation,
  638. const std::vector<uint8_t> &fill_value)
  639. : degrees_(degrees),
  640. translate_range_(translate_range),
  641. scale_range_(scale_range),
  642. shear_ranges_(shear_ranges),
  643. interpolation_(interpolation),
  644. fill_value_(fill_value) {
  645. random_op_ = true;
  646. }
  647. Status RandomAffineOperation::ValidateParams() {
  648. // Degrees
  649. if (degrees_.size() != 2) {
  650. std::string err_msg =
  651. "RandomAffine: degrees expecting size 2, got: degrees.size() = " + std::to_string(degrees_.size());
  652. MS_LOG(ERROR) << err_msg;
  653. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  654. }
  655. if (degrees_[0] > degrees_[1]) {
  656. std::string err_msg =
  657. "RandomAffine: minimum of degrees range is greater than maximum: min = " + std::to_string(degrees_[0]) +
  658. ", max = " + std::to_string(degrees_[1]);
  659. MS_LOG(ERROR) << err_msg;
  660. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  661. }
  662. // Translate
  663. if (translate_range_.size() != 2 && translate_range_.size() != 4) {
  664. std::string err_msg = "RandomAffine: translate_range expecting size 2 or 4, got: translate_range.size() = " +
  665. std::to_string(translate_range_.size());
  666. MS_LOG(ERROR) << err_msg;
  667. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  668. }
  669. if (translate_range_[0] > translate_range_[1]) {
  670. std::string err_msg = "RandomAffine: minimum of translate range on x is greater than maximum: min = " +
  671. std::to_string(translate_range_[0]) + ", max = " + std::to_string(translate_range_[1]);
  672. MS_LOG(ERROR) << err_msg;
  673. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  674. }
  675. if (translate_range_[0] < -1 || translate_range_[0] > 1) {
  676. std::string err_msg = "RandomAffine: minimum of translate range on x is out of range of [-1, 1], value = " +
  677. std::to_string(translate_range_[0]);
  678. MS_LOG(ERROR) << err_msg;
  679. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  680. }
  681. if (translate_range_[1] < -1 || translate_range_[1] > 1) {
  682. std::string err_msg = "RandomAffine: maximum of translate range on x is out of range of [-1, 1], value = " +
  683. std::to_string(translate_range_[1]);
  684. MS_LOG(ERROR) << err_msg;
  685. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  686. }
  687. if (translate_range_.size() == 4) {
  688. if (translate_range_[2] > translate_range_[3]) {
  689. std::string err_msg = "RandomAffine: minimum of translate range on y is greater than maximum: min = " +
  690. std::to_string(translate_range_[2]) + ", max = " + std::to_string(translate_range_[3]);
  691. MS_LOG(ERROR) << err_msg;
  692. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  693. }
  694. if (translate_range_[2] < -1 || translate_range_[2] > 1) {
  695. std::string err_msg = "RandomAffine: minimum of translate range on y is out of range of [-1, 1], value = " +
  696. std::to_string(translate_range_[2]);
  697. MS_LOG(ERROR) << err_msg;
  698. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  699. }
  700. if (translate_range_[3] < -1 || translate_range_[3] > 1) {
  701. std::string err_msg = "RandomAffine: maximum of translate range on y is out of range of [-1, 1], value = " +
  702. std::to_string(translate_range_[3]);
  703. MS_LOG(ERROR) << err_msg;
  704. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  705. }
  706. }
  707. // Scale
  708. if (scale_range_.size() != 2) {
  709. std::string err_msg = "RandomAffine: scale_range vector has incorrect size: scale_range.size() = " +
  710. std::to_string(scale_range_.size());
  711. MS_LOG(ERROR) << err_msg;
  712. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  713. }
  714. if (scale_range_[0] < 0) {
  715. std::string err_msg =
  716. "RandomAffine: min scale range must be greater than or equal to 0, got: " + std::to_string(scale_range_[0]);
  717. MS_LOG(ERROR) << err_msg;
  718. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  719. }
  720. if (scale_range_[1] <= 0) {
  721. std::string err_msg =
  722. "RandomAffine: max scale range must be greater than 0, got: " + std::to_string(scale_range_[1]);
  723. MS_LOG(ERROR) << err_msg;
  724. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  725. }
  726. if (scale_range_[0] > scale_range_[1]) {
  727. std::string err_msg =
  728. "RandomAffine: minimum of scale range is greater than maximum: min = " + std::to_string(scale_range_[0]) +
  729. ", max = " + std::to_string(scale_range_[1]);
  730. MS_LOG(ERROR) << err_msg;
  731. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  732. }
  733. // Shear
  734. if (shear_ranges_.size() != 2 && shear_ranges_.size() != 4) {
  735. std::string err_msg = "RandomAffine: shear_ranges expecting size 2 or 4, got: shear_ranges.size() = " +
  736. std::to_string(shear_ranges_.size());
  737. MS_LOG(ERROR) << err_msg;
  738. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  739. }
  740. if (shear_ranges_[0] > shear_ranges_[1]) {
  741. std::string err_msg = "RandomAffine: minimum of horizontal shear range is greater than maximum: min = " +
  742. std::to_string(shear_ranges_[0]) + ", max = " + std::to_string(shear_ranges_[1]);
  743. MS_LOG(ERROR) << err_msg;
  744. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  745. }
  746. if (shear_ranges_.size() == 4 && shear_ranges_[2] > shear_ranges_[3]) {
  747. std::string err_msg = "RandomAffine: minimum of vertical shear range is greater than maximum: min = " +
  748. std::to_string(shear_ranges_[2]) + ", max = " + std::to_string(scale_range_[3]);
  749. MS_LOG(ERROR) << err_msg;
  750. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  751. }
  752. // Fill Value
  753. if (fill_value_.size() != 3) {
  754. std::string err_msg =
  755. "RandomAffine: fill_value vector has incorrect size: fill_value.size() = " + std::to_string(fill_value_.size());
  756. MS_LOG(ERROR) << err_msg;
  757. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  758. }
  759. for (int32_t i = 0; i < fill_value_.size(); ++i) {
  760. if (fill_value_[i] < 0 || fill_value_[i] > 255) {
  761. std::string err_msg =
  762. "RandomAffine: fill_value has to be between 0 and 255, got:" + std::to_string(fill_value_[i]);
  763. MS_LOG(ERROR) << err_msg;
  764. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  765. }
  766. }
  767. return Status::OK();
  768. }
  769. std::shared_ptr<TensorOp> RandomAffineOperation::Build() {
  770. if (shear_ranges_.size() == 2) {
  771. shear_ranges_.resize(4);
  772. }
  773. if (translate_range_.size() == 2) {
  774. translate_range_.resize(4);
  775. }
  776. auto tensor_op = std::make_shared<RandomAffineOp>(degrees_, translate_range_, scale_range_, shear_ranges_,
  777. interpolation_, fill_value_);
  778. return tensor_op;
  779. }
  780. // RandomColorOperation.
  781. RandomColorOperation::RandomColorOperation(float t_lb, float t_ub) : t_lb_(t_lb), t_ub_(t_ub) { random_op_ = true; }
  782. Status RandomColorOperation::ValidateParams() {
  783. // Do some input validation.
  784. if (t_lb_ < 0 || t_ub_ < 0) {
  785. std::string err_msg =
  786. "RandomColor: lower bound or upper bound must be greater than or equal to 0, got t_lb: " + std::to_string(t_lb_) +
  787. ", t_ub: " + std::to_string(t_ub_);
  788. MS_LOG(ERROR) << err_msg;
  789. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  790. }
  791. if (t_lb_ > t_ub_) {
  792. std::string err_msg =
  793. "RandomColor: lower bound must be less or equal to upper bound, got t_lb: " + std::to_string(t_lb_) +
  794. ", t_ub: " + std::to_string(t_ub_);
  795. MS_LOG(ERROR) << err_msg;
  796. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  797. }
  798. return Status::OK();
  799. }
  800. // RandomColorAdjustOperation.
  801. RandomColorAdjustOperation::RandomColorAdjustOperation(std::vector<float> brightness, std::vector<float> contrast,
  802. std::vector<float> saturation, std::vector<float> hue)
  803. : brightness_(brightness), contrast_(contrast), saturation_(saturation), hue_(hue) {
  804. random_op_ = true;
  805. }
  806. Status RandomColorAdjustOperation::ValidateParams() {
  807. // brightness
  808. if (brightness_.empty() || brightness_.size() > 2) {
  809. std::string err_msg =
  810. "RandomColorAdjust: brightness must be a vector of one or two values, got: " + std::to_string(brightness_.size());
  811. MS_LOG(ERROR) << err_msg;
  812. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  813. }
  814. for (int32_t i = 0; i < brightness_.size(); ++i) {
  815. if (brightness_[i] < 0) {
  816. std::string err_msg =
  817. "RandomColorAdjust: brightness must be greater than or equal to 0, got: " + std::to_string(brightness_[i]);
  818. MS_LOG(ERROR) << err_msg;
  819. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  820. }
  821. }
  822. if (brightness_.size() == 2 && (brightness_[0] > brightness_[1])) {
  823. std::string err_msg = "RandomColorAdjust: brightness lower bound must be less or equal to upper bound, got lb: " +
  824. std::to_string(brightness_[0]) + ", ub: " + std::to_string(brightness_[1]);
  825. MS_LOG(ERROR) << err_msg;
  826. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  827. }
  828. // contrast
  829. if (contrast_.empty() || contrast_.size() > 2) {
  830. std::string err_msg =
  831. "RandomColorAdjust: contrast must be a vector of one or two values, got: " + std::to_string(contrast_.size());
  832. MS_LOG(ERROR) << err_msg;
  833. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  834. }
  835. for (int32_t i = 0; i < contrast_.size(); ++i) {
  836. if (contrast_[i] < 0) {
  837. std::string err_msg =
  838. "RandomColorAdjust: contrast must be greater than or equal to 0, got: " + std::to_string(contrast_[i]);
  839. MS_LOG(ERROR) << err_msg;
  840. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  841. }
  842. }
  843. if (contrast_.size() == 2 && (contrast_[0] > contrast_[1])) {
  844. std::string err_msg = "RandomColorAdjust: contrast lower bound must be less or equal to upper bound, got lb: " +
  845. std::to_string(contrast_[0]) + ", ub: " + std::to_string(contrast_[1]);
  846. MS_LOG(ERROR) << err_msg;
  847. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  848. }
  849. // saturation
  850. if (saturation_.empty() || saturation_.size() > 2) {
  851. std::string err_msg =
  852. "RandomColorAdjust: saturation must be a vector of one or two values, got: " + std::to_string(saturation_.size());
  853. MS_LOG(ERROR) << err_msg;
  854. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  855. }
  856. for (int32_t i = 0; i < saturation_.size(); ++i) {
  857. if (saturation_[i] < 0) {
  858. std::string err_msg =
  859. "RandomColorAdjust: saturation must be greater than or equal to 0, got: " + std::to_string(saturation_[i]);
  860. MS_LOG(ERROR) << err_msg;
  861. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  862. }
  863. }
  864. if (saturation_.size() == 2 && (saturation_[0] > saturation_[1])) {
  865. std::string err_msg = "RandomColorAdjust: saturation lower bound must be less or equal to upper bound, got lb: " +
  866. std::to_string(saturation_[0]) + ", ub: " + std::to_string(saturation_[1]);
  867. MS_LOG(ERROR) << err_msg;
  868. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  869. }
  870. // hue
  871. if (hue_.empty() || hue_.size() > 2) {
  872. std::string err_msg =
  873. "RandomColorAdjust: hue must be a vector of one or two values, got: " + std::to_string(hue_.size());
  874. MS_LOG(ERROR) << err_msg;
  875. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  876. }
  877. for (int32_t i = 0; i < hue_.size(); ++i) {
  878. if (hue_[i] < -0.5 || hue_[i] > 0.5) {
  879. std::string err_msg = "RandomColorAdjust: hue has to be between -0.5 and 0.5, got: " + std::to_string(hue_[i]);
  880. MS_LOG(ERROR) << err_msg;
  881. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  882. }
  883. }
  884. if (hue_.size() == 2 && (hue_[0] > hue_[1])) {
  885. std::string err_msg =
  886. "RandomColorAdjust: hue lower bound must be less or equal to upper bound, got lb: " + std::to_string(hue_[0]) +
  887. ", ub: " + std::to_string(hue_[1]);
  888. MS_LOG(ERROR) << err_msg;
  889. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  890. }
  891. return Status::OK();
  892. }
  893. std::shared_ptr<TensorOp> RandomColorAdjustOperation::Build() {
  894. float brightness_lb, brightness_ub, contrast_lb, contrast_ub, saturation_lb, saturation_ub, hue_lb, hue_ub;
  895. brightness_lb = brightness_[0];
  896. brightness_ub = brightness_[0];
  897. if (brightness_.size() == 2) brightness_ub = brightness_[1];
  898. contrast_lb = contrast_[0];
  899. contrast_ub = contrast_[0];
  900. if (contrast_.size() == 2) contrast_ub = contrast_[1];
  901. saturation_lb = saturation_[0];
  902. saturation_ub = saturation_[0];
  903. if (saturation_.size() == 2) saturation_ub = saturation_[1];
  904. hue_lb = hue_[0];
  905. hue_ub = hue_[0];
  906. if (hue_.size() == 2) hue_ub = hue_[1];
  907. std::shared_ptr<RandomColorAdjustOp> tensor_op = std::make_shared<RandomColorAdjustOp>(
  908. brightness_lb, brightness_ub, contrast_lb, contrast_ub, saturation_lb, saturation_ub, hue_lb, hue_ub);
  909. return tensor_op;
  910. }
  911. // RandomCropOperation
  912. RandomCropOperation::RandomCropOperation(std::vector<int32_t> size, std::vector<int32_t> padding, bool pad_if_needed,
  913. std::vector<uint8_t> fill_value, BorderType padding_mode)
  914. : TensorOperation(true),
  915. size_(size),
  916. padding_(padding),
  917. pad_if_needed_(pad_if_needed),
  918. fill_value_(fill_value),
  919. padding_mode_(padding_mode) {
  920. random_op_ = true;
  921. }
  922. Status RandomCropOperation::ValidateParams() {
  923. // size
  924. if (size_.empty() || size_.size() > 2) {
  925. std::string err_msg = "RandomCrop: size must be a vector of one or two values";
  926. MS_LOG(ERROR) << err_msg;
  927. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  928. }
  929. RETURN_IF_NOT_OK(ValidateVectorPositive("RandomCrop", size_));
  930. // padding
  931. RETURN_IF_NOT_OK(ValidateVectorPadding("RandomCrop", padding_));
  932. // fill_value
  933. RETURN_IF_NOT_OK(ValidateVectorFillvalue("RandomCrop", fill_value_));
  934. return Status::OK();
  935. }
  936. std::shared_ptr<TensorOp> RandomCropOperation::Build() {
  937. int32_t crop_height = size_[0];
  938. int32_t crop_width = size_[0];
  939. // User has specified the crop_width value.
  940. if (size_.size() == 2) {
  941. crop_width = size_[1];
  942. }
  943. int32_t pad_top, pad_bottom, pad_left, pad_right;
  944. switch (padding_.size()) {
  945. case 1:
  946. pad_left = padding_[0];
  947. pad_top = padding_[0];
  948. pad_right = padding_[0];
  949. pad_bottom = padding_[0];
  950. break;
  951. case 2:
  952. pad_left = padding_[0];
  953. pad_top = padding_[1];
  954. pad_right = padding_[0];
  955. pad_bottom = padding_[1];
  956. break;
  957. default:
  958. pad_left = padding_[0];
  959. pad_top = padding_[1];
  960. pad_right = padding_[2];
  961. pad_bottom = padding_[3];
  962. }
  963. uint8_t fill_r, fill_g, fill_b;
  964. fill_r = fill_value_[0];
  965. fill_g = fill_value_[0];
  966. fill_b = fill_value_[0];
  967. if (fill_value_.size() == 3) {
  968. fill_r = fill_value_[0];
  969. fill_g = fill_value_[1];
  970. fill_b = fill_value_[2];
  971. }
  972. auto tensor_op = std::make_shared<RandomCropOp>(crop_height, crop_width, pad_top, pad_bottom, pad_left, pad_right,
  973. padding_mode_, pad_if_needed_, fill_r, fill_g, fill_b);
  974. return tensor_op;
  975. }
  976. // RandomCropDecodeResizeOperation
  977. RandomCropDecodeResizeOperation::RandomCropDecodeResizeOperation(std::vector<int32_t> size, std::vector<float> scale,
  978. std::vector<float> ratio,
  979. InterpolationMode interpolation, int32_t max_attempts)
  980. : TensorOperation(true),
  981. size_(size),
  982. scale_(scale),
  983. ratio_(ratio),
  984. interpolation_(interpolation),
  985. max_attempts_(max_attempts) {}
  986. Status RandomCropDecodeResizeOperation::ValidateParams() {
  987. // size
  988. if (size_.empty() || size_.size() > 2) {
  989. std::string err_msg = "RandomCropDecodeResize: size vector has incorrect size: " + std::to_string(size_.size());
  990. MS_LOG(ERROR) << err_msg;
  991. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  992. }
  993. RETURN_IF_NOT_OK(ValidateVectorPositive("RandomCropDecodeResize", size_));
  994. // rescale
  995. if (scale_.empty() || scale_.size() != 2) {
  996. std::string err_msg = "RandomCropDecodeResize: scale vector has incorrect size: " + std::to_string(scale_.size());
  997. MS_LOG(ERROR) << err_msg;
  998. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  999. }
  1000. if (scale_[0] < 0) {
  1001. std::string err_msg = "RandomCropDecodeResize: invalid scale, min scale must be greater than or equal to 0, got: " +
  1002. std::to_string(scale_[0]);
  1003. MS_LOG(ERROR) << err_msg;
  1004. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1005. }
  1006. if (scale_[1] <= 0) {
  1007. std::string err_msg =
  1008. "RandomCropDecodeResize: invalid scale, max scale must be greater than 0, got: " + std::to_string(scale_[1]);
  1009. MS_LOG(ERROR) << err_msg;
  1010. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1011. }
  1012. if (scale_[0] > scale_[1]) {
  1013. std::string err_msg = "RandomCropDecodeResize: scale should be in (min,max) format. Got (max,min).";
  1014. MS_LOG(ERROR) << err_msg;
  1015. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1016. }
  1017. // ratio
  1018. if (ratio_.empty() || ratio_.size() != 2) {
  1019. std::string err_msg = "RandomCropDecodeResize: ratio vector has incorrect size: " + std::to_string(ratio_.size());
  1020. MS_LOG(ERROR) << err_msg;
  1021. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1022. }
  1023. for (int32_t i = 0; i < ratio_.size(); ++i) {
  1024. if (ratio_[i] <= 0) {
  1025. std::string err_msg =
  1026. "RandomCropDecodeResize: invalid ratio, ratio must be greater than 0, got: " + std::to_string(ratio_[i]);
  1027. MS_LOG(ERROR) << err_msg;
  1028. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1029. }
  1030. }
  1031. if (ratio_[0] > ratio_[1]) {
  1032. std::string err_msg = "RandomCropDecodeResize: ratio should be in (min,max) format. Got (max,min).";
  1033. MS_LOG(ERROR) << err_msg;
  1034. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1035. }
  1036. // max_attempts
  1037. if (max_attempts_ < 1) {
  1038. std::string err_msg =
  1039. "RandomCropDecodeResize: max_attempts must be greater than or equal to 1, got: " + std::to_string(max_attempts_);
  1040. MS_LOG(ERROR) << err_msg;
  1041. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1042. }
  1043. return Status::OK();
  1044. }
  1045. std::shared_ptr<TensorOp> RandomCropDecodeResizeOperation::Build() {
  1046. int32_t crop_height = size_[0];
  1047. int32_t crop_width = size_[0];
  1048. // User has specified the crop_width value.
  1049. if (size_.size() == 2) {
  1050. crop_width = size_[1];
  1051. }
  1052. float scale_lower_bound = scale_[0];
  1053. float scale_upper_bound = scale_[1];
  1054. float aspect_lower_bound = ratio_[0];
  1055. float aspect_upper_bound = ratio_[1];
  1056. auto tensor_op =
  1057. std::make_shared<RandomCropDecodeResizeOp>(crop_height, crop_width, scale_lower_bound, scale_upper_bound,
  1058. aspect_lower_bound, aspect_upper_bound, interpolation_, max_attempts_);
  1059. return tensor_op;
  1060. }
  1061. // RandomCropWithBBoxOperation
  1062. RandomCropWithBBoxOperation::RandomCropWithBBoxOperation(std::vector<int32_t> size, std::vector<int32_t> padding,
  1063. bool pad_if_needed, std::vector<uint8_t> fill_value,
  1064. BorderType padding_mode)
  1065. : TensorOperation(true),
  1066. size_(size),
  1067. padding_(padding),
  1068. pad_if_needed_(pad_if_needed),
  1069. fill_value_(fill_value),
  1070. padding_mode_(padding_mode) {}
  1071. Status RandomCropWithBBoxOperation::ValidateParams() {
  1072. // size
  1073. if (size_.empty() || size_.size() > 2) {
  1074. std::string err_msg = "RandomCropWithBBox: size must be a vector of one or two values";
  1075. MS_LOG(ERROR) << err_msg;
  1076. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1077. }
  1078. RETURN_IF_NOT_OK(ValidateVectorPositive("RandomCropWithBBox", size_));
  1079. // padding
  1080. RETURN_IF_NOT_OK(ValidateVectorPadding("RandomCropWithBBox", padding_));
  1081. // fill_value
  1082. RETURN_IF_NOT_OK(ValidateVectorFillvalue("RandomCropWithBBox", fill_value_));
  1083. return Status::OK();
  1084. }
  1085. std::shared_ptr<TensorOp> RandomCropWithBBoxOperation::Build() {
  1086. int32_t crop_height = size_[0];
  1087. int32_t crop_width = size_[0];
  1088. // User has specified the crop_width value.
  1089. if (size_.size() == 2) {
  1090. crop_width = size_[1];
  1091. }
  1092. int32_t pad_top, pad_bottom, pad_left, pad_right;
  1093. switch (padding_.size()) {
  1094. case 1:
  1095. pad_left = padding_[0];
  1096. pad_top = padding_[0];
  1097. pad_right = padding_[0];
  1098. pad_bottom = padding_[0];
  1099. break;
  1100. case 2:
  1101. pad_left = padding_[0];
  1102. pad_top = padding_[1];
  1103. pad_right = padding_[0];
  1104. pad_bottom = padding_[1];
  1105. break;
  1106. default:
  1107. pad_left = padding_[0];
  1108. pad_top = padding_[1];
  1109. pad_right = padding_[2];
  1110. pad_bottom = padding_[3];
  1111. }
  1112. uint8_t fill_r, fill_g, fill_b;
  1113. fill_r = fill_value_[0];
  1114. fill_g = fill_value_[0];
  1115. fill_b = fill_value_[0];
  1116. if (fill_value_.size() == 3) {
  1117. fill_r = fill_value_[0];
  1118. fill_g = fill_value_[1];
  1119. fill_b = fill_value_[2];
  1120. }
  1121. auto tensor_op =
  1122. std::make_shared<RandomCropWithBBoxOp>(crop_height, crop_width, pad_top, pad_bottom, pad_left, pad_right,
  1123. padding_mode_, pad_if_needed_, fill_r, fill_g, fill_b);
  1124. return tensor_op;
  1125. }
  1126. // RandomHorizontalFlipOperation
  1127. RandomHorizontalFlipOperation::RandomHorizontalFlipOperation(float probability)
  1128. : TensorOperation(true), probability_(probability) {}
  1129. Status RandomHorizontalFlipOperation::ValidateParams() {
  1130. RETURN_IF_NOT_OK(ValidateProbability("RandomHorizontalFlip", probability_));
  1131. return Status::OK();
  1132. }
  1133. std::shared_ptr<TensorOp> RandomHorizontalFlipOperation::Build() {
  1134. std::shared_ptr<RandomHorizontalFlipOp> tensor_op = std::make_shared<RandomHorizontalFlipOp>(probability_);
  1135. return tensor_op;
  1136. }
  1137. // RandomHorizontalFlipWithBBoxOperation
  1138. RandomHorizontalFlipWithBBoxOperation::RandomHorizontalFlipWithBBoxOperation(float probability)
  1139. : TensorOperation(true), probability_(probability) {}
  1140. Status RandomHorizontalFlipWithBBoxOperation::ValidateParams() {
  1141. RETURN_IF_NOT_OK(ValidateProbability("RandomHorizontalFlipWithBBox", probability_));
  1142. return Status::OK();
  1143. }
  1144. std::shared_ptr<TensorOp> RandomHorizontalFlipWithBBoxOperation::Build() {
  1145. std::shared_ptr<RandomHorizontalFlipWithBBoxOp> tensor_op =
  1146. std::make_shared<RandomHorizontalFlipWithBBoxOp>(probability_);
  1147. return tensor_op;
  1148. }
  1149. // RandomPosterizeOperation
  1150. RandomPosterizeOperation::RandomPosterizeOperation(const std::vector<uint8_t> &bit_range)
  1151. : TensorOperation(true), bit_range_(bit_range) {}
  1152. Status RandomPosterizeOperation::ValidateParams() {
  1153. if (bit_range_.size() != 2) {
  1154. std::string err_msg =
  1155. "RandomPosterize: bit_range needs to be of size 2 but is of size: " + std::to_string(bit_range_.size());
  1156. MS_LOG(ERROR) << err_msg;
  1157. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1158. }
  1159. if (bit_range_[0] < 1 || bit_range_[0] > 8) {
  1160. std::string err_msg = "RandomPosterize: min_bit value is out of range [1-8]: " + std::to_string(bit_range_[0]);
  1161. MS_LOG(ERROR) << err_msg;
  1162. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1163. }
  1164. if (bit_range_[1] < 1 || bit_range_[1] > 8) {
  1165. std::string err_msg = "RandomPosterize: max_bit value is out of range [1-8]: " + std::to_string(bit_range_[1]);
  1166. MS_LOG(ERROR) << err_msg;
  1167. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1168. }
  1169. if (bit_range_[1] < bit_range_[0]) {
  1170. std::string err_msg = "RandomPosterize: max_bit value is less than min_bit: max =" + std::to_string(bit_range_[1]) +
  1171. ", min = " + std::to_string(bit_range_[0]);
  1172. MS_LOG(ERROR) << err_msg;
  1173. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1174. }
  1175. return Status::OK();
  1176. }
  1177. std::shared_ptr<TensorOp> RandomPosterizeOperation::Build() {
  1178. std::shared_ptr<RandomPosterizeOp> tensor_op = std::make_shared<RandomPosterizeOp>(bit_range_);
  1179. return tensor_op;
  1180. }
  1181. // RandomResizeOperation
  1182. RandomResizeOperation::RandomResizeOperation(std::vector<int32_t> size) : TensorOperation(true), size_(size) {}
  1183. Status RandomResizeOperation::ValidateParams() {
  1184. // size
  1185. if (size_.size() != 2 && size_.size() != 1) {
  1186. std::string err_msg =
  1187. "RandomResize: size must be a vector of one or two values, got: " + std::to_string(size_.size());
  1188. MS_LOG(ERROR) << err_msg;
  1189. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1190. }
  1191. if (size_[0] <= 0 || (size_.size() == 2 && size_[1] <= 0)) {
  1192. std::string err_msg = "RandomResize: size must only contain positive integers.";
  1193. MS_LOG(ERROR) << "RandomResize: size must only contain positive integers, got: " << size_;
  1194. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1195. }
  1196. return Status::OK();
  1197. }
  1198. std::shared_ptr<TensorOp> RandomResizeOperation::Build() {
  1199. // If size is a single value, the smaller edge of the image will be
  1200. // resized to this value with the same image aspect ratio.
  1201. int32_t height = size_[0];
  1202. int32_t width = 0;
  1203. // User specified the width value.
  1204. if (size_.size() == 2) {
  1205. width = size_[1];
  1206. }
  1207. std::shared_ptr<RandomResizeOp> tensor_op = std::make_shared<RandomResizeOp>(height, width);
  1208. return tensor_op;
  1209. }
  1210. // RandomResizeWithBBoxOperation
  1211. RandomResizeWithBBoxOperation::RandomResizeWithBBoxOperation(std::vector<int32_t> size)
  1212. : TensorOperation(true), size_(size) {}
  1213. Status RandomResizeWithBBoxOperation::ValidateParams() {
  1214. // size
  1215. if (size_.size() != 2 && size_.size() != 1) {
  1216. std::string err_msg =
  1217. "RandomResizeWithBBox: size must be a vector of one or two values, got: " + std::to_string(size_.size());
  1218. MS_LOG(ERROR) << err_msg;
  1219. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1220. }
  1221. if (size_[0] <= 0 || (size_.size() == 2 && size_[1] <= 0)) {
  1222. std::string err_msg = "RandomResizeWithBBox: size must only contain positive integers.";
  1223. MS_LOG(ERROR) << "RandomResizeWithBBox: size must only contain positive integers, got: " << size_;
  1224. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1225. }
  1226. return Status::OK();
  1227. }
  1228. std::shared_ptr<TensorOp> RandomResizeWithBBoxOperation::Build() {
  1229. // If size is a single value, the smaller edge of the image will be
  1230. // resized to this value with the same image aspect ratio.
  1231. int32_t height = size_[0];
  1232. int32_t width = 0;
  1233. // User specified the width value.
  1234. if (size_.size() == 2) {
  1235. width = size_[1];
  1236. }
  1237. std::shared_ptr<RandomResizeWithBBoxOp> tensor_op = std::make_shared<RandomResizeWithBBoxOp>(height, width);
  1238. return tensor_op;
  1239. }
  1240. // RandomResizedCropOperation
  1241. RandomResizedCropOperation::RandomResizedCropOperation(std::vector<int32_t> size, std::vector<float> scale,
  1242. std::vector<float> ratio, InterpolationMode interpolation,
  1243. int32_t max_attempts)
  1244. : TensorOperation(true),
  1245. size_(size),
  1246. scale_(scale),
  1247. ratio_(ratio),
  1248. interpolation_(interpolation),
  1249. max_attempts_(max_attempts) {}
  1250. Status RandomResizedCropOperation::ValidateParams() {
  1251. // size
  1252. if (size_.size() != 2 && size_.size() != 1) {
  1253. std::string err_msg =
  1254. "RandomResizedCrop: size must be a vector of one or two values, got: " + std::to_string(size_.size());
  1255. MS_LOG(ERROR) << err_msg;
  1256. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1257. }
  1258. if (size_[0] <= 0 || (size_.size() == 2 && size_[1] <= 0)) {
  1259. std::string err_msg = "RandomResizedCrop: size must only contain positive integers.";
  1260. MS_LOG(ERROR) << "RandomResizedCrop: size must only contain positive integers, got: " << size_;
  1261. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1262. }
  1263. // scale
  1264. if (scale_.size() != 2) {
  1265. std::string err_msg =
  1266. "RandomResizedCrop: scale must be a vector of two values, got: " + std::to_string(scale_.size());
  1267. MS_LOG(ERROR) << err_msg;
  1268. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1269. }
  1270. if (scale_[0] < 0) {
  1271. std::string err_msg = "RandomResizedCrop: min scale must be greater than or equal to 0.";
  1272. MS_LOG(ERROR) << "RandomResizedCrop: min scale must be greater than or equal to 0, got: " +
  1273. std::to_string(scale_[0]);
  1274. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1275. }
  1276. if (scale_[1] <= 0) {
  1277. std::string err_msg = "RandomResizedCrop: max scale must be greater than 0.";
  1278. MS_LOG(ERROR) << "RandomResizedCrop: max scale must be greater than 0, got: " + std::to_string(scale_[1]);
  1279. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1280. }
  1281. if (scale_[1] < scale_[0]) {
  1282. std::string err_msg = "RandomResizedCrop: scale must have a size of two in the format of (min, max).";
  1283. MS_LOG(ERROR) << "RandomResizedCrop: scale must have a size of two in the format of (min, max), but got: "
  1284. << scale_;
  1285. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1286. }
  1287. // ratio
  1288. if (ratio_.size() != 2) {
  1289. std::string err_msg =
  1290. "RandomResizedCrop: ratio must be a vector of two values, got: " + std::to_string(ratio_.size());
  1291. MS_LOG(ERROR) << err_msg;
  1292. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1293. }
  1294. if (ratio_[0] <= 0 || ratio_[1] <= 0) {
  1295. std::string err_msg = "RandomResizedCrop: ratio must be greater than 0.";
  1296. MS_LOG(ERROR) << "RandomResizedCrop: ratio must be greater than 0, got: " << ratio_;
  1297. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1298. }
  1299. if (ratio_[1] < ratio_[0]) {
  1300. std::string err_msg = "RandomResizedCrop: ratio must have a size of two in the format of (min, max).";
  1301. MS_LOG(ERROR) << "RandomResizedCrop: ratio must have a size of two in the format of (min, max), but got: "
  1302. << ratio_;
  1303. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1304. }
  1305. // max_attempts
  1306. if (max_attempts_ < 1) {
  1307. std::string err_msg =
  1308. "RandomResizedCrop: max_attempts must be greater than or equal to 1, got: " + std::to_string(max_attempts_);
  1309. MS_LOG(ERROR) << err_msg;
  1310. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1311. }
  1312. return Status::OK();
  1313. }
  1314. std::shared_ptr<TensorOp> RandomResizedCropOperation::Build() {
  1315. int32_t height = size_[0];
  1316. int32_t width = size_[0];
  1317. // User specified the width value.
  1318. if (size_.size() == 2) {
  1319. width = size_[1];
  1320. }
  1321. std::shared_ptr<RandomCropAndResizeOp> tensor_op = std::make_shared<RandomCropAndResizeOp>(
  1322. height, width, scale_[0], scale_[1], ratio_[0], ratio_[1], interpolation_, max_attempts_);
  1323. return tensor_op;
  1324. }
  1325. // RandomResizedCropWithBBoxOperation
  1326. RandomResizedCropWithBBoxOperation::RandomResizedCropWithBBoxOperation(std::vector<int32_t> size,
  1327. std::vector<float> scale,
  1328. std::vector<float> ratio,
  1329. InterpolationMode interpolation,
  1330. int32_t max_attempts)
  1331. : size_(size), scale_(scale), ratio_(ratio), interpolation_(interpolation), max_attempts_(max_attempts) {}
  1332. Status RandomResizedCropWithBBoxOperation::ValidateParams() {
  1333. // size
  1334. if (size_.size() != 2 && size_.size() != 1) {
  1335. std::string err_msg =
  1336. "RandomResizedCropWithBBox: size must be a vector of one or two values, got: " + std::to_string(size_.size());
  1337. MS_LOG(ERROR) << err_msg;
  1338. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1339. }
  1340. if (size_[0] <= 0 || (size_.size() == 2 && size_[1] <= 0)) {
  1341. std::string err_msg = "RandomResizedCropWithBBox: size must only contain positive integers.";
  1342. MS_LOG(ERROR) << "RandomResizedCropWithBBox: size must only contain positive integers, got: " << size_;
  1343. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1344. }
  1345. // scale
  1346. if (scale_.size() != 2) {
  1347. std::string err_msg =
  1348. "RandomResizedCropWithBBox: scale must be a vector of two values, got: " + std::to_string(scale_.size());
  1349. MS_LOG(ERROR) << err_msg;
  1350. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1351. }
  1352. if (scale_[0] < 0) {
  1353. std::string err_msg = "RandomResizedCropWithBBox: min scale must be greater than or equal to 0.";
  1354. MS_LOG(ERROR) << "RandomResizedCropWithBBox: min scale must be greater than or equal to 0, got: " +
  1355. std::to_string(scale_[0]);
  1356. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1357. }
  1358. if (scale_[1] <= 0) {
  1359. std::string err_msg = "RandomResizedCropWithBBox: max scale must be greater than 0.";
  1360. MS_LOG(ERROR) << "RandomResizedCropWithBBox: max scale must be greater than 0, got: " + std::to_string(scale_[1]);
  1361. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1362. }
  1363. if (scale_[1] < scale_[0]) {
  1364. std::string err_msg = "RandomResizedCropWithBBox: scale must have a size of two in the format of (min, max).";
  1365. MS_LOG(ERROR) << "RandomResizedCropWithBBox: scale must have a size of two in the format of (min, max), but got: "
  1366. << scale_;
  1367. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1368. }
  1369. // ratio
  1370. if (ratio_.size() != 2) {
  1371. std::string err_msg =
  1372. "RandomResizedCropWithBBox: ratio must be a vector of two values, got: " + std::to_string(ratio_.size());
  1373. MS_LOG(ERROR) << err_msg;
  1374. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1375. }
  1376. if (ratio_[0] <= 0 || ratio_[1] <= 0) {
  1377. std::string err_msg = "RandomResizedCropWithBBox: ratio must be greater than 0.";
  1378. MS_LOG(ERROR) << "RandomResizedCropWithBBox: ratio must be greater than 0, got: " << ratio_;
  1379. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1380. }
  1381. if (ratio_[1] < ratio_[0]) {
  1382. std::string err_msg = "RandomResizedCropWithBBox: ratio must have a size of two in the format of (min, max).";
  1383. MS_LOG(ERROR) << "RandomResizedCropWithBBox: ratio must have a size of two in the format of (min, max), but got: "
  1384. << ratio_;
  1385. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1386. }
  1387. // max_attempts
  1388. if (max_attempts_ < 1) {
  1389. std::string err_msg = "RandomResizedCropWithBBox: max_attempts must be greater than or equal to 1, got: " +
  1390. std::to_string(max_attempts_);
  1391. MS_LOG(ERROR) << err_msg;
  1392. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1393. }
  1394. return Status::OK();
  1395. }
  1396. std::shared_ptr<TensorOp> RandomResizedCropWithBBoxOperation::Build() {
  1397. int32_t height = size_[0];
  1398. int32_t width = size_[0];
  1399. // User specified the width value.
  1400. if (size_.size() == 2) {
  1401. width = size_[1];
  1402. }
  1403. std::shared_ptr<RandomCropAndResizeWithBBoxOp> tensor_op = std::make_shared<RandomCropAndResizeWithBBoxOp>(
  1404. height, width, scale_[0], scale_[1], ratio_[0], ratio_[1], interpolation_, max_attempts_);
  1405. return tensor_op;
  1406. }
  1407. // Function to create RandomRotationOperation.
  1408. RandomRotationOperation::RandomRotationOperation(std::vector<float> degrees, InterpolationMode interpolation_mode,
  1409. bool expand, std::vector<float> center,
  1410. std::vector<uint8_t> fill_value)
  1411. : TensorOperation(true),
  1412. degrees_(degrees),
  1413. interpolation_mode_(interpolation_mode),
  1414. expand_(expand),
  1415. center_(center),
  1416. fill_value_(fill_value) {}
  1417. Status RandomRotationOperation::ValidateParams() {
  1418. // degrees
  1419. if (degrees_.size() != 2 && degrees_.size() != 1) {
  1420. std::string err_msg =
  1421. "RandomRotation: degrees must be a vector of one or two values, got: " + std::to_string(degrees_.size());
  1422. MS_LOG(ERROR) << "RandomRotation: degrees must be a vector of one or two values, got: " << degrees_;
  1423. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1424. }
  1425. if ((degrees_[1] < degrees_[0]) && (degrees_.size() == 2)) {
  1426. std::string err_msg = "RandomRotation: degrees must be in the format of (min, max), got: (" +
  1427. std::to_string(degrees_[0]) + ", " + std::to_string(degrees_[1]) + ")";
  1428. MS_LOG(ERROR) << err_msg;
  1429. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1430. } else if ((degrees_[0] < 0) && degrees_.size() == 1) {
  1431. std::string err_msg =
  1432. "RandomRotation: if degrees only has one value, it must be greater than or equal to 0, got: " +
  1433. std::to_string(degrees_[0]);
  1434. MS_LOG(ERROR) << err_msg;
  1435. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1436. }
  1437. // center
  1438. if (center_.empty() || center_.size() != 2) {
  1439. std::string err_msg =
  1440. "RandomRotation: center must be a vector of two values, got: " + std::to_string(center_.size());
  1441. MS_LOG(ERROR) << err_msg;
  1442. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1443. }
  1444. // fill_value
  1445. RETURN_IF_NOT_OK(ValidateVectorFillvalue("RandomRotation", fill_value_));
  1446. return Status::OK();
  1447. }
  1448. std::shared_ptr<TensorOp> RandomRotationOperation::Build() {
  1449. float start_degree, end_degree;
  1450. if (degrees_.size() == 1) {
  1451. start_degree = -degrees_[0];
  1452. end_degree = degrees_[0];
  1453. } else if (degrees_.size() == 2) {
  1454. start_degree = degrees_[0];
  1455. end_degree = degrees_[1];
  1456. }
  1457. uint8_t fill_r, fill_g, fill_b;
  1458. fill_r = fill_value_[0];
  1459. fill_g = fill_value_[0];
  1460. fill_b = fill_value_[0];
  1461. if (fill_value_.size() == 3) {
  1462. fill_r = fill_value_[0];
  1463. fill_g = fill_value_[1];
  1464. fill_b = fill_value_[2];
  1465. }
  1466. std::shared_ptr<RandomRotationOp> tensor_op = std::make_shared<RandomRotationOp>(
  1467. start_degree, end_degree, center_[0], center_[1], interpolation_mode_, expand_, fill_r, fill_g, fill_b);
  1468. return tensor_op;
  1469. }
  1470. // RandomSelectSubpolicyOperation.
  1471. RandomSelectSubpolicyOperation::RandomSelectSubpolicyOperation(
  1472. std::vector<std::vector<std::pair<std::shared_ptr<TensorOperation>, double>>> policy)
  1473. : TensorOperation(true), policy_(policy) {}
  1474. Status RandomSelectSubpolicyOperation::ValidateParams() {
  1475. if (policy_.empty()) {
  1476. std::string err_msg = "RandomSelectSubpolicy: policy must not be empty";
  1477. MS_LOG(ERROR) << err_msg;
  1478. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1479. }
  1480. for (int32_t i = 0; i < policy_.size(); i++) {
  1481. if (policy_[i].empty()) {
  1482. std::string err_msg = "RandomSelectSubpolicy: policy[" + std::to_string(i) + "] must not be empty";
  1483. MS_LOG(ERROR) << err_msg;
  1484. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1485. }
  1486. for (int32_t j = 0; j < policy_[i].size(); j++) {
  1487. if (policy_[i][j].first == nullptr) {
  1488. std::string transform_pos = "[" + std::to_string(i) + "]" + "[" + std::to_string(j) + "]";
  1489. std::string err_msg = "RandomSelectSubpolicy: transform in policy" + transform_pos + " must not be null";
  1490. MS_LOG(ERROR) << err_msg;
  1491. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1492. }
  1493. if (policy_[i][j].second < 0.0 || policy_[i][j].second > 1.0) {
  1494. std::string transform_pos = "[" + std::to_string(i) + "]" + "[" + std::to_string(j) + "]";
  1495. std::string err_msg = "RandomSelectSubpolicy: probability of transform in policy" + transform_pos +
  1496. " must be between 0.0 and 1.0, got: " + std::to_string(policy_[i][j].second);
  1497. MS_LOG(ERROR) << err_msg;
  1498. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1499. }
  1500. }
  1501. }
  1502. return Status::OK();
  1503. }
  1504. std::shared_ptr<TensorOp> RandomSelectSubpolicyOperation::Build() {
  1505. std::vector<Subpolicy> policy_tensor_ops;
  1506. for (int32_t i = 0; i < policy_.size(); i++) {
  1507. Subpolicy sub_policy_tensor_ops;
  1508. for (int32_t j = 0; j < policy_[i].size(); j++) {
  1509. sub_policy_tensor_ops.push_back(std::make_pair(policy_[i][j].first->Build(), policy_[i][j].second));
  1510. }
  1511. policy_tensor_ops.push_back(sub_policy_tensor_ops);
  1512. }
  1513. std::shared_ptr<RandomSelectSubpolicyOp> tensor_op = std::make_shared<RandomSelectSubpolicyOp>(policy_tensor_ops);
  1514. return tensor_op;
  1515. }
  1516. // Function to create RandomSharpness.
  1517. RandomSharpnessOperation::RandomSharpnessOperation(std::vector<float> degrees)
  1518. : TensorOperation(true), degrees_(degrees) {}
  1519. Status RandomSharpnessOperation::ValidateParams() {
  1520. if (degrees_.size() != 2 || degrees_[0] < 0 || degrees_[1] < 0) {
  1521. std::string err_msg = "RandomSharpness: degrees must be a vector of two values and greater than or equal to 0.";
  1522. MS_LOG(ERROR) << "RandomSharpness: degrees must be a vector of two values and greater than or equal to 0, got: "
  1523. << degrees_;
  1524. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1525. }
  1526. if (degrees_[1] < degrees_[0]) {
  1527. std::string err_msg = "RandomSharpness: degrees must be in the format of (min, max).";
  1528. MS_LOG(ERROR) << "RandomSharpness: degrees must be in the format of (min, max), got: " << degrees_;
  1529. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1530. }
  1531. return Status::OK();
  1532. }
  1533. std::shared_ptr<TensorOp> RandomSharpnessOperation::Build() {
  1534. std::shared_ptr<RandomSharpnessOp> tensor_op = std::make_shared<RandomSharpnessOp>(degrees_[0], degrees_[1]);
  1535. return tensor_op;
  1536. }
  1537. // RandomSolarizeOperation.
  1538. RandomSolarizeOperation::RandomSolarizeOperation(std::vector<uint8_t> threshold)
  1539. : TensorOperation(true), threshold_(threshold) {}
  1540. Status RandomSolarizeOperation::ValidateParams() {
  1541. if (threshold_.size() != 2) {
  1542. std::string err_msg =
  1543. "RandomSolarize: threshold must be a vector of two values, got: " + std::to_string(threshold_.size());
  1544. MS_LOG(ERROR) << err_msg;
  1545. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1546. }
  1547. for (int32_t i = 0; i < threshold_.size(); ++i) {
  1548. if (threshold_[i] < 0 || threshold_[i] > 255) {
  1549. std::string err_msg =
  1550. "RandomSolarize: threshold has to be between 0 and 255, got:" + std::to_string(threshold_[i]);
  1551. MS_LOG(ERROR) << err_msg;
  1552. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1553. }
  1554. }
  1555. if (threshold_[0] > threshold_[1]) {
  1556. std::string err_msg = "RandomSolarize: threshold must be passed in a (min, max) format";
  1557. MS_LOG(ERROR) << err_msg;
  1558. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1559. }
  1560. return Status::OK();
  1561. }
  1562. std::shared_ptr<TensorOp> RandomSolarizeOperation::Build() {
  1563. std::shared_ptr<RandomSolarizeOp> tensor_op = std::make_shared<RandomSolarizeOp>(threshold_);
  1564. return tensor_op;
  1565. }
  1566. // RandomVerticalFlipOperation
  1567. RandomVerticalFlipOperation::RandomVerticalFlipOperation(float probability)
  1568. : TensorOperation(true), probability_(probability) {}
  1569. Status RandomVerticalFlipOperation::ValidateParams() {
  1570. RETURN_IF_NOT_OK(ValidateProbability("RandomVerticalFlip", probability_));
  1571. return Status::OK();
  1572. }
  1573. std::shared_ptr<TensorOp> RandomVerticalFlipOperation::Build() {
  1574. std::shared_ptr<RandomVerticalFlipOp> tensor_op = std::make_shared<RandomVerticalFlipOp>(probability_);
  1575. return tensor_op;
  1576. }
  1577. // RandomVerticalFlipWithBBoxOperation
  1578. RandomVerticalFlipWithBBoxOperation::RandomVerticalFlipWithBBoxOperation(float probability)
  1579. : TensorOperation(true), probability_(probability) {}
  1580. Status RandomVerticalFlipWithBBoxOperation::ValidateParams() {
  1581. RETURN_IF_NOT_OK(ValidateProbability("RandomVerticalFlipWithBBox", probability_));
  1582. return Status::OK();
  1583. }
  1584. std::shared_ptr<TensorOp> RandomVerticalFlipWithBBoxOperation::Build() {
  1585. std::shared_ptr<RandomVerticalFlipWithBBoxOp> tensor_op =
  1586. std::make_shared<RandomVerticalFlipWithBBoxOp>(probability_);
  1587. return tensor_op;
  1588. }
  1589. // RescaleOperation
  1590. RescaleOperation::RescaleOperation(float rescale, float shift) : rescale_(rescale), shift_(shift) {}
  1591. Status RescaleOperation::ValidateParams() {
  1592. if (rescale_ < 0) {
  1593. std::string err_msg = "Rescale: rescale must be greater than or equal to 0, got: " + std::to_string(rescale_);
  1594. MS_LOG(ERROR) << err_msg;
  1595. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1596. }
  1597. return Status::OK();
  1598. }
  1599. std::shared_ptr<TensorOp> RescaleOperation::Build() {
  1600. std::shared_ptr<RescaleOp> tensor_op = std::make_shared<RescaleOp>(rescale_, shift_);
  1601. return tensor_op;
  1602. }
  1603. #endif
  1604. // ResizeOperation
  1605. ResizeOperation::ResizeOperation(std::vector<int32_t> size, InterpolationMode interpolation)
  1606. : size_(size), interpolation_(interpolation) {}
  1607. Status ResizeOperation::ValidateParams() {
  1608. // size
  1609. if (size_.empty() || size_.size() > 2) {
  1610. std::string err_msg = "Resize: size must be a vector of one or two values, got: " + std::to_string(size_.size());
  1611. MS_LOG(ERROR) << err_msg;
  1612. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1613. }
  1614. RETURN_IF_NOT_OK(ValidateVectorPositive("Resize", size_));
  1615. return Status::OK();
  1616. }
  1617. std::shared_ptr<TensorOp> ResizeOperation::Build() {
  1618. // If size is a single value, the smaller edge of the image will be
  1619. // resized to this value with the same image aspect ratio.
  1620. int32_t height = size_[0];
  1621. int32_t width = 0;
  1622. // User specified the width value.
  1623. if (size_.size() == 2) {
  1624. width = size_[1];
  1625. }
  1626. return std::make_shared<ResizeOp>(height, width, interpolation_);
  1627. }
  1628. #ifndef ENABLE_ANDROID
  1629. // ResizeWithBBoxOperation
  1630. ResizeWithBBoxOperation::ResizeWithBBoxOperation(std::vector<int32_t> size, InterpolationMode interpolation)
  1631. : size_(size), interpolation_(interpolation) {}
  1632. Status ResizeWithBBoxOperation::ValidateParams() {
  1633. // size
  1634. if (size_.empty() || size_.size() > 2) {
  1635. std::string err_msg =
  1636. "ResizeWithBBox: size must be a vector of one or two values, got: " + std::to_string(size_.size());
  1637. MS_LOG(ERROR) << err_msg;
  1638. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1639. }
  1640. RETURN_IF_NOT_OK(ValidateVectorPositive("Resize", size_));
  1641. return Status::OK();
  1642. }
  1643. std::shared_ptr<TensorOp> ResizeWithBBoxOperation::Build() {
  1644. int32_t height = size_[0];
  1645. int32_t width = 0;
  1646. // User specified the width value.
  1647. if (size_.size() == 2) {
  1648. width = size_[1];
  1649. }
  1650. return std::make_shared<ResizeWithBBoxOp>(height, width, interpolation_);
  1651. }
  1652. // RgbaToBgrOperation.
  1653. RgbaToBgrOperation::RgbaToBgrOperation() {}
  1654. Status RgbaToBgrOperation::ValidateParams() { return Status::OK(); }
  1655. std::shared_ptr<TensorOp> RgbaToBgrOperation::Build() {
  1656. std::shared_ptr<RgbaToBgrOp> tensor_op = std::make_shared<RgbaToBgrOp>();
  1657. return tensor_op;
  1658. }
  1659. // RgbaToRgbOperation.
  1660. RgbaToRgbOperation::RgbaToRgbOperation() {}
  1661. Status RgbaToRgbOperation::ValidateParams() { return Status::OK(); }
  1662. std::shared_ptr<TensorOp> RgbaToRgbOperation::Build() {
  1663. std::shared_ptr<RgbaToRgbOp> tensor_op = std::make_shared<RgbaToRgbOp>();
  1664. return tensor_op;
  1665. }
  1666. // SoftDvppDecodeRandomCropResizeJpegOperation
  1667. SoftDvppDecodeRandomCropResizeJpegOperation::SoftDvppDecodeRandomCropResizeJpegOperation(std::vector<int32_t> size,
  1668. std::vector<float> scale,
  1669. std::vector<float> ratio,
  1670. int32_t max_attempts)
  1671. : size_(size), scale_(scale), ratio_(ratio), max_attempts_(max_attempts) {}
  1672. Status SoftDvppDecodeRandomCropResizeJpegOperation::ValidateParams() {
  1673. // size
  1674. if (size_.size() != 2 && size_.size() != 1) {
  1675. std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: size must be a vector of one or two values, got: " +
  1676. std::to_string(size_.size());
  1677. MS_LOG(ERROR) << err_msg;
  1678. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1679. }
  1680. if (size_[0] <= 0 || (size_.size() == 2 && size_[1] <= 0)) {
  1681. std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: size must only contain positive integers.";
  1682. MS_LOG(ERROR) << "SoftDvppDecodeRandomCropResizeJpeg: size must only contain positive integers, got: " << size_;
  1683. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1684. }
  1685. // scale
  1686. if (scale_.size() != 2) {
  1687. std::string err_msg =
  1688. "SoftDvppDecodeRandomCropResizeJpeg: scale must be a vector of two values, got: " + std::to_string(scale_.size());
  1689. MS_LOG(ERROR) << err_msg;
  1690. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1691. }
  1692. if (scale_[0] < 0) {
  1693. std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: min scale must be greater than or equal to 0.";
  1694. MS_LOG(ERROR) << "SoftDvppDecodeRandomCropResizeJpeg: min scale must be greater than or equal to 0, got: " +
  1695. std::to_string(scale_[0]);
  1696. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1697. }
  1698. if (scale_[1] <= 0) {
  1699. std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: max scale must be greater than 0.";
  1700. MS_LOG(ERROR) << "SoftDvppDecodeRandomCropResizeJpeg: max scale must be greater than 0, got: " +
  1701. std::to_string(scale_[1]);
  1702. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1703. }
  1704. if (scale_[1] < scale_[0]) {
  1705. std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: scale must be in the format of (min, max).";
  1706. MS_LOG(ERROR) << "SoftDvppDecodeRandomCropResizeJpeg: scale must be in the format of (min, max), but got: "
  1707. << scale_;
  1708. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1709. }
  1710. // ratio
  1711. if (ratio_.size() != 2) {
  1712. std::string err_msg =
  1713. "SoftDvppDecodeRandomCropResizeJpeg: ratio must be a vector of two values, got: " + std::to_string(ratio_.size());
  1714. MS_LOG(ERROR) << err_msg;
  1715. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1716. }
  1717. if (ratio_[0] <= 0 || ratio_[1] <= 0) {
  1718. std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: ratio must be greater than 0.";
  1719. MS_LOG(ERROR) << "SoftDvppDecodeRandomCropResizeJpeg: ratio must be greater than 0, got: " << ratio_;
  1720. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1721. }
  1722. if (ratio_[1] < ratio_[0]) {
  1723. std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: ratio must be in the format of (min, max).";
  1724. MS_LOG(ERROR) << "SoftDvppDecodeRandomCropResizeJpeg: ratio must be in the format of (min, max), but got: "
  1725. << ratio_;
  1726. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1727. }
  1728. // max_attempts
  1729. if (max_attempts_ < 1) {
  1730. std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: max_attempts must be greater than or equal to 1, got: " +
  1731. std::to_string(max_attempts_);
  1732. MS_LOG(ERROR) << err_msg;
  1733. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1734. }
  1735. return Status::OK();
  1736. }
  1737. std::shared_ptr<TensorOp> SoftDvppDecodeRandomCropResizeJpegOperation::Build() {
  1738. int32_t height = size_[0];
  1739. int32_t width = size_[0];
  1740. // User specified the width value.
  1741. if (size_.size() == 2) {
  1742. width = size_[1];
  1743. }
  1744. auto tensor_op = std::make_shared<SoftDvppDecodeRandomCropResizeJpegOp>(height, width, scale_[0], scale_[1],
  1745. ratio_[0], ratio_[1], max_attempts_);
  1746. return tensor_op;
  1747. }
  1748. // SoftDvppDecodeResizeJpegOperation
  1749. SoftDvppDecodeResizeJpegOperation::SoftDvppDecodeResizeJpegOperation(std::vector<int32_t> size) : size_(size) {}
  1750. Status SoftDvppDecodeResizeJpegOperation::ValidateParams() {
  1751. // size
  1752. if (size_.empty() || size_.size() > 2) {
  1753. std::string err_msg =
  1754. "SoftDvppDecodeResizeJpeg: size must be a vector of one or two values, got: " + std::to_string(size_.size());
  1755. MS_LOG(ERROR) << err_msg;
  1756. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1757. }
  1758. RETURN_IF_NOT_OK(ValidateVectorPositive("SoftDvppDecodeResizeJpeg", size_));
  1759. return Status::OK();
  1760. }
  1761. std::shared_ptr<TensorOp> SoftDvppDecodeResizeJpegOperation::Build() {
  1762. // If size is a single value, the smaller edge of the image will be
  1763. // resized to this value with the same image aspect ratio.
  1764. int32_t height = size_[0];
  1765. int32_t width = 0;
  1766. // User specified the width value.
  1767. if (size_.size() == 2) {
  1768. width = size_[1];
  1769. }
  1770. std::shared_ptr<SoftDvppDecodeResizeJpegOp> tensor_op = std::make_shared<SoftDvppDecodeResizeJpegOp>(height, width);
  1771. return tensor_op;
  1772. }
  1773. // SwapRedBlueOperation.
  1774. SwapRedBlueOperation::SwapRedBlueOperation() {}
  1775. Status SwapRedBlueOperation::ValidateParams() { return Status::OK(); }
  1776. std::shared_ptr<TensorOp> SwapRedBlueOperation::Build() {
  1777. std::shared_ptr<SwapRedBlueOp> tensor_op = std::make_shared<SwapRedBlueOp>();
  1778. return tensor_op;
  1779. }
  1780. // UniformAugOperation
  1781. UniformAugOperation::UniformAugOperation(std::vector<std::shared_ptr<TensorOperation>> transforms, int32_t num_ops)
  1782. : transforms_(transforms), num_ops_(num_ops) {}
  1783. Status UniformAugOperation::ValidateParams() {
  1784. // transforms
  1785. RETURN_IF_NOT_OK(ValidateVectorTransforms("UniformAug", transforms_));
  1786. if (num_ops_ > transforms_.size()) {
  1787. std::string err_msg = "UniformAug: num_ops is greater than transforms size, but got: " + std::to_string(num_ops_);
  1788. MS_LOG(ERROR) << err_msg;
  1789. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1790. }
  1791. // num_ops
  1792. if (num_ops_ <= 0) {
  1793. std::string err_msg = "UniformAug: num_ops must be greater than 0, but got: " + std::to_string(num_ops_);
  1794. MS_LOG(ERROR) << err_msg;
  1795. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1796. }
  1797. return Status::OK();
  1798. }
  1799. std::shared_ptr<TensorOp> UniformAugOperation::Build() {
  1800. std::vector<std::shared_ptr<TensorOp>> tensor_ops;
  1801. (void)std::transform(transforms_.begin(), transforms_.end(), std::back_inserter(tensor_ops),
  1802. [](std::shared_ptr<TensorOperation> op) -> std::shared_ptr<TensorOp> { return op->Build(); });
  1803. std::shared_ptr<UniformAugOp> tensor_op = std::make_shared<UniformAugOp>(tensor_ops, num_ops_);
  1804. return tensor_op;
  1805. }
  1806. #endif
  1807. } // namespace vision
  1808. } // namespace dataset
  1809. } // namespace mindspore