
16 Chapter 2. Data Augmentation
Instead, we’ll be focusing on the augmentation parameters you will most likely use in your
own applications. The
rotation_range
parameter controls the degree range of the random
rotations. Here we’ll allow our input image to be randomly rotated
±30
degrees. Both the
width_shift_range
and
height_shift_range
are used for horizontal and vertical shifts, re-
spectively. The parameter value is a fraction of the given dimension, in this case, 10%.
The
shear_range
controls the angle in counterclockwise direction as radians in which our
image will allowed to be sheared. We then have the
zoom_range
, a floating point value that allows
the image to be “zoomed in” or “zoomed out” according to the following uniform distribution of
values: [1 - zoom_range, 1 + zoom_range].
Finally, the horizontal_flip boolean controls whether or not a given input is allowed to be
flipped horizontally during the training process. For most computer vision applications a horizontal
flip of an image does not change the resulting class label – but there are applications where a
horizontal (or vertical) flip does change the semantic meaning of the image. Take care when
applying this type of data augmentation as our goal is to slightly modify the input image, thereby
generating a new training sample, without changing the class label itself. For a more detailed review
of image transformations, please refer to Module #1 in PyImageSearch Gurus ([8], PyImageSearch
Gurus) as well as Szeliski [9].
Once ImageDataGenerator is initialized, we can actually generate new training examples:
32 # construct the actual Python generator
33 print("[INFO] generating images...")
34 imageGen = aug.flow(image, batch_size=1, save_to_dir=args["output"],
35 save_prefix=args["prefix"], save_format="jpg")
36
37 # loop over examples from our image data augmentation generator
38 for image in imageGen:
39 # increment our counter
40 total += 1
41
42 # if we have reached 10 examples, break from the loop
43 if total == 10:
44 break
Lines 34 and 35
initialize a Python generator used to construct our augmented images. We’ll
pass in our input
image
, a
batch_size
of
1
(since we are only augmenting one image), along with
a few additional parameters to specify the output image file paths, the prefix for each file path,
and the image file format.
Line 38
then starts looping over each
image
in the
imageGen
generator.
Internally,
imageGen
is automatically generating a new training sample each time one is requested
via the loop. We then increment the total number of data augmentation examples written to disk
and stop the script from executing once we’ve reached ten examples.
To visualize data augmentation in action, we’ll be using Figure 2.2 (left), an image of Jemma,
my family beagle. To generate new training example images of Jemma, just execute the following
command:
$ python augmentation_demo.py --image jemma.png --output output
After the script executes you should see ten images in the output directory:
$ ls output/
image_0_1227.jpg image_0_2358.jpg image_0_4205.jpg image_0_4770.jpg