RandomCrop

class torchaug.transforms.RandomCrop(size, padding=None, pad_if_needed=False, fill=0, padding_mode='constant')[source]

Crop the input at a random location.

If the input is a torch.Tensor or a TATensor (e.g. Image, Video, BoundingBoxes etc.) it can have arbitrary number of leading batch dimensions. For example, the image can have [..., C, H, W] shape. A bounding box can have [..., 4] shape.

Parameters:
  • size (Union[int, Sequence[int]]) – Desired output size of the crop. If size is an int instead of sequence like (h, w), a square crop (size, size) is made. If provided a sequence of length 1, it will be interpreted as (size[0], size[0]).

  • padding (Union[int, Sequence[int], None], optional) –

    Optional padding on each border of the image. If a single int is provided this is used to pad all borders. If sequence of length 2 is provided this is the padding on left/right and top/bottom respectively. If a sequence of length 4 is provided this is the padding for the left, top, right and bottom borders respectively. Default: None

    Note

    In torchscript mode padding as single int is not supported, use a sequence of length 1: [padding, ].

  • pad_if_needed (bool, optional) – It will pad the image if smaller than the desired size to avoid raising an exception. Since cropping is done after padding, the padding seems to be done at a random offset. Default: False

  • fill (Union[int, float, Sequence[int], Sequence[float], None, Dict[Union[Type, str], Union[int, float, Sequence[int], Sequence[float], None]]], optional) – Pixel fill value used when the padding_mode is constant. If a tuple of length 3, it is used to fill R, G, B channels respectively. Fill value can be also a dictionary mapping data type to the fill value, e.g. fill={ta_tensors.Image: 127, ta_tensors.Mask: 0} where Image will be filled with 127 and Mask will be filled with 0. Default: 0

  • padding_mode (Literal['constant', 'edge', 'reflect', 'symmetric'], optional) –

    Type of padding. Should be: constant, edge, reflect or symmetric. - constant: pads with a constant value, this value is specified with fill Default: "constant"

    • edge: pads with the last value at the edge of the image.

    • reflect: pads with reflection of image without repeating the last value on the edge. For example, padding [1, 2, 3, 4] with 2 elements on both sides in reflect mode will result in [3, 2, 1, 2, 3, 4, 3, 2]

    • symmetric: pads with reflection of image repeating the last value on the edge. For example, padding [1, 2, 3, 4] with 2 elements on both sides in symmetric mode will result in [2, 1, 1, 2, 3, 4, 4, 3]