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 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, 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.
86 total classes • 115+ hours of on-demand code walkthrough videos • Last updated: October 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:
- ✓ 86 courses on essential computer vision, deep learning, and OpenCV topics
- ✓ 86 Certificates of Completion
- ✓ 115+ 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 540+ tutorials on PyImageSearch
- ✓ Easy one-click downloads for code, datasets, pre-trained models, etc.
- ✓ Access on mobile, laptop, desktop, etc.
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!
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.
Manish
Signing up for newsletters.
Adrian Rosebrock
Thanks Manish! 🙂
John Knae
Adrian
I’m a retired Soc PhD with time on my hands so I’m researching/reviewing AGI and Deep Machine Learning and I wanted to express my gratitude for the tutorials you’ve put together on the Web. They have been a wonderful help for ol’ duffers like me and I’m sure for the better informed as well. Thank you
Adrian Rosebrock
Thanks for the kind words John 🙂 I’ll be doing a lot more deep learning and machine learning tutorials in coming posts.
Kenny
Cool Adrian 😉 As always, you never fail to keep me excited to learn more about CV 🙂 Keep going!
Niki
Thank you for such a nice tutorial. I had to install Keras and this tutorial really helped me out. I have a question about graphs. I know about NetworkX library in python which is used for graph analysis. But, didn’t find any good tutorial on image and video processing or computer vision that uses this library to build the graphs. I was wondering if you have had any experience using NetworkX and could suggest any good tutorial or perhaps you have a plan to post something about that in future.
Adrian Rosebrock
I personally haven’t used NetworkX before. Most of my work doesn’t involve graphs. If I ever do work in graphs, I’ll look into NetworkX and consider doing a tutorial on that.
Hilman
I really think you are stepping out this website Adrian. Keep posting something like this. You are really making education accessible freely on the web! Can’t wait to get my hands dirtier with deep learning!
Adrian Rosebrock
Thanks Hilman! You’ll really enjoying next week’s blog post.
Claude
Great post! Thanks!
Adrian Rosebrock
I’m glad it helped Claude! 🙂
Navdeep
Hey! thanks for the great post 😀
I have a doubt..
you said we need to edit or create “.theanorc” file in the home directory, what exactly is that home directory ??
Adrian Rosebrock
Hey Navdeep — before continuing with this tutorial, I would suggest brushing up on a bit of Unix basics to ensure you don’t run into any problems down the line. You can learn more about the home directory here.
Ethan
I get to the final step:
>>> import keras
Using Theano backend.
and it hangs forever. Thoughts?
Adrian Rosebrock
It might not be hanging. In some cases it can take 10-60 seconds if you’re using the GPU. Also, be sure to run “top” and see if anything GPU-like is executing, indicating that Theano is working in the background.
saeed
The solution for this problem is to add to the following to .theanorc file
[cuda]
root = /usr/local/cuda-8.0
Marc-Philippe Huget
Hello Adrian,
Apparently, installing Keras on CPU is little bit longer, I agree on all the steps you described, I did and it worked well on examples provided by Keras but on the blog post you submitted this week, apparently one needs to install BLAS, I have to investigate this point
mph
Adrian Rosebrock
Either BLAS or LAPACK can be used to speedup the Linear Algebra computations. I’ll make note of this.
Jason Scott
Note if you are doing this on a mac the install of opencv seems to create the binary as cv2.cpython-35m-darwin.so cv2.so instead of just cv2.so.
You can safely symlink it into your new anaconda or virtualenv environment as cv2.so though…
Adrian Rosebrock
You can also rename
cv2.cpython-35m-darwin.so
to simplycv2.so
as well.MaxWeiss
Thank you very much! All is clear and in simply words
I have read your article regardfully and will try tomorrow!
Geoffrey Anderson
Looking for Keras examples directory. The website, the find command, the pip show Keras command, this blog post by Adrian, but still no examples. Particularly the CIFAR10 (sp?) is the example I want, which was mentioned but not shown on the Keras website. The reason is I am tired of loading toy programs I see everywhere else which presume images are all contained in some monolithic data file so conveniently. In the real world we have image files like jpg and all the Keras and all the H2O examples use some silly little monolithic file load. OK that’s fine for a toy. Now lets really do something with actual images. CIFAR I hope has example of loading image files which are not contained somehow in a CSV file (!).
Thanks for any info! Sorry I’m so dumb if any of you know the answer and I don’t. (The question is, in case I wrote too much, is where is the examples directory of Keras, particularly the CIFAR example which I hope loads jpg files not some convenient monolithic CSV or other file.)
Adrian Rosebrock
We normally distribute the CIFAR10 file as a compressed archive since the images files are tiny (32 x 32 pixels). These images were never meant to be “visually appealing”, only instead used for training data. Again, as I mentioned in reply to another one of your comments, I’ll be doing more advanced tutorials that train networks from scratch later this year.
Nun YoBiz
What absolute crap keras “installs” in a breeze but then never is usable
Adrian Rosebrock
Hmmm…why do you say that it’s “unusable”? I’m not sure I agree with that.
Damian
Hi Adrian,
firstly I want to thank you for your entire contribution and reviling cv depths 🙂 I have prior experience with cv, although in more academic way, not in the practical you’re showing here.
I have a question regarding deep learning and GPU support. I’m using win10 on my daily basis, but all the cv work I’m doing on Ubuntu 16.04 Guest OS (via VirtualBox).
Have you used GPU support for Deep Learning on VM? I struggled with that and I don’t feel sure if I should follow that path further or just to leave it and use CPU only.
Adrian Rosebrock
Most VMs, without a lot of special configuration, will not allow you to access the GPU directly. I recall seeing something about a Docker instance that could access the GPU, but I honestly can’t remember where I saw that. In either case, if you intend on using the GPU for deep learning, make sure you configure the host system rather than the guest OS for optimal performance.
Xi Chen
Hi Adrian,
I followed your steps.
At last, I can import theano (with GPU)
I can import cv2 (2.4.x)
But I cannot import keras
It says no module named keras
But I do installed keras
Adrian Rosebrock
It sounds like you might not have installed Keras into the Python virtual environment or you have installed Keras into the the Python virtual environment and are forgetting to use the
workon
command to access the virtual environment.Peter Hirt
Nice tutorial indeed!
After my install and testing, it comes back and says that Tensorflow module is missing.
I would like to stay for the moment with Theano and don’t know what I need to do to force keras to use Theano.
Thanks for helping
Adrian Rosebrock
Keras should work out of the box by default with Theano. Provided you followed the instructions in this post Keras should be using Theano. Did you already have TensorFlow installed on your system? Either way, try editing your configuration file and setting the
backend
to betheano
.HV318
Thanks for this tutorial Adrian!. It is very helpful. Could you please guide me to install tensorflow so that I can use Keras with tensorflow backend instead of Theano.
Adrian Rosebrock
I don’t have any tutorials dedicated to install TensorFlow, but I’ll certainly consider it for a future tutorial.
Marcus
Does this all work on windows, or do I need a vm. Also it would be good if you created docker containers with all this in !
Adrian Rosebrock
I don’t recommend using Windows for deep learning (nor do I support Windows on the PyImageSearch blog). If you use a VM you’ll be limited to CPU only, I’m not sure if that is a big problem for you. Otherwise, you might want to look into Amazon EC2 instances that are GPU enabled.
Tim
Hi Adrian, would you mind elaborating a bit, why do you prefer Unix-based systems for machine learning?
Adrian Rosebrock
Unix-based environments tend to be (1) easier to configure and (2) obtain reproducible results. Most state-of-the-art implementations also used Unix-based environments (as opposed to Windows). I would highly suggest you use a Unix-based environment for computer vision or machine learning.
Harry
Hello Adrian,
Will you please tell me does it take long to install theano?? When i run this command sudo python setup.py install it is taking too much time. more than 15 minutes to install. I am out of my patience.
Adrian Rosebrock
Is it just Theano that is taking a long time to install? Run
top
and see the processes consuming the most CPU. You should see a process related to either Python or C++ compiling and installing. 15 minutes does sound like a long time though.Johnson
hi adrian,
thanks for the wonderful post..keeps my interest building.
i keep following your post without fail, but being a newbie i found some difficulties
i have installed keras, but when importing it, the error tensorflow backend comes.
though you have mentioned to change the backend from tensor flow to theano, ia int sure of how to do it. could you please help
Adrian Rosebrock
You need to edit your
~/keras/keras.json
file:$ nano ~/.keras/keras.json
And then update the “backend” to “theano”. You might also need to change “image_dim_ordering” to “th”.
kara
i found the same issue. and now i got the answer 😀
but if I configure the GPU it cause error also, I solved it simply by removing the theanorc file. I hope it wont matter in the future 🙁
but what does it mean with th and tf in image dim ordering?
Adrian Rosebrock
The “th” ordering refers to “Theano” ordering (depth, height, width) whereas “tf” is for “TensorFlow” ordering (height, depth, width).
Abderrahmane
Thanks this is very helpful <3
Adrian Rosebrock
I’m glad I could help 🙂
T.k
Thanks Adrian .
very helpful post
you are genius
Adrian Rosebrock
Thank you for the kind words T.k 🙂
Dattaprasad
Dear Adrian,
You have written excellent blogs on machine learning.
I am new to keras and opencv. Please guide me.
For installing keras, we create one virtual environment and complete all the steps given in this blog. My question is to install Opencv, do we create a new virtual environment named “cv”? (Ref. to your blog: https://pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/)
Can we work in the virtualenv named as keras and inside it create another virtual env. named “cv”?
Do we need to create two separate virtual envs.? If so, how to link both.
I mean to say, in a single pyhton code (like the one you have given in the blog: https://pyimagesearch.com/2016/09/26/a-simple-neural-network-with-python-and-keras/) how do we import keras and opencv?
I need your guidance on the same.
Reagrds
Adrian Rosebrock
I would suggest creating a different virtual environment for Keras. From there, sym-link in the
cv2.so
bindings into thesite-packages
directory of your new environment. From there you’ll be able to import both Keras and OpenCV into the same virtual environment.Debojit
Hi Adrian,
I am getting the error “fatal error: hdf5.h: no such file or directory” while trying to install h5py in windows. Any suggestion?
Adrian Rosebrock
Hi Debojit — I don’t support the Windows OS here on PyImageSearch so I’m not sure regarding the specific HDF5 error. If at all possible I would recommend using a Unix-based environment such as Linux (Ubuntu) or macOS for computer vision development.
Alexander
hi Adrian, great post! Does this instructions work with Python3? I’m referring “venv,pip3” , etc…
Adrian Rosebrock
Yes, these instructions work for Python 3 as well.
Hesham khalil
you used python 2 to install karas.
did you used only python 2 in the course ( deep-learning-computer-vision-python-book)?
and if yes . thats mean Keras , TensorFlow , Theano and mxnet are compatible with python 2.
which one do you suggest to learn? python 2 or 3 . if i want to dive deep in (deep learning)(covolutional neural network) ? because im confused. i cant choose which one of them
should i learn.(which one of them is more supperted in AI)
is there other libraries you suggest for covolutional neural network and machine?
sorry for my many quistions im a newbie
Hesham khalil
same quistion for computer vision. which python version should i use
Adrian Rosebrock
You can use either Python 2.7 or Python 3. Both are compatible with Keras, TensorFlow, and Theano.
Python 3 is the future of the Python programming language and Python 2.7 will eventually be phased out. For that reason alone, you should consider learning Python 3. But in all honestly, the just get started learning. The differences between Python 2.7 and Python 3 are minor. Right now you need to start learning — that’s the most important part.
I would suggest going through Practical Python and OpenCV so you can learn the basics of computer vision and OpenCV. The book is compatible with both Python 2.7 and Python 3.
Rishik Mani
The virtual environment setup had been successfully done. Although, importing keras for the first time did not create the keras.json file.
So, I went ahead and tried to manually create the file. Again when I try to import keras, it fails saying “No module name keras”. To assure you, I am into the virtual environment.
Rishik Mani
My mistake that I did not create the virtual environment in the first place. I first did the whole setup thing and later only created the virtual environment.
Thanks a lot for this post as I have been struggling for more than a week now setting up keras. I highly appreciate your work.
Adrian Rosebrock
Congrats on resolving the issue Rishik! 🙂
Rajib
Great help Adrian. I have been through many tutorials they just assume you have a GPU. You steps show you have thought of various possibilities. Keep it up, Man.
Adrian Rosebrock
Thanks Rajib! You might also be interested in Deep Learning for Computer Vision with Python. The “Starter Bundle” in particular makes no assumptions regarding whether you are using a CPU or GPU.
kaisar khatak
Doesn’t pygpu and libgpuarray need to be installed for GPU utilization???
New syntax for .theanorc:
Use device=cuda*
Thanks.
Adrian Rosebrock
For the new version of Theano, yes. At this point I am recommending users to use TensorFlow over Theano.
Ray Van Raamsdonk
For me, a complete beginner, even the first step didn’t work. c:\Users\Ray Van\Anaconda3)> C:\User\Ray Van>mKvirtualenv keras
I got the message’mkvirtualenv’ is not recognized as internal or external command, operable program or batch file.
When I saw $ mkvirtualenv keras, I ddin’t even know where to type that in since I never saw the $ prompt anywhere so I just tried it in the Anaconda prompt Window which didn’t work of course. So for a complete beginner it’s definitely not easy. I have Windows 10.
Adrian Rosebrock
Hey Ray — it can be pretty challenging to configure your first development environment. In general, I do not recommend using Windows for deep learning or computer vision. Using a Unix-based machine such as Ubuntu or macOS is significantly easier to configure your own development environment. I would recommend using VirtualBox to spin up an Ubuntu machine so you can play around with Ubuntu without having to actually install Ubuntu on your system.
jorgher
Hi Adrian.
I did the sim-link with no error, but when do the import cv2 in python it doesn’t find it.
Any guess why.
I try with two separete environments an the reults are the same.
Thanks
Adrian Rosebrock
Hey jorgher — can you check the “build/lib” directory of your OpenCV compile? It may be the case that the cv2.so bindings were not compiled.
jorgher
I’m not shure about the question you ask, but it is installed in the virtual environmet from where I try to sym-link to the keras environment.
(cv) jorgher@jorgher:~/.virtualenvs/cv/lib/python3.5/site-packages$ ls *.so
cv2.so
Adrian Rosebrock
Can you try doing a “ls -l cv2.so” and see if it points to a valid file? It could be that the sym-link is pointing to a non-existent cv2.so file.
jorgher
I did it and have:
$ ls -l cv2.solrwxrwxrwx 1 jorgher jorgher 45 ago 27 2017 cv2.so -> /usr/local/lib/python3.5/site-packages/cv2.so
seam that the cv2.so is not in the virtualenv, so do I need to sym-link from this to the kera virtualenv?
jorgher
Ok your right I change the path to the one shown for the “ls -l cv2.so ” and it works.
Thanks
Adrian Rosebrock
Awesome, I’m glad it worked jorgher! 🙂
andy
youer the best
Adrian Rosebrock
Thanks Andy 🙂