Skip to main content

Command Palette

Search for a command to run...

Utilizing Keras to Augment Images

Learn how to generate and save augmented images in Python with Keras for machine learning projects involving object recognition

Updated
3 min read
Utilizing Keras to Augment Images
S

Hello!

My name is Sirus, I am currently enrolled at Oregon State University, and pursuing my B.S. in computer science.

Before I even decided to pursue my B.S. in computer science, I had had some solid understanding in programming fundamentals from some courses I took during my first degree program, and also through self-teaching web development through Coursera, Udemy, and the Odin Project.

If you're still reading, you're probably wondering what my first degree is in, and why I chose to pursue a second degree. I graduated from the University of California, Irvine in 2021 with a B.A. in psychology. When I started college, I had no idea what I wanted to do as a career, but I just knew that I was passionate about psychology. During my third year at UCI, I took a cognitive robotics course as part of my required classes for my major, and this was the first time I was ever exposed to coding, and my first time programming. I quickly fell in love with programming, and I decided to take an intro to programming course using Python during my fourth year at UCI. I continued to love and enjoy programming and learning about all the logic involved.

Initially, my plan was to combine my interests for psychology and computer science into one by applying to PhD programs in an extremely niche field called "computational neuroscience". Essentially, this field focuses on mathematical/theoretical models of consciousness, the engineering of brain-computer-interfaces, and artificial intelligence/machine learning.

I was denied from all 8 of the PhD programs I applied to. Instead of being defeated, I decided to continue pursuing my passion for programming. I started with just toying around with web development using HTML and CSS, then learned JavaScript, started making project websites, learned a lot from a Coursera course and Udemy course on web development, and learned a lot from following the Odin Project curriculum.

Even though my passion and interest in programming never died, I found it hard to find the motivation to continue self-teaching. Ultimately, I still wanted to find a career that would involve programming. As a result, I started researching different options for people who wish to receive a second degree in computer science. I found Oregon State University as the perfect match for my needs, as it's entirely virtual while still having the same content as in-person classes, it's affordable compared to other options, and it allowed me to transfer all of my general education requirements from UCI, saving me tons of money and time on starting all over with another degree.

Introduction

In this article, we discuss how to install Keras and Skimage, and demonstrate the process of image augmentation using the ImageDataGenerator object from Keras. Image augmentation is useful for creating larger training datasets for object recognition models, such as HOG or CNN, by artificially generating new images based on the original ones. We provide step-by-step instructions for installing the required libraries, loading and reshaping images, and finally, generating augmented images.


How to install Keras

To install Keras, you have to install the TensorFlow package.

To install TensorFlow:

pip install tensorflow

After installing the TensorFlow package, you've now installed Keras onto your system.

To use Keras in your Python file:

from tensorflow import keras

How to install Skimage

To load in our image, we'll be using SciKit, but you may wish to use another library to load in your image if you'd prefer.

To install Skimage:

pip install scikit-image

Image augmentation

To start, make sure to import the required modules into your Python file:

from keras.preprocessing.image import ImageDataGenerator
from skimage import io

ImageDataGenerator object

The next step is to use the ImageDataGenerator object from Keras to define how you want the augmented image(s) to be transformed:

datagen = ImageDataGenerator(
    rotation_range=40,  # randomly rotate images in the range (degrees, 0 to 180)
    width_shift_range=0.2,  # randomly shift images horizontally (fraction of total width)
    height_shift_range=0.2,  # randomly shift images vertically (fraction of total height)
    shear_range=0.2,  # set range for random shear
    zoom_range=0.2,  # set range for random zoom
    horizontal_flip=True,  # randomly flip images
    fill_mode='nearest')  # set mode for filling points outside the input boundaries

When you set 'fill_mode' to 'nearest', any space created in the augmented images will be filled with colored pixels that match the colors of the pixels near the space.

Another option for 'fill_mode' is to set it to 'constant'. When you set it to constant, the space in the augmented images will be filled with black pixels by default. If you want to use grey or white pixels, you can do so by including a 2nd argument after 'fill_mode', like so: "fill_mode='nearest', '150'".

Load image and reshape

In this step, you'll load your image into Python. Additionally, we'll be reshaping the image array data.

input_image = io.imread('path/to/image')
input_image = input_image.reshape((1,) + input_image.shape)

Generate x augmented images

The final step is to generate your augmented images!

The following code demonstrates creating and saving 20 augmented images:

i = 0
for batch in datagen.flow(input_image, batch_size=16, save_to_dir='path/to/save/images', save_prefix='prefix', save_format='jpeg'):
        i += 1
        if i == 19:
            break

In the above code, we're calling the .flow function on our ImageDataGenerator object and passing in our input_image, a batch size of 16 (this means it will augment 16 sample images at a time), specifying the directory to save the augmented images, specifying the prefix to put at the beginning of all the file names, and finally, the file format of the augmented images.


What's the benefit of image augmentation?

If you're trying to train a histogram of oriented gradients (HOG) model or a convolutional neural network (CNN) model to do any type of object recognition, then image augmentation may be of use to you.

In particular, if you don't have a large enough training dataset of images, then you can artificially create a large dataset of training images by augmenting the original images that you already have.

By creating this image augmentation pipeline, your model will train not only on the original images contained in your training dataset but also all of the augmented images that are generated based on those original images.


a person typing on a laptop on a desk

This article is part of a series called Bit by Bit, a series devoted to all things programming. Whether you're still a computer science undergrad or the CTO of Apple, there's something for you here.

New articles in this series are posted every Tuesday!