Book Image

Hands-On Convolutional Neural Networks with TensorFlow

By : Iffat Zafar, Giounona Tzanidou, Richard Burton, Nimesh Patel, Leonardo Araujo
Book Image

Hands-On Convolutional Neural Networks with TensorFlow

By: Iffat Zafar, Giounona Tzanidou, Richard Burton, Nimesh Patel, Leonardo Araujo

Overview of this book

Convolutional Neural Networks (CNN) are one of the most popular architectures used in computer vision apps. This book is an introduction to CNNs through solving real-world problems in deep learning while teaching you their implementation in popular Python library - TensorFlow. By the end of the book, you will be training CNNs in no time! We start with an overview of popular machine learning and deep learning models, and then get you set up with a TensorFlow development environment. This environment is the basis for implementing and training deep learning models in later chapters. Then, you will use Convolutional Neural Networks to work on problems such as image classification, object detection, and semantic segmentation. After that, you will use transfer learning to see how these models can solve other deep learning problems. You will also get a taste of implementing generative models such as autoencoders and generative adversarial networks. Later on, you will see useful tips on machine learning best practices and troubleshooting. Finally, you will learn how to apply your models on large datasets of millions of images.
Table of Contents (17 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Setting up and installing TensorFlow


TensorFlow is supported on the latest versions of Ubuntu and Windows. TensorFlow on Windows only supports the use of Python 3, while use on Ubuntu allows the use of both Python 2 and 3. We recommend using Python 3, and that is what we will use in this book for code examples.

There are several ways you can install TensorFlow on your system, and here we will go through two of the main ways. The easiest is by simply using the pip package manager. Issuing the following command from a terminal will install the CPU-only version of TensorFlow to your system Python:

$ pip3 install --upgrade tensorflow

To install the version of Tensorflow that supports using your Nvidia GPU, simply type the following:

$ pip3 install --upgrade tensorflow-gpu

One of the advantages of TensorFlow is that it allows you to write code that can run directly on your GPU. With a few exceptions, almost all the major operations in TensorFlow can be run on a GPU to accelerate their execution speed. We will see that this is going to be essential in order to train the large convolutional neural networks described later in this book.

Conda environments

Using pip may be the quickest to get started, but I see that the most convenient method involves using conda environments.

Conda environments allow you to create isolated Python environments, which are completely separate from your system Python or any other Python programs. This way, there is no chance of your TensorFlow installation messing with anything already installed, and vice versa.

To use conda, you must download Anaconda from here: https://www.anaconda.com/download/. This will include conda with it. Once you've installed Anaconda, installing TensorFlow can be done by entering the certain commands in your Command Prompt. First, enter the following:

$ conda create -n tf_env pip python=3.5

This will create your conda environment with the name tf_env, the environment will use Python 3.5, and pip will also be installed for us to use.

Once this environment is created, you can start using it by entering the following on Windows:

$ activate tf_env

If you are using Ubuntu, enter the following command:

$ source activate tf_env

It should now display (tf_env) next to your Command Prompt. To install TensorFlow, we simply do a pip install as previously, depending on if you want CPU only or you want GPU support:

(tf_env)$ pip install --upgrade tensorflow(tf_env)$ pip install --upgrade tensorflow-gpu

Checking whether your installation works

Now that you have installed TensorFlow, let's check whether it works correctly. In your Command Prompt, activate your environment again if it isn't already, and run Python by entering the following:

(tf_env)$ python

Now, enter the following lines into the Python interpreter to test that TensorFlow is installed correctly:

>>>> import tensorflow as tf
 >>>> x = tf.constant('Tensorflow works!')
 >>>> sess = tf.Session()
 >>>> sess.run(x)

If everything is installed correctly, you should see the following output:

b'Tensorflow works!'

What you just typed there is the Hello World of TensorFlow. You created a graph containing a single tf.constant, which is just a constant Tensor. The Tensor was inferred to be of type string as you passed a string to it. You then created a TensorFlow Session, which is needed to run your graph and told your session to run on the Tensor that you created. The result of the Session running was then printed out. There is an extra b there because it's a byte stream that was created.

Note

If you don't see the aforementioned and are getting some errors, your best bet is to check the following pages for solutions to common problems experienced when installing: Ubuntu: https://www.tensorflow.org/install/install_linux#common_installation_problems Windows: https://www.tensorflow.org/install/install_windows#common_installation_problems