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.

py_transforms.py 52 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  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.py_transforms is implemented based on Python PIL.
  17. This module provides many kinds of image augmentations. It also provides
  18. transferring methods between PIL image and NumPy array. For users who prefer
  19. Python PIL in image learning tasks, this module is a good tool to process images.
  20. Users can also self-define their own augmentations with Python PIL.
  21. """
  22. import numbers
  23. import random
  24. import numpy as np
  25. from PIL import Image
  26. from . import py_transforms_util as util
  27. from .c_transforms import parse_padding
  28. from .validators import check_prob, check_crop, check_resize_interpolation, check_random_resize_crop, \
  29. check_normalize_py, check_random_crop, check_random_color_adjust, check_random_rotation, \
  30. check_ten_crop, check_num_channels, check_pad, \
  31. check_random_perspective, check_random_erasing, check_cutout, check_linear_transform, check_random_affine, \
  32. check_mix_up, check_positive_degrees, check_uniform_augment_py, check_auto_contrast
  33. from .utils import Inter, Border
  34. DE_PY_INTER_MODE = {Inter.NEAREST: Image.NEAREST,
  35. Inter.LINEAR: Image.LINEAR,
  36. Inter.CUBIC: Image.CUBIC}
  37. DE_PY_BORDER_TYPE = {Border.CONSTANT: 'constant',
  38. Border.EDGE: 'edge',
  39. Border.REFLECT: 'reflect',
  40. Border.SYMMETRIC: 'symmetric'}
  41. class ToTensor:
  42. """
  43. Convert the input NumPy image array or PIL image of shape (H, W, C) to a NumPy ndarray of shape (C, H, W).
  44. Note:
  45. The values in the input arrays are rescaled from [0, 255] to [0.0, 1.0].
  46. The type is cast to output_type (default NumPy float32).
  47. The number of channels remains the same.
  48. Args:
  49. output_type (NumPy datatype, optional): The datatype of the NumPy output (default=np.float32).
  50. Examples:
  51. >>> import mindspore.dataset.vision.py_transforms as py_vision
  52. >>> from mindspore.dataset.transforms.py_transforms import Compose
  53. >>>
  54. >>> Compose([py_vision.Decode(), py_vision.RandomHorizontalFlip(0.5),
  55. >>> py_vision.ToTensor()])
  56. """
  57. def __init__(self, output_type=np.float32):
  58. self.output_type = output_type
  59. def __call__(self, img):
  60. """
  61. Call method.
  62. Args:
  63. img (PIL image): PIL image to be converted to numpy.ndarray.
  64. Returns:
  65. img (numpy.ndarray), Converted image.
  66. """
  67. return util.to_tensor(img, self.output_type)
  68. class ToType:
  69. """
  70. Convert the input NumPy image array to desired NumPy dtype.
  71. Args:
  72. output_type (NumPy datatype): The datatype of the NumPy output, e.g. numpy.float32.
  73. Examples:
  74. >>> import mindspore.dataset.vision.py_transforms as py_vision
  75. >>> from mindspore.dataset.transforms.py_transforms import Compose
  76. >>> import numpy as np
  77. >>>
  78. >>> Compose([py_vision.Decode(), py_vision.RandomHorizontalFlip(0.5),
  79. >>> py_vision.ToTensor(),
  80. >>> py_vision.ToType(np.float32)])
  81. """
  82. def __init__(self, output_type):
  83. self.output_type = output_type
  84. def __call__(self, img):
  85. """
  86. Call method.
  87. Args:
  88. NumPy object : NumPy object to be type swapped.
  89. Returns:
  90. img (numpy.ndarray), Converted image.
  91. """
  92. return util.to_type(img, self.output_type)
  93. class HWC2CHW:
  94. """
  95. Transpose a NumPy image array; shape (H, W, C) to shape (C, H, W).
  96. Examples:
  97. >>> import mindspore.dataset.vision.py_transforms as py_vision
  98. >>> from mindspore.dataset.transforms.py_transforms import Compose
  99. >>>
  100. >>> Compose([py_vision.Decode(),
  101. >>> py_vision.HWC2CHW()])
  102. """
  103. def __call__(self, img):
  104. """
  105. Call method.
  106. Args:
  107. img (numpy.ndarray): Image array, of shape (H, W, C), to have channels swapped.
  108. Returns:
  109. img (numpy.ndarray), Image array, of shape (C, H, W), with channels swapped.
  110. """
  111. return util.hwc_to_chw(img)
  112. class ToPIL:
  113. """
  114. Convert the input decoded NumPy image array of RGB mode to a PIL image of RGB mode.
  115. Examples:
  116. >>> # data is already decoded, but not in PIL image format
  117. >>> import mindspore.dataset.vision.py_transforms as py_vision
  118. >>> from mindspore.dataset.transforms.py_transforms import Compose
  119. >>>
  120. >>> Compose([py_vision.ToPIL(), py_vision.RandomHorizontalFlip(0.5),
  121. >>> py_vision.ToTensor()])
  122. """
  123. def __call__(self, img):
  124. """
  125. Call method.
  126. Args:
  127. img (numpy.ndarray): Decoded image array, of RGB mode, to be converted to PIL image.
  128. Returns:
  129. img (PIL image), Image converted to PIL image of RGB mode.
  130. """
  131. return util.to_pil(img)
  132. class Decode:
  133. """
  134. Decode the input image to PIL image format in RGB mode.
  135. Examples:
  136. >>> import mindspore.dataset.vision.py_transforms as py_vision
  137. >>> from mindspore.dataset.transforms.py_transforms import Compose
  138. >>>
  139. >>> Compose([py_vision.Decode(),
  140. >>> py_vision.RandomHorizontalFlip(0.5),
  141. >>> py_vision.ToTensor()])
  142. """
  143. def __call__(self, img):
  144. """
  145. Call method.
  146. Args:
  147. img (Bytes-like Objects):Image to be decoded.
  148. Returns:
  149. img (PIL image), Decoded image in RGB mode.
  150. """
  151. return util.decode(img)
  152. class Normalize:
  153. """
  154. Normalize the input NumPy image array of shape (C, H, W) with the given mean and standard deviation.
  155. The values of the array need to be in the range (0.0, 1.0].
  156. Args:
  157. mean (sequence): List or tuple of mean values for each channel, with respect to channel order.
  158. The mean values must be in the range (0.0, 1.0].
  159. std (sequence): List or tuple of standard deviations for each channel, w.r.t. channel order.
  160. The standard deviation values must be in the range (0.0, 1.0].
  161. Examples:
  162. >>> import mindspore.dataset.vision.py_transforms as py_vision
  163. >>> from mindspore.dataset.transforms.py_transforms import Compose
  164. >>>
  165. >>> Compose([py_vision.Decode(),
  166. >>> py_vision.RandomHorizontalFlip(0.5),
  167. >>> py_vision.ToTensor(),
  168. >>> py_vision.Normalize((0.491, 0.482, 0.447), (0.247, 0.243, 0.262))])
  169. """
  170. @check_normalize_py
  171. def __init__(self, mean, std):
  172. self.mean = mean
  173. self.std = std
  174. def __call__(self, img):
  175. """
  176. Call method.
  177. Args:
  178. img (numpy.ndarray): Image array to be normalized.
  179. Returns:
  180. img (numpy.ndarray), Normalized Image array.
  181. """
  182. return util.normalize(img, self.mean, self.std)
  183. class RandomCrop:
  184. """
  185. Crop the input PIL image at a random location.
  186. Args:
  187. size (Union[int, sequence]): The output size of the cropped image.
  188. If size is an integer, a square crop of size (size, size) is returned.
  189. If size is a sequence of length 2, it should be (height, width).
  190. padding (Union[int, sequence], optional): The number of pixels to pad the image (default=None).
  191. If padding is not None, first pad image with padding values.
  192. If a single number is provided, pad all borders with this value.
  193. If a tuple or list of 2 values are provided, pad the (left and top)
  194. with the first value and (right and bottom) with the second value.
  195. If 4 values are provided as a list or tuple,
  196. pad the left, top, right and bottom respectively.
  197. pad_if_needed (bool, optional): Pad the image if either side is smaller than
  198. the given output size (default=False).
  199. fill_value (int or tuple, optional): filling value (default=0).
  200. The pixel intensity of the borders if the padding_mode is Border.CONSTANT.
  201. If it is a 3-tuple, it is used to fill R, G, B channels respectively.
  202. padding_mode (str, optional): The method of padding (default=Border.CONSTANT). It can be any of
  203. [Border.CONSTANT, Border.EDGE, Border.REFLECT, Border.SYMMETRIC].
  204. - Border.CONSTANT, means it fills the border with constant values.
  205. - Border.EDGE, means it pads with the last value on the edge.
  206. - Border.REFLECT, means it reflects the values on the edge omitting the last
  207. value of edge.
  208. - Border.SYMMETRIC, means it reflects the values on the edge repeating the last
  209. value of edge.
  210. Examples:
  211. >>> import mindspore.dataset.vision.py_transforms as py_vision
  212. >>> from mindspore.dataset.transforms.py_transforms import Compose
  213. >>>
  214. >>> Compose([py_vision.Decode(),
  215. >>> py_vision.RandomCrop(224),
  216. >>> py_vision.ToTensor()])
  217. """
  218. @check_random_crop
  219. def __init__(self, size, padding=None, pad_if_needed=False, fill_value=0, padding_mode=Border.CONSTANT):
  220. if padding is None:
  221. padding = (0, 0, 0, 0)
  222. else:
  223. padding = parse_padding(padding)
  224. self.size = size
  225. self.padding = padding
  226. self.pad_if_needed = pad_if_needed
  227. self.fill_value = fill_value
  228. self.padding_mode = DE_PY_BORDER_TYPE[padding_mode]
  229. def __call__(self, img):
  230. """
  231. Call method.
  232. Args:
  233. img (PIL image): Image to be randomly cropped.
  234. Returns:
  235. PIL image, Cropped image.
  236. """
  237. return util.random_crop(img, self.size, self.padding, self.pad_if_needed,
  238. self.fill_value, self.padding_mode)
  239. class RandomHorizontalFlip:
  240. """
  241. Randomly flip the input image horizontally with a given probability.
  242. Args:
  243. prob (float, optional): Probability of the image being flipped (default=0.5).
  244. Examples:
  245. >>> import mindspore.dataset.vision.py_transforms as py_vision
  246. >>> from mindspore.dataset.transforms.py_transforms import Compose
  247. >>>
  248. >>> Compose([py_vision.Decode(),
  249. >>> py_vision.RandomHorizontalFlip(0.5),
  250. >>> py_vision.ToTensor()])
  251. """
  252. @check_prob
  253. def __init__(self, prob=0.5):
  254. self.prob = prob
  255. def __call__(self, img):
  256. """
  257. Call method.
  258. Args:
  259. img (PIL image): Image to be flipped horizontally.
  260. Returns:
  261. img (PIL image), Randomly flipped image.
  262. """
  263. return util.random_horizontal_flip(img, self.prob)
  264. class RandomVerticalFlip:
  265. """
  266. Randomly flip the input image vertically with a given probability.
  267. Args:
  268. prob (float, optional): Probability of the image being flipped (default=0.5).
  269. Examples:
  270. >>> import mindspore.dataset.vision.py_transforms as py_vision
  271. >>> from mindspore.dataset.transforms.py_transforms import Compose
  272. >>>
  273. >>> Compose([py_vision.Decode(),
  274. >>> py_vision.RandomVerticalFlip(0.5),
  275. >>> py_vision.ToTensor()])
  276. """
  277. @check_prob
  278. def __init__(self, prob=0.5):
  279. self.prob = prob
  280. def __call__(self, img):
  281. """
  282. Call method.
  283. Args:
  284. img (PIL image): Image to be flipped vertically.
  285. Returns:
  286. img (PIL image), Randomly flipped image.
  287. """
  288. return util.random_vertical_flip(img, self.prob)
  289. class Resize:
  290. """
  291. Resize the input PIL image to the given size.
  292. Args:
  293. size (Union[int, sequence]): The output size of the resized image.
  294. If size is an integer, the smaller edge of the image will be resized to this value with
  295. the same image aspect ratio.
  296. If size is a sequence of length 2, it should be (height, width).
  297. interpolation (Inter mode, optional): Image interpolation mode (default=Inter.BILINEAR).
  298. It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC].
  299. - Inter.BILINEAR, means the interpolation method is bilinear interpolation.
  300. - Inter.NEAREST, means the interpolation method is nearest-neighbor interpolation.
  301. - Inter.BICUBIC, means the interpolation method is bicubic interpolation.
  302. Examples:
  303. >>> import mindspore.dataset.vision.py_transforms as py_vision
  304. >>> from mindspore.dataset.transforms.py_transforms import Compose
  305. >>>
  306. >>> Compose([py_vision.Decode(),
  307. >>> py_vision.Resize(256),
  308. >>> py_vision.ToTensor()])
  309. """
  310. @check_resize_interpolation
  311. def __init__(self, size, interpolation=Inter.BILINEAR):
  312. self.size = size
  313. self.interpolation = DE_PY_INTER_MODE[interpolation]
  314. def __call__(self, img):
  315. """
  316. Call method.
  317. Args:
  318. img (PIL image): Image to be resized.
  319. Returns:
  320. img (PIL image), Resize image.
  321. """
  322. return util.resize(img, self.size, self.interpolation)
  323. class RandomResizedCrop:
  324. """
  325. Extract crop from the input image and resize it to a random size and aspect ratio.
  326. Args:
  327. size (Union[int, sequence]): The size of the output image.
  328. If size is an integer, a square crop of size (size, size) is returned.
  329. If size is a sequence of length 2, it should be (height, width).
  330. scale (tuple, optional): Range (min, max) of respective size of the original size
  331. to be cropped (default=(0.08, 1.0)).
  332. ratio (tuple, optional): Range (min, max) of aspect ratio to be cropped (default=(3. / 4., 4. / 3.)).
  333. interpolation (Inter mode, optional): Image interpolation mode (default=Inter.BILINEAR).
  334. It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC].
  335. - Inter.BILINEAR, means the interpolation method is bilinear interpolation.
  336. - Inter.NEAREST, means the interpolation method is nearest-neighbor interpolation.
  337. - Inter.BICUBIC, means the interpolation method is bicubic interpolation.
  338. max_attempts (int, optional): The maximum number of attempts to propose a valid
  339. crop area (default=10). If exceeded, fall back to use center crop instead.
  340. Examples:
  341. >>> import mindspore.dataset.vision.py_transforms as py_vision
  342. >>> from mindspore.dataset.transforms.py_transforms import Compose
  343. >>>
  344. >>> Compose([py_vision.Decode(),
  345. >>> py_vision.RandomResizedCrop(224),
  346. >>> py_vision.ToTensor()])
  347. """
  348. @check_random_resize_crop
  349. def __init__(self, size, scale=(0.08, 1.0), ratio=(3. / 4., 4. / 3.),
  350. interpolation=Inter.BILINEAR, max_attempts=10):
  351. self.size = size
  352. self.scale = scale
  353. self.ratio = ratio
  354. self.interpolation = DE_PY_INTER_MODE[interpolation]
  355. self.max_attempts = max_attempts
  356. def __call__(self, img):
  357. """
  358. Call method.
  359. Args:
  360. img (PIL image): Image to be randomly cropped and resized.
  361. Returns:
  362. img (PIL image), Randomly cropped and resized image.
  363. """
  364. return util.random_resize_crop(img, self.size, self.scale, self.ratio,
  365. self.interpolation, self.max_attempts)
  366. class CenterCrop:
  367. """
  368. Crop the central reigion of the input PIL image to the given size.
  369. Args:
  370. size (Union[int, sequence]): The output size of the cropped image.
  371. If size is an integer, a square crop of size (size, size) is returned.
  372. If size is a sequence of length 2, it should be (height, width).
  373. Examples:
  374. >>> import mindspore.dataset.vision.py_transforms as py_vision
  375. >>> from mindspore.dataset.transforms.py_transforms import Compose
  376. >>>
  377. >>> Compose([py_vision.Decode(),
  378. >>> py_vision.CenterCrop(64),
  379. >>> py_vision.ToTensor()])
  380. """
  381. @check_crop
  382. def __init__(self, size):
  383. self.size = size
  384. def __call__(self, img):
  385. """
  386. Call method.
  387. Args:
  388. img (PIL image): Image to be center cropped.
  389. Returns:
  390. img (PIL image), Cropped image.
  391. """
  392. return util.center_crop(img, self.size)
  393. class RandomColorAdjust:
  394. """
  395. Perform a random brightness, contrast, saturation, and hue adjustment on the input PIL image.
  396. Args:
  397. brightness (Union[float, tuple], optional): Brightness adjustment factor (default=(1, 1)). Cannot be negative.
  398. If it is a float, the factor is uniformly chosen from the range [max(0, 1-brightness), 1+brightness].
  399. If it is a sequence, it should be [min, max] for the range.
  400. contrast (Union[float, tuple], optional): Contrast adjustment factor (default=(1, 1)). Cannot be negative.
  401. If it is a float, the factor is uniformly chosen from the range [max(0, 1-contrast), 1+contrast].
  402. If it is a sequence, it should be [min, max] for the range.
  403. saturation (Union[float, tuple], optional): Saturation adjustment factor (default=(1, 1)). Cannot be negative.
  404. If it is a float, the factor is uniformly chosen from the range [max(0, 1-saturation), 1+saturation].
  405. If it is a sequence, it should be [min, max] for the range.
  406. hue (Union[float, tuple], optional): Hue adjustment factor (default=(0, 0)).
  407. If it is a float, the range will be [-hue, hue]. Value should be 0 <= hue <= 0.5.
  408. If it is a sequence, it should be [min, max] where -0.5 <= min <= max <= 0.5.
  409. Examples:
  410. >>> import mindspore.dataset.vision.py_transforms as py_vision
  411. >>> from mindspore.dataset.transforms.py_transforms import Compose
  412. >>>
  413. >>> Compose([py_vision.Decode(),
  414. >>> py_vision.RandomColorAdjust(0.4, 0.4, 0.4, 0.1),
  415. >>> py_vision.ToTensor()])
  416. """
  417. @check_random_color_adjust
  418. def __init__(self, brightness=(1, 1), contrast=(1, 1), saturation=(1, 1), hue=(0, 0)):
  419. self.brightness = brightness
  420. self.contrast = contrast
  421. self.saturation = saturation
  422. self.hue = hue
  423. def __call__(self, img):
  424. """
  425. Call method.
  426. Args:
  427. img (PIL image): Image to have its color adjusted randomly.
  428. Returns:
  429. img (PIL image), Image after random adjustment of its color.
  430. """
  431. return util.random_color_adjust(img, self.brightness, self.contrast, self.saturation, self.hue)
  432. class RandomRotation:
  433. """
  434. Rotate the input PIL image by a random angle.
  435. Note:
  436. See https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.rotate.
  437. Args:
  438. degrees (Union[int, float, sequence]): Range of random rotation degrees.
  439. If degrees is a number, the range will be converted to (-degrees, degrees).
  440. If degrees is a sequence, it should be (min, max).
  441. resample (Inter mode, optional): An optional resampling filter (default=Inter.NEAREST).
  442. If omitted, or if the image has mode "1" or "P", it is set to be Inter.NEAREST.
  443. It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC].
  444. - Inter.BILINEAR, means the resampling method is bilinear interpolation.
  445. - Inter.NEAREST, means the resampling method is nearest-neighbor interpolation.
  446. - Inter.BICUBIC, means the resampling method is bicubic interpolation.
  447. expand (bool, optional): Optional expansion flag (default=False). If set to True, expand the output
  448. image to make it large enough to hold the entire rotated image.
  449. If set to False or omitted, make the output image the same size as the input.
  450. Note that the expand flag assumes rotation around the center and no translation.
  451. center (tuple, optional): Optional center of rotation (a 2-tuple) (default=None).
  452. Origin is the top left corner. Default None sets to the center of the image.
  453. fill_value (int or tuple, optional): Optional fill color for the area outside the rotated
  454. image (default=0).
  455. If it is a 3-tuple, it is used for R, G, B channels respectively.
  456. If it is an integer, it is used for all RGB channels. Default is 0.
  457. Examples:
  458. >>> import mindspore.dataset.vision.py_transforms as py_vision
  459. >>> from mindspore.dataset.transforms.py_transforms import Compose
  460. >>>
  461. >>> Compose([py_vision.Decode(),
  462. >>> py_vision.RandomRotation(30),
  463. >>> py_vision.ToTensor()])
  464. """
  465. @check_random_rotation
  466. def __init__(self, degrees, resample=Inter.NEAREST, expand=False, center=None, fill_value=0):
  467. self.degrees = degrees
  468. self.resample = DE_PY_INTER_MODE[resample]
  469. self.expand = expand
  470. self.center = center
  471. self.fill_value = fill_value
  472. def __call__(self, img):
  473. """
  474. Call method.
  475. Args:
  476. img (PIL image): Image to be rotated.
  477. Returns:
  478. img (PIL image), Rotated image.
  479. """
  480. return util.random_rotation(img, self.degrees, self.resample, self.expand, self.center, self.fill_value)
  481. class FiveCrop:
  482. """
  483. Generate 5 cropped images (one central image and four corners images).
  484. Args:
  485. size (int or sequence): The output size of the crop.
  486. If size is an integer, a square crop of size (size, size) is returned.
  487. If size is a sequence of length 2, it should be (height, width).
  488. Examples:
  489. >>> import mindspore.dataset.vision.py_transforms as py_vision
  490. >>> from mindspore.dataset.transforms.py_transforms import Compose
  491. >>>
  492. >>> Compose([py_vision.Decode(),
  493. >>> py_vision.FiveCrop(size=200),
  494. >>> # 4D stack of 5 images
  495. >>> lambda *images: numpy.stack([py_vision.ToTensor()(image) for image in images])])
  496. """
  497. @check_crop
  498. def __init__(self, size):
  499. self.size = size
  500. def __call__(self, img):
  501. """
  502. Call method.
  503. Args:
  504. img (PIL image): PIL image to be cropped.
  505. Returns:
  506. img_tuple (tuple), a tuple of 5 PIL images
  507. (top_left, top_right, bottom_left, bottom_right, center).
  508. """
  509. return util.five_crop(img, self.size)
  510. class TenCrop:
  511. """
  512. Generate 10 cropped images (first 5 images from FiveCrop, second 5 images from their flipped version
  513. as per input flag to flip vertically or horizontally).
  514. Args:
  515. size (Union[int, sequence]): The output size of the crop.
  516. If size is an integer, a square crop of size (size, size) is returned.
  517. If size is a sequence of length 2, it should be (height, width).
  518. use_vertical_flip (bool, optional): Flip the image vertically instead of horizontally
  519. if set to True (default=False).
  520. Examples:
  521. >>> import mindspore.dataset.vision.py_transforms as py_vision
  522. >>> from mindspore.dataset.transforms.py_transforms import Compose
  523. >>>
  524. >>> Compose([py_vision.Decode(),
  525. >>> py_vision.TenCrop(size=200),
  526. >>> # 4D stack of 10 images
  527. >>> lambda *images: numpy.stack([py_vision.ToTensor()(image) for image in images])])
  528. """
  529. @check_ten_crop
  530. def __init__(self, size, use_vertical_flip=False):
  531. if isinstance(size, int):
  532. size = (size, size)
  533. self.size = size
  534. self.use_vertical_flip = use_vertical_flip
  535. def __call__(self, img):
  536. """
  537. Call method.
  538. Args:
  539. img (PIL image): PIL image to be cropped.
  540. Returns:
  541. img_tuple (tuple), a tuple of 10 PIL images
  542. (top_left, top_right, bottom_left, bottom_right, center) of original image +
  543. (top_left, top_right, bottom_left, bottom_right, center) of flipped image.
  544. """
  545. return util.ten_crop(img, self.size, self.use_vertical_flip)
  546. class Grayscale:
  547. """
  548. Convert the input PIL image to grayscale image.
  549. Args:
  550. num_output_channels (int): Number of channels of the output grayscale image (1 or 3).
  551. Default is 1. If set to 3, the returned image has 3 identical RGB channels.
  552. Examples:
  553. >>> import mindspore.dataset.vision.py_transforms as py_vision
  554. >>> from mindspore.dataset.transforms.py_transforms import Compose
  555. >>>
  556. >>> Compose([py_vision.Decode(),
  557. >>> py_vision.Grayscale(3),
  558. >>> py_vision.ToTensor()])
  559. """
  560. @check_num_channels
  561. def __init__(self, num_output_channels=1):
  562. self.num_output_channels = num_output_channels
  563. def __call__(self, img):
  564. """
  565. Call method.
  566. Args:
  567. img (PIL image): PIL image to be converted to grayscale.
  568. Returns:
  569. img (PIL image), grayscaled image.
  570. """
  571. return util.grayscale(img, num_output_channels=self.num_output_channels)
  572. class RandomGrayscale:
  573. """
  574. Randomly convert the input image into grayscale image with a given probability.
  575. Args:
  576. prob (float, optional): Probability of the image being converted to grayscale (default=0.1).
  577. Examples:
  578. >>> import mindspore.dataset.vision.py_transforms as py_vision
  579. >>> from mindspore.dataset.transforms.py_transforms import Compose
  580. >>>
  581. >>> Compose([py_vision.Decode(),
  582. >>> py_vision.RandomGrayscale(0.3),
  583. >>> py_vision.ToTensor()])
  584. """
  585. @check_prob
  586. def __init__(self, prob=0.1):
  587. self.prob = prob
  588. def __call__(self, img):
  589. """
  590. Call method.
  591. Args:
  592. img (PIL image): PIL image to be converted to grayscale randomly.
  593. Returns:
  594. img (PIL image), Randomly apply grayscale to image, same number of channels as the input image.
  595. If input image has 1 channel, the output grayscale image is 1 channel.
  596. If input image has 3 channels, the output image has 3 identical grayscale channels.
  597. """
  598. if img.mode == 'L':
  599. num_output_channels = 1
  600. else:
  601. num_output_channels = 3
  602. if self.prob > random.random():
  603. return util.grayscale(img, num_output_channels=num_output_channels)
  604. return img
  605. class Pad:
  606. """
  607. Pad the input PIL image according to padding parameters.
  608. Args:
  609. padding (Union[int, sequence]): The number of pixels to pad the image.
  610. If a single number is provided, pad all borders with this value.
  611. If a tuple or list of 2 values is provided, pad the left and top
  612. with the first value and the right and bottom with the second value.
  613. If 4 values are provided as a list or tuple,
  614. pad the left, top, right and bottom respectively.
  615. fill_value (Union[int, tuple], optional): Filling value for the pixel intensity
  616. of the borders if the padding_mode is Border.CONSTANT (Default=0).
  617. If it is a 3-tuple, it is used to fill R, G, B channels respectively.
  618. padding_mode (Border mode, optional): The method of padding (default=Border.CONSTANT).
  619. It can be any of [Border.CONSTANT, Border.EDGE, Border.REFLECT, Border.SYMMETRIC].
  620. - Border.CONSTANT, means it fills the border with constant values.
  621. - Border.EDGE, means it pads with the last value on the edge.
  622. - Border.REFLECT, means it reflects the values on the edge omitting the last
  623. value of edge.
  624. - Border.SYMMETRIC, means it reflects the values on the edge repeating the last
  625. value of edge.
  626. Examples:
  627. >>> import mindspore.dataset.vision.py_transforms as py_vision
  628. >>> from mindspore.dataset.transforms.py_transforms import Compose
  629. >>>
  630. >>> Compose([py_vision.Decode(),
  631. >>> # adds 10 pixels (default black) to each side of the border of the image
  632. >>> py_vision.Pad(padding=10),
  633. >>> py_vision.ToTensor()])
  634. """
  635. @check_pad
  636. def __init__(self, padding, fill_value=0, padding_mode=Border.CONSTANT):
  637. parse_padding(padding)
  638. self.padding = padding
  639. self.fill_value = fill_value
  640. self.padding_mode = DE_PY_BORDER_TYPE[padding_mode]
  641. def __call__(self, img):
  642. """
  643. Call method.
  644. Args:
  645. img (PIL image): Image to be padded.
  646. Returns:
  647. img (PIL image), Padded image.
  648. """
  649. return util.pad(img, self.padding, self.fill_value, self.padding_mode)
  650. class RandomPerspective:
  651. """
  652. Randomly apply perspective transformation to the input PIL image with a given probability.
  653. Args:
  654. distortion_scale (float, optional): The scale of distortion, a float value between 0 and 1 (default=0.5).
  655. prob (float, optional): Probability of the image being applied perspective transformation (default=0.5).
  656. interpolation (Inter mode, optional): Image interpolation mode (default=Inter.BICUBIC).
  657. It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC].
  658. - Inter.BILINEAR, means the interpolation method is bilinear interpolation.
  659. - Inter.NEAREST, means the interpolation method is nearest-neighbor interpolation.
  660. - Inter.BICUBIC, means the interpolation method is bicubic interpolation.
  661. Examples:
  662. >>> import mindspore.dataset.vision.py_transforms as py_vision
  663. >>> from mindspore.dataset.transforms.py_transforms import Compose
  664. >>>
  665. >>> Compose([py_vision.Decode(),
  666. >>> py_vision.RandomPerspective(prob=0.1),
  667. >>> py_vision.ToTensor()])
  668. """
  669. @check_random_perspective
  670. def __init__(self, distortion_scale=0.5, prob=0.5, interpolation=Inter.BICUBIC):
  671. self.distortion_scale = distortion_scale
  672. self.prob = prob
  673. self.interpolation = DE_PY_INTER_MODE[interpolation]
  674. def __call__(self, img):
  675. """
  676. Call method.
  677. Args:
  678. img (PIL image): PIL image to apply perspective transformation randomly.
  679. Returns:
  680. img (PIL image), Image after being perspectively transformed randomly.
  681. """
  682. if self.prob > random.random():
  683. start_points, end_points = util.get_perspective_params(img, self.distortion_scale)
  684. return util.perspective(img, start_points, end_points, self.interpolation)
  685. return img
  686. class RandomErasing:
  687. """
  688. Erase the pixels, within a selected rectangle region, to the given value.
  689. Randomly applied on the input NumPy image array with a given probability.
  690. Zhun Zhong et al. 'Random Erasing Data Augmentation' 2017 See https://arxiv.org/pdf/1708.04896.pdf
  691. Args:
  692. prob (float, optional): Probability of applying RandomErasing (default=0.5).
  693. scale (sequence of floats, optional): Range of the relative erase area to the
  694. original image (default=(0.02, 0.33)).
  695. ratio (sequence of floats, optional): Range of the aspect ratio of the erase
  696. area (default=(0.3, 3.3)).
  697. value (Union[int, sequence, string]): Erasing value (default=0).
  698. If value is a single intieger, it is applied to all pixels to be erased.
  699. If value is a sequence of length 3, it is applied to R, G, B channels respectively.
  700. If value is a string 'random', the erase value will be obtained from a standard normal distribution.
  701. inplace (bool, optional): Apply this transform in-place (default=False).
  702. max_attempts (int, optional): The maximum number of attempts to propose a valid
  703. erase_area (default=10). If exceeded, return the original image.
  704. Examples:
  705. >>> import mindspore.dataset.vision.py_transforms as py_vision
  706. >>> from mindspore.dataset.transforms.py_transforms import Compose
  707. >>>
  708. >>> Compose([py_vision.Decode(),
  709. >>> py_vision.ToTensor(),
  710. >>> py_vision.RandomErasing(value='random')])
  711. """
  712. @check_random_erasing
  713. def __init__(self, prob=0.5, scale=(0.02, 0.33), ratio=(0.3, 3.3), value=0, inplace=False, max_attempts=10):
  714. self.prob = prob
  715. self.scale = scale
  716. self.ratio = ratio
  717. self.value = value
  718. self.inplace = inplace
  719. self.max_attempts = max_attempts
  720. def __call__(self, np_img):
  721. """
  722. Call method.
  723. Args:
  724. np_img (numpy.ndarray): NumPy image array of shape (C, H, W) to be randomly erased.
  725. Returns:
  726. np_img (numpy.ndarray), Erased NumPy image array.
  727. """
  728. bounded = True
  729. if self.prob > random.random():
  730. i, j, erase_h, erase_w, erase_value = util.get_erase_params(np_img, self.scale, self.ratio,
  731. self.value, bounded, self.max_attempts)
  732. return util.erase(np_img, i, j, erase_h, erase_w, erase_value, self.inplace)
  733. return np_img
  734. class Cutout:
  735. """
  736. Randomly cut (mask) out a given number of square patches from the input NumPy image array.
  737. Terrance DeVries and Graham W. Taylor 'Improved Regularization of Convolutional Neural Networks with Cutout' 2017
  738. See https://arxiv.org/pdf/1708.04552.pdf
  739. Args:
  740. length (int): The side length of each square patch.
  741. num_patches (int, optional): Number of patches to be cut out of an image (default=1).
  742. Examples:
  743. >>> import mindspore.dataset.vision.py_transforms as py_vision
  744. >>> from mindspore.dataset.transforms.py_transforms import Compose
  745. >>>
  746. >>> Compose([py_vision.Decode(),
  747. >>> py_vision.ToTensor(),
  748. >>> py_vision.Cutout(80)])
  749. """
  750. @check_cutout
  751. def __init__(self, length, num_patches=1):
  752. self.length = length
  753. self.num_patches = num_patches
  754. def __call__(self, np_img):
  755. """
  756. Call method.
  757. Args:
  758. np_img (numpy.ndarray): NumPy image array of shape (C, H, W) to be cut out.
  759. Returns:
  760. np_img (numpy.ndarray), NumPy image array with square patches cut out.
  761. """
  762. if not isinstance(np_img, np.ndarray):
  763. raise TypeError('img should be NumPy array. Got {}'.format(type(np_img)))
  764. _, image_h, image_w = np_img.shape
  765. scale = (self.length * self.length) / (image_h * image_w)
  766. bounded = False
  767. for _ in range(self.num_patches):
  768. i, j, erase_h, erase_w, erase_value = util.get_erase_params(np_img, (scale, scale), (1, 1), 0, bounded,
  769. 1)
  770. np_img = util.erase(np_img, i, j, erase_h, erase_w, erase_value)
  771. return np_img
  772. class LinearTransformation:
  773. """
  774. Apply linear transformation to the input NumPy image array, given a square transformation matrix and
  775. a mean vector.
  776. The transformation first flattens the input array and subtracts the mean vector from it. It then computes
  777. the dot product with the transformation matrix, and reshapes it back to its original shape.
  778. Args:
  779. transformation_matrix (numpy.ndarray): a square transformation matrix of shape (D, D), D = C x H x W.
  780. mean_vector (numpy.ndarray): a NumPy ndarray of shape (D,) where D = C x H x W.
  781. Examples:
  782. >>> import mindspore.dataset.vision.py_transforms as py_vision
  783. >>> from mindspore.dataset.transforms.py_transforms import Compose
  784. >>>
  785. >>> Compose([py_vision.Decode(),
  786. >>> py_vision.Resize(256),
  787. >>> py_vision.ToTensor(),
  788. >>> py_vision.LinearTransformation(transformation_matrix, mean_vector)])
  789. """
  790. @check_linear_transform
  791. def __init__(self, transformation_matrix, mean_vector):
  792. self.transformation_matrix = transformation_matrix
  793. self.mean_vector = mean_vector
  794. def __call__(self, np_img):
  795. """
  796. Call method.
  797. Args:
  798. np_img (numpy.ndarray): NumPy image array of shape (C, H, W) to be linear transformed.
  799. Returns:
  800. np_img (numpy.ndarray), Linear transformed image.
  801. """
  802. return util.linear_transform(np_img, self.transformation_matrix, self.mean_vector)
  803. class RandomAffine:
  804. """
  805. Apply Random affine transformation to the input PIL image.
  806. Args:
  807. degrees (Union[int, float, sequence]): Range of the rotation degrees.
  808. If degrees is a number, the range will be (-degrees, degrees).
  809. If degrees is a sequence, it should be (min, max).
  810. translate (sequence, optional): Sequence (tx, ty) of maximum translation in
  811. x(horizontal) and y(vertical) directions (default=None).
  812. The horizontal shift and vertical shift are selected randomly from the range:
  813. (-tx*width, tx*width) and (-ty*height, ty*height), respectively.
  814. If None, no translations are applied.
  815. scale (sequence, optional): Scaling factor interval (default=None, original scale is used).
  816. shear (Union[int, float, sequence], optional): Range of shear factor (default=None).
  817. If shear is an integer, then a shear parallel to the X axis in the range of (-shear, +shear) is applied.
  818. If shear is a tuple or list of size 2, then a shear parallel to the X axis in the range of
  819. (shear[0], shear[1]) is applied.
  820. If shear is a tuple of list of size 4, then a shear parallel to X axis in the range of
  821. (shear[0], shear[1]) and a shear parallel to Y axis in the range of (shear[2], shear[3]) is applied.
  822. If shear is None, no shear is applied.
  823. resample (Inter mode, optional): An optional resampling filter (default=Inter.NEAREST).
  824. If omitted, or if the image has mode "1" or "P", it is set to be Inter.NEAREST.
  825. It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC].
  826. - Inter.BILINEAR, means resample method is bilinear interpolation.
  827. - Inter.NEAREST, means resample method is nearest-neighbor interpolation.
  828. - Inter.BICUBIC, means resample method is bicubic interpolation.
  829. fill_value (Union[tuple, int], optional): Optional filling value to fill the area outside the transform
  830. in the output image. There must be three elements in the tuple and the value of a single element is
  831. within the range [0, 255].
  832. Used only in Pillow versions > 5.0.0 (default=0, filling is performed).
  833. Raises:
  834. ValueError: If degrees is negative.
  835. ValueError: If translation value is not between 0 and 1.
  836. ValueError: If scale is not positive.
  837. ValueError: If shear is a number but is not positive.
  838. TypeError: If degrees is not a number or a list or a tuple.
  839. If degrees is a list or tuple, its length is not 2.
  840. TypeError: If translate is specified but is not list or a tuple of length 2.
  841. TypeError: If scale is not a list or tuple of length 2.
  842. TypeError: If shear is not a list or tuple of length 2 or 4.
  843. TypeError: If fill_value is not a single integer or a 3-tuple.
  844. Examples:
  845. >>> import mindspore.dataset.vision.py_transforms as py_vision
  846. >>> from mindspore.dataset.transforms.py_transforms import Compose
  847. >>>
  848. >>> Compose([py_vision.Decode(),
  849. >>> py_vision.RandomAffine(degrees=15, translate=(0.1, 0.1), scale=(0.9, 1.1)),
  850. >>> py_vision.ToTensor()])
  851. """
  852. @check_random_affine
  853. def __init__(self, degrees, translate=None, scale=None, shear=None, resample=Inter.NEAREST, fill_value=0):
  854. # Parameter checking
  855. # rotation
  856. if shear is not None:
  857. if isinstance(shear, numbers.Number):
  858. shear = (-1 * shear, shear)
  859. else:
  860. if len(shear) == 2:
  861. shear = [shear[0], shear[1], 0., 0.]
  862. elif len(shear) == 4:
  863. shear = [s for s in shear]
  864. if isinstance(degrees, numbers.Number):
  865. degrees = (-degrees, degrees)
  866. self.degrees = degrees
  867. self.translate = translate
  868. self.scale_ranges = scale
  869. self.shear = shear
  870. self.resample = DE_PY_INTER_MODE[resample]
  871. self.fill_value = fill_value
  872. def __call__(self, img):
  873. """
  874. Call method.
  875. Args:
  876. img (PIL image): Image to apply affine transformation.
  877. Returns:
  878. img (PIL image), Randomly affine transformed image.
  879. """
  880. return util.random_affine(img,
  881. self.degrees,
  882. self.translate,
  883. self.scale_ranges,
  884. self.shear,
  885. self.resample,
  886. self.fill_value)
  887. class MixUp:
  888. """
  889. Apply mix up transformation to the input image and label. Make one input data combined with others.
  890. Args:
  891. batch_size (int): Batch size of dataset.
  892. alpha (float): Mix up rate.
  893. is_single (bool): Identify if single batch or multi-batch mix up transformation is to be used
  894. (Default=True, which is single batch).
  895. Examples:
  896. >>> import mindspore.dataset.vision.py_transforms as py_vision
  897. >>>
  898. >>> # Setup multi-batch mixup transformation
  899. >>> transform = [py_vision.MixUp(batch_size=16, alpha=0.2, is_single=False)]
  900. >>> # Apply the transform to the dataset through dataset.map()
  901. >>> dataset = dataset.map(input_columns="image", operations=transform())
  902. """
  903. @check_mix_up
  904. def __init__(self, batch_size, alpha, is_single=True):
  905. self.image = 0
  906. self.label = 0
  907. self.is_first = True
  908. self.batch_size = batch_size
  909. self.alpha = alpha
  910. self.is_single = is_single
  911. def __call__(self, image, label):
  912. """
  913. Call method.
  914. Args:
  915. image (numpy.ndarray): NumPy image to apply mix up transformation.
  916. label(numpy.ndarray): NumPy label to apply mix up transformation.
  917. Returns:
  918. image (numpy.ndarray): NumPy image after applying mix up transformation.
  919. label(numpy.ndarray): NumPy label after applying mix up transformation.
  920. """
  921. if self.is_single:
  922. return util.mix_up_single(self.batch_size, image, label, self.alpha)
  923. return util.mix_up_muti(self, self.batch_size, image, label, self.alpha)
  924. class RgbToHsv:
  925. """
  926. Convert a NumPy RGB image or a batch of NumPy RGB images to HSV images.
  927. Args:
  928. is_hwc (bool): The flag of image shape, (H, W, C) or (N, H, W, C) if True
  929. and (C, H, W) or (N, C, H, W) if False (default=False).
  930. Examples:
  931. >>> import mindspore.dataset.vision.py_transforms as py_vision
  932. >>> from mindspore.dataset.transforms.py_transforms import Compose
  933. >>>
  934. >>> Compose([py_vision.Decode(),
  935. >>> py_vision.CenterCrop(20),
  936. >>> py_vision.ToTensor(),
  937. >>> py_vision.RgbToHsv()])
  938. """
  939. def __init__(self, is_hwc=False):
  940. self.is_hwc = is_hwc
  941. def __call__(self, rgb_imgs):
  942. """
  943. Call method.
  944. Args:
  945. rgb_imgs (numpy.ndarray): NumPy RGB images array of shape (H, W, C) or (N, H, W, C),
  946. or (C, H, W) or (N, C, H, W) to be converted.
  947. Returns:
  948. np_hsv_img (numpy.ndarray), NumPy HSV images with same shape of rgb_imgs.
  949. """
  950. return util.rgb_to_hsvs(rgb_imgs, self.is_hwc)
  951. class HsvToRgb:
  952. """
  953. Convert a NumPy HSV image or one batch NumPy HSV images to RGB images.
  954. Args:
  955. is_hwc (bool): The flag of image shape, (H, W, C) or (N, H, W, C) if True
  956. and (C, H, W) or (N, C, H, W) if False (default=False).
  957. Examples:
  958. >>> import mindspore.dataset.vision.py_transforms as py_vision
  959. >>> from mindspore.dataset.transforms.py_transforms import Compose
  960. >>>
  961. >>> Compose([py_vision.Decode(),
  962. >>> py_vision.CenterCrop(20),
  963. >>> py_vision.ToTensor(),
  964. >>> py_vision.HsvToRgb()])
  965. """
  966. def __init__(self, is_hwc=False):
  967. self.is_hwc = is_hwc
  968. def __call__(self, hsv_imgs):
  969. """
  970. Call method.
  971. Args:
  972. hsv_imgs (numpy.ndarray): NumPy HSV images array of shape (H, W, C) or (N, H, W, C),
  973. or (C, H, W) or (N, C, H, W) to be converted.
  974. Returns:
  975. rgb_imgs (numpy.ndarray), NumPy RGB image with same shape of hsv_imgs.
  976. """
  977. return util.hsv_to_rgbs(hsv_imgs, self.is_hwc)
  978. class RandomColor:
  979. """
  980. Adjust the color of the input PIL image by a random degree.
  981. Args:
  982. degrees (sequence): Range of random color adjustment degrees.
  983. It should be in (min, max) format (default=(0.1,1.9)).
  984. Examples:
  985. >>> import mindspore.dataset.vision.py_transforms as py_vision
  986. >>> from mindspore.dataset.transforms.py_transforms import Compose
  987. >>>
  988. >>> Compose([py_vision.Decode(),
  989. >>> py_vision.RandomColor((0.5, 2.0)),
  990. >>> py_vision.ToTensor()])
  991. """
  992. @check_positive_degrees
  993. def __init__(self, degrees=(0.1, 1.9)):
  994. self.degrees = degrees
  995. def __call__(self, img):
  996. """
  997. Call method.
  998. Args:
  999. img (PIL image): Image to be color adjusted.
  1000. Returns:
  1001. img (PIL image), Color adjusted image.
  1002. """
  1003. return util.random_color(img, self.degrees)
  1004. class RandomSharpness:
  1005. """
  1006. Adjust the sharpness of the input PIL image by a random degree.
  1007. Args:
  1008. degrees (sequence): Range of random sharpness adjustment degrees.
  1009. It should be in (min, max) format (default=(0.1,1.9)).
  1010. Examples:
  1011. >>> import mindspore.dataset.vision.py_transforms as py_vision
  1012. >>> from mindspore.dataset.transforms.py_transforms import Compose
  1013. >>>
  1014. >>> Compose([py_vision.Decode(),
  1015. >>> py_vision.RandomSharpness((0.5, 1.5)),
  1016. >>> py_vision.ToTensor()])
  1017. """
  1018. @check_positive_degrees
  1019. def __init__(self, degrees=(0.1, 1.9)):
  1020. self.degrees = degrees
  1021. def __call__(self, img):
  1022. """
  1023. Call method.
  1024. Args:
  1025. img (PIL image): Image to be sharpness adjusted.
  1026. Returns:
  1027. img (PIL image), Color adjusted image.
  1028. """
  1029. return util.random_sharpness(img, self.degrees)
  1030. class AutoContrast:
  1031. """
  1032. Automatically maximize the contrast of the input PIL image.
  1033. Args:
  1034. cutoff (float, optional): Percent of pixels to cut off from the histogram (default=0.0).
  1035. ignore (Union[int, sequence], optional): Pixel values to ignore (default=None).
  1036. Examples:
  1037. >>> import mindspore.dataset.vision.py_transforms as py_vision
  1038. >>> from mindspore.dataset.transforms.py_transforms import Compose
  1039. >>>
  1040. >>> Compose([py_vision.Decode(),
  1041. >>> py_vision.AutoContrast(),
  1042. >>> py_vision.ToTensor()])
  1043. """
  1044. @check_auto_contrast
  1045. def __init__(self, cutoff=0.0, ignore=None):
  1046. self.cutoff = cutoff
  1047. self.ignore = ignore
  1048. def __call__(self, img):
  1049. """
  1050. Call method.
  1051. Args:
  1052. img (PIL image): Image to be augmented with AutoContrast.
  1053. Returns:
  1054. img (PIL image), Augmented image.
  1055. """
  1056. return util.auto_contrast(img, self.cutoff, self.ignore)
  1057. class Invert:
  1058. """
  1059. Invert colors of input PIL image.
  1060. Examples:
  1061. >>> import mindspore.dataset.vision.py_transforms as py_vision
  1062. >>> from mindspore.dataset.transforms.py_transforms import Compose
  1063. >>>
  1064. >>> Compose([py_vision.Decode(),
  1065. >>> py_vision.Invert(),
  1066. >>> py_vision.ToTensor()])
  1067. """
  1068. def __call__(self, img):
  1069. """
  1070. Call method.
  1071. Args:
  1072. img (PIL image): Image to be color Inverted.
  1073. Returns:
  1074. img (PIL image), Color inverted image.
  1075. """
  1076. return util.invert_color(img)
  1077. class Equalize:
  1078. """
  1079. Equalize the histogram of input PIL image.
  1080. Examples:
  1081. >>> import mindspore.dataset.vision.py_transforms as py_vision
  1082. >>> from mindspore.dataset.transforms.py_transforms import Compose
  1083. >>>
  1084. >>> Compose([py_vision.Decode(),
  1085. >>> py_vision.Equalize(),
  1086. >>> py_vision.ToTensor()])
  1087. """
  1088. def __call__(self, img):
  1089. """
  1090. Call method.
  1091. Args:
  1092. img (PIL image): Image to be equalized.
  1093. Returns:
  1094. img (PIL image), Equalized image.
  1095. """
  1096. return util.equalize(img)
  1097. class UniformAugment:
  1098. """
  1099. Uniformly select and apply a number of transforms sequentially from
  1100. a list of transforms. Randomly assign a probability to each transform for
  1101. each image to decide whether to apply the transform or not.
  1102. All the transforms in transform list must have the same input/output data type.
  1103. Args:
  1104. transforms (list): List of transformations to be chosen from to apply.
  1105. num_ops (int, optional): number of transforms to sequentially apply (default=2).
  1106. Examples:
  1107. >>> import mindspore.dataset.vision.py_transforms as py_vision
  1108. >>> from mindspore.dataset.transforms.py_transforms import Compose
  1109. >>>
  1110. >>> transforms_list = [py_vision.CenterCrop(64),
  1111. >>> py_vision.RandomColor(),
  1112. >>> py_vision.RandomSharpness(),
  1113. >>> py_vision.RandomRotation(30)]
  1114. >>> Compose([py_vision.Decode(),
  1115. >>> py_vision.UniformAugment(transforms_list),
  1116. >>> py_vision.ToTensor()])
  1117. """
  1118. @check_uniform_augment_py
  1119. def __init__(self, transforms, num_ops=2):
  1120. self.transforms = transforms
  1121. self.num_ops = num_ops
  1122. def __call__(self, img):
  1123. """
  1124. Call method.
  1125. Args:
  1126. img (PIL image): Image to apply transformation.
  1127. Returns:
  1128. img (PIL image), Transformed image.
  1129. """
  1130. return util.uniform_augment(img, self.transforms.copy(), self.num_ops)