Title: Installing Keras for deep learning - PyImageSearch

URL Source: https://pyimagesearch.com/2016/07/18/installing-keras-for-deep-learning/

Published Time: 2016-07-18T14:00:40+00:00

Markdown Content:
[![Image 1](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20530%20170'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2016/07/install_keras_header.jpg)

The purpose of this blog post is to demonstrate how to install the [Keras](https://github.com/fchollet/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**_ TensorFlow or Theano 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!

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:

*   [How to install CUDA Toolkit and cuDNN for deep learning](https://pyimagesearch.com/2016/07/04/how-to-install-cuda-toolkit-and-cudnn-for-deep-learning/)
*   [Compiling OpenCV with CUDA support](https://pyimagesearch.com/2016/07/11/compiling-opencv-with-cuda-support/)

I’ll be using my same Amazon EC2 p2.xlarge instance running Ubuntu 16.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 4-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](https://realpython.com/blog/python/python-virtual-environments-a-primer/). Utilizing virtual environments is _especially important_ when we start working with various deep learning libraries (Keras, mxnet, TensorFlow, etc.) and versioning issues can _easily_ occur (especially surrounding which version of TensorFlow is used).

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

$ mkvirtualenv keras -p python3

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 pillow
$ pip install h5py

We also need to install [Tensorflow](https://www.tensorflow.org/). You can certainly use `pip` to install TensorFlow:

$ pip install tensorflow

**GPU users** that have already [installed CUDA and cuDNN](https://pyimagesearch.com/2017/09/27/setting-up-ubuntu-16-04-cuda-gpu-for-deep-learning-with-python/) can install the GPU version of TensorFlow with pip:

$ pip install tensorflow-gpu

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 #3** for an example on how to do this).

### Step #3: 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` :

$ ls /usr/local/lib/python3.6/site-packages/
cv2.cpython-36m-x86\_64-linux-gnu.so
$ cd /usr/local/lib/python3.6/site-packages/
$ mv cv2.cpython-36m-x86\_64-linux-gnu.so cv2.so
$ cd ~/.virtualenvs/keras/lib/python3.6/site-packages/
$ ln -s /usr/local/lib/python3.6/site-packages/cv2.so cv2.so
$ cd ~

As I detailed in [last week’s tutorial](https://pyimagesearch.com/2016/07/11/compiling-opencv-with-cuda-support/), after compiling and installing OpenCV, my `cv2.so` bindings were found in `/usr/local/lib/python3.6/site-packages/cv2.cpython-36m-x86_64-linux-gnu.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/python3.6/site-packages/cv2.cpython-36m-x86\_64-linux-gnu.so
./home/adrian/.virtualenvs/cv/lib/python3.6/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 #4: 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:

[![Image 2](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20453'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2016/07/install_keras_import_keras.jpg)

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

Notice that the TensorFlow backend is being used.

Also notice that the GPU is being used, in this case the K80 that is installed on the Amazon EC2 p2.xlarge instance. For more information on how I installed the CUDA Toolkit and cuDNN, [please see this blog post](https://pyimagesearch.com/2017/09/27/setting-up-ubuntu-16-04-cuda-gpu-for-deep-learning-with-python/).

Optionally, if you performed **Step #3** and want to test your OpenCV sym-link, try to import your OpenCV bindings into the `keras`  virtual environment as well:

[![Image 3](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20453'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2016/07/install_keras_import_opencv.jpg)

**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!

### What's next? We recommend [PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend).

**Course information:**  
84 total classes • 114+ hours of on-demand code walkthrough videos • Last updated: February 2024  
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled

**I strongly believe that if you had the right teacher you could _master_ computer vision and deep learning.**

Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?

That’s _not_ the case.

All you need to master computer vision and deep learning is for someone to explain things to you in _simple, intuitive_ terms. _And that’s exactly what I do_. My mission is to change education and how complex Artificial Intelligence topics are taught.

If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to _successfully_ and _confidently_ apply computer vision to your work, research, and projects. Join me in computer vision mastery.

**Inside PyImageSearch University you'll find:**

*   ✓ **84 courses** on essential computer vision, deep learning, and OpenCV topics
*   ✓ **84 Certificates** of Completion
*   ✓ **114+ hours** of on-demand video
*   ✓ **Brand new courses released _regularly_**, ensuring you can keep up with state-of-the-art techniques
*   ✓ **Pre-configured Jupyter Notebooks in Google Colab**
*   ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
*   ✓ Access to **centralized code repos for _all_ 536+ tutorials** on PyImageSearch
*   ✓ **Easy one-click downloads** for code, datasets, pre-trained models, etc.
*   ✓ **Access** on mobile, laptop, desktop, etc.

[Click here to join PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend)

Summary
-------

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

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!**

![Image 4](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20410%20567'%3E%3C/svg%3E)

#### Join the PyImageSearch Newsletter and Grab My FREE 17-page Resource Guide PDF

Enter your email address below to **join the PyImageSearch Newsletter** and **download my FREE 17-page Resource Guide PDF** on Computer Vision, OpenCV, and Deep Learning.