Boosting the CNN classifier with data augmentation
Data augmentation means expanding the size of an existing training dataset in order to improve the generalization performance. It overcomes the cost involved in collecting and labeling more data. In PyTorch, we use the torchvision.transforms
module to implement image augmentation in real time.
Flipping for data augmentation
There are many ways to augment image data. The simplest one is probably flipping an image horizontally or vertically. For instance, we will have a new image if we flip an existing image horizontally. To create a horizontally flipped image, we utilize transforms.functional.hflip
, as follows:
>>> image = images[1]
>>> img_flipped = transforms.functional.hflip(image)
Let’s take a look at the flipped image:
>>> def display_image_greys(image):
npimg = image.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)), cmap="Greys")
plt.xticks([])
...