mindspore.dataset.vision.Affine

class mindspore.dataset.vision.Affine(degrees, translate, scale, shear, resample=Inter.NEAREST, fill_value=0)[source]

Apply Affine transformation to the input image, keeping the center of the image unchanged.

Supports Ascend hardware acceleration and can be enabled through the .device(“Ascend”) method.

Parameters
  • degrees (float) – Rotation angle in degrees between -180 and 180, clockwise direction.

  • translate (Sequence[float, float]) – The horizontal and vertical translations, must be a sequence of size 2 and value between -1 and 1.

  • scale (float) – Scaling factor, which must be positive.

  • shear (Union[float, Sequence[float, float]]) – Shear angle value in degrees between -180 to 180. If float is provided, shear along the x axis with this value, without shearing along the y axis; If Sequence[float, float] is provided, shear along the x axis and y axis with these two values separately.

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

  • fill_value (Union[int, tuple[int, int, 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.

Raises
  • TypeError – If degrees is not of type float.

  • TypeError – If translate is not of type Sequence[float, float].

  • TypeError – If scale is not of type float.

  • ValueError – If scale is non positive.

  • TypeError – If shear is not of float or Sequence[float, float].

  • TypeError – If resample is not of type Inter .

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

  • RuntimeError – If shape of the input image 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
>>> data = np.random.randint(0, 255, size=(1, 100, 100, 3)).astype(np.uint8)
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["image"])
>>> affine_op = vision.Affine(degrees=15, translate=[0.2, 0.2], scale=1.1, shear=[1.0, 1.0],
...                           resample=Inter.BILINEAR)
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=[affine_op], 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.array([[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5]], dtype=np.uint8).reshape((2, 2, 3))
>>> output = vision.Affine(degrees=15, translate=[0.2, 0.2], scale=1.1,
...                        shear=[1.0, 1.0], resample=Inter.BILINEAR)(data)
>>> print(output.shape, output.dtype)
(2, 2, 3) uint8
Tutorial Examples:
device(device_target='CPU')[source]

Set the device for the current operator execution.

  • When the device is Ascend, input shape should be limited from [4, 6] to [32768, 32768].

Parameters

device_target (str, optional) – The operator will be executed on this device. Currently supports CPU and Ascend . Default: CPU .

Raises
  • TypeError – If device_target is not of type str.

  • ValueError – If device_target is not within the valid set of [‘CPU’, ‘Ascend’].

Supported Platforms:

CPU Ascend

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
>>> data = np.random.randint(0, 255, size=(1, 100, 100, 3)).astype(np.uint8)
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["image"])
>>> affine_op = vision.Affine(degrees=15, translate=[0.2, 0.2], scale=1.1,
...                           shear=[1.0, 1.0], resample=Inter.BILINEAR).device("Ascend")
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=[affine_op], 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.Affine(degrees=15, translate=[0.2, 0.2], scale=1.1,
...                        shear=[1.0, 1.0], resample=Inter.BILINEAR).device("Ascend")(data)
>>> print(output.shape, output.dtype)
(100, 100, 3) uint8
Tutorial Examples: