Class RandomCrop<T>
- Namespace
- AiDotNet.Augmentation.Image
- Assembly
- AiDotNet.dll
Randomly crops a region from the image.
public class RandomCrop<T> : SpatialAugmentationBase<T, ImageTensor<T>>, ISpatialAugmentation<T, ImageTensor<T>>, IAugmentation<T, ImageTensor<T>>
Type Parameters
TThe numeric type for calculations.
- Inheritance
-
AugmentationBase<T, ImageTensor<T>>RandomCrop<T>
- Implements
-
ISpatialAugmentation<T, ImageTensor<T>>IAugmentation<T, ImageTensor<T>>
- Inherited Members
Remarks
Random cropping extracts a random rectangular region from the image. This is one of the most effective augmentations for teaching models to recognize objects even when partially visible or at different positions within the frame.
For Beginners: Think of this like taking a photo with a camera that randomly zooms in on different parts of the scene. Even if only part of an object is visible, it's still that object. This teaches your model to recognize objects even when they're partially out of frame.
When to use:
- Image classification where objects may be partially visible
- Training for translation invariance
- When input images are larger than needed for the model
When NOT to use:
- Object detection (may crop out target objects entirely)
- When the full context of the image is important
Padding behavior: When UseScaleCropping is false and the input image is smaller than the requested crop dimensions (CropWidth x CropHeight), the output tensor will be zero-padded in the areas beyond the source image bounds. If you require the source image to be at least as large as the crop dimensions, validate this before applying the augmentation.
Constructors
RandomCrop(int, int, double)
Creates a new random crop augmentation with fixed output size.
public RandomCrop(int cropHeight, int cropWidth, double probability = 1)
Parameters
cropHeightintThe output height after cropping.
cropWidthintThe output width after cropping.
probabilitydoubleThe probability of applying this augmentation (0.0 to 1.0). Industry standard default is 1.0 (always crop).
RandomCrop(int, int, double, double, double, double, double)
Creates a new random resized crop augmentation (scale-based cropping).
public RandomCrop(int outputHeight, int outputWidth, double minScale, double maxScale, double minAspectRatio = 0.75, double maxAspectRatio = 1.333, double probability = 1)
Parameters
outputHeightintThe output height after cropping and resizing.
outputWidthintThe output width after cropping and resizing.
minScaledoubleThe minimum scale of the crop relative to image area. Industry standard default is 0.08 (8% of image area).
maxScaledoubleThe maximum scale of the crop relative to image area. Industry standard default is 1.0 (full image).
minAspectRatiodoubleThe minimum aspect ratio (width/height) for the crop. Industry standard default is 0.75 (3:4 ratio).
maxAspectRatiodoubleThe maximum aspect ratio (width/height) for the crop. Industry standard default is 1.333 (4:3 ratio).
probabilitydoubleThe probability of applying this augmentation.
Properties
CropHeight
Gets the output height after cropping.
public int CropHeight { get; }
Property Value
CropWidth
Gets the output width after cropping.
public int CropWidth { get; }
Property Value
MaxAspectRatio
Gets the maximum aspect ratio (width/height) for the crop.
public double MaxAspectRatio { get; }
Property Value
MaxScale
Gets the maximum scale factor for the crop region relative to image size.
public double MaxScale { get; }
Property Value
MinAspectRatio
Gets the minimum aspect ratio (width/height) for the crop.
public double MinAspectRatio { get; }
Property Value
MinScale
Gets the minimum scale factor for the crop region relative to image size.
public double MinScale { get; }
Property Value
UseScaleCropping
Gets whether to use scale-based random cropping (like RandomResizedCrop).
public bool UseScaleCropping { get; }
Property Value
Methods
ApplyWithTransformParams(ImageTensor<T>, AugmentationContext<T>)
Applies the random crop transformation and returns transform parameters.
protected override (ImageTensor<T> data, IDictionary<string, object> parameters) ApplyWithTransformParams(ImageTensor<T> data, AugmentationContext<T> context)
Parameters
dataImageTensor<T>contextAugmentationContext<T>
Returns
- (ImageTensor<T> data, IDictionary<string, object> parameters)
GetParameters()
Gets the parameters of this augmentation.
public override IDictionary<string, object> GetParameters()
Returns
- IDictionary<string, object>
A dictionary of parameter names to values.
TransformBoundingBox(BoundingBox<T>, IDictionary<string, object>, AugmentationContext<T>)
Transforms a bounding box after random crop.
protected override BoundingBox<T> TransformBoundingBox(BoundingBox<T> box, IDictionary<string, object> transformParams, AugmentationContext<T> context)
Parameters
boxBoundingBox<T>transformParamsIDictionary<string, object>contextAugmentationContext<T>
Returns
- BoundingBox<T>
TransformKeypoint(Keypoint<T>, IDictionary<string, object>, AugmentationContext<T>)
Transforms a keypoint after random crop.
protected override Keypoint<T> TransformKeypoint(Keypoint<T> keypoint, IDictionary<string, object> transformParams, AugmentationContext<T> context)
Parameters
keypointKeypoint<T>transformParamsIDictionary<string, object>contextAugmentationContext<T>
Returns
- Keypoint<T>
TransformMask(SegmentationMask<T>, IDictionary<string, object>, AugmentationContext<T>)
Transforms a segmentation mask after random crop.
protected override SegmentationMask<T> TransformMask(SegmentationMask<T> mask, IDictionary<string, object> transformParams, AugmentationContext<T> context)
Parameters
maskSegmentationMask<T>transformParamsIDictionary<string, object>contextAugmentationContext<T>