Training image classifiers

Intermediate Deep Learning with PyTorch

Michal Oleszak

Machine Learning Engineer

Data augmentation revisited

Image of a cat.

Intermediate Deep Learning with PyTorch

Data augmentation revisited

Image of a cat flipped horizontally and rotated.

Intermediate Deep Learning with PyTorch

What should not be augmented

Yellow lemon and green lime look the same, only differ in color.

Intermediate Deep Learning with PyTorch

What should not be augmented

Yellow lemon and green lime look the same, only differ in color.

Intermediate Deep Learning with PyTorch

What should not be augmented

The letter "W" after vertical flip looks like the letter "M".

  • Augmentations can impact the label
  • Whether this is confusing depends on the task
  • Always choose augmentations with the data and task in mind!
Intermediate Deep Learning with PyTorch

Augmentations for cloud classification

A sample of cloud pictures.

  • Random rotation: expose model to different angles of cloud formations
  • Horizontal flip: simulate different viewpoints of the sky
  • Auto contrast adjustment: simulate different lighting conditions
train_transforms = transforms.Compose([
    transforms.RandomHorizontalFlip(),
    transforms.RandomRotation(45),
    transforms.RandomAutocontrast(),
    transforms.ToTensor(),
    transforms.Resize((128, 128))
])
Intermediate Deep Learning with PyTorch

Cross-Entropy loss

  • Binary classification: binary cross-entropy (BCE) loss
  • Multi-class classification: cross-entropy loss
  • criterion = nn.CrossEntropyLoss()
Intermediate Deep Learning with PyTorch

Image classifier training loop

net = Net(num_classes=7)

criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters(), lr=0.001)


for epoch in range(10): for images, labels in dataloader_train: optimizer.zero_grad() outputs = net(images) loss = criterion(outputs, labels) loss.backward() optimizer.step()
Intermediate Deep Learning with PyTorch

Let's practice!

Intermediate Deep Learning with PyTorch

Preparing Video For Download...