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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986
  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. Status RandomAffineOperation::ValidateParams() {
  646. // Degrees
  647. if (degrees_.size() != 2) {
  648. std::string err_msg =
  649. "RandomAffine: degrees expecting size 2, got: degrees.size() = " + std::to_string(degrees_.size());
  650. MS_LOG(ERROR) << err_msg;
  651. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  652. }
  653. if (degrees_[0] > degrees_[1]) {
  654. std::string err_msg =
  655. "RandomAffine: minimum of degrees range is greater than maximum: min = " + std::to_string(degrees_[0]) +
  656. ", max = " + std::to_string(degrees_[1]);
  657. MS_LOG(ERROR) << err_msg;
  658. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  659. }
  660. // Translate
  661. if (translate_range_.size() != 2 && translate_range_.size() != 4) {
  662. std::string err_msg = "RandomAffine: translate_range expecting size 2 or 4, got: translate_range.size() = " +
  663. std::to_string(translate_range_.size());
  664. MS_LOG(ERROR) << err_msg;
  665. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  666. }
  667. if (translate_range_[0] > translate_range_[1]) {
  668. std::string err_msg = "RandomAffine: minimum of translate range on x is greater than maximum: min = " +
  669. std::to_string(translate_range_[0]) + ", max = " + std::to_string(translate_range_[1]);
  670. MS_LOG(ERROR) << err_msg;
  671. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  672. }
  673. if (translate_range_[0] < -1 || translate_range_[0] > 1) {
  674. std::string err_msg = "RandomAffine: minimum of translate range on x is out of range of [-1, 1], value = " +
  675. std::to_string(translate_range_[0]);
  676. MS_LOG(ERROR) << err_msg;
  677. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  678. }
  679. if (translate_range_[1] < -1 || translate_range_[1] > 1) {
  680. std::string err_msg = "RandomAffine: maximum of translate range on x is out of range of [-1, 1], value = " +
  681. std::to_string(translate_range_[1]);
  682. MS_LOG(ERROR) << err_msg;
  683. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  684. }
  685. if (translate_range_.size() == 4) {
  686. if (translate_range_[2] > translate_range_[3]) {
  687. std::string err_msg = "RandomAffine: minimum of translate range on y is greater than maximum: min = " +
  688. std::to_string(translate_range_[2]) + ", max = " + std::to_string(translate_range_[3]);
  689. MS_LOG(ERROR) << err_msg;
  690. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  691. }
  692. if (translate_range_[2] < -1 || translate_range_[2] > 1) {
  693. std::string err_msg = "RandomAffine: minimum of translate range on y is out of range of [-1, 1], value = " +
  694. std::to_string(translate_range_[2]);
  695. MS_LOG(ERROR) << err_msg;
  696. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  697. }
  698. if (translate_range_[3] < -1 || translate_range_[3] > 1) {
  699. std::string err_msg = "RandomAffine: maximum of translate range on y is out of range of [-1, 1], value = " +
  700. std::to_string(translate_range_[3]);
  701. MS_LOG(ERROR) << err_msg;
  702. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  703. }
  704. }
  705. // Scale
  706. if (scale_range_.size() != 2) {
  707. std::string err_msg = "RandomAffine: scale_range vector has incorrect size: scale_range.size() = " +
  708. std::to_string(scale_range_.size());
  709. MS_LOG(ERROR) << err_msg;
  710. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  711. }
  712. for (int32_t i = 0; i < scale_range_.size(); ++i) {
  713. if (scale_range_[i] <= 0) {
  714. std::string err_msg = "RandomAffine: scale must be greater than 0, got: " + std::to_string(fill_value_[i]);
  715. MS_LOG(ERROR) << err_msg;
  716. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  717. }
  718. }
  719. if (scale_range_[0] > scale_range_[1]) {
  720. std::string err_msg =
  721. "RandomAffine: minimum of scale range is greater than maximum: min = " + std::to_string(scale_range_[0]) +
  722. ", max = " + std::to_string(scale_range_[1]);
  723. MS_LOG(ERROR) << err_msg;
  724. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  725. }
  726. // Shear
  727. if (shear_ranges_.size() != 2 && shear_ranges_.size() != 4) {
  728. std::string err_msg = "RandomAffine: shear_ranges expecting size 2 or 4, got: shear_ranges.size() = " +
  729. std::to_string(shear_ranges_.size());
  730. MS_LOG(ERROR) << err_msg;
  731. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  732. }
  733. if (shear_ranges_[0] > shear_ranges_[1]) {
  734. std::string err_msg = "RandomAffine: minimum of horizontal shear range is greater than maximum: min = " +
  735. std::to_string(shear_ranges_[0]) + ", max = " + std::to_string(shear_ranges_[1]);
  736. MS_LOG(ERROR) << err_msg;
  737. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  738. }
  739. if (shear_ranges_.size() == 4 && shear_ranges_[2] > shear_ranges_[3]) {
  740. std::string err_msg = "RandomAffine: minimum of vertical shear range is greater than maximum: min = " +
  741. std::to_string(shear_ranges_[2]) + ", max = " + std::to_string(scale_range_[3]);
  742. MS_LOG(ERROR) << err_msg;
  743. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  744. }
  745. // Fill Value
  746. if (fill_value_.size() != 3) {
  747. std::string err_msg =
  748. "RandomAffine: fill_value vector has incorrect size: fill_value.size() = " + std::to_string(fill_value_.size());
  749. MS_LOG(ERROR) << err_msg;
  750. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  751. }
  752. for (int32_t i = 0; i < fill_value_.size(); ++i) {
  753. if (fill_value_[i] < 0 || fill_value_[i] > 255) {
  754. std::string err_msg =
  755. "RandomAffine: fill_value has to be between 0 and 255, got:" + std::to_string(fill_value_[i]);
  756. MS_LOG(ERROR) << err_msg;
  757. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  758. }
  759. }
  760. return Status::OK();
  761. }
  762. std::shared_ptr<TensorOp> RandomAffineOperation::Build() {
  763. if (shear_ranges_.size() == 2) {
  764. shear_ranges_.resize(4);
  765. }
  766. if (translate_range_.size() == 2) {
  767. translate_range_.resize(4);
  768. }
  769. auto tensor_op = std::make_shared<RandomAffineOp>(degrees_, translate_range_, scale_range_, shear_ranges_,
  770. interpolation_, fill_value_);
  771. return tensor_op;
  772. }
  773. // RandomColorOperation.
  774. RandomColorOperation::RandomColorOperation(float t_lb, float t_ub) : t_lb_(t_lb), t_ub_(t_ub) {}
  775. Status RandomColorOperation::ValidateParams() {
  776. // Do some input validation.
  777. if (t_lb_ < 0 || t_ub_ < 0) {
  778. std::string err_msg =
  779. "RandomColor: lower bound or upper bound must be greater than or equal to 0, got t_lb: " + std::to_string(t_lb_) +
  780. ", t_ub: " + std::to_string(t_ub_);
  781. MS_LOG(ERROR) << err_msg;
  782. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  783. }
  784. if (t_lb_ > t_ub_) {
  785. std::string err_msg =
  786. "RandomColor: lower bound must be less or equal to upper bound, 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. return Status::OK();
  792. }
  793. // RandomColorAdjustOperation.
  794. RandomColorAdjustOperation::RandomColorAdjustOperation(std::vector<float> brightness, std::vector<float> contrast,
  795. std::vector<float> saturation, std::vector<float> hue)
  796. : brightness_(brightness), contrast_(contrast), saturation_(saturation), hue_(hue) {}
  797. Status RandomColorAdjustOperation::ValidateParams() {
  798. // brightness
  799. if (brightness_.empty() || brightness_.size() > 2) {
  800. std::string err_msg =
  801. "RandomColorAdjust: brightness must be a vector of one or two values, got: " + std::to_string(brightness_.size());
  802. MS_LOG(ERROR) << err_msg;
  803. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  804. }
  805. for (int32_t i = 0; i < brightness_.size(); ++i) {
  806. if (brightness_[i] < 0) {
  807. std::string err_msg =
  808. "RandomColorAdjust: brightness must be greater than or equal to 0, got: " + std::to_string(brightness_[i]);
  809. MS_LOG(ERROR) << err_msg;
  810. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  811. }
  812. }
  813. if (brightness_.size() == 2 && (brightness_[0] > brightness_[1])) {
  814. std::string err_msg = "RandomColorAdjust: brightness lower bound must be less or equal to upper bound, got lb: " +
  815. std::to_string(brightness_[0]) + ", ub: " + std::to_string(brightness_[1]);
  816. MS_LOG(ERROR) << err_msg;
  817. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  818. }
  819. // contrast
  820. if (contrast_.empty() || contrast_.size() > 2) {
  821. std::string err_msg =
  822. "RandomColorAdjust: contrast must be a vector of one or two values, got: " + std::to_string(contrast_.size());
  823. MS_LOG(ERROR) << err_msg;
  824. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  825. }
  826. for (int32_t i = 0; i < contrast_.size(); ++i) {
  827. if (contrast_[i] < 0) {
  828. std::string err_msg =
  829. "RandomColorAdjust: contrast must be greater than or equal to 0, got: " + std::to_string(contrast_[i]);
  830. MS_LOG(ERROR) << err_msg;
  831. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  832. }
  833. }
  834. if (contrast_.size() == 2 && (contrast_[0] > contrast_[1])) {
  835. std::string err_msg = "RandomColorAdjust: contrast lower bound must be less or equal to upper bound, got lb: " +
  836. std::to_string(contrast_[0]) + ", ub: " + std::to_string(contrast_[1]);
  837. MS_LOG(ERROR) << err_msg;
  838. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  839. }
  840. // saturation
  841. if (saturation_.empty() || saturation_.size() > 2) {
  842. std::string err_msg =
  843. "RandomColorAdjust: saturation must be a vector of one or two values, got: " + std::to_string(saturation_.size());
  844. MS_LOG(ERROR) << err_msg;
  845. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  846. }
  847. for (int32_t i = 0; i < saturation_.size(); ++i) {
  848. if (saturation_[i] < 0) {
  849. std::string err_msg =
  850. "RandomColorAdjust: saturation must be greater than or equal to 0, got: " + std::to_string(saturation_[i]);
  851. MS_LOG(ERROR) << err_msg;
  852. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  853. }
  854. }
  855. if (saturation_.size() == 2 && (saturation_[0] > saturation_[1])) {
  856. std::string err_msg = "RandomColorAdjust: saturation lower bound must be less or equal to upper bound, got lb: " +
  857. std::to_string(saturation_[0]) + ", ub: " + std::to_string(saturation_[1]);
  858. MS_LOG(ERROR) << err_msg;
  859. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  860. }
  861. // hue
  862. if (hue_.empty() || hue_.size() > 2) {
  863. std::string err_msg =
  864. "RandomColorAdjust: hue must be a vector of one or two values, got: " + std::to_string(hue_.size());
  865. MS_LOG(ERROR) << err_msg;
  866. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  867. }
  868. for (int32_t i = 0; i < hue_.size(); ++i) {
  869. if (hue_[i] < -0.5 || hue_[i] > 0.5) {
  870. std::string err_msg = "RandomColorAdjust: hue has to be between -0.5 and 0.5, got: " + std::to_string(hue_[i]);
  871. MS_LOG(ERROR) << err_msg;
  872. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  873. }
  874. }
  875. if (hue_.size() == 2 && (hue_[0] > hue_[1])) {
  876. std::string err_msg =
  877. "RandomColorAdjust: hue lower bound must be less or equal to upper bound, got lb: " + std::to_string(hue_[0]) +
  878. ", ub: " + std::to_string(hue_[1]);
  879. MS_LOG(ERROR) << err_msg;
  880. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  881. }
  882. return Status::OK();
  883. }
  884. std::shared_ptr<TensorOp> RandomColorAdjustOperation::Build() {
  885. float brightness_lb, brightness_ub, contrast_lb, contrast_ub, saturation_lb, saturation_ub, hue_lb, hue_ub;
  886. brightness_lb = brightness_[0];
  887. brightness_ub = brightness_[0];
  888. if (brightness_.size() == 2) brightness_ub = brightness_[1];
  889. contrast_lb = contrast_[0];
  890. contrast_ub = contrast_[0];
  891. if (contrast_.size() == 2) contrast_ub = contrast_[1];
  892. saturation_lb = saturation_[0];
  893. saturation_ub = saturation_[0];
  894. if (saturation_.size() == 2) saturation_ub = saturation_[1];
  895. hue_lb = hue_[0];
  896. hue_ub = hue_[0];
  897. if (hue_.size() == 2) hue_ub = hue_[1];
  898. std::shared_ptr<RandomColorAdjustOp> tensor_op = std::make_shared<RandomColorAdjustOp>(
  899. brightness_lb, brightness_ub, contrast_lb, contrast_ub, saturation_lb, saturation_ub, hue_lb, hue_ub);
  900. return tensor_op;
  901. }
  902. // RandomCropOperation
  903. RandomCropOperation::RandomCropOperation(std::vector<int32_t> size, std::vector<int32_t> padding, bool pad_if_needed,
  904. std::vector<uint8_t> fill_value, BorderType padding_mode)
  905. : size_(size),
  906. padding_(padding),
  907. pad_if_needed_(pad_if_needed),
  908. fill_value_(fill_value),
  909. padding_mode_(padding_mode) {}
  910. Status RandomCropOperation::ValidateParams() {
  911. // size
  912. if (size_.empty() || size_.size() > 2) {
  913. std::string err_msg = "RandomCrop: size must be a vector of one or two values";
  914. MS_LOG(ERROR) << err_msg;
  915. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  916. }
  917. RETURN_IF_NOT_OK(ValidateVectorPositive("RandomCrop", size_));
  918. // padding
  919. RETURN_IF_NOT_OK(ValidateVectorPadding("RandomCrop", padding_));
  920. // fill_value
  921. RETURN_IF_NOT_OK(ValidateVectorFillvalue("RandomCrop", fill_value_));
  922. return Status::OK();
  923. }
  924. std::shared_ptr<TensorOp> RandomCropOperation::Build() {
  925. int32_t crop_height = size_[0];
  926. int32_t crop_width = size_[0];
  927. // User has specified the crop_width value.
  928. if (size_.size() == 2) {
  929. crop_width = size_[1];
  930. }
  931. int32_t pad_top, pad_bottom, pad_left, pad_right;
  932. switch (padding_.size()) {
  933. case 1:
  934. pad_left = padding_[0];
  935. pad_top = padding_[0];
  936. pad_right = padding_[0];
  937. pad_bottom = padding_[0];
  938. break;
  939. case 2:
  940. pad_left = padding_[0];
  941. pad_top = padding_[1];
  942. pad_right = padding_[0];
  943. pad_bottom = padding_[1];
  944. break;
  945. default:
  946. pad_left = padding_[0];
  947. pad_top = padding_[1];
  948. pad_right = padding_[2];
  949. pad_bottom = padding_[3];
  950. }
  951. uint8_t fill_r, fill_g, fill_b;
  952. fill_r = fill_value_[0];
  953. fill_g = fill_value_[0];
  954. fill_b = fill_value_[0];
  955. if (fill_value_.size() == 3) {
  956. fill_r = fill_value_[0];
  957. fill_g = fill_value_[1];
  958. fill_b = fill_value_[2];
  959. }
  960. auto tensor_op = std::make_shared<RandomCropOp>(crop_height, crop_width, pad_top, pad_bottom, pad_left, pad_right,
  961. padding_mode_, pad_if_needed_, fill_r, fill_g, fill_b);
  962. return tensor_op;
  963. }
  964. // RandomCropDecodeResizeOperation
  965. RandomCropDecodeResizeOperation::RandomCropDecodeResizeOperation(std::vector<int32_t> size, std::vector<float> scale,
  966. std::vector<float> ratio,
  967. InterpolationMode interpolation, int32_t max_attempts)
  968. : size_(size), scale_(scale), ratio_(ratio), interpolation_(interpolation), max_attempts_(max_attempts) {}
  969. Status RandomCropDecodeResizeOperation::ValidateParams() {
  970. // size
  971. if (size_.empty() || size_.size() > 2) {
  972. std::string err_msg = "RandomCropDecodeResize: size vector has incorrect size: " + std::to_string(size_.size());
  973. MS_LOG(ERROR) << err_msg;
  974. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  975. }
  976. RETURN_IF_NOT_OK(ValidateVectorPositive("RandomCropDecodeResize", size_));
  977. // rescale
  978. if (scale_.empty() || scale_.size() != 2) {
  979. std::string err_msg = "RandomCropDecodeResize: scale vector has incorrect size: " + std::to_string(scale_.size());
  980. MS_LOG(ERROR) << err_msg;
  981. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  982. }
  983. for (int32_t i = 0; i < scale_.size(); ++i) {
  984. if (scale_[i] < 0) {
  985. std::string err_msg = "RandomCropDecodeResize: invalid scale, scale must be greater than or equal to 0, got: " +
  986. std::to_string(scale_[i]);
  987. MS_LOG(ERROR) << err_msg;
  988. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  989. }
  990. if (scale_[i] == INT_MAX) {
  991. std::string err_msg = "RandomCropDecodeResize: invalid scale, scale too large, got: " + std::to_string(scale_[i]);
  992. MS_LOG(ERROR) << err_msg;
  993. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  994. }
  995. }
  996. if (scale_[0] > scale_[1]) {
  997. std::string err_msg = "RandomCropDecodeResize: scale should be in (min,max) format. Got (max,min).";
  998. MS_LOG(ERROR) << err_msg;
  999. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1000. }
  1001. // ratio
  1002. if (ratio_.empty() || ratio_.size() != 2) {
  1003. std::string err_msg = "RandomCropDecodeResize: ratio vector has incorrect size: " + std::to_string(ratio_.size());
  1004. MS_LOG(ERROR) << err_msg;
  1005. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1006. }
  1007. for (int32_t i = 0; i < ratio_.size(); ++i) {
  1008. if (ratio_[i] < 0) {
  1009. std::string err_msg = "RandomCropDecodeResize: invalid ratio, ratio must be greater than or equal to 0, got: " +
  1010. std::to_string(ratio_[i]);
  1011. MS_LOG(ERROR) << err_msg;
  1012. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1013. }
  1014. if (ratio_[i] == INT_MAX) {
  1015. std::string err_msg = "RandomCropDecodeResize: invalid ratio, ratio too large, got: " + std::to_string(ratio_[i]);
  1016. MS_LOG(ERROR) << err_msg;
  1017. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1018. }
  1019. }
  1020. if (ratio_[0] > ratio_[1]) {
  1021. std::string err_msg = "RandomCropDecodeResize: ratio should be in (min,max) format. Got (max,min).";
  1022. MS_LOG(ERROR) << err_msg;
  1023. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1024. }
  1025. // max_attempts
  1026. if (max_attempts_ < 1) {
  1027. std::string err_msg =
  1028. "RandomCropDecodeResize: max_attempts must be greater than or equal to 1, got: " + std::to_string(max_attempts_);
  1029. MS_LOG(ERROR) << err_msg;
  1030. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1031. }
  1032. return Status::OK();
  1033. }
  1034. std::shared_ptr<TensorOp> RandomCropDecodeResizeOperation::Build() {
  1035. int32_t crop_height = size_[0];
  1036. int32_t crop_width = size_[0];
  1037. // User has specified the crop_width value.
  1038. if (size_.size() == 2) {
  1039. crop_width = size_[1];
  1040. }
  1041. float scale_lower_bound = scale_[0];
  1042. float scale_upper_bound = scale_[1];
  1043. float aspect_lower_bound = ratio_[0];
  1044. float aspect_upper_bound = ratio_[1];
  1045. auto tensor_op =
  1046. std::make_shared<RandomCropDecodeResizeOp>(crop_height, crop_width, scale_lower_bound, scale_upper_bound,
  1047. aspect_lower_bound, aspect_upper_bound, interpolation_, max_attempts_);
  1048. return tensor_op;
  1049. }
  1050. // RandomCropWithBBoxOperation
  1051. RandomCropWithBBoxOperation::RandomCropWithBBoxOperation(std::vector<int32_t> size, std::vector<int32_t> padding,
  1052. bool pad_if_needed, std::vector<uint8_t> fill_value,
  1053. BorderType padding_mode)
  1054. : size_(size),
  1055. padding_(padding),
  1056. pad_if_needed_(pad_if_needed),
  1057. fill_value_(fill_value),
  1058. padding_mode_(padding_mode) {}
  1059. Status RandomCropWithBBoxOperation::ValidateParams() {
  1060. // size
  1061. if (size_.empty() || size_.size() > 2) {
  1062. std::string err_msg = "RandomCropWithBBox: size must be a vector of one or two values";
  1063. MS_LOG(ERROR) << err_msg;
  1064. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1065. }
  1066. RETURN_IF_NOT_OK(ValidateVectorPositive("RandomCropWithBBox", size_));
  1067. // padding
  1068. RETURN_IF_NOT_OK(ValidateVectorPadding("RandomCropWithBBox", padding_));
  1069. // fill_value
  1070. RETURN_IF_NOT_OK(ValidateVectorFillvalue("RandomCropWithBBox", fill_value_));
  1071. return Status::OK();
  1072. }
  1073. std::shared_ptr<TensorOp> RandomCropWithBBoxOperation::Build() {
  1074. int32_t crop_height = size_[0];
  1075. int32_t crop_width = size_[0];
  1076. // User has specified the crop_width value.
  1077. if (size_.size() == 2) {
  1078. crop_width = size_[1];
  1079. }
  1080. int32_t pad_top, pad_bottom, pad_left, pad_right;
  1081. switch (padding_.size()) {
  1082. case 1:
  1083. pad_left = padding_[0];
  1084. pad_top = padding_[0];
  1085. pad_right = padding_[0];
  1086. pad_bottom = padding_[0];
  1087. break;
  1088. case 2:
  1089. pad_left = padding_[0];
  1090. pad_top = padding_[1];
  1091. pad_right = padding_[0];
  1092. pad_bottom = padding_[1];
  1093. break;
  1094. default:
  1095. pad_left = padding_[0];
  1096. pad_top = padding_[1];
  1097. pad_right = padding_[2];
  1098. pad_bottom = padding_[3];
  1099. }
  1100. uint8_t fill_r, fill_g, fill_b;
  1101. fill_r = fill_value_[0];
  1102. fill_g = fill_value_[0];
  1103. fill_b = fill_value_[0];
  1104. if (fill_value_.size() == 3) {
  1105. fill_r = fill_value_[0];
  1106. fill_g = fill_value_[1];
  1107. fill_b = fill_value_[2];
  1108. }
  1109. auto tensor_op =
  1110. std::make_shared<RandomCropWithBBoxOp>(crop_height, crop_width, pad_top, pad_bottom, pad_left, pad_right,
  1111. padding_mode_, pad_if_needed_, fill_r, fill_g, fill_b);
  1112. return tensor_op;
  1113. }
  1114. // RandomHorizontalFlipOperation
  1115. RandomHorizontalFlipOperation::RandomHorizontalFlipOperation(float probability) : probability_(probability) {}
  1116. Status RandomHorizontalFlipOperation::ValidateParams() {
  1117. RETURN_IF_NOT_OK(ValidateProbability("RandomHorizontalFlip", probability_));
  1118. return Status::OK();
  1119. }
  1120. std::shared_ptr<TensorOp> RandomHorizontalFlipOperation::Build() {
  1121. std::shared_ptr<RandomHorizontalFlipOp> tensor_op = std::make_shared<RandomHorizontalFlipOp>(probability_);
  1122. return tensor_op;
  1123. }
  1124. // RandomHorizontalFlipWithBBoxOperation
  1125. RandomHorizontalFlipWithBBoxOperation::RandomHorizontalFlipWithBBoxOperation(float probability)
  1126. : probability_(probability) {}
  1127. Status RandomHorizontalFlipWithBBoxOperation::ValidateParams() {
  1128. RETURN_IF_NOT_OK(ValidateProbability("RandomHorizontalFlipWithBBox", probability_));
  1129. return Status::OK();
  1130. }
  1131. std::shared_ptr<TensorOp> RandomHorizontalFlipWithBBoxOperation::Build() {
  1132. std::shared_ptr<RandomHorizontalFlipWithBBoxOp> tensor_op =
  1133. std::make_shared<RandomHorizontalFlipWithBBoxOp>(probability_);
  1134. return tensor_op;
  1135. }
  1136. // RandomPosterizeOperation
  1137. RandomPosterizeOperation::RandomPosterizeOperation(const std::vector<uint8_t> &bit_range) : bit_range_(bit_range) {}
  1138. Status RandomPosterizeOperation::ValidateParams() {
  1139. if (bit_range_.size() != 2) {
  1140. std::string err_msg =
  1141. "RandomPosterize: bit_range needs to be of size 2 but is of size: " + std::to_string(bit_range_.size());
  1142. MS_LOG(ERROR) << err_msg;
  1143. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1144. }
  1145. if (bit_range_[0] < 1 || bit_range_[0] > 8) {
  1146. std::string err_msg = "RandomPosterize: min_bit value is out of range [1-8]: " + std::to_string(bit_range_[0]);
  1147. MS_LOG(ERROR) << err_msg;
  1148. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1149. }
  1150. if (bit_range_[1] < 1 || bit_range_[1] > 8) {
  1151. std::string err_msg = "RandomPosterize: max_bit value is out of range [1-8]: " + std::to_string(bit_range_[1]);
  1152. MS_LOG(ERROR) << err_msg;
  1153. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1154. }
  1155. if (bit_range_[1] < bit_range_[0]) {
  1156. std::string err_msg = "RandomPosterize: max_bit value is less than min_bit: max =" + std::to_string(bit_range_[1]) +
  1157. ", min = " + std::to_string(bit_range_[0]);
  1158. MS_LOG(ERROR) << err_msg;
  1159. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1160. }
  1161. return Status::OK();
  1162. }
  1163. std::shared_ptr<TensorOp> RandomPosterizeOperation::Build() {
  1164. std::shared_ptr<RandomPosterizeOp> tensor_op = std::make_shared<RandomPosterizeOp>(bit_range_);
  1165. return tensor_op;
  1166. }
  1167. // RandomResizeOperation
  1168. RandomResizeOperation::RandomResizeOperation(std::vector<int32_t> size) : size_(size) {}
  1169. Status RandomResizeOperation::ValidateParams() {
  1170. // size
  1171. if (size_.size() != 2 && size_.size() != 1) {
  1172. std::string err_msg =
  1173. "RandomResize: size must be a vector of one or two values, got: " + std::to_string(size_.size());
  1174. MS_LOG(ERROR) << err_msg;
  1175. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1176. }
  1177. if (size_[0] <= 0 || (size_.size() == 2 && size_[1] <= 0)) {
  1178. std::string err_msg = "RandomResize: size must only contain positive integers.";
  1179. MS_LOG(ERROR) << "RandomResize: size must only contain positive integers, got: " << size_;
  1180. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1181. }
  1182. return Status::OK();
  1183. }
  1184. std::shared_ptr<TensorOp> RandomResizeOperation::Build() {
  1185. // If size is a single value, the smaller edge of the image will be
  1186. // resized to this value with the same image aspect ratio.
  1187. int32_t height = size_[0];
  1188. int32_t width = 0;
  1189. // User specified the width value.
  1190. if (size_.size() == 2) {
  1191. width = size_[1];
  1192. }
  1193. std::shared_ptr<RandomResizeOp> tensor_op = std::make_shared<RandomResizeOp>(height, width);
  1194. return tensor_op;
  1195. }
  1196. // RandomResizeWithBBoxOperation
  1197. RandomResizeWithBBoxOperation::RandomResizeWithBBoxOperation(std::vector<int32_t> size) : size_(size) {}
  1198. Status RandomResizeWithBBoxOperation::ValidateParams() {
  1199. // size
  1200. if (size_.size() != 2 && size_.size() != 1) {
  1201. std::string err_msg =
  1202. "RandomResizeWithBBox: size must be a vector of one or two values, got: " + std::to_string(size_.size());
  1203. MS_LOG(ERROR) << err_msg;
  1204. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1205. }
  1206. if (size_[0] <= 0 || (size_.size() == 2 && size_[1] <= 0)) {
  1207. std::string err_msg = "RandomResizeWithBBox: size must only contain positive integers.";
  1208. MS_LOG(ERROR) << "RandomResizeWithBBox: size must only contain positive integers, got: " << size_;
  1209. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1210. }
  1211. return Status::OK();
  1212. }
  1213. std::shared_ptr<TensorOp> RandomResizeWithBBoxOperation::Build() {
  1214. // If size is a single value, the smaller edge of the image will be
  1215. // resized to this value with the same image aspect ratio.
  1216. int32_t height = size_[0];
  1217. int32_t width = 0;
  1218. // User specified the width value.
  1219. if (size_.size() == 2) {
  1220. width = size_[1];
  1221. }
  1222. std::shared_ptr<RandomResizeWithBBoxOp> tensor_op = std::make_shared<RandomResizeWithBBoxOp>(height, width);
  1223. return tensor_op;
  1224. }
  1225. // RandomResizedCropOperation
  1226. RandomResizedCropOperation::RandomResizedCropOperation(std::vector<int32_t> size, std::vector<float> scale,
  1227. std::vector<float> ratio, InterpolationMode interpolation,
  1228. int32_t max_attempts)
  1229. : size_(size), scale_(scale), ratio_(ratio), interpolation_(interpolation), max_attempts_(max_attempts) {}
  1230. Status RandomResizedCropOperation::ValidateParams() {
  1231. // size
  1232. if (size_.size() != 2 && size_.size() != 1) {
  1233. std::string err_msg =
  1234. "RandomResizedCrop: size must be a vector of one or two values, got: " + std::to_string(size_.size());
  1235. MS_LOG(ERROR) << err_msg;
  1236. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1237. }
  1238. if (size_[0] <= 0 || (size_.size() == 2 && size_[1] <= 0)) {
  1239. std::string err_msg = "RandomResizedCrop: size must only contain positive integers.";
  1240. MS_LOG(ERROR) << "RandomResizedCrop: size must only contain positive integers, got: " << size_;
  1241. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1242. }
  1243. // scale
  1244. if (scale_.size() != 2) {
  1245. std::string err_msg =
  1246. "RandomResizedCrop: scale must be a vector of two values, got: " + std::to_string(scale_.size());
  1247. MS_LOG(ERROR) << err_msg;
  1248. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1249. }
  1250. if (scale_[0] < 0 || scale_[1] < 0) {
  1251. std::string err_msg = "RandomResizedCrop: scale must be greater than or equal to 0.";
  1252. MS_LOG(ERROR) << "RandomResizedCrop: scale must be greater than or equal to 0, got: " << scale_;
  1253. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1254. }
  1255. if (scale_[1] < scale_[0]) {
  1256. std::string err_msg = "RandomResizedCrop: scale must have a size of two in the format of (min, max).";
  1257. MS_LOG(ERROR) << "RandomResizedCrop: scale must have a size of two in the format of (min, max), but got: "
  1258. << scale_;
  1259. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1260. }
  1261. // ratio
  1262. if (ratio_.size() != 2) {
  1263. std::string err_msg =
  1264. "RandomResizedCrop: ratio must be a vector of two values, got: " + std::to_string(ratio_.size());
  1265. MS_LOG(ERROR) << err_msg;
  1266. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1267. }
  1268. if (ratio_[0] < 0 || ratio_[1] < 0) {
  1269. std::string err_msg = "RandomResizedCrop: ratio must be greater than or equal to 0.";
  1270. MS_LOG(ERROR) << "RandomResizedCrop: ratio must be greater than or equal to 0, got: " << ratio_;
  1271. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1272. }
  1273. if (ratio_[1] < ratio_[0]) {
  1274. std::string err_msg = "RandomResizedCrop: ratio must have a size of two in the format of (min, max).";
  1275. MS_LOG(ERROR) << "RandomResizedCrop: ratio must have a size of two in the format of (min, max), but got: "
  1276. << ratio_;
  1277. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1278. }
  1279. // max_attempts
  1280. if (max_attempts_ < 1) {
  1281. std::string err_msg =
  1282. "RandomResizedCrop: max_attempts must be greater than or equal to 1, got: " + std::to_string(max_attempts_);
  1283. MS_LOG(ERROR) << err_msg;
  1284. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1285. }
  1286. return Status::OK();
  1287. }
  1288. std::shared_ptr<TensorOp> RandomResizedCropOperation::Build() {
  1289. int32_t height = size_[0];
  1290. int32_t width = size_[0];
  1291. // User specified the width value.
  1292. if (size_.size() == 2) {
  1293. width = size_[1];
  1294. }
  1295. std::shared_ptr<RandomCropAndResizeOp> tensor_op = std::make_shared<RandomCropAndResizeOp>(
  1296. height, width, scale_[0], scale_[1], ratio_[0], ratio_[1], interpolation_, max_attempts_);
  1297. return tensor_op;
  1298. }
  1299. // RandomResizedCropWithBBoxOperation
  1300. RandomResizedCropWithBBoxOperation::RandomResizedCropWithBBoxOperation(std::vector<int32_t> size,
  1301. std::vector<float> scale,
  1302. std::vector<float> ratio,
  1303. InterpolationMode interpolation,
  1304. int32_t max_attempts)
  1305. : size_(size), scale_(scale), ratio_(ratio), interpolation_(interpolation), max_attempts_(max_attempts) {}
  1306. Status RandomResizedCropWithBBoxOperation::ValidateParams() {
  1307. // size
  1308. if (size_.size() != 2 && size_.size() != 1) {
  1309. std::string err_msg =
  1310. "RandomResizedCropWithBBox: size must be a vector of one or two values, got: " + std::to_string(size_.size());
  1311. MS_LOG(ERROR) << err_msg;
  1312. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1313. }
  1314. if (size_[0] <= 0 || (size_.size() == 2 && size_[1] <= 0)) {
  1315. std::string err_msg = "RandomResizedCropWithBBox: size must only contain positive integers.";
  1316. MS_LOG(ERROR) << "RandomResizedCropWithBBox: size must only contain positive integers, got: " << size_;
  1317. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1318. }
  1319. // scale
  1320. if (scale_.size() != 2) {
  1321. std::string err_msg =
  1322. "RandomResizedCropWithBBox: scale must be a vector of two values, got: " + std::to_string(scale_.size());
  1323. MS_LOG(ERROR) << err_msg;
  1324. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1325. }
  1326. if (scale_[0] < 0 || scale_[1] < 0) {
  1327. std::string err_msg = "RandomResizedCropWithBBox: scale must be greater than or equal to 0.";
  1328. MS_LOG(ERROR) << "RandomResizedCropWithBBox: scale must be greater than or equal to 0, got: " << scale_;
  1329. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1330. }
  1331. if (scale_[1] < scale_[0]) {
  1332. std::string err_msg = "RandomResizedCropWithBBox: scale must have a size of two in the format of (min, max).";
  1333. MS_LOG(ERROR) << "RandomResizedCropWithBBox: scale must have a size of two in the format of (min, max), but got: "
  1334. << scale_;
  1335. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1336. }
  1337. // ratio
  1338. if (ratio_.size() != 2) {
  1339. std::string err_msg =
  1340. "RandomResizedCropWithBBox: ratio must be a vector of two values, got: " + std::to_string(ratio_.size());
  1341. MS_LOG(ERROR) << err_msg;
  1342. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1343. }
  1344. if (ratio_[0] < 0 || ratio_[1] < 0) {
  1345. std::string err_msg = "RandomResizedCropWithBBox: ratio must be greater than or equal to 0.";
  1346. MS_LOG(ERROR) << "RandomResizedCropWithBBox: ratio must be greater than or equal to 0, got: " << ratio_;
  1347. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1348. }
  1349. if (ratio_[1] < ratio_[0]) {
  1350. std::string err_msg = "RandomResizedCropWithBBox: ratio must have a size of two in the format of (min, max).";
  1351. MS_LOG(ERROR) << "RandomResizedCropWithBBox: ratio must have a size of two in the format of (min, max), but got: "
  1352. << ratio_;
  1353. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1354. }
  1355. // max_attempts
  1356. if (max_attempts_ < 1) {
  1357. std::string err_msg = "RandomResizedCropWithBBox: max_attempts must be greater than or equal to 1, got: " +
  1358. std::to_string(max_attempts_);
  1359. MS_LOG(ERROR) << err_msg;
  1360. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1361. }
  1362. return Status::OK();
  1363. }
  1364. std::shared_ptr<TensorOp> RandomResizedCropWithBBoxOperation::Build() {
  1365. int32_t height = size_[0];
  1366. int32_t width = size_[0];
  1367. // User specified the width value.
  1368. if (size_.size() == 2) {
  1369. width = size_[1];
  1370. }
  1371. std::shared_ptr<RandomCropAndResizeWithBBoxOp> tensor_op = std::make_shared<RandomCropAndResizeWithBBoxOp>(
  1372. height, width, scale_[0], scale_[1], ratio_[0], ratio_[1], interpolation_, max_attempts_);
  1373. return tensor_op;
  1374. }
  1375. // Function to create RandomRotationOperation.
  1376. RandomRotationOperation::RandomRotationOperation(std::vector<float> degrees, InterpolationMode interpolation_mode,
  1377. bool expand, std::vector<float> center,
  1378. std::vector<uint8_t> fill_value)
  1379. : degrees_(degrees),
  1380. interpolation_mode_(interpolation_mode),
  1381. expand_(expand),
  1382. center_(center),
  1383. fill_value_(fill_value) {}
  1384. Status RandomRotationOperation::ValidateParams() {
  1385. // degrees
  1386. if (degrees_.size() != 2 && degrees_.size() != 1) {
  1387. std::string err_msg =
  1388. "RandomRotation: degrees must be a vector of one or two values, got: " + std::to_string(degrees_.size());
  1389. MS_LOG(ERROR) << "RandomRotation: degrees must be a vector of one or two values, got: " << degrees_;
  1390. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1391. }
  1392. if ((degrees_[1] < degrees_[0]) && (degrees_.size() == 2)) {
  1393. std::string err_msg = "RandomRotation: degrees must be in the format of (min, max), got: (" +
  1394. std::to_string(degrees_[0]) + ", " + std::to_string(degrees_[1]) + ")";
  1395. MS_LOG(ERROR) << err_msg;
  1396. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1397. } else if ((degrees_[0] < 0) && degrees_.size() == 1) {
  1398. std::string err_msg =
  1399. "RandomRotation: if degrees only has one value, it must be greater than or equal to 0, got: " +
  1400. std::to_string(degrees_[0]);
  1401. MS_LOG(ERROR) << err_msg;
  1402. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1403. }
  1404. // center
  1405. if (center_.empty() || center_.size() != 2) {
  1406. std::string err_msg =
  1407. "RandomRotation: center must be a vector of two values, got: " + std::to_string(center_.size());
  1408. MS_LOG(ERROR) << err_msg;
  1409. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1410. }
  1411. // fill_value
  1412. RETURN_IF_NOT_OK(ValidateVectorFillvalue("RandomRotation", fill_value_));
  1413. return Status::OK();
  1414. }
  1415. std::shared_ptr<TensorOp> RandomRotationOperation::Build() {
  1416. float start_degree, end_degree;
  1417. if (degrees_.size() == 1) {
  1418. start_degree = -degrees_[0];
  1419. end_degree = degrees_[0];
  1420. } else if (degrees_.size() == 2) {
  1421. start_degree = degrees_[0];
  1422. end_degree = degrees_[1];
  1423. }
  1424. uint8_t fill_r, fill_g, fill_b;
  1425. fill_r = fill_value_[0];
  1426. fill_g = fill_value_[0];
  1427. fill_b = fill_value_[0];
  1428. if (fill_value_.size() == 3) {
  1429. fill_r = fill_value_[0];
  1430. fill_g = fill_value_[1];
  1431. fill_b = fill_value_[2];
  1432. }
  1433. std::shared_ptr<RandomRotationOp> tensor_op = std::make_shared<RandomRotationOp>(
  1434. start_degree, end_degree, center_[0], center_[1], interpolation_mode_, expand_, fill_r, fill_g, fill_b);
  1435. return tensor_op;
  1436. }
  1437. // RandomSelectSubpolicyOperation.
  1438. RandomSelectSubpolicyOperation::RandomSelectSubpolicyOperation(
  1439. std::vector<std::vector<std::pair<std::shared_ptr<TensorOperation>, double>>> policy)
  1440. : policy_(policy) {}
  1441. Status RandomSelectSubpolicyOperation::ValidateParams() {
  1442. if (policy_.empty()) {
  1443. std::string err_msg = "RandomSelectSubpolicy: policy must not be empty";
  1444. MS_LOG(ERROR) << err_msg;
  1445. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1446. }
  1447. for (int32_t i = 0; i < policy_.size(); i++) {
  1448. if (policy_[i].empty()) {
  1449. std::string err_msg = "RandomSelectSubpolicy: policy[" + std::to_string(i) + "] must not be empty";
  1450. MS_LOG(ERROR) << err_msg;
  1451. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1452. }
  1453. for (int32_t j = 0; j < policy_[i].size(); j++) {
  1454. if (policy_[i][j].first == nullptr) {
  1455. std::string transform_pos = "[" + std::to_string(i) + "]" + "[" + std::to_string(j) + "]";
  1456. std::string err_msg = "RandomSelectSubpolicy: transform in policy" + transform_pos + " must not be null";
  1457. MS_LOG(ERROR) << err_msg;
  1458. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1459. }
  1460. if (policy_[i][j].second < 0.0 || policy_[i][j].second > 1.0) {
  1461. std::string transform_pos = "[" + std::to_string(i) + "]" + "[" + std::to_string(j) + "]";
  1462. std::string err_msg = "RandomSelectSubpolicy: probability of transform in policy" + transform_pos +
  1463. " must be between 0.0 and 1.0, got: " + std::to_string(policy_[i][j].second);
  1464. MS_LOG(ERROR) << err_msg;
  1465. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1466. }
  1467. }
  1468. }
  1469. return Status::OK();
  1470. }
  1471. std::shared_ptr<TensorOp> RandomSelectSubpolicyOperation::Build() {
  1472. std::vector<Subpolicy> policy_tensor_ops;
  1473. for (int32_t i = 0; i < policy_.size(); i++) {
  1474. Subpolicy sub_policy_tensor_ops;
  1475. for (int32_t j = 0; j < policy_[i].size(); j++) {
  1476. sub_policy_tensor_ops.push_back(std::make_pair(policy_[i][j].first->Build(), policy_[i][j].second));
  1477. }
  1478. policy_tensor_ops.push_back(sub_policy_tensor_ops);
  1479. }
  1480. std::shared_ptr<RandomSelectSubpolicyOp> tensor_op = std::make_shared<RandomSelectSubpolicyOp>(policy_tensor_ops);
  1481. return tensor_op;
  1482. }
  1483. // Function to create RandomSharpness.
  1484. RandomSharpnessOperation::RandomSharpnessOperation(std::vector<float> degrees) : degrees_(degrees) {}
  1485. Status RandomSharpnessOperation::ValidateParams() {
  1486. if (degrees_.size() != 2 || degrees_[0] < 0 || degrees_[1] < 0) {
  1487. std::string err_msg = "RandomSharpness: degrees must be a vector of two values and greater than or equal to 0.";
  1488. MS_LOG(ERROR) << "RandomSharpness: degrees must be a vector of two values and greater than or equal to 0, got: "
  1489. << degrees_;
  1490. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1491. }
  1492. if (degrees_[1] < degrees_[0]) {
  1493. std::string err_msg = "RandomSharpness: degrees must be in the format of (min, max).";
  1494. MS_LOG(ERROR) << "RandomSharpness: degrees must be in the format of (min, max), got: " << degrees_;
  1495. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1496. }
  1497. return Status::OK();
  1498. }
  1499. std::shared_ptr<TensorOp> RandomSharpnessOperation::Build() {
  1500. std::shared_ptr<RandomSharpnessOp> tensor_op = std::make_shared<RandomSharpnessOp>(degrees_[0], degrees_[1]);
  1501. return tensor_op;
  1502. }
  1503. // RandomSolarizeOperation.
  1504. RandomSolarizeOperation::RandomSolarizeOperation(std::vector<uint8_t> threshold) : threshold_(threshold) {}
  1505. Status RandomSolarizeOperation::ValidateParams() {
  1506. if (threshold_.size() != 2) {
  1507. std::string err_msg =
  1508. "RandomSolarize: threshold must be a vector of two values, got: " + std::to_string(threshold_.size());
  1509. MS_LOG(ERROR) << err_msg;
  1510. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1511. }
  1512. for (int32_t i = 0; i < threshold_.size(); ++i) {
  1513. if (threshold_[i] < 0 || threshold_[i] > 255) {
  1514. std::string err_msg =
  1515. "RandomSolarize: threshold has to be between 0 and 255, got:" + std::to_string(threshold_[i]);
  1516. MS_LOG(ERROR) << err_msg;
  1517. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1518. }
  1519. }
  1520. if (threshold_[0] > threshold_[1]) {
  1521. std::string err_msg = "RandomSolarize: threshold must be passed in a (min, max) format";
  1522. MS_LOG(ERROR) << err_msg;
  1523. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1524. }
  1525. return Status::OK();
  1526. }
  1527. std::shared_ptr<TensorOp> RandomSolarizeOperation::Build() {
  1528. std::shared_ptr<RandomSolarizeOp> tensor_op = std::make_shared<RandomSolarizeOp>(threshold_);
  1529. return tensor_op;
  1530. }
  1531. // RandomVerticalFlipOperation
  1532. RandomVerticalFlipOperation::RandomVerticalFlipOperation(float probability) : probability_(probability) {}
  1533. Status RandomVerticalFlipOperation::ValidateParams() {
  1534. RETURN_IF_NOT_OK(ValidateProbability("RandomVerticalFlip", probability_));
  1535. return Status::OK();
  1536. }
  1537. std::shared_ptr<TensorOp> RandomVerticalFlipOperation::Build() {
  1538. std::shared_ptr<RandomVerticalFlipOp> tensor_op = std::make_shared<RandomVerticalFlipOp>(probability_);
  1539. return tensor_op;
  1540. }
  1541. // RandomVerticalFlipWithBBoxOperation
  1542. RandomVerticalFlipWithBBoxOperation::RandomVerticalFlipWithBBoxOperation(float probability)
  1543. : probability_(probability) {}
  1544. Status RandomVerticalFlipWithBBoxOperation::ValidateParams() {
  1545. RETURN_IF_NOT_OK(ValidateProbability("RandomVerticalFlipWithBBox", probability_));
  1546. return Status::OK();
  1547. }
  1548. std::shared_ptr<TensorOp> RandomVerticalFlipWithBBoxOperation::Build() {
  1549. std::shared_ptr<RandomVerticalFlipWithBBoxOp> tensor_op =
  1550. std::make_shared<RandomVerticalFlipWithBBoxOp>(probability_);
  1551. return tensor_op;
  1552. }
  1553. // RescaleOperation
  1554. RescaleOperation::RescaleOperation(float rescale, float shift) : rescale_(rescale), shift_(shift) {}
  1555. Status RescaleOperation::ValidateParams() {
  1556. if (rescale_ < 0) {
  1557. std::string err_msg = "Rescale: rescale must be greater than or equal to 0, got: " + std::to_string(rescale_);
  1558. MS_LOG(ERROR) << err_msg;
  1559. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1560. }
  1561. return Status::OK();
  1562. }
  1563. std::shared_ptr<TensorOp> RescaleOperation::Build() {
  1564. std::shared_ptr<RescaleOp> tensor_op = std::make_shared<RescaleOp>(rescale_, shift_);
  1565. return tensor_op;
  1566. }
  1567. #endif
  1568. // ResizeOperation
  1569. ResizeOperation::ResizeOperation(std::vector<int32_t> size, InterpolationMode interpolation)
  1570. : size_(size), interpolation_(interpolation) {}
  1571. Status ResizeOperation::ValidateParams() {
  1572. // size
  1573. if (size_.empty() || size_.size() > 2) {
  1574. std::string err_msg = "Resize: size must be a vector of one or two values, got: " + std::to_string(size_.size());
  1575. MS_LOG(ERROR) << err_msg;
  1576. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1577. }
  1578. RETURN_IF_NOT_OK(ValidateVectorPositive("Resize", size_));
  1579. return Status::OK();
  1580. }
  1581. std::shared_ptr<TensorOp> ResizeOperation::Build() {
  1582. // If size is a single value, the smaller edge of the image will be
  1583. // resized to this value with the same image aspect ratio.
  1584. int32_t height = size_[0];
  1585. int32_t width = 0;
  1586. // User specified the width value.
  1587. if (size_.size() == 2) {
  1588. width = size_[1];
  1589. }
  1590. return std::make_shared<ResizeOp>(height, width, interpolation_);
  1591. }
  1592. #ifndef ENABLE_ANDROID
  1593. // ResizeWithBBoxOperation
  1594. ResizeWithBBoxOperation::ResizeWithBBoxOperation(std::vector<int32_t> size, InterpolationMode interpolation)
  1595. : size_(size), interpolation_(interpolation) {}
  1596. Status ResizeWithBBoxOperation::ValidateParams() {
  1597. // size
  1598. if (size_.empty() || size_.size() > 2) {
  1599. std::string err_msg =
  1600. "ResizeWithBBox: size must be a vector of one or two values, got: " + std::to_string(size_.size());
  1601. MS_LOG(ERROR) << err_msg;
  1602. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1603. }
  1604. RETURN_IF_NOT_OK(ValidateVectorPositive("Resize", size_));
  1605. return Status::OK();
  1606. }
  1607. std::shared_ptr<TensorOp> ResizeWithBBoxOperation::Build() {
  1608. int32_t height = size_[0];
  1609. int32_t width = 0;
  1610. // User specified the width value.
  1611. if (size_.size() == 2) {
  1612. width = size_[1];
  1613. }
  1614. return std::make_shared<ResizeWithBBoxOp>(height, width, interpolation_);
  1615. }
  1616. // RgbaToBgrOperation.
  1617. RgbaToBgrOperation::RgbaToBgrOperation() {}
  1618. Status RgbaToBgrOperation::ValidateParams() { return Status::OK(); }
  1619. std::shared_ptr<TensorOp> RgbaToBgrOperation::Build() {
  1620. std::shared_ptr<RgbaToBgrOp> tensor_op = std::make_shared<RgbaToBgrOp>();
  1621. return tensor_op;
  1622. }
  1623. // RgbaToRgbOperation.
  1624. RgbaToRgbOperation::RgbaToRgbOperation() {}
  1625. Status RgbaToRgbOperation::ValidateParams() { return Status::OK(); }
  1626. std::shared_ptr<TensorOp> RgbaToRgbOperation::Build() {
  1627. std::shared_ptr<RgbaToRgbOp> tensor_op = std::make_shared<RgbaToRgbOp>();
  1628. return tensor_op;
  1629. }
  1630. // SoftDvppDecodeRandomCropResizeJpegOperation
  1631. SoftDvppDecodeRandomCropResizeJpegOperation::SoftDvppDecodeRandomCropResizeJpegOperation(std::vector<int32_t> size,
  1632. std::vector<float> scale,
  1633. std::vector<float> ratio,
  1634. int32_t max_attempts)
  1635. : size_(size), scale_(scale), ratio_(ratio), max_attempts_(max_attempts) {}
  1636. Status SoftDvppDecodeRandomCropResizeJpegOperation::ValidateParams() {
  1637. // size
  1638. if (size_.size() != 2 && size_.size() != 1) {
  1639. std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: size must be a vector of one or two values, got: " +
  1640. std::to_string(size_.size());
  1641. MS_LOG(ERROR) << err_msg;
  1642. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1643. }
  1644. if (size_[0] <= 0 || (size_.size() == 2 && size_[1] <= 0)) {
  1645. std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: size must only contain positive integers.";
  1646. MS_LOG(ERROR) << "SoftDvppDecodeRandomCropResizeJpeg: size must only contain positive integers, got: " << size_;
  1647. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1648. }
  1649. // scale
  1650. if (scale_.size() != 2) {
  1651. std::string err_msg =
  1652. "SoftDvppDecodeRandomCropResizeJpeg: scale must be a vector of two values, got: " + std::to_string(scale_.size());
  1653. MS_LOG(ERROR) << err_msg;
  1654. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1655. }
  1656. if (scale_[0] < 0 || scale_[1] < 0) {
  1657. std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: scale must be greater than or equal to 0.";
  1658. MS_LOG(ERROR) << "SoftDvppDecodeRandomCropResizeJpeg: scale must be greater than or equal to 0, got: " << scale_;
  1659. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1660. }
  1661. if (scale_[1] < scale_[0]) {
  1662. std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: scale must be in the format of (min, max).";
  1663. MS_LOG(ERROR) << "SoftDvppDecodeRandomCropResizeJpeg: scale must be in the format of (min, max), but got: "
  1664. << scale_;
  1665. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1666. }
  1667. // ratio
  1668. if (ratio_.size() != 2) {
  1669. std::string err_msg =
  1670. "SoftDvppDecodeRandomCropResizeJpeg: ratio must be a vector of two values, got: " + std::to_string(ratio_.size());
  1671. MS_LOG(ERROR) << err_msg;
  1672. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1673. }
  1674. if (ratio_[0] < 0 || ratio_[1] < 0) {
  1675. std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: ratio must be greater than or equal to 0.";
  1676. MS_LOG(ERROR) << "SoftDvppDecodeRandomCropResizeJpeg: ratio must be greater than or equal to 0, got: " << ratio_;
  1677. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1678. }
  1679. if (ratio_[1] < ratio_[0]) {
  1680. std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: ratio must be in the format of (min, max).";
  1681. MS_LOG(ERROR) << "SoftDvppDecodeRandomCropResizeJpeg: ratio must be in the format of (min, max), but got: "
  1682. << ratio_;
  1683. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1684. }
  1685. // max_attempts
  1686. if (max_attempts_ < 1) {
  1687. std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: max_attempts must be greater than or equal to 1, got: " +
  1688. std::to_string(max_attempts_);
  1689. MS_LOG(ERROR) << err_msg;
  1690. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1691. }
  1692. return Status::OK();
  1693. }
  1694. std::shared_ptr<TensorOp> SoftDvppDecodeRandomCropResizeJpegOperation::Build() {
  1695. int32_t height = size_[0];
  1696. int32_t width = size_[0];
  1697. // User specified the width value.
  1698. if (size_.size() == 2) {
  1699. width = size_[1];
  1700. }
  1701. auto tensor_op = std::make_shared<SoftDvppDecodeRandomCropResizeJpegOp>(height, width, scale_[0], scale_[1],
  1702. ratio_[0], ratio_[1], max_attempts_);
  1703. return tensor_op;
  1704. }
  1705. // SoftDvppDecodeResizeJpegOperation
  1706. SoftDvppDecodeResizeJpegOperation::SoftDvppDecodeResizeJpegOperation(std::vector<int32_t> size) : size_(size) {}
  1707. Status SoftDvppDecodeResizeJpegOperation::ValidateParams() {
  1708. // size
  1709. if (size_.empty() || size_.size() > 2) {
  1710. std::string err_msg =
  1711. "SoftDvppDecodeResizeJpeg: size must be a vector of one or two values, got: " + std::to_string(size_.size());
  1712. MS_LOG(ERROR) << err_msg;
  1713. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1714. }
  1715. RETURN_IF_NOT_OK(ValidateVectorPositive("SoftDvppDecodeResizeJpeg", size_));
  1716. return Status::OK();
  1717. }
  1718. std::shared_ptr<TensorOp> SoftDvppDecodeResizeJpegOperation::Build() {
  1719. // If size is a single value, the smaller edge of the image will be
  1720. // resized to this value with the same image aspect ratio.
  1721. int32_t height = size_[0];
  1722. int32_t width = 0;
  1723. // User specified the width value.
  1724. if (size_.size() == 2) {
  1725. width = size_[1];
  1726. }
  1727. std::shared_ptr<SoftDvppDecodeResizeJpegOp> tensor_op = std::make_shared<SoftDvppDecodeResizeJpegOp>(height, width);
  1728. return tensor_op;
  1729. }
  1730. // SwapRedBlueOperation.
  1731. SwapRedBlueOperation::SwapRedBlueOperation() {}
  1732. Status SwapRedBlueOperation::ValidateParams() { return Status::OK(); }
  1733. std::shared_ptr<TensorOp> SwapRedBlueOperation::Build() {
  1734. std::shared_ptr<SwapRedBlueOp> tensor_op = std::make_shared<SwapRedBlueOp>();
  1735. return tensor_op;
  1736. }
  1737. // UniformAugOperation
  1738. UniformAugOperation::UniformAugOperation(std::vector<std::shared_ptr<TensorOperation>> transforms, int32_t num_ops)
  1739. : transforms_(transforms), num_ops_(num_ops) {}
  1740. Status UniformAugOperation::ValidateParams() {
  1741. // transforms
  1742. RETURN_IF_NOT_OK(ValidateVectorTransforms("UniformAug", transforms_));
  1743. if (num_ops_ > transforms_.size()) {
  1744. std::string err_msg = "UniformAug: num_ops is greater than transforms size, but got: " + std::to_string(num_ops_);
  1745. MS_LOG(ERROR) << err_msg;
  1746. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1747. }
  1748. // num_ops
  1749. if (num_ops_ <= 0) {
  1750. std::string err_msg = "UniformAug: num_ops must be greater than 0, but got: " + std::to_string(num_ops_);
  1751. MS_LOG(ERROR) << err_msg;
  1752. RETURN_STATUS_SYNTAX_ERROR(err_msg);
  1753. }
  1754. return Status::OK();
  1755. }
  1756. std::shared_ptr<TensorOp> UniformAugOperation::Build() {
  1757. std::vector<std::shared_ptr<TensorOp>> tensor_ops;
  1758. (void)std::transform(transforms_.begin(), transforms_.end(), std::back_inserter(tensor_ops),
  1759. [](std::shared_ptr<TensorOperation> op) -> std::shared_ptr<TensorOp> { return op->Build(); });
  1760. std::shared_ptr<UniformAugOp> tensor_op = std::make_shared<UniformAugOp>(tensor_ops, num_ops_);
  1761. return tensor_op;
  1762. }
  1763. #endif
  1764. } // namespace vision
  1765. } // namespace dataset
  1766. } // namespace mindspore