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 × 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

