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[int, int]]) – The size of the cropped image. If int is provided, a square of size (size, size) will be cropped with this value. If Sequence[int, int] is provided, its two elements will be taken as the cropped height and width.
padding (Union[int, Sequence[int, int], Sequence[int, int, int, int]], optional) – The number of pixels to pad on each border. When specified, it will pad the image before random cropping. If int is provided, pad all borders with this value. If Sequence[int, int] is provided, pad the left and top borders with the first value and the right and bottom borders with the second value. If Sequence[int, int, int, int] is provided, pad the left, top, right and bottom borders respectively. Default: None, means not to pad.
pad_if_needed (bool, optional) – Whether to pad the image if either side is shorter than the given cropping size. Default: False, means not to pad.
fill_value (Union[int, tuple[int, int, int]], optional) – Pixel value used to pad the borders, only valid when padding_mode is Border.CONSTANT. If int is provided, it will be used for all RGB channels. If tuple[int, int, int] is provided, it will be used for R, G, B channels respectively. Default: 0.
padding_mode (Border, optional) –
Method of padding. It can be Border.CONSTANT, Border.EDGE, Border.REFLECT or Border.SYMMETRIC. Default: Border.CONSTANT.
Border.CONSTANT, pads with a constant value.
Border.EDGE, pads with the last value at the edge of the image.
Border.REFLECT, pads with reflection of the image omitting the last value on the edge.
Border.SYMMETRIC, pads with reflection of the image repeating the last value on the edge.
- Raises
TypeError – If size is not of type int or Sequence[int, int].
TypeError – If padding is not of type int, Sequence[int, int] or Sequence[int, int, int, int].
TypeError – If pad_if_needed is not of type bool.
TypeError – If fill_value is not of type int or tuple[int, int, int].
TypeError – If padding_mode is not of type
mindspore.dataset.vision.Border
.ValueError – If size is not positive.
ValueError – If padding is negative.
ValueError – If fill_value is not in range of [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")