liveBook · Manning

Crop

As explained in Inside Deep Learning: Math, Algorithms, Models

A preprocessing operation that extracts a spatial subregion from an image, here used to create training and test variants for shift analysis.

Crop extracts a spatial subregion from an image. In CNN experiments, smaller crops create input diversity during training and expose how image shifts affect predictions. section 14.1.1

Use different crop policies for different dataset roles:

  • Apply a random 24 × 24 crop to each 32 × 32 CIFAR-10 training image, producing varied inputs across epochs. section 14.1.1
  • Apply a deterministic 24 × 24 center crop to test images so repeated evaluations remain comparable. section 14.1.1
  • Keep an uncropped 32 × 32 test set when evaluating predictions across manually selected spatial offsets. section 14.1.1

Measuring prediction changes

Take one uncropped test image and extract every 24 × 24 window with offsets from 0 through 7 horizontally and vertically. Run each crop through the CNN, record the correct-class probability, and display the results in an 8 × 8 grid. section 14.1.1

B = 128 
epochs = 30

train_transform = transforms.Compose(             #1 
    [
        transforms.RandomCrop((24,24)), 
        transforms.ToTensor(), 
    ]) 
test_transform = transforms.Compose(              #2 
    [ 
        transforms.CenterCrop((24,24)), 
        transforms.ToTensor(), 
    ])

trainset = torchvision.datasets.CIFAR10(root=’./data’, train=True, 
 download=True, transform=train_transform) 
train_loader = torch.utils.data.DataLoader(trainset, batch_size=B, 
 shuffle=True, num_workers=2) 
testset_nocrop = torchvision.datasets.CIFAR10(    #3
 root=’./data’, train=False, download=True, 
 transform=transforms.ToTensor())

testset = torchvision.datasets.CIFAR10(root=’./data’, train=False, 
 download=True, transform=test_transform)

test_loader = torch.utils.data.DataLoader(testset, #4 
 batch_size=B, shuffle=False, num_workers=2) 
cifar10_classes = (’plane’, ’car’, ’bird’, ’cat’,  #5 
 ’deer’, ’dog’, ’frog’, ’horse’, ’ship’, ’truck’)
#1 Transform for training: random crop to PyTorch tensor
#2 Transform for testing: crops the center to a PyTorch tensor
#3 A version of the test set with full <span class="math">32 &times; 32</span> images, so we can test specific crops
#4 The test loader used during evaluation is the deterministic center crop.
#5 Maps the class index back to their original names for CIFAR-10

The PyTorch setup pairs RandomCrop for training with CenterCrop for deterministic testing and preserves a separate uncropped test dataset. section 14.1.1

How Inside Deep Learning: Math, Algorithms, Models covers it

How deep it goes
Explains crop size, random versus center policies, dataset wiring, and offset evaluation, but defers cropping internals.
Examples
Demonstrates 24 × 24 crops from 32 × 32 CIFAR-10 images and an 8 × 8 grid of shifted predictions.
What it assumes
Assumes basic image dimensions, dataset loaders, and CNN evaluation while teaching crop-policy selection from implementation examples.
What you can do after
Apply cropping via PyTorch transforms and diagnose shift sensitivity by comparing predictions across manually extracted windows.
How it connects
Connects cropping with CNNs, aliasing, pooling, translation invariance, data augmentation, and deterministic evaluation.

Also explained in

Compare how 2 books explain crop →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Inside Deep Learning: Math, Algorithms, Models. The section links point to the parts of the book it draws on.