Training image classifiers

Deep Learning intermedio con PyTorch

Michal Oleszak

Machine Learning Engineer

Data augmentation revisited

Image of a cat.

Deep Learning intermedio con PyTorch

Data augmentation revisited

Image of a cat flipped horizontally and rotated.

Deep Learning intermedio con PyTorch

What should not be augmented

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

Deep Learning intermedio con PyTorch

What should not be augmented

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

Deep Learning intermedio con 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!
Deep Learning intermedio con 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))
])
Deep Learning intermedio con PyTorch

Cross-Entropy loss

  • Binary classification: binary cross-entropy (BCE) loss
  • Multi-class classification: cross-entropy loss
  • criterion = nn.CrossEntropyLoss()
Deep Learning intermedio con 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()
Deep Learning intermedio con PyTorch

Let's practice!

Deep Learning intermedio con PyTorch

Preparing Video For Download...