Reader small image

You're reading from  Hands-On Deep Learning for Images with TensorFlow

Product typeBook
Published inJul 2018
Reading LevelBeginner
PublisherPackt
ISBN-139781789538670
Edition1st Edition
Languages
Right arrow
Author (1)
Will Ballard
Will Ballard
author image
Will Ballard

Will Ballard is the chief technology officer at GLG, responsible for engineering and IT. He was also responsible for the design and operation of large data centers that helped run site services for customers including Gannett, Hearst Magazines, NFL, NPR, The Washington Post, and Whole Foods. He has also held leadership roles in software development at NetSolve (now Cisco), NetSpend, and Works (now Bank of America). https://www.linkedin.com/in/will-ballard-b09115/
Read more about Will Ballard

Right arrow

A Convolutional Neural Network

In the previous chapter, we learned all about dense neural networks.

In this chapter, we're going to move on to a more recent technique: the convolutional neural network. This is an approach that you can use to process a wide array of images, and you'll find that, as we show you how it works, it's actually much more accurate and effective than the classical neural network.

In this section, we're going to learn all about convolutions and how to apply them to images, and then we're going to learn about another operation known as pooling. Armed with these two new techniques, we're going to build an actual convolutional neural network and train it in our MNIST digits, which will reveal that it's much more accurate. Finally, we're going to build an actual deep network. The idea behind deep learning is that you take...

Convolutions

In this section, we will learn about convolutions. We're going to see the structure of a convolutional network, and then we're going to apply that to two dimensions, just like we would if we were using it for an image. Finally, we're going to discuss the benefits of a convolutional network and why you would choose to use one.

Alright, let's get started! First, we're going to import the networkx packages and matplotlib, just like we did for the classical neural network:

Importing packages

The code here is similar to what we learned in the previous chapter, but there's a minor change:

Connecting from the inputs to the activation

You will have noticed that where we are connecting from the inputs to the activation, rather than connecting every input to every activation, we have a window. In this case, we're using a window of three...

Pooling

Now, in this section, we'll move on to pooling. We'll be learning about the one-dimensional pooling operation; the two-dimensional pooling operation, such as you would use on an image; and then finally, we are going to discuss image channels and how they're used in this data.

Okay, from the top, we're going to be importing keras and some additional layers this timeparticularly MaxPooling1D, and MaxPooling2Dand we're going to go ahead and import the convolutional 2D layer, which we'll be using a little bit later on. So, if you take a peek at the code, what we're doing is setting up a matrix, and this matrix just has some values. You can think of it as a square matrix of almost all ones, but I've sprinkled some higher values in here; there's 2, 3, 4, and 5. What the max pooling is going to do is extract out the...

Building a convolutional neural network

In this section, we're going to build a full convolutional neural network. We're going to cover the MNIST digits and transform that data to have channels construct the convolutional neural network with multiple layers, and then finally, run and train our convolutional neural network and see how it compares to the classical dense network.

Alright! Let's load up our MNIST digits, as shown in the following screenshot :

Loading MNIST digits

You can see that we're performing a similar operation to what we did for the dense neural network, except we're making a fundamental transformation to the data. Here, we're using NumPy's expand_dims call (again, passing -1, meaning the last dimension) to expand our image tensors from the 28 x 28 pixel MNIST images to actually have an additional dimension of one, which encodes...

Deep neural network

Now, we're going to create an actual deep neural network using convolution.

In this section, we'll cover how to check to make sure we're running on a GPU, which is an important performance tip. Then, we'll load up our image data, and then we'll build a multiple block deep neural network, which is much deeper than anything we've created before. Finally, we'll compare the results of this deep neural network with our shallow convolutional neural network in the previous section.

Here, at the top, we're importing the necessary Python packages:

Importing packages

This is the same as we did for the standard convolutional neural network. The difference regarding making a deep neural network is that we're simply going to be using the same layers even more. In this next block, we're going directly to tensorflow and importing...

Summary

Alright! We've learned about convolutions, which are a loosely connected way of moving over an image to extract features; we've learned about pooling, which summarizes the most important features; we've built a convolutional neural network using these techniques; and then finally, we combined many layers of convolution and pooling in order to generate a deep neural network.

In the next chapter, we're going to switch over to a bit more application development. We're going to be building an image classification REST server that can take different neural network models and serve those as APIs.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Deep Learning for Images with TensorFlow
Published in: Jul 2018Publisher: PacktISBN-13: 9781789538670
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
Will Ballard

Will Ballard is the chief technology officer at GLG, responsible for engineering and IT. He was also responsible for the design and operation of large data centers that helped run site services for customers including Gannett, Hearst Magazines, NFL, NPR, The Washington Post, and Whole Foods. He has also held leadership roles in software development at NetSolve (now Cisco), NetSpend, and Works (now Bank of America). https://www.linkedin.com/in/will-ballard-b09115/
Read more about Will Ballard