How to do it...

So, how do we attack the high-level problem of doing image augmentation? Luckily, there are great folks out there developing awesome libraries to solve these very problems. One of my favorite libraries is a library called imgaug. This library allows you to dynamically and randomly apply transformations. Why is that advantageous? During the training process in deep learners, augmentations will force the learners to generalize. The imgaug library is going to make your life so much easier in this facet. I've got a small demo code set that we'll go over in the next section to ensure you've got a good idea of the power of this library:

  1. Import the required packages:
import imgaug as ia
from imgaug import augmenters as iaa
import numpy as np
  1. This seed can be changed-random seed:
ia.seed(1)
  1. Here we have an example batch of 100 images:
images = np.array(
[ia.quokka(size=(64, 64)) for _ in range(100)],
dtype=np.uint8
)
  1. Create the transformer function by specifying the different augmentations:
seq = iaa.Sequential([# Horizontal Flips
iaa.Fliplr(0.5),

# Random Crops
iaa.Crop(percent=(0, 0.1)),# Gaussian blur for 50% of the images
iaa.Sometimes(0.5,
iaa.GaussianBlur(sigma=(0, 0.5))
),
# Strengthen or weaken the contrast in each image.
iaa.ContrastNormalization((0.75, 1.5)),

# Add gaussian noise.
iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5),

# Make some images brighter and some darker.
iaa.Multiply((0.8, 1.2), per_channel=0.2),

  1. Apply Affine transformations to each image:
    iaa.Affine(
scale={"x": (0.5, 1.5), "y": (0.5, 1.5)},
translate_percent={"x": (-0.5, 0.5), "y": (-0.5, 0.5)},
rotate=(-10, 10),
shear=(-10, 10)
)],
  1. Apply augmenters in random order:
random_order=True) 
  1. This should display a random set of augmentations in a window:
images_aug = seq.augment_images(images)
seq.show_grid(images[0], cols=8, rows=8)

Let's go over this code in detail!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.76.138