mindspore.dataset.vision.py_transforms.RandomCrop

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

Crop the input PIL Image at a random location with the specified size.

Parameters
  • size (Union[int, sequence]) – The output size of the cropped image. If size is an integer, a square of size (size, size) is returned. If size is a sequence of length 2, it should be in shape of (height, width).

  • padding (Union[int, sequence], optional) – Padding on each border of the image (default=None). If padding is not None, pad the image before cropping. If a single number is provided, pad all borders with this value. If a sequence of length 2 is provided, pad the left/top border with the first value and right/bottom border with the second value. If a sequence of length 4 is provided, pad the left, top, right and bottom borders respectively.

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

  • fill_value (Union[int, tuple], optional) – Pixel fill value to pad the borders when padding_mode is Border.CONSTANT (default=0). If a tuple of length 3 is provided, it is used to fill R, G, B channels respectively.

  • padding_mode (Border, optional) –

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

    • Border.CONSTANT, means to pad with given constant values.

    • Border.EDGE, means to pad with the last value at the edge.

    • Border.REFLECT, means to pad with reflection of image omitting the last value at the edge.

    • Border.SYMMETRIC, means to pad with reflection of image repeating the last value at the edge.

Raises
  • TypeError – If size is not of type integer or sequence of integer.

  • TypeError – If padding is not of type integer or sequence of integer.

  • TypeError – If pad_if_needed is not of type boolean.

  • TypeError – If fill_value is not of type integer or sequence of integer.

  • TypeError – If padding_mode is not of type Border.

  • ValueError – If size is not positive.

  • ValueError – If padding is negative.

  • ValueError – If fill_value is not in range [0, 255].

Supported Platforms:

CPU

Examples

>>> from mindspore.dataset.transforms.py_transforms import Compose
>>> transforms_list = Compose([py_vision.Decode(),
...                            py_vision.RandomCrop(224),
...                            py_vision.ToTensor()])
>>> # apply the transform to dataset through map function
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns="image")