mindspore.dataset.transforms.vision.c_transforms

The module vision.c_transforms is inheritted from _c_dataengine which is implemented basing on opencv in C++. It’s a high performance module to process image augmentations. Users can apply suitable augmentations on image data to improve their training models.

Note

Constructor’s arguments for every class in this module must be saved into the class attributes (self.xxx) to support save() and load().

Examples

>>> import mindspore.dataset as ds
>>> import mindspore.dataset.transforms.c_transforms as c_transforms
>>> import mindspore.dataset.transforms.vision.c_transforms as vision
>>> dataset_dir = "path/to/imagefolder_directory"
>>> # create a dataset that reads all files in dataset_dir with 8 threads
>>> dataset = ds.ImageFolderDatasetV2(dataset_dir, num_parallel_workers=8)
>>> # create a list of transformations to be applied to the image data
>>> transforms_list = [vision.Decode(),
>>>                    vision.Resize((256, 256)),
>>>                    vision.RandomRotation((0, 15)),
>>>                    vision.Normalize((100,  115.0, 121.0), (71.0, 68.0, 70.0)),
>>>                    vision.HWC2CHW()]
>>> onehot_op = c_transforms.OneHot(num_classes)
>>> # apply the transform to the dataset through dataset.map()
>>> dataset = dataset.map(input_columns="image", operations=transforms_list)
>>> dataset = dataset.map(input_columns="label", operations=onehot_op)
class mindspore.dataset.transforms.vision.c_transforms.CenterCrop(size)[source]

Crops the input image at the center to the given size.

Parameters

size (int or sequence) – The output size of the cropped image. If size is an int, a square crop of size (size, size) is returned. If size is a sequence of length 2, it should be (height, width).

class mindspore.dataset.transforms.vision.c_transforms.CutOut(length, num_patches=1)[source]

Randomly cut (mask) out a given number of square patches from the input Numpy image array.

Parameters
  • length (int) – The side length of each square patch.

  • num_patches (int, optional) – Number of patches to be cut out of an image (default=1).

class mindspore.dataset.transforms.vision.c_transforms.Decode(rgb=True)[source]

Decode the input image in RGB mode.

class mindspore.dataset.transforms.vision.c_transforms.HWC2CHW[source]

Transpose the input image; shape (H, W, C) to shape (C, H, W).

class mindspore.dataset.transforms.vision.c_transforms.Normalize(mean, std)[source]

Normalize the input image with respect to mean and standard deviation.

Parameters
  • mean (list) – List of mean values for each channel, w.r.t channel order.

  • std (list) – List of standard deviations for each channel, w.r.t. channel order.

class mindspore.dataset.transforms.vision.c_transforms.Pad(padding, fill_value=0, padding_mode=Border.CONSTANT)[source]

Pads the image according to padding parameters.

Parameters
  • padding (int or sequence) – The number of pixels to pad the image. If a single number is provided, it pads all borders with this value. If a tuple or list of 2 values are provided, it pads the (left and top) with the first value and (right and bottom) with the second value. If 4 values are provided as a list or tuple, it pads the left, top, right and bottom respectively.

  • fill_value (int or tuple, optional) – The pixel intensity of the borders if the padding_mode is Border.CONSTANT (default=0). If it is a 3-tuple, it is used to fill R, G, B channels respectively.

  • padding_mode (Border mode) –

    The method of padding (default=Border.CONSTANT). Can be any of [Border.CONSTANT, Border.EDGE, Border.REFLECT, Border.SYMMETRIC].

    • Border.CONSTANT, means it fills the border with constant values.

    • Border.EDGE, means it pads with the last value on the edge.

    • Border.REFLECT, means it reflects the values on the edge omitting the last value of edge.

    • Border.SYMMETRIC, means it reflects the values on the edge repeating the last value of edge.

class mindspore.dataset.transforms.vision.c_transforms.RandomColorAdjust(brightness=(1, 1), contrast=(1, 1), saturation=(1, 1), hue=(0, 0))[source]

Randomly adjust the brightness, contrast, saturation, and hue of the input image.

Parameters
  • brightness (float or tuple, optional) – Brightness adjustment factor (default=(1, 1)). Cannot be negative. If it is a float, the factor is uniformly chosen from the range [max(0, 1-brightness), 1+brightness]. If it is a sequence, it should be [min, max] for the range.

  • contrast (float or tuple, optional) – Contrast adjustment factor (default=(1, 1)). Cannot be negative. If it is a float, the factor is uniformly chosen from the range [max(0, 1-contrast), 1+contrast]. If it is a sequence, it should be [min, max] for the range.

  • saturation (float or tuple, optional) – Saturation adjustment factor (default=(1, 1)). Cannot be negative. If it is a float, the factor is uniformly chosen from the range [max(0, 1-saturation), 1+saturation]. If it is a sequence, it should be [min, max] for the range.

  • hue (float or tuple, optional) – Hue adjustment factor (default=(0, 0)). If it is a float, the range will be [-hue, hue]. Value should be 0 <= hue <= 0.5. If it is a sequence, it should be [min, max] where -0.5 <= min <= max <= 0.5.

class mindspore.dataset.transforms.vision.c_transforms.RandomCrop(size, padding=None, pad_if_needed=False, fill_value=0, padding_mode=Border.CONSTANT)[source]

Crop the input image at a random location.

Parameters
  • size (int or sequence) – The output size of the cropped image. If size is an int, a square crop of size (size, size) is returned. If size is a sequence of length 2, it should be (height, width).

  • padding (int or sequence, optional) – The number of pixels to pad the image (default=None). If a single number is provided, it pads all borders with this value. If a tuple or list of 2 values are provided, it pads the (left and top) with the first value and (right and bottom) with the second value. If 4 values are provided as a list or tuple, it pads the left, top, right and bottom respectively.

  • pad_if_needed (bool, optional) – Pad the image if either side is smaller than the given output size (default=False).

  • fill_value (int or tuple, optional) – The pixel intensity of the borders if the padding_mode is Border.CONSTANT (default=0). If it is a 3-tuple, it is used to fill R, G, B channels respectively.

  • padding_mode (Border mode, optional) –

    The method of padding (default=Border.CONSTANT). Can be any of [Border.CONSTANT, Border.EDGE, Border.REFLECT, Border.SYMMETRIC].

    • Border.CONSTANT, means it fills the border with constant values.

    • Border.EDGE, means it pads with the last value on the edge.

    • Border.REFLECT, means it reflects the values on the edge omitting the last value of edge.

    • Border.SYMMETRIC, means it reflects the values on the edge repeating the last value of edge.

class mindspore.dataset.transforms.vision.c_transforms.RandomCropDecodeResize(size, scale=(0.08, 1.0), ratio=(0.75, 1.3333333333333333), interpolation=Inter.BILINEAR, max_attempts=10)[source]

Equivalent to RandomResizedCrop, but crops before decodes.

Parameters
  • size (int or sequence, optional) – The size of the output image. If size is an int, a square crop of size (size, size) is returned. If size is a sequence of length 2, it should be (height, width).

  • scale (tuple, optional) – Range (min, max) of respective size of the original size to be cropped (default=(0.08, 1.0)).

  • ratio (tuple, optional) – Range (min, max) of aspect ratio to be cropped (default=(3. / 4., 4. / 3.)).

  • interpolation (Inter mode, optional) –

    Image interpolation mode (default=Inter.BILINEAR). It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC].

    • Inter.BILINEAR, means interpolation method is bilinear interpolation.

    • Inter.NEAREST, means interpolation method is nearest-neighbor interpolation.

    • Inter.BICUBIC, means interpolation method is bicubic interpolation.

  • max_attempts (int, optional) – The maximum number of attempts to propose a valid crop_area (default=10). If exceeded, fall back to use center_crop instead.

class mindspore.dataset.transforms.vision.c_transforms.RandomHorizontalFlip(prob=0.5)[source]

Flip the input image horizontally, randomly with a given probability.

Parameters

prob (float) – Probability of the image being flipped (default=0.5).

class mindspore.dataset.transforms.vision.c_transforms.RandomResize(size)[source]

Tensor operation to resize the input image using a randomly selected interpolation mode.

Parameters

size (int or sequence) – The output size of the resized image. If size is an int, smaller edge of the image will be resized to this value with the same image aspect ratio. If size is a sequence of length 2, it should be (height, width).

class mindspore.dataset.transforms.vision.c_transforms.RandomResizedCrop(size, scale=(0.08, 1.0), ratio=(0.75, 1.3333333333333333), interpolation=Inter.BILINEAR, max_attempts=10)[source]

Crop the input image to a random size and aspect ratio.

Parameters
  • size (int or sequence) – The size of the output image. If size is an int, a square crop of size (size, size) is returned. If size is a sequence of length 2, it should be (height, width).

  • scale (tuple, optional) – Range (min, max) of respective size of the original size to be cropped (default=(0.08, 1.0)).

  • ratio (tuple, optional) – Range (min, max) of aspect ratio to be cropped (default=(3. / 4., 4. / 3.)).

  • interpolation (Inter mode, optional) –

    Image interpolation mode (default=Inter.BILINEAR). It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC].

    • Inter.BILINEAR, means interpolation method is bilinear interpolation.

    • Inter.NEAREST, means interpolation method is nearest-neighbor interpolation.

    • Inter.BICUBIC, means interpolation method is bicubic interpolation.

  • max_attempts (int, optional) – The maximum number of attempts to propose a valid crop_area (default=10). If exceeded, fall back to use center_crop instead.

class mindspore.dataset.transforms.vision.c_transforms.RandomRotation(degrees, resample=Inter.NEAREST, expand=False, center=None, fill_value=0)[source]

Rotate the input image by a random angle.

Parameters
  • degrees (int or float or sequence) – Range of random rotation degrees. If degrees is a number, the range will be converted to (-degrees, degrees). If degrees is a sequence, it should be (min, max).

  • resample (Inter mode, optional) –

    An optional resampling filter (default=Inter.NEAREST). If omitted, or if the image has mode “1” or “P”, it is set to be Inter.NEAREST. It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC].

    • Inter.BILINEAR, means resample method is bilinear interpolation.

    • Inter.NEAREST, means resample method is nearest-neighbor interpolation.

    • Inter.BICUBIC, means resample method is bicubic interpolation.

  • expand (bool, optional) – Optional expansion flag (default=False). If set to True, expand the output image to make it large enough to hold the entire rotated image. If set to False or omitted, make the output image the same size as the input. Note that the expand flag assumes rotation around the center and no translation.

  • center (tuple, optional) – Optional center of rotation (a 2-tuple) (default=None). Origin is the top left corner. None sets to the center of the image.

  • fill_value (int or tuple, optional) – Optional fill color for the area outside the rotated image (default=0). If it is a 3-tuple, it is used for R, G, B channels respectively. If it is an int, it is used for all RGB channels.

class mindspore.dataset.transforms.vision.c_transforms.RandomVerticalFlip(prob=0.5)[source]

Flip the input image vertically, randomly with a given probability.

Parameters

prob (float) – Probability of the image being flipped (default=0.5).

class mindspore.dataset.transforms.vision.c_transforms.Rescale(rescale, shift)[source]

Tensor operation to rescale the input image.

Parameters
  • rescale (float) – Rescale factor.

  • shift (float) – Shift factor.

class mindspore.dataset.transforms.vision.c_transforms.Resize(size, interpolation=Inter.BILINEAR)[source]

Resize the input image to the given size.

Parameters
  • size (int or sequence) – The output size of the resized image. If size is an int, smaller edge of the image will be resized to this value with the same image aspect ratio. If size is a sequence of length 2, it should be (height, width).

  • interpolation (Inter mode, optional) –

    Image interpolation mode (default=Inter.LINEAR). It can be any of [Inter.LINEAR, Inter.NEAREST, Inter.BICUBIC].

    • Inter.LINEAR, means interpolation method is bilinear interpolation.

    • Inter.NEAREST, means interpolation method is nearest-neighbor interpolation.

    • Inter.BICUBIC, means interpolation method is bicubic interpolation.