# Using data augmentation ## Table of contents
1. Introduction Data augmentation has proved an effective technique to reduce the overfit of a network and make it generalize better. It is generally useful when you have a small dataset or a dataset that is too easy for the network to learn. A rich set of functions are available in the Model Zoo to enable you to develop effective data augmentation solutions for your applications. All functions were written to run efficiently with Tensorflow using GPU resources. The data augmentation transforms you want to apply to the images are specified in the YAML configuration file. The transforms are only applied to the images during training. They are not applied when the model is evaluated or quantized. The Model Zoo code can be customized to implement your own data augmentation. In particular, *dynamic data augmentation*, i.e. data augmentation that changes as the training progresses instead of staying the same from beginning to end, can be easily implemented.
2. Specifying your data augmentation The data augmentation transforms to apply to the input images are specified in the configuration file using a `data_augmentation` section, as illustrated in the YAML code below: ```yaml data_augmentation: random_flip: mode: horizontal_and_vertical random_translation: width_factor: 0.2 height_factor: 0.1 random_contrast: factor: 0.4 random_brightness: factor: 0.3 ``` In this example, the following transforms are successively applied to the input images in their order of appearance in the configuration file: 1. Random flip 2. Random translation 3. Random contrast adjustment 4. Random brightness adjustment These transforms are performed by functions from the Model Zoo that are called successively as follows: ```python images = random_flip(input_images, mode="horizontal_and_vertical") images = random_translation(images, width_factor=0.2, height_factor=0.1) images = random_contrast(images, factor=0.4) images = random_brightness(images, factor=0.3) return images ``` As it can be seen, the names of the data augmentation transforms and their attributes that are used in the configuration file are names of functions and arguments. From now on, we will use the term "function" instead of "transform". If an argument of a function is not specified in the configuration file, its default value is used. For example, the `random_translation()` function is defined as follows: ```python def random_translation( images, width_factor=None, height_factor=None, fill_mode='reflect', interpolation='bilinear', fill_value=0.0, change_rate=1.0): ``` In the YAML code above, only the `width_factor` and `height_factor` arguments of the `random_translation()` function are mentioned. Therefore, the `fill_mode`, `interpolation`, `fill_value` and `change_rate` arguments will take their default values, which respectively are 'reflect', 'bilinear', 0.0 and 1.0. There are no constraints on the number of functions, types of functions and order of functions that you can use in your configuration file. However, as the YAML language does not support multiple occurrences of the same attribute in the same section, each function can only be used once.
3. Available data augmentation functions Four packages of data augmentation functions are provided with the Model Zoo: - `random_color.py`, contains functions that alter color features, such as contrast and brightness. - `random_affine.py`, contains functions that apply affine transformations to images, such as flip and rotation. - `random_erasing.py`, contains functions that erase rectangles from images. - `random_misc.py`, contains a number of miscellaneous functions. The available data augmentation functions are listed in the table below. | Package | Function | Changes made to input images | |:---------|:------|:----------------------| | random_color.py | random_contrast | Adjust contrast | | random_color.py | random_brightness | Adjust brightness | | random_color.py | random_gamma_adjust | Adjust gamma | | random_color.py | random_hue | Adjust hue | | random_color.py | random_saturation | Adjust saturation | | random_color.py | random_value | Adjust value | | random_color.py | random_hsv | Adjust hue, saturation and value simultaneously | | random_color.py | random_rgb_to_hsv | Convert from RGB representation to HSV (Hue, Saturation, Value) representation | | random_color.py | random_rgb_to_grayscale | Convert from RGB to grayscale | | random_color.py | random_sharpness | Adjust sharpness | | random_color.py | random_posterize | Change the number of bits used to encode colors | | random_color.py | random_invert | Invert pixels | | random_color.py | random_solarize | Invert pixels with a value above a given threshold | | random_color.py | random_equalize | Equalize images | | random_color.py | random_autocontrast | Maximize the contrast of images | | random_misc.py | random_blur | Blur images | | random_misc.py | random_gaussian_noise | Add gaussian noise to images | | random_misc.py | random_jpeg_quality | Adjust JPEG quality | | random_affine.py | random_flip | Flip images in horizontal and/or vertical directions | | random_affine.py | random_translation | Translate images in horizontal and/or vertical directions | | random_affine.py | random_rotation | Rotate images | | random_affine.py | random_zoom | zoom in/out | | random_affine.py | random_shear | Shear images | | random_affine.py | random_shear_x | Shear images along the x axis only | | random_affine.py | random_shear_y | Shear images along the y axis only | | random_erasing.py | random_rectangle_erasing | Erase rectangles from images | | random_erasing.py | random_grid_cell_erasing | Partition images in a grid of cells and erase some of them | | random_misc.py | random_periodic_resizing | Periodically resize images to a set of random sizes| These functions and their arguments are documented in the exhibits of this document. You can also refer to the source code of the data augmentation packages. They are all in the *\/image_classification/src/data_augmentation* directory. Comments are included at the top of each function that explain what the function does and how its arguments should be used.
4. Getting familiar with the available data augmentation functions A script called `demo_data_augment.py` is available in the *\/image_classification/src/data_augmentation* directory that you can use to get familiar with the available data augmentation functions. This script samples a number of images from a dataset of your choice and applies each data augmentation function to them. For each function, it displays before/after images side-by-side that show the effect of the function on the input images. To run the script, change the current directory to *\/image_classification/src/data_augmentation* and execute `demo_data_augmentation.py`. The arguments the script takes are listed in the table below. | Argument | Type | Default | Usage | |:---------|:------|:------|:----------------------| | dataset | String | None | Path to the root directory of a dataset in the imagenet format (such as Flowers or PlantLeafDiseases) or a path to a directory containing images | | num_images | Integer | 8 | Number of images to sample, augment and display | | image_size | Tuple of 2 integers | (224, 224) | Image resizing size | | interpolation | String | "bilinear" | Image resizing method, one of {"bilinear", "nearest", "bicubic", "area", "lanczos3", "lanczos5", "gaussian", "mitchellcubic"} | | rescaling | Tuple of 2 floats | (1/127.5, -1.0) | Image rescaling scale and offset | | seed | integer | None | Seed to use to sample the images from the dataset | | grayscale | N/A | N/A | Argument with no value. If present, images from the dataset are converted to grayscale before being augmented and display. | Note that some functions are not applicable to grayscale images. For example, the `random_hue` function that changes the hue of images does not make sense for grayscale images. A random generator is used to sample the images from the dataset. By default, different images will be selected every time you run the script. If you want to run the script multiple times with the same set of images, then set the seed argument to an integer value. If you use different seed values, different sets of images will be sampled. We encourage you to run this script as it is an efficient way to learn about the data augmentation functions that are provided with the Model Zoo.
5. Setting the rate of change Each data augmentation function has an argument called `change_rate` that enables you to control the average percentage of images that get changed by the function. For example, if you set the `change_rate` argument of a given function to 0.25, the function will change 25% of the input images on average, leaving 75% unchanged. If you set it to 1.0, all the images will be changed by the function. If you set it to 0.0, no images will be changed (the function has no effect). The default value of `change_rate` is set to 1.0 for all the data augmentation functions to the exception of the functions listed in the table below. | Function | Default `change_rate` value | |:---------|:------| | random_flip | 0.5 | | random_rgb_to_hsv | 0.25 | | random_rgb_to_grayscale | 0.25 | | random_invert | 0.25 | | random_solarize | 0.25 | | random_equalize | 0.25 | | random_autocontrast | 0.25 |
6. Testing your data augmentation As you work on your data augmentation, we strongly recommend that you carefully examine the effects it has on the images of your dataset. Some transformations may be too aggressive, some others not enough. If you use too much data augmentation, you may create images that are not representative of your dataset, or simply don't make sense. If you don't use enough data augmentation, you may not create enough image diversity to have a significant impact on the overfit and accuracy. Experimenting is key. A script called `test_data_augment.py` is available in the *\/image_classification/src/data_augmentation* directory that you can use to visualize the effects on images of the data augmentation you specified in your configuration file. This scripts samples a number of images from the dataset, applies the data augmentation functions to them, and displays before/after images side-by-side. The script reads the `dataset`, `preprocessing` and `data_augmentation` sections of the configuration file. If a `training` section is present, it uses it to obtain the model input shape and derives the image size from it. If there is no `training` section, the '--image_size' argument is available to specify the image size. The `test_data_augment.py` script takes the following arguments: | Argument | Type | Default | Usage | |:---------|:------|:------|:----------------------| | config_file | String | ../user_config.yaml | Path to the YAML configuration file | | num_images | Integer | 8 | Number of images to sample from the dataset and augment | | image_size | Tuple of 2 integers | None | Image size, used to specify the image size when the model input shape is not available in the configuration file | | seed | integer | None | Seed to use to sample the images from the dataset | A random generator is used to sample the images from the dataset. By default, different images will be selected every time you run the script. If you want to always use the same set of images, then set the seed argument to an integer value. If you use different seed values, different sets of images will be sampled. We encourage you to run the `test_data_augment.py` script. No extra work is required to use it as it only needs your configuration file. It could prove instrumental in helping you develop effective data augmentation solutions.
7. Customizing the data augmentation
Exhibit A: Data augmentation functions of the random_color.py package
Exhibit B: Data augmentation functions of the random_misc.py package
Exhibit C: Data augmentation functions of the random_affine.py package
Exhibit D: Data augmentation functions of the random_erasing.py package