mindspore.dataset.vision.RandomAffine

View Source On Gitee
class mindspore.dataset.vision.RandomAffine(degrees, translate=None, scale=None, shear=None, resample=Inter.NEAREST, fill_value=0)[source]

Apply Random affine transformation to the input image.

Parameters
  • degrees (Union[int, float, sequence]) – Range of the rotation degrees. If degrees is a number, the range will be (-degrees, degrees). If degrees is a sequence, it should be (min, max).

  • translate (sequence, optional) – Sequence (tx_min, tx_max, ty_min, ty_max) of minimum/maximum translation in x(horizontal) and y(vertical) directions, range [-1.0, 1.0]. Default: None. The horizontal and vertical shift is selected randomly from the range: (tx_min*width, tx_max*width) and (ty_min*height, ty_max*height), respectively. If a tuple or list of size 2, then a translate parallel to the X axis in the range of (translate[0], translate[1]) is applied. If a tuple or list of size 4, then a translate parallel to the X axis in the range of (translate[0], translate[1]) and a translate parallel to the Y axis in the range of (translate[2], translate[3]) are applied. If None, no translation is applied.

  • scale (sequence, optional) – Scaling factor interval, which must be non negative. Default: None, original scale is used.

  • shear (Union[float, Sequence[float, float], Sequence[float, float, float, float]], optional) – Range of shear factor to select from. If float is provided, a shearing parallel to X axis with a factor selected from ( -shear , shear ) will be applied. If Sequence[float, float] is provided, a shearing parallel to X axis with a factor selected from ( shear [0], shear [1]) will be applied. If Sequence[float, float, float, float] is provided, a shearing parallel to X axis with a factor selected from ( shear [0], shear [1]) and a shearing parallel to Y axis with a factor selected from ( shear [2], shear [3]) will be applied. Default: None, means no shearing.

  • resample (Inter, optional) – Image interpolation method defined by Inter . Default: Inter.NEAREST.

  • fill_value (Union[int, tuple[int]], optional) – Optional fill_value to fill the area outside the transform in the output image. There must be three elements in tuple and the value of single element is [0, 255]. Default: 0, filling is performed.

Raises
  • TypeError – If degrees is not of type int, float or sequence.

  • TypeError – If translate is not of type sequence.

  • TypeError – If scale is not of type sequence.

  • TypeError – If shear is not of type int, float or sequence.

  • TypeError – If resample is not of type Inter .

  • TypeError – If fill_value is not of type int or tuple[int].

  • ValueError – If degrees is negative.

  • ValueError – If translate is not in range [-1.0, 1.0].

  • ValueError – If scale is negative.

  • ValueError – If shear is not positive.

  • RuntimeError – If given tensor shape is not <H, W> or <H, W, C>.

Supported Platforms:

CPU

Examples

>>> import numpy as np
>>> import mindspore.dataset as ds
>>> import mindspore.dataset.vision as vision
>>> from mindspore.dataset.vision import Inter
>>>
>>> # Use the transform in dataset pipeline mode
>>> random_affine_op = vision.RandomAffine(degrees=15,
...                                        translate=(-0.1, 0.1, 0, 0),
...                                        scale=(0.9, 1.1),
...                                        resample=Inter.NEAREST)
>>> transforms_list = [random_affine_op]
>>> data = np.random.randint(0, 255, size=(1, 100, 100, 3)).astype(np.uint8)
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["image"])
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=transforms_list, input_columns=["image"])
>>> for item in numpy_slices_dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...     print(item["image"].shape, item["image"].dtype)
...     break
(100, 100, 3) uint8
>>>
>>> # Use the transform in eager mode
>>> data = np.random.randint(0, 255, size=(100, 100, 3)).astype(np.uint8)
>>> output = vision.RandomAffine(degrees=15, translate=(-0.1, 0.1, 0, 0),
...                              scale=(0.9, 1.1), resample=Inter.NEAREST)(data)
>>> print(output.shape, output.dtype)
(100, 100, 3) uint8
Tutorial Examples: