Latest YouTube Video

Monday, July 18, 2016

Installing Keras for deep learning

install_keras_header

The purpose of this blog post is to demonstrate how to install the Keras library for deep learning. The installation procedure will show how to install Keras:

  • With GPU support, so you can leverage your GPU, CUDA Toolkit, cuDNN, etc., for faster network training.
  • Without GPU support, so even if you do not have a GPU for training neural networks, you’ll still be able to follow along.

Let me start by saying that Keras is my favorite deep learning Python library. It’s a minimalist, modular neural network library that can use either Theano or TensorFlow as a backend.

Furthermore, the primary motivation behind Keras really resonates with me: you should be able to experiment super quickly — going from idea to result, as fast as possible.

Coming from a world that mixes both academia and entrepreneurship, the ability to iterate quickly is extremely valuable, especially in the deep learning world where it can take days to weeks to train just a single model.

I’ll be using Keras extensively in the coming PyImageSearch blog posts, so make sure you follow this tutorial to get Keras installed on your machine!

Installing Keras for deep learning

I’ll be making the assumption that you’ve been following along in this series of blog posts on setting up your deep learning development environment:

I’ll be using my same Amazon EC2 g2.2xlarge instance running Ubuntu 14.04 as I have in previous tutorials — feel free to use the same machine you’ve been using to follow along as well.

Overall, installing Keras is a 5-step procedure, with three of these steps being optional.

The first optional step is whether or not you would like to use Python virtual environments — I suggest that you do, but that decision is entirely up to you.

The second optional step is whether or not you want to use the GPU to speedup training your neural networks — this is obviously dependent on whether you own a CUDA-compatible GPU. The Keras library can run on the CPU just fine, but if you really want to train deep neural networks, you’ll want to get a GPU installation setup.

The final optional step is whether or not you would like to have OpenCV bindings in your Python virtual environment along with your Keras installation. If you do, you’ll want to pay attention to Step #4.

With all that said, let’s get started!

Step #1: Create a separate Python virtual environment (optional)

If you’ve been following along in this series of posts, then you already know that I like using Python virtual environments. Utilizing virtual environments is especially important when we start working with various deep learning libraries (Keras, Theano, mxnet, TensorFlow, etc.) and versioning issues can easily occur (especially surrounding which version of Theano is used).

Because of the problems related to conflicting library versions, I suggest creating a virtual environment exclusively for Keras-based projects:

$ mkvirtualenv keras

This will create a Python virtual environment named

keras
 . Anytime you would like to access this virtual environment, just use the
workon
  command followed by the name of the virtual environment:
$ workon <virtual env name>

In this case, we can access the

keras
  virtual environment by executing the following command:
$ workon keras

Step #2: Install Keras

Installing Keras is a breeze —

pip
  can do all the hard work for us. First, we need to install a few dependencies:
$ pip install numpy scipy
$ pip install scikit-learn
$ pip install h5py
$ pip install Theano

From there, we can use

pip
  to install Keras as well:
$ pip install keras

After Keras has finished installing, you can verify the install by opening up a terminal, accessing the

keras
  virtual environment, and then importing the library (see Step #5 for an example on how to do this).

However, if you want to optimize Keras so that it uses your GPU, CUDA Toolkit, and cuDNN, you’ll want to proceed to Step #3.

Step #3: Setup GPU support (only for GPU users)

Under the hood, Keras is using Theano as a backend — think of Keras is a wrapper around Theano to make it more user friendly (and easier to build deep neural networks).

In order to get Keras and Theano to utilize our GPU rather than the CPU, we need to create a file named

.theanorc
  in our home directory (if it does not already exist).

This file stores various configurations for the Theano library. You can supply these values via command line argument with the

THEANO_FLAGS
  variable; however, I find it more convenient to store global configurations inside the
.theanorc
  file.

Open up the

.theanorc
  file using your favorite command line text editor:
$ nano ~/.theanorc

And then add the following lines to the file:

[global]
floatX = float32
device = gpu0
 
[nvcc]
fastmath = True

Save the file and then exit the editor.

Step #4: Sym-link in OpenCV (optional)

If you want to have access to your OpenCV bindings from the

keras
  virtual environment, you’ll need to sym-link in the
cv2.so
  file into the
site-packages
  directory of
keras
 :
$ cd ~/.virtualenvs/keras/lib/python2.7/site-packages/
$ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so
$ cd ~

As I detailed in last week’s tutorial, after compiling and installing OpenCV, my

cv2.so
  bindings were found in
/usr/local/lib/python2.7/site-packages/cv2.so
 . Depending on how you installed OpenCV on your own system, your
cv2.so
  bindings may be in a different location. If you cannot remember where your
cv2.so
  bindings are, or if you no longer have your CMake output (which does indicate where the bindings will be stored), you can use the
find
  utility program to help locate them:
$ cd /
$ sudo find . -name '*cv2.so*'
./usr/local/lib/python2.7/site-packages/cv2.so
./home/adrian/.virtualenvs/cv/lib/python2.7/site-packages/cv2.so

Again, this is a totally optional step and only needs to be done if you want to have access to OpenCV from the

keras
  virtual environment.

Step #5: Test out the installation

To verify that Keras has been installed, access the

keras
  virtual environment, open up a Python shell, and import it:
$ workon keras
$ python
>>> import keras
>>>

Below follows a screenshot from my own EC2 instance:

Figure 1: Installing the Keras Python library for deep learning.

Figure 1: Installing the Keras Python library for deep learning.

Notice what when

theano
  is imported by
keras
 , I’m given additional debugging information on the installation, such as the CUDA Toolkit and cuDNN v5 being utilized.

Also notice that the GPU is being used, in this case the K520 that is installed on the Amazon EC2 g2.2xlarge instance. For more information on how I installed the CUDA Toolkit and cuDNN, please see this blog post.

Optionally, if you performed Step #5 and want to test your OpenCV sym-link, try to import your OpenCV bindings into the

keras
  virtual environment as well:
Figure 2: Importing OpenCV and Keras together for deep learning with Python.

Figure 2: Importing OpenCV and Keras together for deep learning with Python.

At this point, you should now be able to import Keras and OpenCV into the same Python virtual environment. Take a second to congratulate yourself — you now have all the building blocks in place to start constructing deep neural networks!

Summary

In today’s blog post, I demonstrated how to install the Keras Python package for deep learning with GPU support and without GPU support.

We’ll be using the Keras library extensively in future PyImageSearch blog posts, so I highly encourage you to get Keras installed on your machine, even if it’s just the CPU version — this will enable you to follow along in future PyImageSearch tutorials on deep learning.

Next week, we’ll take another step in our deep learning journey by studying convolutions, what they are, how they work, and how you’re already using them in your computer vision applications (whether you realize it or not).

Be sure to signup for the PyImageSearch Newsletter using the form below — you won’t want to miss when this upcoming post on convolutions goes live!

The post Installing Keras for deep learning appeared first on PyImageSearch.



from PyImageSearch http://ift.tt/2a3dZAU
via IFTTT

No comments: