mindspore.dataset.transforms.vision.c_transforms

The module vision.c_transforms is inheritted from _c_dataenginewhich is implemented basing on opencv in C++. It’s a high performance module toprocess image augmentations. Users can apply suitable augmentations on image datato improve their training models.

Note

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

Examples

  1. Copy>>> import mindspore.dataset as ds
  2. >>> import mindspore.dataset.transforms.c_transforms as c_transforms
  3. >>> import mindspore.dataset.transforms.vision.c_transforms as vision
  4. >>> dataset_dir = "path/to/imagefolder_directory"
  5. >>> # create a dataset that reads all files in dataset_dir with 8 threads
  6. >>> dataset = ds.ImageFolderDatasetV2(dataset_dir, num_parallel_workers=8)
  7. >>> # create a list of transformations to be applied to the image data
  8. >>> transforms_list = [vision.Decode(),
  9. >>> vision.Resize((256, 256)),
  10. >>> vision.RandomRotation((0, 15)),
  11. >>> vision.Normalize((100, 115.0, 121.0), (71.0, 68.0, 70.0)),
  12. >>> vision.HWC2CHW()]
  13. >>> onehot_op = c_transforms.OneHot(num_classes)
  14. >>> # apply the transform to the dataset through dataset.map()
  15. >>> dataset = dataset.map(input_columns="image", operations=transforms_list)
  16. >>> dataset = dataset.map(input_columns="label", operations=onehot_op)
  • class mindspore.dataset.transforms.vision.ctransforms.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.ctransforms.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.ctransforms.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.ctransforms.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.ctransforms.Pad(_padding, fill_value=0, padding_mode=)[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 ifthe padding_mode is Border.CONSTANT (default=0). If it is a 3-tuple, it is used tofill 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].

  1. -

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

  1. -

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

  1. -

Border.REFLECT, means it reflects the values on the edge omitting the lastvalue of edge.

  1. -

Border.SYMMETRIC, means it reflects the values on the edge repeating the lastvalue of edge.

  • class mindspore.dataset.transforms.vision.ctransforms.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.ctransforms.RandomCrop(_size, padding=None, pad_if_needed=False, fill_value=0, padding_mode=)[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 thanthe given output size (default=False).

      • fill_value (int or tuple, __optional) – The pixel intensity of the borders ifthe padding_mode is Border.CONSTANT (default=0). If it is a 3-tuple, it is used tofill 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].

  1. -

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

  1. -

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

  1. -

Border.REFLECT, means it reflects the values on the edge omitting the lastvalue of edge.

  1. -

Border.SYMMETRIC, means it reflects the values on the edge repeating the lastvalue of edge.

  • class mindspore.dataset.transforms.vision.ctransforms.RandomCropDecodeResize(_size, scale=(0.08, 1.0), ratio=(0.75, 1.3333333333333333), interpolation=, 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 theoriginal size to be cropped (default=(0.08, 1.0)).

      • ratio (tuple, __optional) – Range (min, max) of aspect ratio to becropped (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].

  1. -

Inter.BILINEAR, means interpolation method is bilinear interpolation.

  1. -

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

  1. -

Inter.BICUBIC, means interpolation method is bicubic interpolation.

  1. -

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.ctransforms.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.ctransforms.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 withthe same image aspect ratio.If size is a sequence of length 2, it should be (height, width).
  • class mindspore.dataset.transforms.vision.ctransforms.RandomResizedCrop(_size, scale=(0.08, 1.0), ratio=(0.75, 1.3333333333333333), interpolation=, 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 originalsize 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].

  1. -

Inter.BILINEAR, means interpolation method is bilinear interpolation.

  1. -

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

  1. -

Inter.BICUBIC, means interpolation method is bicubic interpolation.

  1. -

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

  • class mindspore.dataset.transforms.vision.ctransforms.RandomRotation(_degrees, resample=, 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].

  1. -

Inter.BILINEAR, means resample method is bilinear interpolation.

  1. -

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

  1. -

Inter.BICUBIC, means resample method is bicubic interpolation.

  1. -

expand (bool, __optional) – Optional expansion flag (default=False). If set to True, expand the outputimage 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.

  1. -

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.

  1. -

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.ctransforms.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.ctransforms.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.ctransforms.Resize(_size, interpolation=)[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 withthe 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].

  1. -

Inter.LINEAR, means interpolation method is bilinear interpolation.

  1. -

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

  1. -

Inter.BICUBIC, means interpolation method is bicubic interpolation.