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 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!
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 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. 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. You can certainly use
pip to install TensorFlow:
$ pip install tensorflow
GPU users that have already installed CUDA and cuDNN 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
$ 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, 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:
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.
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:
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.
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.
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!
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.