Files
2024-08-20 16:25:10 +08:00

1.8 MiB

Image Augmentation

🏷️sec_image_augmentation

In :numref:sec_alexnet, we mentioned that large datasets are a prerequisite for the success of deep neural networks in various applications. Image augmentation generates similar but distinct training examples after a series of random changes to the training images, thereby expanding the size of the training set. Alternatively, image augmentation can be motivated by the fact that random tweaks of training examples allow models to rely less on certain attributes, thereby improving their generalization ability. For example, we can crop an image in different ways to make the object of interest appear in different positions, thereby reducing the dependence of a model on the position of the object. We can also adjust factors such as brightness and color to reduce a model's sensitivity to color. It is probably true that image augmentation was indispensable for the success of AlexNet at that time. In this section we will discuss this widely used technique in computer vision.

In [1]:
%matplotlib inline
import torch
import torchvision
from torch import nn
from d2l import torch as d2l

Common Image Augmentation Methods

In our investigation of common image augmentation methods, we will use the following 400\times 500 image an example.

In [2]:
d2l.set_figsize()
img = d2l.Image.open('../img/cat1.jpg')
d2l.plt.imshow(img);

Most image augmentation methods have a certain degree of randomness. To make it easier for us to observe the effect of image augmentation, next we define an auxiliary function apply. This function runs the image augmentation method aug multiple times on the input image img and shows all the results.

In [3]:
def apply(img, aug, num_rows=2, num_cols=4, scale=1.5):
    Y = [aug(img) for _ in range(num_rows * num_cols)]
    d2l.show_images(Y, num_rows, num_cols, scale=scale)

Flipping and Cropping

[Flipping the image left and right] usually does not change the category of the object. This is one of the earliest and most widely used methods of image augmentation. Next, we use the transforms module to create the RandomHorizontalFlip instance, which flips an image left and right with a 50% chance.

In [4]:
apply(img, torchvision.transforms.RandomHorizontalFlip())

[Flipping up and down] is not as common as flipping left and right. But at least for this example image, flipping up and down does not hinder recognition. Next, we create a RandomVerticalFlip instance to flip an image up and down with a 50% chance.

In [5]:
apply(img, torchvision.transforms.RandomVerticalFlip())

In the example image we used, the cat is in the middle of the image, but this may not be the case in general. In :numref:sec_pooling, we explained that the pooling layer can reduce the sensitivity of a convolutional layer to the target position. In addition, we can also randomly crop the image to make objects appear in different positions in the image at different scales, which can also reduce the sensitivity of a model to the target position.

In the code below, we [randomly crop] an area with an area of 10\% \sim 100\% of the original area each time, and the ratio of width to height of this area is randomly selected from 0.5 \sim 2. Then, the width and height of the region are both scaled to 200 pixels. Unless otherwise specified, the random number between a and b in this section refers to a continuous value obtained by random and uniform sampling from the interval [a, b].

In [6]:
shape_aug = torchvision.transforms.RandomResizedCrop(
    (200, 200), scale=(0.1, 1), ratio=(0.5, 2))
apply(img, shape_aug)

Changing Colors

Another augmentation method is changing colors. We can change four aspects of the image color: brightness, contrast, saturation, and hue. In the example below, we [randomly change the brightness] of the image to a value between 50% (1-0.5) and 150% (1+0.5) of the original image.

In [7]:
apply(img, torchvision.transforms.ColorJitter(
    brightness=0.5, contrast=0, saturation=0, hue=0))

Similarly, we can [randomly change the hue] of the image.

In [8]:
apply(img, torchvision.transforms.ColorJitter(
    brightness=0, contrast=0, saturation=0, hue=0.5))

We can also create a RandomColorJitter instance and set how to [randomly change the brightness, contrast, saturation, and hue of the image at the same time].

In [9]:
color_aug = torchvision.transforms.ColorJitter(
    brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5)
apply(img, color_aug)

Combining Multiple Image Augmentation Methods

In practice, we will [combine multiple image augmentation methods]. For example, we can combine the different image augmentation methods defined above and apply them to each image via a Compose instance.

In [10]:
augs = torchvision.transforms.Compose([
    torchvision.transforms.RandomHorizontalFlip(), color_aug, shape_aug])
apply(img, augs)

[Training with Image Augmentation]

Let's train a model with image augmentation. Here we use the CIFAR-10 dataset instead of the Fashion-MNIST dataset that we used before. This is because the position and size of the objects in the Fashion-MNIST dataset have been normalized, while the color and size of the objects in the CIFAR-10 dataset have more significant differences. The first 32 training images in the CIFAR-10 dataset are shown below.

In [11]:
all_images = torchvision.datasets.CIFAR10(train=True, root="../data",
                                          download=True)
d2l.show_images([all_images[i][0] for i in range(32)], 4, 8, scale=0.8);
Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to ../data/cifar-10-python.tar.gz
  0%|          | 0/170498071 [00:00<?, ?it/s]
  0%|          | 65536/170498071 [00:00<06:06, 465602.77it/s]
  0%|          | 229376/170498071 [00:00<03:15, 870075.02it/s]
  1%|          | 917504/170498071 [00:00<01:03, 2680566.44it/s]
  2%|▏         | 3604480/170498071 [00:00<00:18, 9055226.73it/s]
  6%|▌         | 9601024/170498071 [00:00<00:06, 23469988.96it/s]
  7%|▋         | 12779520/170498071 [00:00<00:06, 24635235.07it/s]
 11%|█         | 18087936/170498071 [00:00<00:04, 32644362.62it/s]
 13%|█▎        | 22085632/170498071 [00:00<00:04, 33766951.34it/s]
 15%|█▌        | 25919488/170498071 [00:01<00:04, 34874971.42it/s]
 18%|█▊        | 31424512/170498071 [00:01<00:03, 39206606.36it/s]
 21%|██        | 35454976/170498071 [00:01<00:03, 38026146.88it/s]
 24%|██▍       | 40730624/170498071 [00:01<00:03, 42003142.31it/s]
 26%|██▋       | 45023232/170498071 [00:01<00:03, 39377573.03it/s]
 29%|██▉       | 50266112/170498071 [00:01<00:02, 42853627.52it/s]
 32%|███▏      | 54657024/170498071 [00:01<00:02, 40134646.49it/s]
 35%|███▌      | 59998208/170498071 [00:01<00:02, 43645137.55it/s]
 38%|███▊      | 64487424/170498071 [00:02<00:02, 40692269.19it/s]
 41%|████      | 69599232/170498071 [00:02<00:02, 43277945.93it/s]
 43%|████▎     | 74022912/170498071 [00:02<00:02, 40757798.12it/s]
 46%|████▋     | 78970880/170498071 [00:02<00:02, 42534999.51it/s]
 49%|████▉     | 83296256/170498071 [00:02<00:02, 40565223.57it/s]
 52%|█████▏    | 88408064/170498071 [00:02<00:01, 43070011.08it/s]
 54%|█████▍    | 92798976/170498071 [00:02<00:01, 40641250.05it/s]
 57%|█████▋    | 97943552/170498071 [00:02<00:01, 43576448.82it/s]
 60%|██████    | 102400000/170498071 [00:02<00:01, 40609636.82it/s]
 63%|██████▎   | 107577344/170498071 [00:03<00:01, 43606965.98it/s]
 66%|██████▌   | 112033792/170498071 [00:03<00:01, 40736940.01it/s]
 69%|██████▊   | 117112832/170498071 [00:03<00:01, 43233593.74it/s]
 71%|███████▏  | 121536512/170498071 [00:03<00:01, 40710642.41it/s]
 74%|███████▍  | 126615552/170498071 [00:03<00:01, 43290286.82it/s]
 77%|███████▋  | 131039232/170498071 [00:03<00:00, 40679159.83it/s]
 80%|███████▉  | 136118272/170498071 [00:03<00:00, 43195601.76it/s]
 82%|████████▏ | 140541952/170498071 [00:03<00:00, 40621722.11it/s]
 85%|████████▌ | 145555456/170498071 [00:03<00:00, 43050443.46it/s]
 88%|████████▊ | 149946368/170498071 [00:04<00:00, 40446858.58it/s]
 91%|█████████ | 155123712/170498071 [00:04<00:00, 43392878.70it/s]
 94%|█████████▎| 159547392/170498071 [00:04<00:00, 40552440.46it/s]
 97%|█████████▋| 164823040/170498071 [00:04<00:00, 43601133.14it/s]
 99%|█████████▉| 169279488/170498071 [00:04<00:00, 40752923.51it/s]
100%|██████████| 170498071/170498071 [00:04<00:00, 37716809.52it/s]
Extracting ../data/cifar-10-python.tar.gz to ../data

In order to obtain definitive results during prediction, we usually only apply image augmentation to training examples, and do not use image augmentation with random operations during prediction. [Here we only use the simplest random left-right flipping method]. In addition, we use a ToTensor instance to convert a minibatch of images into the format required by the deep learning framework, i.e., 32-bit floating point numbers between 0 and 1 with the shape of (batch size, number of channels, height, width).

In [12]:
train_augs = torchvision.transforms.Compose([
     torchvision.transforms.RandomHorizontalFlip(),
     torchvision.transforms.ToTensor()])

test_augs = torchvision.transforms.Compose([
     torchvision.transforms.ToTensor()])

Next, we [define an auxiliary function to facilitate reading the image and applying image augmentation]. The transform argument provided by PyTorch's dataset applies augmentation to transform the images. For a detailed introduction to DataLoader, please refer to :numref:sec_fashion_mnist.

