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.

c_transforms.py 57 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  1. # Copyright 2019 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """
  16. The module vision.c_transforms is inherited from _c_dataengine
  17. and is implemented based on OpenCV in C++. It's a high performance module to
  18. process images. Users can apply suitable augmentations on image data
  19. to improve their training models.
  20. .. Note::
  21. A constructor's arguments for every class in this module must be saved into the
  22. class attributes (self.xxx) to support save() and load().
  23. Examples:
  24. >>> import mindspore.dataset as ds
  25. >>> import mindspore.dataset.transforms.c_transforms as c_transforms
  26. >>> import mindspore.dataset.vision.c_transforms as c_vision
  27. >>> from mindspore.dataset.vision import Border, Inter
  28. >>>
  29. >>> dataset_dir = "path/to/imagefolder_directory"
  30. >>> # create a dataset that reads all files in dataset_dir with 8 threads
  31. >>> data1 = ds.ImageFolderDataset(dataset_dir, num_parallel_workers=8)
  32. >>> # create a list of transformations to be applied to the image data
  33. >>> transforms_list = [c_vision.Decode(),
  34. >>> c_vision.Resize((256, 256), interpolation=Inter.LINEAR),
  35. >>> c_vision.RandomCrop(200, padding_mode=Border.EDGE),
  36. >>> c_vision.RandomRotation((0, 15)),
  37. >>> c_vision.Normalize((100, 115.0, 121.0), (71.0, 68.0, 70.0)),
  38. >>> c_vision.HWC2CHW()]
  39. >>> onehot_op = c_transforms.OneHot(num_classes=10)
  40. >>> # apply the transformation to the dataset through data1.map()
  41. >>> data1 = data1.map(operations=transforms_list, input_columns="image")
  42. >>> data1 = data1.map(operations=onehot_op, input_columns="label")
  43. """
  44. import numbers
  45. import mindspore._c_dataengine as cde
  46. from .utils import Inter, Border, ImageBatchFormat
  47. from .validators import check_prob, check_crop, check_resize_interpolation, check_random_resize_crop, \
  48. check_mix_up_batch_c, check_normalize_c, check_random_crop, check_random_color_adjust, check_random_rotation, \
  49. check_range, check_resize, check_rescale, check_pad, check_cutout, \
  50. check_uniform_augment_cpp, \
  51. check_bounding_box_augment_cpp, check_random_select_subpolicy_op, check_auto_contrast, check_random_affine, \
  52. check_random_solarize, check_soft_dvpp_decode_random_crop_resize_jpeg, check_positive_degrees, FLOAT_MAX_INTEGER, \
  53. check_cut_mix_batch_c, check_posterize
  54. DE_C_INTER_MODE = {Inter.NEAREST: cde.InterpolationMode.DE_INTER_NEAREST_NEIGHBOUR,
  55. Inter.LINEAR: cde.InterpolationMode.DE_INTER_LINEAR,
  56. Inter.CUBIC: cde.InterpolationMode.DE_INTER_CUBIC}
  57. DE_C_BORDER_TYPE = {Border.CONSTANT: cde.BorderType.DE_BORDER_CONSTANT,
  58. Border.EDGE: cde.BorderType.DE_BORDER_EDGE,
  59. Border.REFLECT: cde.BorderType.DE_BORDER_REFLECT,
  60. Border.SYMMETRIC: cde.BorderType.DE_BORDER_SYMMETRIC}
  61. DE_C_IMAGE_BATCH_FORMAT = {ImageBatchFormat.NHWC: cde.ImageBatchFormat.DE_IMAGE_BATCH_FORMAT_NHWC,
  62. ImageBatchFormat.NCHW: cde.ImageBatchFormat.DE_IMAGE_BATCH_FORMAT_NCHW}
  63. def parse_padding(padding):
  64. if isinstance(padding, numbers.Number):
  65. padding = [padding] * 4
  66. if len(padding) == 2:
  67. left = right = padding[0]
  68. top = bottom = padding[1]
  69. padding = (left, top, right, bottom,)
  70. if isinstance(padding, list):
  71. padding = tuple(padding)
  72. return padding
  73. class AutoContrast(cde.AutoContrastOp):
  74. """
  75. Apply automatic contrast on input image.
  76. Args:
  77. cutoff (float, optional): Percent of pixels to cut off from the histogram (default=0.0).
  78. ignore (Union[int, sequence], optional): Pixel values to ignore (default=None).
  79. Examples:
  80. >>> import mindspore.dataset.vision.c_transforms as c_vision
  81. >>>
  82. >>> transforms_list = [c_vision.Decode(), c_vision.AutoContrast(cutoff=10.0, ignore=[10, 20])]
  83. >>> data1 = data1.map(operations=transforms_list, input_columns=["image"])
  84. """
  85. @check_auto_contrast
  86. def __init__(self, cutoff=0.0, ignore=None):
  87. if ignore is None:
  88. ignore = []
  89. if isinstance(ignore, int):
  90. ignore = [ignore]
  91. super().__init__(cutoff, ignore)
  92. class RandomSharpness(cde.RandomSharpnessOp):
  93. """
  94. Adjust the sharpness of the input image by a fixed or random degree. Degree of 0.0 gives a blurred image,
  95. degree of 1.0 gives the original image, and degree of 2.0 gives a sharpened image.
  96. Args:
  97. degrees (tuple, optional): Range of random sharpness adjustment degrees. It should be in (min, max) format.
  98. If min=max, then it is a single fixed magnitude operation (default = (0.1, 1.9)).
  99. Raises:
  100. TypeError : If degrees is not a list or tuple.
  101. ValueError: If degrees is negative.
  102. ValueError: If degrees is in (max, min) format instead of (min, max).
  103. Examples:
  104. >>> import mindspore.dataset.vision.c_transforms as c_vision
  105. >>>
  106. >>> transforms_list = [c_vision.Decode(), c_vision.RandomSharpness(degrees=(0.2, 1.9))]
  107. >>> data1 = data1.map(operations=transforms_list, input_columns=["image"])
  108. """
  109. @check_positive_degrees
  110. def __init__(self, degrees=(0.1, 1.9)):
  111. self.degrees = degrees
  112. super().__init__(*degrees)
  113. class Equalize(cde.EqualizeOp):
  114. """
  115. Apply histogram equalization on input image.
  116. Examples:
  117. >>> import mindspore.dataset.vision.c_transforms as c_vision
  118. >>>
  119. >>> transforms_list = [c_vision.Decode(), c_vision.Equalize()]
  120. >>> data1 = data1.map(operations=transforms_list, input_columns=["image"])
  121. """
  122. class Invert(cde.InvertOp):
  123. """
  124. Apply invert on input image in RGB mode.
  125. Examples:
  126. >>> import mindspore.dataset.vision.c_transforms as c_vision
  127. >>>
  128. >>> transforms_list = [c_vision.Decode(), c_vision.Invert()]
  129. >>> data1 = data1.map(operations=transforms_list, input_columns=["image"])
  130. """
  131. class Decode(cde.DecodeOp):
  132. """
  133. Decode the input image in RGB mode.
  134. Args:
  135. rgb (bool, optional): Mode of decoding input image (default=True).
  136. Examples:
  137. >>> import mindspore.dataset.vision.c_transforms as c_vision
  138. >>>
  139. >>> transforms_list = [c_vision.Decode(), c_vision.RandomHorizontalFlip()]
  140. >>> data1 = data1.map(operations=transforms_list, input_columns=["image"])
  141. """
  142. def __init__(self, rgb=True):
  143. self.rgb = rgb
  144. super().__init__(self.rgb)
  145. class CutMixBatch(cde.CutMixBatchOp):
  146. """
  147. Apply CutMix transformation on input batch of images and labels.
  148. Note that you need to make labels into one-hot format and batch before calling this function.
  149. Args:
  150. image_batch_format (Image Batch Format): The method of padding. Can be any of
  151. [ImageBatchFormat.NHWC, ImageBatchFormat.NCHW]
  152. alpha (float, optional): hyperparameter of beta distribution (default = 1.0).
  153. prob (float, optional): The probability by which CutMix is applied to each image (default = 1.0).
  154. Examples:
  155. >>> import mindspore.dataset.transforms.c_transforms as c_transforms
  156. >>> import mindspore.dataset.vision.c_transforms as c_vision
  157. >>> from mindspore.dataset.transforms.vision import ImageBatchFormat
  158. >>>
  159. >>> onehot_op = c_transforms.OneHot(num_classes=10)
  160. >>> data1 = data1.map(operations=onehot_op, input_columns=["label"])
  161. >>> cutmix_batch_op = c_vision.CutMixBatch(ImageBatchFormat.NHWC, 1.0, 0.5)
  162. >>> data1 = data1.batch(5)
  163. >>> data1 = data1.map(operations=cutmix_batch_op, input_columns=["image", "label"])
  164. """
  165. @check_cut_mix_batch_c
  166. def __init__(self, image_batch_format, alpha=1.0, prob=1.0):
  167. self.image_batch_format = image_batch_format.value
  168. self.alpha = alpha
  169. self.prob = prob
  170. super().__init__(DE_C_IMAGE_BATCH_FORMAT[image_batch_format], alpha, prob)
  171. class CutOut(cde.CutOutOp):
  172. """
  173. Randomly cut (mask) out a given number of square patches from the input NumPy image array.
  174. Args:
  175. length (int): The side length of each square patch.
  176. num_patches (int, optional): Number of patches to be cut out of an image (default=1).
  177. Examples:
  178. >>> import mindspore.dataset.vision.c_transforms as c_vision
  179. >>>
  180. >>> transforms_list = [c_vision.Decode(), c_vision.CutOut(80, num_patches=10)]
  181. >>> data1 = data1.map(operations=transforms_list, input_columns=["image"])
  182. """
  183. @check_cutout
  184. def __init__(self, length, num_patches=1):
  185. self.length = length
  186. self.num_patches = num_patches
  187. fill_value = (0, 0, 0)
  188. super().__init__(length, length, num_patches, False, *fill_value)
  189. class MixUpBatch(cde.MixUpBatchOp):
  190. """
  191. Apply MixUp transformation on input batch of images and labels. Each image is multiplied by a random weight (lambda)
  192. and then added to a randomly selected image from the batch multiplied by (1 - lambda). The same formula is also
  193. applied to the one-hot labels.
  194. Note that you need to make labels into one-hot format and batch before calling this function.
  195. Args:
  196. alpha (float, optional): Hyperparameter of beta distribution (default = 1.0).
  197. Examples:
  198. >>> import mindspore.dataset.transforms.c_transforms as c_transforms
  199. >>> import mindspore.dataset.vision.c_transforms as c_vision
  200. >>>
  201. >>> onehot_op = c_transforms.OneHot(num_classes=10)
  202. >>> data1 = data1.map(operations=onehot_op, input_columns=["label"])
  203. >>> mixup_batch_op = c_vision.MixUpBatch(alpha=0.9)
  204. >>> data1 = data1.batch(5)
  205. >>> data1 = data1.map(operations=mixup_batch_op, input_columns=["image", "label"])
  206. """
  207. @check_mix_up_batch_c
  208. def __init__(self, alpha=1.0):
  209. self.alpha = alpha
  210. super().__init__(alpha)
  211. class Normalize(cde.NormalizeOp):
  212. """
  213. Normalize the input image with respect to mean and standard deviation.
  214. Args:
  215. mean (sequence): List or tuple of mean values for each channel, with respect to channel order.
  216. The mean values must be in range (0.0, 255.0].
  217. std (sequence): List or tuple of standard deviations for each channel, with respect to channel order.
  218. The standard deviation values must be in range (0.0, 255.0].
  219. Examples:
  220. >>> import mindspore.dataset.vision.c_transforms as c_vision
  221. >>>
  222. >>> decode_op = c_vision.Decode()
  223. >>> normalize_op = c_vision.Normalize(mean=[121.0, 115.0, 100.0], std=[70.0, 68.0, 71.0])
  224. >>> transforms_list = [decode_op, normalize_op]
  225. >>> data1 = data1.map(operations=transforms_list, input_columns=["image"])
  226. """
  227. @check_normalize_c
  228. def __init__(self, mean, std):
  229. self.mean = mean
  230. self.std = std
  231. super().__init__(*mean, *std)
  232. class RandomAffine(cde.RandomAffineOp):
  233. """
  234. Apply Random affine transformation to the input image.
  235. Args:
  236. degrees (int or float or sequence): Range of the rotation degrees.
  237. If degrees is a number, the range will be (-degrees, degrees).
  238. If degrees is a sequence, it should be (min, max).
  239. translate (sequence, optional): Sequence (tx_min, tx_max, ty_min, ty_max) of minimum/maximum translation in
  240. x(horizontal) and y(vertical) directions (default=None).
  241. The horizontal and vertical shift is selected randomly from the range:
  242. (tx_min*width, tx_max*width) and (ty_min*height, ty_max*height), respectively.
  243. If a tuple or list of size 2, then a translate parallel to the X axis in the range of
  244. (translate[0], translate[1]) is applied.
  245. If a tuple of list of size 4, then a translate parallel to the X axis in the range of
  246. (translate[0], translate[1]) and a translate parallel to the Y axis in the range of
  247. (translate[2], translate[3]) are applied.
  248. If None, no translation is applied.
  249. scale (sequence, optional): Scaling factor interval (default=None, original scale is used).
  250. shear (int or float or sequence, optional): Range of shear factor (default=None).
  251. If a number, then a shear parallel to the X axis in the range of (-shear, +shear) is applied.
  252. If a tuple or list of size 2, then a shear parallel to the X axis in the range of (shear[0], shear[1])
  253. is applied.
  254. If a tuple of list of size 4, then a shear parallel to X axis in the range of (shear[0], shear[1])
  255. and a shear parallel to Y axis in the range of (shear[2], shear[3]) is applied.
  256. If None, no shear is applied.
  257. resample (Inter mode, optional): An optional resampling filter (default=Inter.NEAREST).
  258. If omitted, or if the image has mode "1" or "P", it is set to be Inter.NEAREST.
  259. It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC].
  260. - Inter.BILINEAR, means resample method is bilinear interpolation.
  261. - Inter.NEAREST, means resample method is nearest-neighbor interpolation.
  262. - Inter.BICUBIC, means resample method is bicubic interpolation.
  263. fill_value (tuple or int, optional): Optional fill_value to fill the area outside the transform
  264. in the output image. There must be three elements in tuple and the value of single element is [0, 255].
  265. Used only in Pillow versions > 5.0.0 (default=0, filling is performed).
  266. Raises:
  267. ValueError: If degrees is negative.
  268. ValueError: If translation value is not between -1 and 1.
  269. ValueError: If scale is not positive.
  270. ValueError: If shear is a number but is not positive.
  271. TypeError: If degrees is not a number or a list or a tuple.
  272. If degrees is a list or tuple, its length is not 2.
  273. TypeError: If translate is specified but is not list or a tuple of length 2 or 4.
  274. TypeError: If scale is not a list or tuple of length 2.''
  275. TypeError: If shear is not a list or tuple of length 2 or 4.
  276. TypeError: If fill_value is not a single integer or a 3-tuple.
  277. Examples:
  278. >>> import mindspore.dataset.vision.c_transforms as c_vision
  279. >>> from mindspore.dataset.transforms.vision import Inter
  280. >>>
  281. >>> decode_op = c_vision.Decode()
  282. >>> random_affine_op = c_vision.RandomAffine(degrees=15, translate=(-0.1, 0.1, 0, 0), scale=(0.9, 1.1),
  283. >>> resample=Inter.NEAREST)
  284. >>> transforms_list = [decode_op, random_affine_op]
  285. >>> data1 = data1.map(operations=transforms_list, input_columns=["image"])
  286. """
  287. @check_random_affine
  288. def __init__(self, degrees, translate=None, scale=None, shear=None, resample=Inter.NEAREST, fill_value=0):
  289. # Parameter checking
  290. if shear is not None:
  291. if isinstance(shear, numbers.Number):
  292. shear = (-1 * shear, shear, 0., 0.)
  293. else:
  294. if len(shear) == 2:
  295. shear = [shear[0], shear[1], 0., 0.]
  296. elif len(shear) == 4:
  297. shear = [s for s in shear]
  298. if isinstance(degrees, numbers.Number):
  299. degrees = (-1 * degrees, degrees)
  300. if isinstance(fill_value, numbers.Number):
  301. fill_value = (fill_value, fill_value, fill_value)
  302. # translation
  303. if translate is None:
  304. translate = (0.0, 0.0, 0.0, 0.0)
  305. # scale
  306. if scale is None:
  307. scale = (1.0, 1.0)
  308. # shear
  309. if shear is None:
  310. shear = (0.0, 0.0, 0.0, 0.0)
  311. self.degrees = degrees
  312. self.translate = translate
  313. self.scale_ = scale
  314. self.shear = shear
  315. self.resample = DE_C_INTER_MODE[resample]
  316. self.fill_value = fill_value
  317. super().__init__(degrees, translate, scale, shear, DE_C_INTER_MODE[resample], fill_value)
  318. class RandomCrop(cde.RandomCropOp):
  319. """
  320. Crop the input image at a random location.
  321. Args:
  322. size (Union[int, sequence]): The output size of the cropped image.
  323. If size is an integer, a square crop of size (size, size) is returned.
  324. If size is a sequence of length 2, it should be (height, width).
  325. padding (Union[int, sequence], optional): The number of pixels to pad the image (default=None).
  326. If padding is not None, pad image firstly with padding values.
  327. If a single number is provided, pad all borders with this value.
  328. If a tuple or list of 2 values are provided, pad the (left and top)
  329. with the first value and (right and bottom) with the second value.
  330. If 4 values are provided as a list or tuple,
  331. pad the left, top, right and bottom respectively.
  332. pad_if_needed (bool, optional): Pad the image if either side is smaller than
  333. the given output size (default=False).
  334. fill_value (Union[int, tuple], optional): The pixel intensity of the borders if
  335. the padding_mode is Border.CONSTANT (default=0). If it is a 3-tuple, it is used to
  336. fill R, G, B channels respectively.
  337. padding_mode (Border mode, optional): The method of padding (default=Border.CONSTANT). It can be any of
  338. [Border.CONSTANT, Border.EDGE, Border.REFLECT, Border.SYMMETRIC].
  339. - Border.CONSTANT, means it fills the border with constant values.
  340. - Border.EDGE, means it pads with the last value on the edge.
  341. - Border.REFLECT, means it reflects the values on the edge omitting the last
  342. value of edge.
  343. - Border.SYMMETRIC, means it reflects the values on the edge repeating the last
  344. value of edge.
  345. Examples:
  346. >>> import mindspore.dataset.vision.c_transforms as c_vision
  347. >>>
  348. >>> decode_op = c_vision.Decode()
  349. >>> random_crop_op = c_vision.RandomCrop(512, [200, 200, 200, 200], padding_mode=Border.EDGE)
  350. >>> transforms_list = [decode_op, random_crop_op]
  351. >>> data1 = data1.map(operations=transforms_list, input_columns=["image"])
  352. """
  353. @check_random_crop
  354. def __init__(self, size, padding=None, pad_if_needed=False, fill_value=0, padding_mode=Border.CONSTANT):
  355. if isinstance(size, int):
  356. size = (size, size)
  357. if padding is None:
  358. padding = (0, 0, 0, 0)
  359. else:
  360. padding = parse_padding(padding)
  361. if isinstance(fill_value, int):
  362. fill_value = tuple([fill_value] * 3)
  363. border_type = DE_C_BORDER_TYPE[padding_mode]
  364. self.size = size
  365. self.padding = padding
  366. self.pad_if_needed = pad_if_needed
  367. self.fill_value = fill_value
  368. self.padding_mode = padding_mode.value
  369. super().__init__(*size, *padding, border_type, pad_if_needed, *fill_value)
  370. class RandomCropWithBBox(cde.RandomCropWithBBoxOp):
  371. """
  372. Crop the input image at a random location and adjust bounding boxes accordingly.
  373. Args:
  374. size (Union[int, sequence]): The output size of the cropped image.
  375. If size is an integer, a square crop of size (size, size) is returned.
  376. If size is a sequence of length 2, it should be (height, width).
  377. padding (Union[int, sequence], optional): The number of pixels to pad the image (default=None).
  378. If padding is not None, first pad image with padding values.
  379. If a single number is provided, pad all borders with this value.
  380. If a tuple or list of 2 values are provided, pad the (left and top)
  381. with the first value and (right and bottom) with the second value.
  382. If 4 values are provided as a list or tuple, pad the left, top, right and bottom respectively.
  383. pad_if_needed (bool, optional): Pad the image if either side is smaller than
  384. the given output size (default=False).
  385. fill_value (Union[int, tuple], optional): The pixel intensity of the borders if
  386. the padding_mode is Border.CONSTANT (default=0). If it is a 3-tuple, it is used to
  387. fill R, G, B channels respectively.
  388. padding_mode (Border mode, optional): The method of padding (default=Border.CONSTANT). It can be any of
  389. [Border.CONSTANT, Border.EDGE, Border.REFLECT, Border.SYMMETRIC].
  390. - Border.CONSTANT, means it fills the border with constant values.
  391. - Border.EDGE, means it pads with the last value on the edge.
  392. - Border.REFLECT, means it reflects the values on the edge omitting the last
  393. value of edge.
  394. - Border.SYMMETRIC, means it reflects the values on the edge repeating the last
  395. value of edge.
  396. Examples:
  397. >>> import mindspore.dataset.vision.c_transforms as c_vision
  398. >>>
  399. >>> decode_op = c_vision.Decode()
  400. >>> random_crop_with_bbox_op = c_vision.RandomCrop([512, 512], [200, 200, 200, 200])
  401. >>> transforms_list = [decode_op, random_crop_with_bbox_op]
  402. >>> data3 = data3.map(operations=transforms_list, input_columns=["image"])
  403. """
  404. @check_random_crop
  405. def __init__(self, size, padding=None, pad_if_needed=False, fill_value=0, padding_mode=Border.CONSTANT):
  406. if isinstance(size, int):
  407. size = (size, size)
  408. if padding is None:
  409. padding = (0, 0, 0, 0)
  410. else:
  411. padding = parse_padding(padding)
  412. if isinstance(fill_value, int):
  413. fill_value = tuple([fill_value] * 3)
  414. border_type = DE_C_BORDER_TYPE[padding_mode]
  415. self.size = size
  416. self.padding = padding
  417. self.pad_if_needed = pad_if_needed
  418. self.fill_value = fill_value
  419. self.padding_mode = padding_mode.value
  420. super().__init__(*size, *padding, border_type, pad_if_needed, *fill_value)
  421. class RandomHorizontalFlip(cde.RandomHorizontalFlipOp):
  422. """
  423. Flip the input image horizontally, randomly with a given probability.
  424. Args:
  425. prob (float, optional): Probability of the image being flipped (default=0.5).
  426. Examples:
  427. >>> import mindspore.dataset.vision.c_transforms as c_vision
  428. >>>
  429. >>> transforms_list = [c_vision.Decode(), c_vision.RandomHorizontalFlip(0.75)]
  430. >>> data1 = data1.map(operations=transforms_list, input_columns=["image"])
  431. """
  432. @check_prob
  433. def __init__(self, prob=0.5):
  434. self.prob = prob
  435. super().__init__(prob)
  436. class RandomHorizontalFlipWithBBox(cde.RandomHorizontalFlipWithBBoxOp):
  437. """
  438. Flip the input image horizontally, randomly with a given probability and adjust bounding boxes accordingly.
  439. Args:
  440. prob (float, optional): Probability of the image being flipped (default=0.5).
  441. Examples:
  442. >>> import mindspore.dataset.vision.c_transforms as c_vision
  443. >>>
  444. >>> transforms_list = [c_vision.Decode(), c_vision.RandomHorizontalFlipWithBBox(0.70)]
  445. >>> data1 = data1.map(operations=transforms_list, input_columns=["image"])
  446. """
  447. @check_prob
  448. def __init__(self, prob=0.5):
  449. self.prob = prob
  450. super().__init__(prob)
  451. class RandomPosterize(cde.RandomPosterizeOp):
  452. """
  453. Reduce the number of bits for each color channel.
  454. Args:
  455. bits (sequence or int, optional): Range of random posterize to compress image.
  456. Bits values must be in range of [1,8], and include at
  457. least one integer value in the given range. It must be in
  458. (min, max) or integer format. If min=max, then it is a single fixed
  459. magnitude operation (default=[4, 8]).
  460. Examples:
  461. >>> import mindspore.dataset.vision.c_transforms as c_vision
  462. >>>
  463. >>> transforms_list = [c_vision.Decode(), c_vision.RandomPosterize((6, 8))]
  464. >>> data1 = data1.map(operations=transforms_list, input_columns=["image"])
  465. """
  466. @check_posterize
  467. def __init__(self, bits=(8, 8)):
  468. self.bits = bits
  469. if isinstance(bits, int):
  470. bits = (bits, bits)
  471. super().__init__(bits)
  472. class RandomVerticalFlip(cde.RandomVerticalFlipOp):
  473. """
  474. Flip the input image vertically, randomly with a given probability.
  475. Args:
  476. prob (float, optional): Probability of the image being flipped (default=0.5).
  477. Examples:
  478. >>> import mindspore.dataset.vision.c_transforms as c_vision
  479. >>>
  480. >>> transforms_list = [c_vision.Decode(), c_vision.RandomVerticalFlip(0.25)]
  481. >>> data1 = data1.map(operations=transforms_list, input_columns=["image"])
  482. """
  483. @check_prob
  484. def __init__(self, prob=0.5):
  485. self.prob = prob
  486. super().__init__(prob)
  487. class RandomVerticalFlipWithBBox(cde.RandomVerticalFlipWithBBoxOp):
  488. """
  489. Flip the input image vertically, randomly with a given probability and adjust bounding boxes accordingly.
  490. Args:
  491. prob (float, optional): Probability of the image being flipped (default=0.5).
  492. Examples:
  493. >>> import mindspore.dataset.vision.c_transforms as c_vision
  494. >>>
  495. >>> transforms_list = [c_vision.Decode(), c_vision.RandomVerticalFlipWithBBox(0.20)]
  496. >>> data1 = data1.map(operations=transforms_list, input_columns=["image"])
  497. """
  498. @check_prob
  499. def __init__(self, prob=0.5):
  500. self.prob = prob
  501. super().__init__(prob)
  502. class BoundingBoxAugment(cde.BoundingBoxAugmentOp):
  503. """
  504. Apply a given image transform on a random selection of bounding box regions of a given image.
  505. Args:
  506. transform: C++ transformation function to be applied on random selection
  507. of bounding box regions of a given image.
  508. ratio (float, optional): Ratio of bounding boxes to apply augmentation on.
  509. Range: [0, 1] (default=0.3).
  510. Examples:
  511. >>> import mindspore.dataset.vision.c_transforms as c_vision
  512. >>>
  513. >>> # set bounding box operation with ratio of 1 to apply rotation on all bounding boxes
  514. >>> bbox_aug_op = c_vision.BoundingBoxAugment(c_vision.RandomRotation(90), 1)
  515. >>> # map to apply ops
  516. >>> data3 = data3.map(operations=[bbox_aug_op],
  517. >>> input_columns=["image", "bbox"],
  518. >>> output_columns=["image", "bbox"],
  519. >>> column_order=["image", "bbox"])
  520. """
  521. @check_bounding_box_augment_cpp
  522. def __init__(self, transform, ratio=0.3):
  523. self.ratio = ratio
  524. self.transform = transform
  525. super().__init__(transform, ratio)
  526. class Resize(cde.ResizeOp):
  527. """
  528. Resize the input image to the given size.
  529. Args:
  530. size (Union[int, sequence]): The output size of the resized image.
  531. If size is an integer, the smaller edge of the image will be resized to this value with
  532. the same image aspect ratio.
  533. If size is a sequence of length 2, it should be (height, width).
  534. interpolation (Inter mode, optional): Image interpolation mode (default=Inter.LINEAR).
  535. It can be any of [Inter.LINEAR, Inter.NEAREST, Inter.BICUBIC].
  536. - Inter.LINEAR, means interpolation method is bilinear interpolation.
  537. - Inter.NEAREST, means interpolation method is nearest-neighbor interpolation.
  538. - Inter.BICUBIC, means interpolation method is bicubic interpolation.
  539. Examples:
  540. >>> import mindspore.dataset.vision.c_transforms as c_vision
  541. >>> from mindspore.dataset.transforms.vision import Inter
  542. >>>
  543. >>> decode_op = c_vision.Decode()
  544. >>> resize_op = c_vision.Resize([100, 75], Inter.BICUBIC)
  545. >>> transforms_list = [decode_op, resize_op]
  546. >>> data1 = data1.map(operations=transforms_list, input_columns=["image"])
  547. """
  548. @check_resize_interpolation
  549. def __init__(self, size, interpolation=Inter.LINEAR):
  550. self.size = size
  551. self.interpolation = interpolation
  552. interpoltn = DE_C_INTER_MODE[interpolation]
  553. if isinstance(size, int):
  554. size = (size, 0)
  555. super().__init__(*size, interpoltn)
  556. class ResizeWithBBox(cde.ResizeWithBBoxOp):
  557. """
  558. Resize the input image to the given size and adjust bounding boxes accordingly.
  559. Args:
  560. size (Union[int, sequence]): The output size of the resized image.
  561. If size is an integer, smaller edge of the image will be resized to this value with
  562. the same image aspect ratio.
  563. If size is a sequence of length 2, it should be (height, width).
  564. interpolation (Inter mode, optional): Image interpolation mode (default=Inter.LINEAR).
  565. It can be any of [Inter.LINEAR, Inter.NEAREST, Inter.BICUBIC].
  566. - Inter.LINEAR, means interpolation method is bilinear interpolation.
  567. - Inter.NEAREST, means interpolation method is nearest-neighbor interpolation.
  568. - Inter.BICUBIC, means interpolation method is bicubic interpolation.
  569. Examples:
  570. >>> import mindspore.dataset.vision.c_transforms as c_vision
  571. >>> from mindspore.dataset.transforms.vision import Inter
  572. >>>
  573. >>> decode_op = c_vision.Decode()
  574. >>> bbox_op = c_vision.ResizeWithBBox(50, Inter.NEAREST)
  575. >>> transforms_list = [decode_op, bbox_op]
  576. >>> data3 = data3.map(operations=transforms_list, input_columns=["image"])
  577. """
  578. @check_resize_interpolation
  579. def __init__(self, size, interpolation=Inter.LINEAR):
  580. self.size = size
  581. self.interpolation = interpolation
  582. interpoltn = DE_C_INTER_MODE[interpolation]
  583. if isinstance(size, int):
  584. size = (size, 0)
  585. super().__init__(*size, interpoltn)
  586. class RandomResizedCropWithBBox(cde.RandomCropAndResizeWithBBoxOp):
  587. """
  588. Crop the input image to a random size and aspect ratio and adjust bounding boxes accordingly.
  589. Args:
  590. size (Union[int, sequence]): The size of the output image.
  591. If size is an integer, a square crop of size (size, size) is returned.
  592. If size is a sequence of length 2, it should be (height, width).
  593. scale (tuple, optional): Range (min, max) of respective size of the original
  594. size to be cropped (default=(0.08, 1.0)).
  595. ratio (tuple, optional): Range (min, max) of aspect ratio to be cropped
  596. (default=(3. / 4., 4. / 3.)).
  597. interpolation (Inter mode, optional): Image interpolation mode (default=Inter.BILINEAR).
  598. It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC].
  599. - Inter.BILINEAR, means interpolation method is bilinear interpolation.
  600. - Inter.NEAREST, means interpolation method is nearest-neighbor interpolation.
  601. - Inter.BICUBIC, means interpolation method is bicubic interpolation.
  602. max_attempts (int, optional): The maximum number of attempts to propose a valid
  603. crop area (default=10). If exceeded, fall back to use center crop instead.
  604. Examples:
  605. >>> import mindspore.dataset.vision.c_transforms as c_vision
  606. >>> from mindspore.dataset.transforms.vision import Inter
  607. >>>
  608. >>> decode_op = c_vision.Decode()
  609. >>> bbox_op = c_vision.RandomResizedCropWithBBox(size=50, interpolation=Inter.NEAREST)
  610. >>> transforms_list = [decode_op, bbox_op]
  611. >>> data3 = data3.map(operations=transforms_list, input_columns=["image"])
  612. """
  613. @check_random_resize_crop
  614. def __init__(self, size, scale=(0.08, 1.0), ratio=(3. / 4., 4. / 3.),
  615. interpolation=Inter.BILINEAR, max_attempts=10):
  616. if isinstance(size, int):
  617. size = (size, size)
  618. self.size = size
  619. self.scale = scale
  620. self.ratio = ratio
  621. self.interpolation = interpolation
  622. self.max_attempts = max_attempts
  623. interpoltn = DE_C_INTER_MODE[interpolation]
  624. super().__init__(*size, *scale, *ratio, interpoltn, max_attempts)
  625. class RandomResizedCrop(cde.RandomCropAndResizeOp):
  626. """
  627. Crop the input image to a random size and aspect ratio.
  628. Args:
  629. size (Union[int, sequence]): The size of the output image.
  630. If size is an integer, a square crop of size (size, size) is returned.
  631. If size is a sequence of length 2, it should be (height, width).
  632. scale (tuple, optional): Range [min, max) of respective size of the original
  633. size to be cropped (default=(0.08, 1.0)).
  634. ratio (tuple, optional): Range [min, max) of aspect ratio to be cropped
  635. (default=(3. / 4., 4. / 3.)).
  636. interpolation (Inter mode, optional): Image interpolation mode (default=Inter.BILINEAR).
  637. It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC].
  638. - Inter.BILINEAR, means interpolation method is bilinear interpolation.
  639. - Inter.NEAREST, means interpolation method is nearest-neighbor interpolation.
  640. - Inter.BICUBIC, means interpolation method is bicubic interpolation.
  641. max_attempts (int, optional): The maximum number of attempts to propose a valid
  642. crop_area (default=10). If exceeded, fall back to use center_crop instead.
  643. Examples:
  644. >>> import mindspore.dataset.vision.c_transforms as c_vision
  645. >>> from mindspore.dataset.transforms.vision import Inter
  646. >>>
  647. >>> decode_op = c_vision.Decode()
  648. >>> resize_crop_op = c_vision.RandomResizedCrop(size=(50, 75), scale=(0.25, 0.5),
  649. >>> interpolation=Inter.BILINEAR)
  650. >>> transforms_list = [decode_op, resize_crop_op]
  651. >>> data1 = data1.map(operations=transforms_list, input_columns=["image"])
  652. """
  653. @check_random_resize_crop
  654. def __init__(self, size, scale=(0.08, 1.0), ratio=(3. / 4., 4. / 3.),
  655. interpolation=Inter.BILINEAR, max_attempts=10):
  656. if isinstance(size, int):
  657. size = (size, size)
  658. self.size = size
  659. self.scale = scale
  660. self.ratio = ratio
  661. self.interpolation = interpolation
  662. self.max_attempts = max_attempts
  663. interpoltn = DE_C_INTER_MODE[interpolation]
  664. super().__init__(*size, *scale, *ratio, interpoltn, max_attempts)
  665. class CenterCrop(cde.CenterCropOp):
  666. """
  667. Crops the input image at the center to the given size.
  668. Args:
  669. size (Union[int, sequence]): The output size of the cropped image.
  670. If size is an integer, a square crop of size (size, size) is returned.
  671. If size is a sequence of length 2, it should be (height, width).
  672. Examples:
  673. >>> import mindspore.dataset.vision.c_transforms as c_vision
  674. >>>
  675. >>> # crop image to a square
  676. >>> transforms_list1 = [c_vision.Decode(), c_vision.CenterCrop(50)]
  677. >>> data1 = data1.map(operations=transforms_list1, input_columns=["image"])
  678. >>> # crop image to portrait style
  679. >>> transforms_list2 = [c_vision.Decode(), c_vision.CenterCrop((60, 40))]
  680. >>> data2 = data2.map(operations=transforms_list2, input_columns=["image"])
  681. """
  682. @check_crop
  683. def __init__(self, size):
  684. if isinstance(size, int):
  685. size = (size, size)
  686. self.size = size
  687. super().__init__(*size)
  688. class RandomColor(cde.RandomColorOp):
  689. """
  690. Adjust the color of the input image by a fixed or random degree.
  691. This operation works only with 3-channel color images.
  692. Args:
  693. degrees (sequence, optional): Range of random color adjustment degrees.
  694. It should be in (min, max) format. If min=max, then it is a
  695. single fixed magnitude operation (default=(0.1, 1.9)).
  696. Examples:
  697. >>> import mindspore.dataset.vision.c_transforms as c_vision
  698. >>>
  699. >>> transforms_list = [c_vision.Decode(), c_vision.RandomColor((0.5, 2.0))]
  700. >>> data1 = data1.map(operations=transforms_list, input_columns=["image"])
  701. """
  702. @check_positive_degrees
  703. def __init__(self, degrees=(0.1, 1.9)):
  704. super().__init__(*degrees)
  705. class RandomColorAdjust(cde.RandomColorAdjustOp):
  706. """
  707. Randomly adjust the brightness, contrast, saturation, and hue of the input image.
  708. Args:
  709. brightness (Union[float, tuple], optional): Brightness adjustment factor (default=(1, 1)). Cannot be negative.
  710. If it is a float, the factor is uniformly chosen from the range [max(0, 1-brightness), 1+brightness].
  711. If it is a sequence, it should be [min, max] for the range.
  712. contrast (Union[float, tuple], optional): Contrast adjustment factor (default=(1, 1)). Cannot be negative.
  713. If it is a float, the factor is uniformly chosen from the range [max(0, 1-contrast), 1+contrast].
  714. If it is a sequence, it should be [min, max] for the range.
  715. saturation (Union[float, tuple], optional): Saturation adjustment factor (default=(1, 1)). Cannot be negative.
  716. If it is a float, the factor is uniformly chosen from the range [max(0, 1-saturation), 1+saturation].
  717. If it is a sequence, it should be [min, max] for the range.
  718. hue (Union[float, tuple], optional): Hue adjustment factor (default=(0, 0)).
  719. If it is a float, the range will be [-hue, hue]. Value should be 0 <= hue <= 0.5.
  720. If it is a sequence, it should be [min, max] where -0.5 <= min <= max <= 0.5.
  721. Examples:
  722. >>> import mindspore.dataset.vision.c_transforms as c_vision
  723. >>>
  724. >>> decode_op = c_vision.Decode()
  725. >>> transform_op = c_vision.RandomColorAdjust(brightness=(0.5, 1), contrast=(0.4, 1), saturation=(0.3, 1))
  726. >>> transforms_list = [decode_op, transform_op]
  727. >>> data1 = data1.map(operations=transforms_list, input_columns=["image"])
  728. """
  729. @check_random_color_adjust
  730. def __init__(self, brightness=(1, 1), contrast=(1, 1), saturation=(1, 1), hue=(0, 0)):
  731. brightness = self.expand_values(brightness)
  732. contrast = self.expand_values(contrast)
  733. saturation = self.expand_values(saturation)
  734. hue = self.expand_values(hue, center=0, bound=(-0.5, 0.5), non_negative=False)
  735. self.brightness = brightness
  736. self.contrast = contrast
  737. self.saturation = saturation
  738. self.hue = hue
  739. super().__init__(*brightness, *contrast, *saturation, *hue)
  740. def expand_values(self, value, center=1, bound=(0, FLOAT_MAX_INTEGER), non_negative=True):
  741. if isinstance(value, numbers.Number):
  742. value = [center - value, center + value]
  743. if non_negative:
  744. value[0] = max(0, value[0])
  745. check_range(value, bound)
  746. return (value[0], value[1])
  747. class RandomRotation(cde.RandomRotationOp):
  748. """
  749. Rotate the input image by a random angle.
  750. Args:
  751. degrees (Union[int, float, sequence): Range of random rotation degrees.
  752. If degrees is a number, the range will be converted to (-degrees, degrees).
  753. If degrees is a sequence, it should be (min, max).
  754. resample (Inter mode, optional): An optional resampling filter (default=Inter.NEAREST).
  755. If omitted, or if the image has mode "1" or "P", it is set to be Inter.NEAREST.
  756. It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC].
  757. - Inter.BILINEAR, means resample method is bilinear interpolation.
  758. - Inter.NEAREST, means resample method is nearest-neighbor interpolation.
  759. - Inter.BICUBIC, means resample method is bicubic interpolation.
  760. expand (bool, optional): Optional expansion flag (default=False). If set to True, expand the output
  761. image to make it large enough to hold the entire rotated image.
  762. If set to False or omitted, make the output image the same size as the input.
  763. Note that the expand flag assumes rotation around the center and no translation.
  764. center (tuple, optional): Optional center of rotation (a 2-tuple) (default=None).
  765. Origin is the top left corner. None sets to the center of the image.
  766. fill_value (Union[int, tuple], optional): Optional fill color for the area outside the rotated image
  767. (default=0).
  768. If it is a 3-tuple, it is used for R, G, B channels respectively.
  769. If it is an integer, it is used for all RGB channels.
  770. Examples:
  771. >>> import mindspore.dataset.vision.c_transforms as c_vision
  772. >>> from mindspore.dataset.transforms.vision import Inter
  773. >>>
  774. >>> transforms_list = [c_vision.Decode(),
  775. >>> c_vision.RandomRotation(degrees=5.0, resample=Inter.NEAREST, expand=True)]
  776. >>> data1 = data1.map(operations=transforms_list, input_columns=["image"])
  777. """
  778. @check_random_rotation
  779. def __init__(self, degrees, resample=Inter.NEAREST, expand=False, center=None, fill_value=0):
  780. self.degrees = degrees
  781. self.resample = resample
  782. self.expand = expand
  783. self.center = center
  784. self.fill_value = fill_value
  785. if isinstance(degrees, numbers.Number):
  786. degrees = (-degrees, degrees)
  787. if center is None:
  788. center = (-1, -1)
  789. if isinstance(fill_value, int):
  790. fill_value = tuple([fill_value] * 3)
  791. interpolation = DE_C_INTER_MODE[resample]
  792. super().__init__(*degrees, *center, interpolation, expand, *fill_value)
  793. class Rescale(cde.RescaleOp):
  794. """
  795. Tensor operation to rescale the input image.
  796. Args:
  797. rescale (float): Rescale factor.
  798. shift (float): Shift factor.
  799. Examples:
  800. >>> import mindspore.dataset.vision.c_transforms as c_vision
  801. >>>
  802. >>> transforms_list = [c_vision.Decode(), c_vision.Rescale(1.0 / 255.0, -1.0)]
  803. >>> data1 = data1.map(operations=transforms_list, input_columns=["image"])
  804. """
  805. @check_rescale
  806. def __init__(self, rescale, shift):
  807. self.rescale = rescale
  808. self.shift = shift
  809. super().__init__(rescale, shift)
  810. class RandomResize(cde.RandomResizeOp):
  811. """
  812. Tensor operation to resize the input image using a randomly selected interpolation mode.
  813. Args:
  814. size (Union[int, sequence]): The output size of the resized image.
  815. If size is an integer, smaller edge of the image will be resized to this value with
  816. the same image aspect ratio.
  817. If size is a sequence of length 2, it should be (height, width).
  818. Examples:
  819. >>> import mindspore.dataset.vision.c_transforms as c_vision
  820. >>>
  821. >>> # randomly resize image, keeping aspect ratio
  822. >>> transforms_list1 = [c_vision.Decode(), c_vision.RandomResize(50)]
  823. >>> data1 = data1.map(operations=transforms_list1, input_columns=["image"])
  824. >>> # randomly resize image to landscape style
  825. >>> transforms_list2 = [c_vision.Decode(), c_vision.RandomResize((40, 60))]
  826. >>> data2 = data2.map(operations=transforms_list2, input_columns=["image"])
  827. """
  828. @check_resize
  829. def __init__(self, size):
  830. self.size = size
  831. if isinstance(size, int):
  832. size = (size, 0)
  833. super().__init__(*size)
  834. class RandomResizeWithBBox(cde.RandomResizeWithBBoxOp):
  835. """
  836. Tensor operation to resize the input image using a randomly selected interpolation mode and adjust
  837. bounding boxes accordingly.
  838. Args:
  839. size (Union[int, sequence]): The output size of the resized image.
  840. If size is an integer, smaller edge of the image will be resized to this value with
  841. the same image aspect ratio.
  842. If size is a sequence of length 2, it should be (height, width).
  843. Examples:
  844. >>> import mindspore.dataset.vision.c_transforms as c_vision
  845. >>>
  846. >>> # randomly resize image with bounding boxes, keeping aspect ratio
  847. >>> transforms_list1 = [c_vision.Decode(), c_vision.RandomResizeWithBBox(60)]
  848. >>> data1 = data1.map(operations=transforms_list1, input_columns=["image"])
  849. >>> # randomly resize image with bounding boxes to portrait style
  850. >>> transforms_list2 = [c_vision.Decode(), c_vision.RandomResizeWithBBox((80, 60))]
  851. >>> data2 = data2.map(operations=transforms_list2, input_columns=["image"])
  852. """
  853. @check_resize
  854. def __init__(self, size):
  855. self.size = size
  856. if isinstance(size, int):
  857. size = (size, 0)
  858. super().__init__(*size)
  859. class HWC2CHW(cde.ChannelSwapOp):
  860. """
  861. Transpose the input image; shape (H, W, C) to shape (C, H, W).
  862. Examples:
  863. >>> import mindspore.dataset.vision.c_transforms as c_vision
  864. >>>
  865. >>> transforms_list = [c_vision.Decode(), c_vision.RandomHorizontalFlip(0.75), c_vision.RandomCrop(),
  866. >>> c_vision.HWC2CHW()]
  867. >>> data1 = data1.map(operations=transforms_list, input_columns=["image"])
  868. """
  869. class RandomCropDecodeResize(cde.RandomCropDecodeResizeOp):
  870. """
  871. Equivalent to RandomResizedCrop, but crops before decodes.
  872. Args:
  873. size (Union[int, sequence]): The size of the output image.
  874. If size is an integer, a square crop of size (size, size) is returned.
  875. If size is a sequence of length 2, it should be (height, width).
  876. scale (tuple, optional): Range [min, max) of respective size of the
  877. original size to be cropped (default=(0.08, 1.0)).
  878. ratio (tuple, optional): Range [min, max) of aspect ratio to be
  879. cropped (default=(3. / 4., 4. / 3.)).
  880. interpolation (Inter mode, optional): Image interpolation mode (default=Inter.BILINEAR).
  881. It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC].
  882. - Inter.BILINEAR, means interpolation method is bilinear interpolation.
  883. - Inter.NEAREST, means interpolation method is nearest-neighbor interpolation.
  884. - Inter.BICUBIC, means interpolation method is bicubic interpolation.
  885. max_attempts (int, optional): The maximum number of attempts to propose a valid crop_area (default=10).
  886. If exceeded, fall back to use center_crop instead.
  887. Examples:
  888. >>> import mindspore.dataset.vision.c_transforms as c_vision
  889. >>> from mindspore.dataset.transforms.vision import Inter
  890. >>>
  891. >>> resize_crop_decode_op = c_vision.RandomCropDecodeResize(size=(50, 75), scale=(0.25, 0.5),
  892. >>> interpolation=Inter.NEAREST, max_attempts=5)
  893. >>> transforms_list = [resize_crop_decode_op]
  894. >>> data1 = data1.map(operations=transforms_list, input_columns=["image"])
  895. """
  896. @check_random_resize_crop
  897. def __init__(self, size, scale=(0.08, 1.0), ratio=(3. / 4., 4. / 3.),
  898. interpolation=Inter.BILINEAR, max_attempts=10):
  899. if isinstance(size, int):
  900. size = (size, size)
  901. self.size = size
  902. self.scale = scale
  903. self.ratio = ratio
  904. self.interpolation = interpolation
  905. self.max_attempts = max_attempts
  906. interpoltn = DE_C_INTER_MODE[interpolation]
  907. super().__init__(*size, *scale, *ratio, interpoltn, max_attempts)
  908. class Pad(cde.PadOp):
  909. """
  910. Pads the image according to padding parameters.
  911. Args:
  912. padding (Union[int, sequence]): The number of pixels to pad the image.
  913. If a single number is provided, it pads all borders with this value.
  914. If a tuple or list of 2 values are provided, it pads the (left and top)
  915. with the first value and (right and bottom) with the second value.
  916. If 4 values are provided as a list or tuple,
  917. it pads the left, top, right and bottom respectively.
  918. fill_value (Union[int, tuple], optional): The pixel intensity of the borders if
  919. the padding_mode is Border.CONSTANT (default=0). If it is a 3-tuple, it is used to
  920. fill R, G, B channels respectively.
  921. padding_mode (Border mode, optional): The method of padding (default=Border.CONSTANT). Can be any of
  922. [Border.CONSTANT, Border.EDGE, Border.REFLECT, Border.SYMMETRIC].
  923. - Border.CONSTANT, means it fills the border with constant values.
  924. - Border.EDGE, means it pads with the last value on the edge.
  925. - Border.REFLECT, means it reflects the values on the edge omitting the last
  926. value of edge.
  927. - Border.SYMMETRIC, means it reflects the values on the edge repeating the last
  928. value of edge.
  929. Examples:
  930. >>> import mindspore.dataset.vision.c_transforms as c_vision
  931. >>> from mindspore.dataset.transforms.vision import Border
  932. >>>
  933. >>> transforms_list = [c_vision.Decode(), c_vision.Pad([100, 100, 100, 100])]
  934. >>> data1 = data1.map(operations=transforms_list, input_columns=["image"])
  935. """
  936. @check_pad
  937. def __init__(self, padding, fill_value=0, padding_mode=Border.CONSTANT):
  938. padding = parse_padding(padding)
  939. if isinstance(fill_value, int):
  940. fill_value = tuple([fill_value] * 3)
  941. padding_mode = DE_C_BORDER_TYPE[padding_mode]
  942. self.padding = padding
  943. self.fill_value = fill_value
  944. self.padding_mode = padding_mode
  945. super().__init__(*padding, padding_mode, *fill_value)
  946. class UniformAugment(cde.UniformAugOp):
  947. """
  948. Tensor operation to perform randomly selected augmentation.
  949. Args:
  950. transforms: List of C++ operations (Python operations are not accepted).
  951. num_ops (int, optional): Number of operations to be selected and applied (default=2).
  952. Examples:
  953. >>> import mindspore.dataset.vision.c_transforms as c_vision
  954. >>> import mindspore.dataset.vision.py_transforms as py_vision
  955. >>>
  956. >>> transforms_list = [c_vision.RandomHorizontalFlip(),
  957. >>> c_vision.RandomVerticalFlip(),
  958. >>> c_vision.RandomColorAdjust(),
  959. >>> c_vision.RandomRotation(degrees=45)]
  960. >>> uni_aug_op = c_vision.UniformAugment(transforms=transforms_list, num_ops=2)
  961. >>> transforms_all = [c_vision.Decode(), c_vision.Resize(size=[224, 224]),
  962. >>> uni_aug_op, py_vision.ToTensor()]
  963. >>> data_aug = data1.map(operations=transforms_all, input_columns="image",
  964. >>> num_parallel_workers=1)
  965. """
  966. @check_uniform_augment_cpp
  967. def __init__(self, transforms, num_ops=2):
  968. self.transforms = transforms
  969. self.num_ops = num_ops
  970. super().__init__(transforms, num_ops)
  971. class RandomSelectSubpolicy(cde.RandomSelectSubpolicyOp):
  972. """
  973. Choose a random sub-policy from a list to be applied on the input image. A sub-policy is a list of tuples
  974. (op, prob), where op is a TensorOp operation and prob is the probability that this op will be applied. Once
  975. a sub-policy is selected, each op within the subpolicy with be applied in sequence according to its probability.
  976. Args:
  977. policy (list(list(tuple(TensorOp,float))): List of sub-policies to choose from.
  978. Examples:
  979. >>> import mindspore.dataset.vision.c_transforms as c_vision
  980. >>>
  981. >>> policy = [[(c_vision.RandomRotation((45, 45)), 0.5), (c_vision.RandomVerticalFlip(), 1),
  982. >>> (c_vision.RandomColorAdjust(), 0.8)],
  983. >>> [(c_vision.RandomRotation((90, 90)), 1), (c_vision.RandomColorAdjust(), 0.2)]]
  984. >>> data_policy = data1.map(operations=c_vision.RandomSelectSubpolicy(policy), input_columns=["image"])
  985. """
  986. @check_random_select_subpolicy_op
  987. def __init__(self, policy):
  988. super().__init__(policy)
  989. class SoftDvppDecodeResizeJpeg(cde.SoftDvppDecodeResizeJpegOp):
  990. """
  991. Tensor operation to decode and resize JPEG image using the simulation algorithm of
  992. Ascend series chip DVPP module.
  993. It is recommended to use this algorithm in the following scenarios:
  994. When training, the DVPP of the Ascend chip is not used,
  995. and the DVPP of the Ascend chip is used during inference,
  996. and the accuracy of inference is lower than the accuracy of training;
  997. and the input image size should be in range [32*32, 8192*8192].
  998. The zoom-out and zoom-in multiples of the image length and width should in the range [1/32, 16].
  999. Only images with an even resolution can be output. The output of odd resolution is not supported.
  1000. Args:
  1001. size (Union[int, sequence]): The output size of the resized image.
  1002. If size is an integer, smaller edge of the image will be resized to this value with
  1003. the same image aspect ratio.
  1004. If size is a sequence of length 2, it should be (height, width).
  1005. Examples:
  1006. >>> import mindspore.dataset.vision.c_transforms as c_vision
  1007. >>>
  1008. >>> # decode and resize image, keeping aspect ratio
  1009. >>> transforms_list1 = [c_vision.Decode(), c_vision.SoftDvppDecodeResizeJpeg(70)]
  1010. >>> data1 = data1.map(operations=transforms_list1, input_columns=["image"])
  1011. >>> # decode and resize to portrait style
  1012. >>> transforms_list2 = [c_vision.Decode(), c_vision.SoftDvppDecodeResizeJpeg((80, 60))]
  1013. >>> data2 = data2.map(operations=transforms_list2, input_columns=["image"])
  1014. """
  1015. @check_resize
  1016. def __init__(self, size):
  1017. if isinstance(size, int):
  1018. size = (size, 0)
  1019. self.size = size
  1020. super().__init__(*size)
  1021. class SoftDvppDecodeRandomCropResizeJpeg(cde.SoftDvppDecodeRandomCropResizeJpegOp):
  1022. """
  1023. Tensor operation to decode, random crop and resize JPEG image using the simulation algorithm of
  1024. Ascend series chip DVPP module.
  1025. The usage scenario is consistent with SoftDvppDecodeReiszeJpeg.
  1026. The input image size should be in range [32*32, 8192*8192].
  1027. The zoom-out and zoom-in multiples of the image length and width should in the range [1/32, 16].
  1028. Only images with an even resolution can be output. The output of odd resolution is not supported.
  1029. Args:
  1030. size (Union[int, sequence]): The size of the output image.
  1031. If size is an integer, a square crop of size (size, size) is returned.
  1032. If size is a sequence of length 2, it should be (height, width).
  1033. scale (tuple, optional): Range [min, max) of respective size of the
  1034. original size to be cropped (default=(0.08, 1.0)).
  1035. ratio (tuple, optional): Range [min, max) of aspect ratio to be
  1036. cropped (default=(3. / 4., 4. / 3.)).
  1037. max_attempts (int, optional): The maximum number of attempts to propose a valid crop_area (default=10).
  1038. If exceeded, fall back to use center_crop instead.
  1039. Examples:
  1040. >>> import mindspore.dataset.vision.c_transforms as c_vision
  1041. >>>
  1042. >>> # decode, randomly crop and resize image, keeping aspect ratio
  1043. >>> transforms_list1 = [c_vision.Decode(), c_vision.SoftDvppDecodeRandomCropResizeJpeg(90)]
  1044. >>> data1 = data1.map(operations=transforms_list1, input_columns=["image"])
  1045. >>> # decode, randomly crop and resize to landscape style
  1046. >>> transforms_list2 = [c_vision.Decode(), c_vision.SoftDvppDecodeRandomCropResizeJpeg((80, 100))]
  1047. >>> data2 = data2.map(operations=transforms_list2, input_columns=["image"])
  1048. """
  1049. @check_soft_dvpp_decode_random_crop_resize_jpeg
  1050. def __init__(self, size, scale=(0.08, 1.0), ratio=(3. / 4., 4. / 3.), max_attempts=10):
  1051. if isinstance(size, int):
  1052. size = (size, size)
  1053. self.size = size
  1054. self.scale = scale
  1055. self.ratio = ratio
  1056. self.max_attempts = max_attempts
  1057. super().__init__(*size, *scale, *ratio, max_attempts)
  1058. class RandomSolarize(cde.RandomSolarizeOp):
  1059. """
  1060. Invert all pixel values above a threshold.
  1061. Args:
  1062. threshold (tuple, optional): Range of random solarize threshold. Threshold values should always be
  1063. in the range (0, 255), include at least one integer value in the given range and
  1064. be in (min, max) format. If min=max, then it is a single fixed magnitude operation (default=(0, 255)).
  1065. Examples:
  1066. >>> import mindspore.dataset.vision.c_transforms as c_vision
  1067. >>>
  1068. >>> transforms_list = [c_vision.Decode(), c_vision.RandomSolarize(threshold=(10,100))]
  1069. >>> data1 = data1.map(operations=transforms_list, input_columns=["image"])
  1070. """
  1071. @check_random_solarize
  1072. def __init__(self, threshold=(0, 255)):
  1073. super().__init__(threshold)