Image Augmentation

:label: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 less rely 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.

```{.python .input} %matplotlib inline from d2l import mxnet as d2l from mxnet import autograd, gluon, image, init, np, npx from mxnet.gluon import nn

npx.set_np()

  1. ```{.python .input}
  2. #@tab pytorch
  3. %matplotlib inline
  4. from d2l import torch as d2l
  5. import torch
  6. import torchvision
  7. from torch import nn

Common Image Augmentation Methods

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

```{.python .input} d2l.set_figsize() img = image.imread(‘../img/cat1.jpg’) d2l.plt.imshow(img.asnumpy());

  1. ```{.python .input}
  2. #@tab pytorch
  3. d2l.set_figsize()
  4. img = d2l.Image.open('../img/cat1.jpg')
  5. 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.

```{.python .input}

@tab all

def apply(img, aug, numrows=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)

  1. ### Flipping and Cropping
  2. Flipping the image left and right usually does not change the category of the object.
  3. This is one of the earliest and most widely used methods of image augmentation.
  4. Next, we use the `transforms` module to create the `RandomFlipLeftRight` instance, which flips
  5. an image left and right with a 50% chance.
  6. ```{.python .input}
  7. apply(img, gluon.data.vision.transforms.RandomFlipLeftRight())

```{.python .input}

@tab pytorch

apply(img, torchvision.transforms.RandomHorizontalFlip())

  1. 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.
  2. Next, we create a `RandomFlipTopBottom` instance to flip
  3. an image up and down with a 50% chance.
  4. ```{.python .input}
  5. apply(img, gluon.data.vision.transforms.RandomFlipTopBottom())

```{.python .input}

@tab pytorch

apply(img, torchvision.transforms.RandomVerticalFlip())

  1. In the example image we used, the cat is in the middle of the image, but this may not be the case in general.
  2. In :numref:`sec_pooling`, we explained that the pooling layer can reduce the sensitivity of a convolutional layer to the target position.
  3. 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.
  4. 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.
  5. 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]$.
  6. ```{.python .input}
  7. shape_aug = gluon.data.vision.transforms.RandomResizedCrop(
  8. (200, 200), scale=(0.1, 1), ratio=(0.5, 2))
  9. apply(img, shape_aug)

```{.python .input}

@tab pytorch

shape_aug = torchvision.transforms.RandomResizedCrop( (200, 200), scale=(0.1, 1), ratio=(0.5, 2)) apply(img, shape_aug)

  1. ### Changing Colors
  2. 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.
  3. ```{.python .input}
  4. apply(img, gluon.data.vision.transforms.RandomBrightness(0.5))

```{.python .input}

@tab pytorch

apply(img, torchvision.transforms.ColorJitter( brightness=0.5, contrast=0, saturation=0, hue=0))

  1. Similarly, we can randomly change the hue of the image.
  2. ```{.python .input}
  3. apply(img, gluon.data.vision.transforms.RandomHue(0.5))

```{.python .input}

@tab pytorch

apply(img, torchvision.transforms.ColorJitter( brightness=0, contrast=0, saturation=0, hue=0.5))

  1. 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.
  2. ```{.python .input}
  3. color_aug = gluon.data.vision.transforms.RandomColorJitter(
  4. brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5)
  5. apply(img, color_aug)

```{.python .input}

@tab pytorch

color_aug = torchvision.transforms.ColorJitter( brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5) apply(img, color_aug)

  1. ### Combining Multiple Image Augmentation Methods
  2. In practice, we will combine multiple image augmentation methods.
  3. For example,
  4. we can combine the different image augmentation methods defined above and apply them to each image via a `Compose` instance.
  5. ```{.python .input}
  6. augs = gluon.data.vision.transforms.Compose([
  7. gluon.data.vision.transforms.RandomFlipLeftRight(), color_aug, shape_aug])
  8. apply(img, augs)

```{.python .input}

@tab pytorch

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

  1. ## Training with Image Augmentation
  2. Let us train a model with image augmentation.
  3. Here we use the CIFAR-10 dataset instead of the Fashion-MNIST dataset that we used before.
  4. 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.
  5. The first 32 training images in the CIFAR-10 dataset are shown below.
  6. ```{.python .input}
  7. d2l.show_images(gluon.data.vision.CIFAR10(
  8. train=True)[0:32][0], 4, 8, scale=0.8);

