Reader small image

You're reading from  Computer Vision Projects with OpenCV and Python 3

Product typeBook
Published inDec 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781789954555
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Matthew Rever
Matthew Rever
author image
Matthew Rever

Matthew Rever received his PhD. in electrical engineering from the University of Michigan, Ann Arbor. His career revolves around image processing, computer vision, and machine learning for scientific research applications. He started programming in C++, a language he still uses today, over 20 years ago, and has also used Matlab and most heavily Python in the past few years, using OpenCV, SciPy, scikit-learn, TensorFlow, and PyTorch. He believes it is important to stay up to date on the latest tools to be as productive as possible. Dr. Rever is the author of Packt's Computer Vision Projects with Python 3 and Advanced Computer Vision Projects.
Read more about Matthew Rever

Right arrow

Deep Learning Image Classification with TensorFlow

In this chapter, we will learn how to classify images using TensorFlow. First, we will use a pre-trained model, and then we'll proceed with training our own model using custom images.

Toward the end of the chapter, we will make use of the GPU to help us speed up our computations.

In this chapter, we will cover the following:

  • A deep introduction to TensorFlow
  • Using a pre-trained model (Inception) for image classification
  • Retraining with our own images
  • Speeding up computation with the GPU

Technical requirements

An introduction to TensorFlow

In this chapter, we'll go deeper into TensorFlow and see how we can build a general-purpose image classifier using its deep learning method.

This will be an extension of what we learned in Chapter 2, Handwritten Digit Recognition with scikit-learn and TensorFlow, where we learned how to classify handwritten digits. However, this method is quite a bit more powerful, as it will work on general images of people, animals, food, everyday objects, and so on.

To start, let's talk a little bit about what TensorFlow does, and the general workflow of TensorFlow.

To begin, what is a tensor? Wikipedia states this:

"In mathematics, tensors are geometric objects that describe linear relations between geometric vectors, scalars, and other tensors... Given a reference basis of vectors, a tensor can be represented as an organized multi-dimensional array...

Using Inception for image classification

In this section, we're going to use a pre-trained model, Inception, from Google to perform image classification. We'll then move on and build our own modelor, at least do some retraining on the model in order to train on our own images and classify our own objects.

For now, we want to see what we can do with a model that's already trained, which would take a lot of time to reproduce from scratch. Let's get started with the code.

Let's go back to Jupyter Notebook. The Notebook file can be found at https://github.com/PacktPublishing/Computer-Vision-Projects-with-OpenCV-and-Python-3/Chapter04.

In order to run the code, we're going to need to download a file from TensorFlow's website, from the following link: http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz. This is the...

Retraining with our own images

In this section, we're going to go beyond what we did with the pre-built classifier and use our own images with our own labels.

The first thing I should mention is that this isn't really training from scratch with deep learningthere are multiple layers and algorithms for training the whole thing, which are very time-consumingbut we can take advantage of something called transfer learning, where we take the first few layers that were trained with a very large number of images, as illustrated in the following diagram:

It's one of the caveats of deep learning that having a few hundred or a few thousand images isn't enough. You need hundreds of thousands or even millions of samples in order to get good results, and gathering that much data is very time-consuming. Also, running it on a personal computer, which I expect...

Speeding up computation with your GPU

In this section, we'll talk briefly about speeding up computations with your GPU. The good news is that TensorFlow is actually very smart about using the GPU, so if you have everything set up, then it's pretty simple.

Let's see what things look like if we have the GPU properly set up. First, import TensorFlow as follows:

import tensorflow

Next, we print tensorflow.Session(). This just gives us information about our CPU and GPU (if it is properly set up):

print(tensorflow.Session())

The output is as follows:

As we can see from the output, we're using a laptop with a GeForce GTX 970M, which is CUDA-compatible. This is needed in order to run TensorFlow with the GPU. If everything is set up properly, you will see a message very similar to the preceding output for your GPU, your card model, and details about it such as its...

Summary

In this chapter, we learned how to classify images using a pre-trained model based on TensorFlow. We then retrained our model to work with custom images.

Finally, we had a brief overview of how to speed up the classification process by carrying out the computation on a GPU.

Using the examples covered in this book, you will be able to carry your our custom projects using Python, OpenCV, and TensorFlow.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Computer Vision Projects with OpenCV and Python 3
Published in: Dec 2018Publisher: PacktISBN-13: 9781789954555
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
Matthew Rever

Matthew Rever received his PhD. in electrical engineering from the University of Michigan, Ann Arbor. His career revolves around image processing, computer vision, and machine learning for scientific research applications. He started programming in C++, a language he still uses today, over 20 years ago, and has also used Matlab and most heavily Python in the past few years, using OpenCV, SciPy, scikit-learn, TensorFlow, and PyTorch. He believes it is important to stay up to date on the latest tools to be as productive as possible. Dr. Rever is the author of Packt's Computer Vision Projects with Python 3 and Advanced Computer Vision Projects.
Read more about Matthew Rever