Reader small image

You're reading from  Artificial Intelligence with Python - Second Edition

Product typeBook
Published inJan 2020
Reading LevelBeginner
PublisherPackt
ISBN-139781839219535
Edition2nd Edition
Languages
Right arrow
Author (1)
Prateek Joshi
Prateek Joshi
author image
Prateek Joshi

Prateek Joshi is the founder of Plutoshift and a published author of 9 books on Artificial Intelligence. He has been featured on Forbes 30 Under 30, NBC, Bloomberg, CNBC, TechCrunch, and The Business Journals. He has been an invited speaker at conferences such as TEDx, Global Big Data Conference, Machine Learning Developers Conference, and Silicon Valley Deep Learning. Apart from Artificial Intelligence, some of the topics that excite him are number theory, cryptography, and quantum computing. His greater goal is to make Artificial Intelligence accessible to everyone so that it can impact billions of people around the world.
Read more about Prateek Joshi

Right arrow

Deep Learning with Convolutional Neural Networks

In this chapter, we are going to learn about deep learning and Convolutional Neural Networks (CNNs). CNNs have gained a lot of momentum over the last few years, especially in the field of image recognition. We will talk about the architecture of CNNs and the type of layers used inside. We are going to see how to use a package called TensorFlow. We will build a perceptron-based linear regressor. We are going to learn how to build an image classifier using a single-layer neural network.

We will then build an image classifier using a CNN. Image classifiers have many applications. It's a fancy name, but it's just the ability of computers to discern what an object is. For example, you might build a classifier that determines if something is a hotdog or not a hotdog. This is a lighthearted example, but image classifiers can also have life-or-death applications. Picture a drone that has image classification software embedded...

The basics of Convolutional Neural Networks

CNNs in general, and Generative Adversarial Networks (GANs) in particular, have been in the news lately. A GAN is a class of CNN developed by Ian Goodfellow and his colleagues initially in 2014. In GANs, two neural networks compete against each other in a game (in a game theory sense). Given a dataset, a GAN learns to create new data examples similar to the training set. For example, it might be a little slow but there is a website that will generate faces of people that don't exist.

We'll let your imagination run wild but it would certainly be possible to create a film using some of these generated "humans" to star in the movie. There is other research to try to solve the converse. Given an image, can we determine if it's a GAN-generated image or a real person? You can play around with the website here:

https://thispersondoesnotexist.com/

To use it, just keep on refreshing the page and it will generate...

Architecture of CNNs

When we are working with ordinary neural networks, we need to convert the input data into a single vector. This vector acts as the input to the neural network, which then passes through the layers of the neural network. In these layers, each neuron is connected to all the neurons in the previous layer. It is also worth noting that the neurons within each layer are not connected to each other. They are only connected to the neurons in the adjacent layers. The last layer in the network is the output layer and it represents the final output.

If we use this structure for images, it will quickly become unmanageable. For example, let's consider an image dataset consisting of 256×256 RGB images. Since these are 3-channel images, there would be 256 * 256 * 3 = 196,608 weights. Note that this is just for a single neuron! Each layer will have multiple neurons, so the number of weights tends to increase rapidly. This means that the model will now have an enormous...

Types of layers in a CNN

CNNs typically use the following types of layers:

Input layer – This layer takes the raw image data as it is.

Convolutional layer – This layer computes the convolutions between the neurons and the various patches in the input. If you need a quick refresher on image convolutions, you can check out this link:

http://web.pdx.edu/~jduh/courses/Archive/geog481w07/Students/Ludwig_ImageConvolution.pdf

The convolutional layer basically computes the dot product between the weights and a small patch in the output of the previous layer.

Rectified Linear Unit layer – This layer applies an activation function to the output of the previous layer. This function is usually something like max(0, x). This layer is needed to add non-linearity to the network so that it can generalize well to any type of function.

Pooling layer – This layer samples the output of the previous layer resulting in a structure with smaller dimensions...

Building a perceptron-based linear regressor

Before we build a CNN, let's set the stage with a more basic model and the see how we can improve using CNNs. In this section we will see how to build a linear regression model using perceptrons. We have already seen linear regression in previous chapters, but this section is about building a linear regression model using a neural network approach.

We will be using TensorFlow in this chapter. It is a popular deep learning package that's widely used to build various real-world systems. In this section, we will get familiar with how it works. Make sure to install it before you proceed. The installation instructions can be found here:

https://www.tensorflow.org/get_started/os_setup

Once you verify that it's installed, create a new Python file and import the following packages:

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf

We will be generating some data points and see...

Building an image classifier using a single-layer neural network

Let's see how to create a single-layer neural network using TensorFlow and use it to build an image classifier. We will be using the MNIST image dataset to build our system. It is a dataset containing images of handwritten digits. Our goal is to build a classifier that can correctly identify the digit in each image.

Create a new Python file and import the following packages:

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

Extract the MNIST image data. The one_hot flag specifies that we will be using one-hot encoding in our labels. It means that if we have n classes, then the label for a given data point will be an array of length n. Each element in this array corresponds to a given class. To specify a class, the value at the corresponding index will be set to 1 and everything else will be 0:

# Get the MNIST data
mnist = input_data.read_data_sets("./mnist_data...

Building an image classifier using a Convolutional Neural Network

The image classifier in the previous section didn't perform that well. Getting 92.1% on the MNIST dataset is relatively easy. Let's see how we can use CNNs to achieve a much higher accuracy. We will build an image classifier using the same dataset, but with a CNN instead of a single-layer neural network.

Create a new Python file and import the following packages:

import argparse
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

Define a function to create values for weights in each layer:

def get_weights(shape):
    data = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(data)

Define a function to create values for biases in each layer:

def get_biases(shape):
    data = tf.constant(0.1, shape=shape)
    return tf.Variable(data)

Define a function to create a layer based on the input shape:

def create_layer...

Summary

In this chapter, we learned about deep learning and CNNs. We discussed what CNNs are and why we need them. We talked about the architecture of CNNs. We learned about the various type of layers used within a CNN. We discussed how to use TensorFlow. We used it to build a perceptron-based linear regressor. We learned how to build an image classifier using a single-layer neural network. We then built an image classifier using a CNN.

In the next chapter we will learn about CNNs' other popular brother – Recurrent Neural Networks (RNNs). Like CNNs, RNNs have taken flight and are extremely popular right now. They have achieved impressive results against previous models; in some cases, in some cases, even surpassing human performance.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Artificial Intelligence with Python - Second Edition
Published in: Jan 2020Publisher: PacktISBN-13: 9781839219535
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
Prateek Joshi

Prateek Joshi is the founder of Plutoshift and a published author of 9 books on Artificial Intelligence. He has been featured on Forbes 30 Under 30, NBC, Bloomberg, CNBC, TechCrunch, and The Business Journals. He has been an invited speaker at conferences such as TEDx, Global Big Data Conference, Machine Learning Developers Conference, and Silicon Valley Deep Learning. Apart from Artificial Intelligence, some of the topics that excite him are number theory, cryptography, and quantum computing. His greater goal is to make Artificial Intelligence accessible to everyone so that it can impact billions of people around the world.
Read more about Prateek Joshi