Table of Contents

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

T

The numeric type for calculations.

Inheritance
RandomCrop<T>
Implements
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

cropHeight int

The output height after cropping.

cropWidth int

The output width after cropping.

probability double

The 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

outputHeight int

The output height after cropping and resizing.

outputWidth int

The output width after cropping and resizing.

minScale double

The minimum scale of the crop relative to image area. Industry standard default is 0.08 (8% of image area).

maxScale double

The maximum scale of the crop relative to image area. Industry standard default is 1.0 (full image).

minAspectRatio double

The minimum aspect ratio (width/height) for the crop. Industry standard default is 0.75 (3:4 ratio).

maxAspectRatio double

The maximum aspect ratio (width/height) for the crop. Industry standard default is 1.333 (4:3 ratio).

probability double

The probability of applying this augmentation.

Properties

CropHeight

Gets the output height after cropping.

public int CropHeight { get; }

Property Value

int

CropWidth

Gets the output width after cropping.

public int CropWidth { get; }

Property Value

int

MaxAspectRatio

Gets the maximum aspect ratio (width/height) for the crop.

public double MaxAspectRatio { get; }

Property Value

double

MaxScale

Gets the maximum scale factor for the crop region relative to image size.

public double MaxScale { get; }

Property Value

double

MinAspectRatio

Gets the minimum aspect ratio (width/height) for the crop.

public double MinAspectRatio { get; }

Property Value

double

MinScale

Gets the minimum scale factor for the crop region relative to image size.

public double MinScale { get; }

Property Value

double

UseScaleCropping

Gets whether to use scale-based random cropping (like RandomResizedCrop).

public bool UseScaleCropping { get; }

Property Value

bool

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

data ImageTensor<T>
context AugmentationContext<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

box BoundingBox<T>
transformParams IDictionary<string, object>
context AugmentationContext<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

keypoint Keypoint<T>
transformParams IDictionary<string, object>
context AugmentationContext<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

mask SegmentationMask<T>
transformParams IDictionary<string, object>
context AugmentationContext<T>

Returns

SegmentationMask<T>