```{.python .input}

@tab pytorch

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);

  1. In order to obtain a definite result when predicting, we usually only apply image augmentation to training samples, and do not use image augmentation with random operations when predicting. Here we only use the simplest random left and right flip. In addition, we use the ToTensor instance to convert the small batch of images into the format required by MXNet, that is, the shape is (batch size, number of channels, height, width), the value range is between 0 and 1, and the type is a 32-bit floating point number.
  2. In order to obtain definitive results during prediction, we usually only apply image augmentation to the training example, and do not use image augmentation with random operations during prediction.
  3. 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.,
  4. 32-bit floating point numbers between 0 and 1 with the shape of (batch size, number of channels, height, width).
  5. ```{.python .input}
  6. train_augs = gluon.data.vision.transforms.Compose([
  7. gluon.data.vision.transforms.RandomFlipLeftRight(),
  8. gluon.data.vision.transforms.ToTensor()])
  9. test_augs = gluon.data.vision.transforms.Compose([
  10. gluon.data.vision.transforms.ToTensor()])

```{.python .input}

@tab pytorch

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

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

  1. :begin_tab:`mxnet`
  2. Next, we define an auxiliary function to facilitate reading the image and
  3. applying image augmentation.
  4. The `transform_first` function provided by Gluon's
  5. datasets applies image augmentation to the first element of each training
  6. example (image and label), i.e., the image.
  7. For
  8. a detailed introduction to `DataLoader`, please refer to :numref:`sec_fashion_mnist`.
  9. :end_tab:
  10. :begin_tab:`pytorch`
  11. Next, we define an auxiliary function to facilitate reading the image and
  12. applying image augmentation.
  13. The `transform` argument provided by PyTorch's
  14. dataset applies augmentation to transform the images.
  15. For
  16. a detailed introduction to `DataLoader`, please refer to :numref:`sec_fashion_mnist`.
  17. :end_tab:
  18. ```{.python .input}
  19. def load_cifar10(is_train, augs, batch_size):
  20. return gluon.data.DataLoader(
  21. gluon.data.vision.CIFAR10(train=is_train).transform_first(augs),
  22. batch_size=batch_size, shuffle=is_train,
  23. num_workers=d2l.get_dataloader_workers())

```{.python .input}

@tab pytorch

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

  1. ### Multi-GPU Training
  2. We train the ResNet-18 model from
  3. :numref:`sec_resnet` on the
  4. CIFAR-10 dataset.
  5. Recall the introduction to
  6. multi-GPU training in :numref:`sec_multi_gpu_concise`.
  7. In the following,
  8. we define a function to train and evaluate the model using multiple GPUs.
  9. ```{.python .input}
  10. #@save
  11. def train_batch_ch13(net, features, labels, loss, trainer, devices,
  12. split_f=d2l.split_batch):
  13. X_shards, y_shards = split_f(features, labels, devices)
  14. with autograd.record():
  15. pred_shards = [net(X_shard) for X_shard in X_shards]
  16. ls = [loss(pred_shard, y_shard) for pred_shard, y_shard
  17. in zip(pred_shards, y_shards)]
  18. for l in ls:
  19. l.backward()
  20. # The `True` flag allows parameters with stale gradients, which is useful
  21. # later (e.g., in fine-tuning BERT)
  22. trainer.step(labels.shape[0], ignore_stale_grad=True)
  23. train_loss_sum = sum([float(l.sum()) for l in ls])
  24. train_acc_sum = sum(d2l.accuracy(pred_shard, y_shard)
  25. for pred_shard, y_shard in zip(pred_shards, y_shards))
  26. return train_loss_sum, train_acc_sum

