Reader small image

You're reading from  Deep Learning Quick Reference

Product typeBook
Published inMar 2018
Reading LevelExpert
PublisherPackt
ISBN-139781788837996
Edition1st Edition
Languages
Right arrow
Author (1)
Mike Bernico
Mike Bernico
author image
Mike Bernico

Mike Bernico is a Lead Data Scientist at State Farm Mutual Insurance Companies. He also works as an adjunct for the University of Illinois at Springfield, where he teaches Essentials of Data Science, and Advanced Neural Networks and Deep Learning. Mike earned his MSCS from the University of Illinois at Springfield. He's an advocate for open source software and the good it can bring to the world. As a lifelong learner with umpteen hobbies, Mike also enjoys cycling, travel photography, and wine making.
Read more about Mike Bernico

Right arrow

Training a CNN from Scratch

Deep neural networks have revolutionized computer vision. In fact, I'd argue that the advances made in computer vision in just the last few years have made deep neural networks something that many consumers use every day. We've already seen a computer vision classifier in Chapter 5, Using Keras for Multiclass Classification, where we used a deep network to classify handwritten digits. Now I want to show you how convolutional layers work, how you can use them, and how you can build your own convolutional neural network in Keras to build better, more powerful deep neural networks to solve computer vision problems.

We will cover the following topics in this chapter:

  • Introducing convolutions
  • Training a convolutional neural network in Keras
  • Using data augmentation

Introducing convolutions

A trained convolutional layer is made up of many feature detectors, called filters, that slide over an input image as a moving window. We will talk about what's inside a filter in a moment, but for now it can be a black box. Imagine a single filter that has already been trained. Maybe that filter has been trained to detect edges in images, which you might think of as transitions between dark and light. As it passes over the image, its output represents the presence and location of the feature it detects, which can be useful for a second layer of filters. Extending our thought experiment slightly further, now imagine a single filter, in a second convolutional layer, that has also already been trained. Perhaps this new layer has learned to detect right angles, where two edges that have been found by the previous layer are present. On and on we go; as...

Training a convolutional neural network in Keras

Now that we've covered the fundamentals of convolutional neural networks, it's time to build one. In this case study, we will be taking on a well-known problem known as CIFAR-10. This dataset was created by Alex Krizhevsky, Vinod Nair, and Geoffrey Hinton.

Input

The CIFAR-10 dataset is made up of 60,000 32 x 32 color images that belong to 10 classes, with 6,000 images per class. I'll be using 50,000 images as a training set, 5,000 images as a validation set, and 5,000 images as a test set.

The input tensor layer for the convolutional neural network will be (N, 32, 32, 3), which we will pass to the build_network function as we have previously done. The following...

Using data augmentation

Data augmentation is a technique where we apply transformations to an image and use both the original image and the transformed images to train on. Imagine we had a training set with a cat in it:

If we were to apply a horizontal flip to this image, we'd get something that looks like this:

This is exactly the same image, of course, but we can use both the original and transformation as training examples. This isn't quite as good as two separate cats in our training set; however, it does allow us to teach the computer that a cat is a cat regardless of the direction it's facing.

In practice, we can do a lot more than just a horizontal flip. We can vertically flip, when it makes sense, shift, and randomly rotate images as well. This allows us to artificially amplify our dataset and make it seem bigger than it is. Of course you can only push...

Summary

In this chapter, we covered a lot of ground fast. We talked about convolutional layers and how they can be used for neural networks. We also covered batch normalization, pooling layers, and data augmentation. Finally, we trained a convolutional neural network from scratch using Keras and then improved that network using data augmentation.

We also talked about how data-hungry computer vision-based deep neural network problems are. In the next chapter I will show you transfer learning, which is one of my favorite techniques. It will help solve computer vision problems quickly, with amazing results and much less data.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Deep Learning Quick Reference
Published in: Mar 2018Publisher: PacktISBN-13: 9781788837996
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Mike Bernico

Mike Bernico is a Lead Data Scientist at State Farm Mutual Insurance Companies. He also works as an adjunct for the University of Illinois at Springfield, where he teaches Essentials of Data Science, and Advanced Neural Networks and Deep Learning. Mike earned his MSCS from the University of Illinois at Springfield. He's an advocate for open source software and the good it can bring to the world. As a lifelong learner with umpteen hobbies, Mike also enjoys cycling, travel photography, and wine making.
Read more about Mike Bernico