transforms.RandomCrop¶
- class lucid.transforms.RandomCrop(size: tuple[int, int])¶
The RandomCrop class is a transformation that randomly crops an input tensor to the specified dimensions. This transformation is commonly used in data augmentation for image-based neural networks, as it introduces positional variability to the training samples.
Class Signature¶
class lucid.transforms.RandomCrop(size: tuple[int, int])
Parameters¶
size (tuple[int, int]): The target size to which the input tensor will be cropped. The tuple represents the height and width of the cropped output tensor.
Attributes¶
size (tuple[int, int]): The size to which the input tensor will be cropped, stored as an internal attribute.
Forward Calculation¶
The cropping of an input tensor \(\mathbf{x}\) is performed as follows:
The dimensions of the input tensor are checked to ensure they are larger than or equal to the specified crop size.
Random starting indices \((i, j)\) for the height and width are generated to determine the top-left corner of the crop.
The sub-tensor of size \((h, w)\) is extracted from \(\mathbf{x}\), where \(h\) and \(w\) are the specified crop sizes.
This process can be represented mathematically as:
Where \(i\) and \(j\) are the starting indices for the crop, and \(h\) and \(w\) are the specified crop sizes.
Examples¶
Example 1: Basic Usage
>>> import lucid.transforms as T
>>> input_tensor = lucid.Tensor([[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]) # Shape: (1, 3, 3)
>>> crop = T.RandomCrop(size=(2, 2)) # Crop to 2x2
>>> output = crop(input_tensor)
>>> print(output)
Tensor([[[4.0, 5.0],
[7.0, 8.0]]], grad=None) # Example crop, actual result depends on randomness
Example 2: Cropping an Image Batch
>>> import lucid.transforms as T
>>> input_tensor = lucid.Tensor([[[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]],
[[10.0, 11.0, 12.0], [13.0, 14.0, 15.0], [16.0, 17.0, 18.0]]]]) # Shape: (1, 2, 3, 3)
>>> crop = T.RandomCrop(size=(2, 2)) # Crop each image in the batch to 2x2
>>> output = crop(input_tensor)
>>> print(output)
Tensor([[[[4.0, 5.0],
[7.0, 8.0]],
[[13.0, 14.0],
[16.0, 17.0]]]], grad=None) # Example crop, actual result depends on randomness
Note
The crop size must be smaller than or equal to the dimensions of the input tensor.
The random cropping introduces positional variation, which is useful for improving the robustness of neural networks during training.