In [13]:
def load_cifar10(is_train, augs, batch_size):
    dataset = torchvision.datasets.CIFAR10(root="../data", train=is_train,
                                           transform=augs, download=True)
    dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size,
                    shuffle=is_train, num_workers=d2l.get_dataloader_workers())
    return dataloader

Multi-GPU Training

We train the ResNet-18 model from :numref:sec_resnet on the CIFAR-10 dataset. Recall the introduction to multi-GPU training in :numref:sec_multi_gpu_concise. In the following, [we define a function to train and evaluate the model using multiple GPUs].

In [14]:
#@save
def train_batch_ch13(net, X, y, loss, trainer, devices):
    """Train for a minibatch with multiple GPUs (defined in Chapter 13)."""
    if isinstance(X, list):
        # Required for BERT fine-tuning (to be covered later)
        X = [x.to(devices[0]) for x in X]
    else:
        X = X.to(devices[0])
    y = y.to(devices[0])
    net.train()
    trainer.zero_grad()
    pred = net(X)
    l = loss(pred, y)
    l.sum().backward()
    trainer.step()
    train_loss_sum = l.sum()
    train_acc_sum = d2l.accuracy(pred, y)
    return train_loss_sum, train_acc_sum
In [15]:
#@save
def train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs,
               devices=d2l.try_all_gpus()):
    """Train a model with multiple GPUs (defined in Chapter 13)."""
    timer, num_batches = d2l.Timer(), len(train_iter)
    animator = d2l.Animator(xlabel='epoch', xlim=[1, num_epochs], ylim=[0, 1],
                            legend=['train loss', 'train acc', 'test acc'])
    net = nn.DataParallel(net, device_ids=devices).to(devices[0])
    for epoch in range(num_epochs):
        # Sum of training loss, sum of training accuracy, no. of examples,
        # no. of predictions
        metric = d2l.Accumulator(4)
        for i, (features, labels) in enumerate(train_iter):
            timer.start()
            l, acc = train_batch_ch13(
                net, features, labels, loss, trainer, devices)
            metric.add(l, acc, labels.shape[0], labels.numel())
            timer.stop()
            if (i + 1) % (num_batches // 5) == 0 or i == num_batches - 1:
                animator.add(epoch + (i + 1) / num_batches,
                             (metric[0] / metric[2], metric[1] / metric[3],
                              None))
        test_acc = d2l.evaluate_accuracy_gpu(net, test_iter)
        animator.add(epoch + 1, (None, None, test_acc))
    print(f'loss {metric[0] / metric[2]:.3f}, train acc '
          f'{metric[1] / metric[3]:.3f}, test acc {test_acc:.3f}')
    print(f'{metric[2] * num_epochs / timer.sum():.1f} examples/sec on '
          f'{str(devices)}')

Now we can [define the train_with_data_aug function to train the model with image augmentation]. This function gets all available GPUs, uses Adam as the optimization algorithm, applies image augmentation to the training dataset, and finally calls the train_ch13 function just defined to train and evaluate the model.

In [16]:
batch_size, devices, net = 256, d2l.try_all_gpus(), d2l.resnet18(10, 3)
net.apply(d2l.init_cnn)

def train_with_data_aug(train_augs, test_augs, net, lr=0.001):
    train_iter = load_cifar10(True, train_augs, batch_size)
    test_iter = load_cifar10(False, test_augs, batch_size)
    loss = nn.CrossEntropyLoss(reduction="none")
    trainer = torch.optim.Adam(net.parameters(), lr=lr)
    net(next(iter(train_iter))[0])
    train_ch13(net, train_iter, test_iter, loss, trainer, 10, devices)

Let's [train the model] using image augmentation based on random left-right flipping.

In [17]:
train_with_data_aug(train_augs, test_augs, net)
loss 0.215, train acc 0.925, test acc 0.810
4728.8 examples/sec on [device(type='cuda', index=0), device(type='cuda', index=1)]

Summary

  • Image augmentation generates random images based on existing training data to improve the generalization ability of models.
  • In order to obtain definitive results during prediction, we usually only apply image augmentation to training examples, and do not use image augmentation with random operations during prediction.
  • Deep learning frameworks provide many different image augmentation methods, which can be applied simultaneously.

Exercises

  1. Train the model without using image augmentation: train_with_data_aug(test_augs, test_augs). Compare training and testing accuracy when using and not using image augmentation. Can this comparative experiment support the argument that image augmentation can mitigate overfitting? Why?
  2. Combine multiple different image augmentation methods in model training on the CIFAR-10 dataset. Does it improve test accuracy?
  3. Refer to the online documentation of the deep learning framework. What other image augmentation methods does it also provide?