Image Augmentation for Deep Learning using PyTorch – Feature Engineering for Images

Well, this helps make our deep learning model more robust.

Let’s see how we can do that.

We will use a Gaussian filter for blurring the image: View the code on Gist.

Sigma here is the standard deviation for the Gaussian filter.

I have taken it as 1.

The higher the sigma value, the more will be the blurring effect.

Setting Multichannel to true ensures that each channel of the image is filtered separately.

Again, you can try different sigma values to change the magnitude of blurriness.

These are some of the image augmentation techniques which help to make our deep learning model robust and generalizable.

This also helps increase the size of the training set.

We’re almost at the implementation part of this tutorial.

Before that, let’s look at some of the basic guidelines for deciding the right image augmentation technique.

  Basic Guidelines for Selecting the Right Augmentation Technique There are a few guidelines that I think are important while deciding the augmentation technique based on the problem that you are trying to solve.

Here is a brief summary of these guidelines: The first step in any model building process is to make sure that the size of our input matches what is expected by the model.

We also have to make sure that the size of all the images should be similar.

For this, we can resize our images to the appropriate size.

Let’s say you are working on a classification problem and have relatively less number of data samples.

In such scenarios, you can use different augmentation techniques like image rotation, image noising, flipping, shift, etc.

Remember all these operations are applicable for classification problems where the location of objects in the image does not matter.

If you are working on an object detection task, where the location of objects is what we want to detect, these techniques might not be appropriate.

Normalizing image pixel values is always a good strategy to ensure better and faster convergence of the model.

If there are some specific requirements of the model, we must pre-process the images as per the model’s requirement.

Now, without waiting further, let’s move on to the model building part.

We will apply the augmentation techniques that are discussed in this article to generate images and then use those images to train the model.

  Case Study: Solving an Image Classification Problem and Applying Image Augmentation We will be working on the emergency vs non-emergency vehicle classification problem.

You should be familiar with the problem statement if you’ve gone through my previous PyTorch articles.

The aim of this project is to classify the images of vehicles as emergency or non-emergency.

And you guessed it – it’s an image classification problem.

You can download the dataset from here.

  Loading the dataset Let’s begin!.We’ll start by loading the data into our notebook.

Then, we’ll apply image augmentation techniques and finally, build a convolutional neural network (CNN) model.

Let’s import the required libraries: View the code on Gist.

Now, we will read the CSV file that contains the names of images and their corresponding labels: View the code on Gist.

0 here represents that the vehicle is non-emergency and 1 means it’s an emergency vehicle.

Let’s now import all the images from our dataset: View the code on Gist.

We have a total of 1,646 images in the dataset.

Let’s split this data into training and validation set.

We will use the validation set to evaluate how well the model will perform on unseen data: View the code on Gist.

I have kept the test_size as 0.

1 and hence 10% data will be randomly selected as the validation set and the remaining 90% will be used to train the model.

We have 1,481 images in the training set which is quite less to train a deep learning model.

So next, we will augment these training images to increase the training set and possibly improve our model’s performance.

  Augmenting the Images We will be using the image augmentation techniques we discussed earlier: View the code on Gist.

We have generated 4 augmented images for each of the 1,481 images in the training set.

Let’s convert the images in the form of an array and verify the size of our dataset: View the code on Gist.

This confirms that we have augmented the images and increased the size of our training set.

Let’s visualize these augmented images: View the code on Gist.

The first image here is the original image from the dataset.

The remaining four images are generated using different image augmentation techniques – rotation, left-to-right flip, up-down flip and adding random noise respectively.

Our dataset is now ready.

It’s time to define the architecture of our deep learning model and then train it on the augmented training set.

Let’s first import all the functions from PyTorch: View the code on Gist.

We’ll have to convert both the training and validation sets into PyTorch format: View the code on Gist.

  Similarly, we will convert the validation set: View the code on Gist.

  Model Architecture Next, we will define the architecture of the model.

This is a bit complex since the architecture has 4 convolutional blocks and then 4 fully connected dense layers: View the code on Gist.

Let’s define the other hyperparameters of the model as well, including the optimizer, learning rate, and the loss function: View the code on Gist.

  Training the Model Let’s train our deep learning model for 20 epochs: View the code on Gist.

This is a summary of the training phase.

You’ll notice that the training loss decreases as we increase the epochs.

Let’s save the weights of the trained model so we can use them in the future without retraining the model: View the code on Gist.

If you do not wish to train the model at your end, you can download the weights of the model which I trained for 20 epochs using this link.

Next, let’s load this model: View the code on Gist.

  Checking Our Model’s Performance Finally, let’s make predictions for the training and validation set and check the respective accuracy: View the code on Gist.

We got an accuracy of more than 91% on the training set!.That’s quite promising.

But let’s wait before we celebrate.

We need to check the same for the validation set: View the code on Gist.

The validation accuracy is around 78%.

That’s quite good!.  End Notes This is how we can use image augmentation techniques when we are given less training data to begin with.

In this article, we covered most of the commonly used image augmentation techniques.

We learned how to rotate, shift, and flip images.

We also learned how we can add random noise to images or blur them.

Then we discussed basic guidelines for selecting the right augmentation technique.

You can try these image augmentation techniques on any image classification problem and then compare the performance with and without augmentation.

Feel free to share your results in the comments section below.

And if you’re new to deep learning, computer vision and image data in general, I suggest going through the below course: Computer Vision using Deep Learning 2.

0 You can also read this article on Analytics Vidhyas Android APP Share this:Click to share on LinkedIn (Opens in new window)Click to share on Facebook (Opens in new window)Click to share on Twitter (Opens in new window)Click to share on Pocket (Opens in new window)Click to share on Reddit (Opens in new window) Related Articles (adsbygoogle = window.

adsbygoogle || []).

push({});.. More details

Leave a Reply