Random Resized Crop

Overview

The Random Resized Crop sample demonstrates how to use CV-CUDA’s GPU-accelerated cvcuda.random_resized_crop() operator to randomly select a sub-region of an image, resize it to a fixed output size, and write the result. This operation is the core augmentation used in standard ImageNet training pipelines (e.g., torchvision’s RandomResizedCrop): a crop whose area is a random fraction of the original image area and whose aspect ratio is sampled from a configurable range.

Usage

Basic Usage

Apply random resized crop with default 224×224 output:

python3 random_resized_crop.py -i input.jpg

Custom Output Size

Specify a different target resolution:

python3 random_resized_crop.py -i input.jpg -o cropped.jpg --width 320 --height 320

Command-Line Arguments

Argument

Short Form

Default

Description

--input

-i

tabby_tiger_cat.jpg

Input image file path

--output

-o

cvcuda/.cache/cat_random_resized_crop.jpg

Output image file path

--width

224

Target output width in pixels

--height

224

Target output height in pixels

Implementation

Random Resized Crop

# The operator works on batched (NHWC) tensors, so wrap the single HWC image
# in a batch dimension of size 1. The output shape must also carry the batch dim.
batched_input: cvcuda.Tensor = input_image.reshape((1, *input_image.shape), "NHWC")
_, _, _, c = batched_input.shape
output_h, output_w = args.height, args.width
output_shape = (1, output_h, output_w, c)

# Scale bounds control what fraction of the original image area the crop covers.
# A min_scale of 0.08 and max_scale of 1.0 matches the standard torchvision
# RandomResizedCrop defaults used in ImageNet training pipelines.
min_scale: float = 0.08
max_scale: float = 1.0

# Ratio bounds set the aspect-ratio range (width/height) for the crop window
# before it is resized to the target output dimensions.
min_ratio: float = 0.75
max_ratio: float = 1.3333333
seed: int = 42

Key points:

  1. Batched NHWC input: The operator expects an NHWC tensor (batch dimension first). A single HWC image is reshaped to (1, H, W, C) before the call and the result is reshaped back to (H, W, C) afterwards.

  2. Scale bounds: min_scale and max_scale control what fraction of the original image area the random crop covers. The defaults (0.08, 1.0) match standard ImageNet pre-processing.

  3. Ratio bounds: min_ratio and max_ratio bound the width-to-height ratio of the crop region before it is scaled to the output size, letting the network see both tall and wide crops.

  4. Interpolation: cvcuda.Interp.LINEAR (bilinear) gives a good quality/speed trade-off; NEAREST is faster, CUBIC provides higher fidelity.

  5. Reproducibility: The seed parameter makes the crop deterministic, which is useful for debugging or ablation experiments.

Expected Output

The output shows a randomly selected and resized crop of the original image:

../../_images/tabby_tiger_cat.jpg

Original Input Image

../../_images/cat_random_resized_crop.jpg

Output: Random Resized Crop to 224×224

CV-CUDA Operators Used

Operator

Purpose

cvcuda.random_resized_crop()

Randomly crop a sub-region of the image and resize it to the target dimensions

Common Utilities Used

See Also