```{.python .input}

@tab pytorch

@save

def train_batch_ch13(net, X, y, loss, trainer, devices): if isinstance(X, list):

  1. # Required for BERT fine-tuning (to be covered later)
  2. X = [x.to(devices[0]) for x in X]
  3. else:
  4. X = X.to(devices[0])
  5. y = y.to(devices[0])
  6. net.train()
  7. trainer.zero_grad()
  8. pred = net(X)
  9. l = loss(pred, y)
  10. l.sum().backward()
  11. trainer.step()
  12. train_loss_sum = l.sum()
  13. train_acc_sum = d2l.accuracy(pred, y)
  14. return train_loss_sum, train_acc_sum
  1. ```{.python .input}
  2. #@save
  3. def train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs,
  4. devices=d2l.try_all_gpus(), split_f=d2l.split_batch):
  5. timer, num_batches = d2l.Timer(), len(train_iter)
  6. animator = d2l.Animator(xlabel='epoch', xlim=[1, num_epochs], ylim=[0, 1],
  7. legend=['train loss', 'train acc', 'test acc'])
  8. for epoch in range(num_epochs):
  9. # Sum of training loss, sum of training accuracy, no. of examples,
  10. # no. of predictions
  11. metric = d2l.Accumulator(4)
  12. for i, (features, labels) in enumerate(train_iter):
  13. timer.start()
  14. l, acc = train_batch_ch13(
  15. net, features, labels, loss, trainer, devices, split_f)
  16. metric.add(l, acc, labels.shape[0], labels.size)
  17. timer.stop()
  18. if (i + 1) % (num_batches // 5) == 0 or i == num_batches - 1:
  19. animator.add(epoch + (i + 1) / num_batches,
  20. (metric[0] / metric[2], metric[1] / metric[3],
  21. None))
  22. test_acc = d2l.evaluate_accuracy_gpus(net, test_iter, split_f)
  23. animator.add(epoch + 1, (None, None, test_acc))
  24. print(f'loss {metric[0] / metric[2]:.3f}, train acc '
  25. f'{metric[1] / metric[3]:.3f}, test acc {test_acc:.3f}')
  26. print(f'{metric[2] * num_epochs / timer.sum():.1f} examples/sec on '
  27. f'{str(devices)}')

```{.python .input}

@tab pytorch

@save

def train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs, devices=d2l.try_all_gpus()): 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):

  1. # Sum of training loss, sum of training accuracy, no. of examples,
  2. # no. of predictions
  3. metric = d2l.Accumulator(4)
  4. for i, (features, labels) in enumerate(train_iter):
  5. timer.start()
  6. l, acc = train_batch_ch13(
  7. net, features, labels, loss, trainer, devices)
  8. metric.add(l, acc, labels.shape[0], labels.numel())
  9. timer.stop()
  10. if (i + 1) % (num_batches // 5) == 0 or i == num_batches - 1:
  11. animator.add(epoch + (i + 1) / num_batches,
  12. (metric[0] / metric[2], metric[1] / metric[3],
  13. None))
  14. test_acc = d2l.evaluate_accuracy_gpu(net, test_iter)
  15. animator.add(epoch + 1, (None, None, test_acc))
  16. print(f'loss {metric[0] / metric[2]:.3f}, train acc '
  17. f'{metric[1] / metric[3]:.3f}, test acc {test_acc:.3f}')
  18. print(f'{metric[2] * num_epochs / timer.sum():.1f} examples/sec on '
  19. f'{str(devices)}')
  1. Now we can define the `train_with_data_aug` function to train the model with image augmentation.
  2. This function gets all available GPUs,
  3. uses Adam as the optimization algorithm,
  4. applies image augmentation to the training dataset,
  5. and finally calls the `train_ch13` function just defined to train and evaluate the model.
  6. ```{.python .input}
  7. batch_size, devices, net = 256, d2l.try_all_gpus(), d2l.resnet18(10)
  8. net.initialize(init=init.Xavier(), ctx=devices)
  9. def train_with_data_aug(train_augs, test_augs, net, lr=0.001):
  10. train_iter = load_cifar10(True, train_augs, batch_size)
  11. test_iter = load_cifar10(False, test_augs, batch_size)
  12. loss = gluon.loss.SoftmaxCrossEntropyLoss()
  13. trainer = gluon.Trainer(net.collect_params(), 'adam',
  14. {'learning_rate': lr})
  15. train_ch13(net, train_iter, test_iter, loss, trainer, 10, devices)

```{.python .input}

@tab pytorch

batch_size, devices, net = 256, d2l.try_all_gpus(), d2l.resnet18(10, 3)

def initweights(m): if type(m) in [nn.Linear, nn.Conv2d]: nn.init.xavier_uniform(m.weight)

net.apply(init_weights)

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) train_ch13(net, train_iter, test_iter, loss, trainer, 10, devices)

  1. Let us train the model using image augmentation based on random left-right flipping.
  2. ```{.python .input}
  3. #@tab all
  4. train_with_data_aug(train_augs, test_augs, net)

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?

:begin_tab:mxnet Discussions :end_tab:

:begin_tab:pytorch Discussions :end_tab: