Vous êtes sur la page 1sur 31

Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

Installing Keras with TensorFlow


backend
by Adrian Rosebrock on November 14, 2016 in Deep Learning, Libraries, Tutorials

A few months ago I demonstrated how to install the Keras deep learning library with
a Theano backend.

In today’s blog post I provide detailed, step-by-step instructions to install Keras using a
TensorFlow backend, originally developed by the researchers and engineers on the Google
Brain Team.

I’ll also (optionally) demonstrate how you can integrate OpenCV into this setup for a full-
fledged computer vision + deep learning development environment.

1 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

Installing Keras with TensorFlow backend


The first part of this blog post provides a short discussion of Keras backends and why we
should (or should not) care which one we are using.

From there I provide detailed instructions that you can use to install Keras with a
TensorFlow backend for machine learning on your own system.

TensorFlow? Theano? Who cares?


It’s important to start this discussion by saying that Keras is simply a wrapper around more
complex numerical computation engines such as TensorFlow and Theano.

Keras abstracts away much of the complexity of building a deep neural network, leaving us
with a very simple, nice, and easy to use interface to rapidly build, test, and deploy deep
learning architectures.

When it comes to Keras you have two choices for a backend engine — either TensorFlow
or Theano. Theano is older than TensorFlow and was originally the only choice when
selecting a backend for Keras.

So why might you want to use TensorFlow over a different backend (such as the no-longer-
being-developed Theano)?

The short version is that TensorFlow is extremely flexible, allowing you to deploy network
computation to multiple CPUs, GPUs, servers, or even mobile systems without having to
change a single line of code.

This makes TensorFlow an excellent choice for training distributed deep learning networks
in an architecture agnostic way, something that Theano does not (currently) provide.

To be totally honest with you, I started using Keras well before TensorFlow was released
(or even rumored to exist) — this was back when Theano was the only possible choice of
backend.

I haven’t given much thought to whether Theano or TensorFlow should be my “go to”
backend. Theano was working well for what I needed it for, so why bother switching?

My eyes started to open when I ran this recent poll on Twitter asking my followers which
backend they preferred when using Keras:

2 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

Figure 1: I polled my Twitter followers (@pyimagesearch) to determine whether they preferred using Theano or
TensorFlow as their Keras backend.

67% of respondents said they were using TensorFlow as their backend. I was honestly
quite surprised. How, as a long-time Keras user, could I possibly be in the minority?

This 67% of respondents might be swayed since TensorFlow is now the default backend
when installing Keras…or it could be because many of my followers are finding TensorFlow
a better, more efficient backend (and using more TensorFlow specific features).

Regardless of the exact reasoning, there is one thing you cannot dispute: TensorFlow is
here to stay.

If you need further proof all you need to do is take a look at this deep learning GitHub
activity analysis from François Chollet (creator and maintainer of Keras):

3 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

Figure 2: TensorFlow tops the charts as the deep learning library with most GitHub activity.  Keras follows at #2
with Theano all the way at #9.

As we can see, TensorFlow is topping the charts by a mile (#1) with Theano at #9.

While Keras makes it simple for us to switch backends (all we need to do is install our
respective backends and edit a simple JSON configuration file), we still need to be mindful
of what the trends are telling us: that TensorFlow will continue to be the preferred
Keras backend in the (near) future.

Update 2018-06-04: Theano is no-longer being actively developed (announcement


2017-09-29) and as you guessed it, TensorFlow is now the default.

Step #1: Setup Python virtual environment


If you’ve ever read any of my previous tutorials (whether for OpenCV, CUDA, Keras, etc.)
you’ve likely picked up on the fact that I’m a huge fan of using Python virtual environments.

I especially recommend Python virtual environments when working in the deep learning

4 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

uire different versions of various

For example, if you wanted to use Keras + Theano together you would need the latest
version of Theano (i.e., their latest GitHub commit, which isn’t always the version published
on PyPI).

However, if you wanted to try a library such as scikit-theano you would need a previous
version of Theano that is not compatible with Keras.

The dependency version issue only compounds as you start to add in other Python
libraries, especially deep learning ones (such as TensorFlow), which are volatile in their
very nature (since deep learning is a fast-moving field with updates and new features being
pushed online every day).

The solution?

Use Python virtual environments.

I won’t go into a huge rant on the benefits of Python virtual environments (as I’ve already
done that in the first half of this blog post), but the gist is that by using Python virtual
environments you can create a separate, sequestered Python environment for each of your
projects, ensuring that each Python environment is independent of each other. Doing this
allows you to totally and completely avoid the version dependency issue.

I’m going to assume that you have both virtualenv and virtualenvwrapper installed on your
system (if not, both are pip-installable and require only a small update to your shell
configuration; just follow the links above for more information).

Once you have virtualenv and virtualenvwrapper installed, let’s create a Python 3 virtual
environment exclusively for our Keras + TensorFlow-based projects:

Installing Keras with TensorFlow backend Shell


1 $ mkvirtualenv keras_tf -p python3

I’ll name this virtual environment keras_tf   for Keras + TensorFlow (I also have a virtual
environment named keras_th   for Keras + Theano).

Anytime you need to access a given Python virtual environment just use the workon
command:

Installing Keras with TensorFlow backend Shell


1 $ workon <virtual_env_name>

In this particular case we can access the keras_tf   virtual environment using:

5 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

Shell

Again, while this is an optional step, I really encourage you to take the time to properly
setup your development environment if you are even remotely serious about doing deep
learning research, experiments, or development using the Python programming language
— while it’s more work upfront, you’ll thank me in the long run.

Step #2: Install TensorFlow


Installing TensorFlow is trivially easy as pip   will do all the heavy lifting for us:

Installing Keras with TensorFlow backend Shell


1 $ pip install --upgrade tensorflow

Below you can see a screenshot of TensorFlow being downloaded and installed:

6 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

Figure 3: Installing TensorFlow for deep learning via pip into my Python virtual environment.

Assuming your TensorFlow install exited without error you can now test the installation by
opening a Python shell and trying to import the tensorflow   package:

Installing Keras with TensorFlow backend Shell


1 $ python
2 >>> import tensorflow
3 >>>

7 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

Installing Keras is even easier than installing TensorFlow.

First, let’s install a few Python dependencies:

Installing Keras with TensorFlow backend Shell


1 $ pip install numpy scipy
2 $ pip install scikit-learn
3 $ pip install pillow
4 $ pip install h5py

Followed by installing keras   itself:

Installing Keras with TensorFlow backend Shell


1 $ pip install keras

That’s it! Keras is now installed on your system!

Step #4: Verify that your keras.json file is configured correctly


Before we get too far we should check the contents of our keras.json   configuration file.
You can find this file in ~/.keras/keras.json  .

Open it using your favorite text editor and take a peak at the contents. The default values
should look something like this:

Installing Keras with TensorFlow backend JavaScript


1 {
2     "floatx": "float32",
3     "epsilon": 1e-07,
4     "backend": "tensorflow",
5     "image_data_format": "channels_last"
6 }

Specifically, you’ll want to ensure that image_data_format   is set to "channels_last"


(indicating that the TensorFlow image dimension ordering is used rather than
"channels_first"   for Theano).

You’ll also want to ensure that the backend   is properly set to tensorflow   (rather than
theano  ). Again, both of these requirements should be satisfied by the default Keras
configuration but it doesn’t hurt to double check.

Make any required updates (if any) to your configuration file and then exit your editor.

A quick note on image_data_format


You might be wondering what exactly image_data_format   controls.

Using TensorFlow, images are represented as NumPy arrays with the shape (height, width,

8 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

However, if you are using Theano, images are instead assumed to be represented
as (depth, height, width).

This little nuance is the source of a lot of headaches when using Keras (and a lot of if
statments looking for these particular configurations).

If you are getting strange results when using Keras (or an error message related to the
shape of a given tensor) you should:

1. Check your backend.


2. Ensure your image dimension ordering matches your backend.

Can’t find your keras.json file?


On most systems the keras.json   file (and associated subdirectories) will not be
created until you open up a Python shell and directly import the keras   package itself.

If you find that the ~/.keras/keras.json   file does not exist on your system, simply open
up a shell, (optionally) access your Python virtual environment (if you are using virtual
environments), and then import Keras:

Installing Keras with TensorFlow backend Shell


1 $ workon keras_tf
2 $ python
3 >>> import keras
4 >>> quit()

From there, you should see that your keras.json   file now exists on your local disk.

If you see any errors when importing keras   go back to the top of this section and ensure
your keras.json   configuration file has been properly updated.

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


This step is entirely optional, but if you have OpenCV installed on your system and would
like to access your OpenCV bindings from a Python virtual environment, you first need to
sym-link in the cv2.so   file to the site-packages   directory of your environment.

To do this, first find where your cv2.so   bindings are located on your system:

Installing Keras with TensorFlow backend Shell


1 $ cd /
2 $ sudo find . -name '*cv2.so*'
3 ./Users/adrianrosebrock/.virtualenvs/cv/lib/python3.6/site-packages/cv2.so
4 ./Users/adrianrosebrock/.virtualenvs/gurus/lib/python3.6/site-packages/cv2.so
5 ./Users/adrianrosebrock/.virtualenvs/keras_th/lib/python3.6/site-packages/cv2.so
6 ./usr/local/lib/python3.6/site-packages/cv2.so

9 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

(unless you specified a custom


OpenCV install directory).

Note: The other cv2.so   files returned by my find   command are simply sym-links back
to the original cv2.so   file in /usr/local/lib  .

From there, change directory into the site-packages   directory of your Python virtual
environment (in this case the keras_tf   environment) and create the sym-link:

Installing Keras with TensorFlow backend Shell


1 $ cd ~/.virtualenvs/keras_tf/lib/python3.6/site-packages/
2 $ ln -s /usr/local/lib/python3.6/site-packages/cv2.so cv2.so
3 $ cd ~

Again, this step is totally optional and only needs to be done if you want to have access to
OpenCV from inside the keras_tf   virtual environment.

Step #6: Test out the Keras + TensorFlow installation


To verify that Keras + TensorFlow have been installed, simply access the keras_tf
environment using the workon   command, open up a Python shell, and import keras  :

Installing Keras with TensorFlow backend Shell


1 (keras_tf) ~ $ python
2 Python 3.6.4 (default, Mar 27 2018, 15:31:37)
3 [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
4 Type "help", "copyright", "credits" or "license" for more information.
5 >>> import keras
6 Using TensorFlow backend.
7 >>>

Specifically, you can see the text Using TensorFlow backend   display when importing
Keras — this successfully demonstrates that Keras has been installed with the TensorFlow
backend.

Provided you performed the optional Step #5 and want to to test out your OpenCV sym-
link, try importing your OpenCV bindings as well:

Installing Keras with TensorFlow backend Shell


1 (keras_tf) ~ $ python
2 Python 3.6.4 (default, Mar 27 2018, 15:31:37)
3 [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
4 Type "help", "copyright", "credits" or "license" for more information.
5 >>> import keras
6 Using TensorFlow backend.
7 >>> import cv2
8 >>>

If you get an error message related to OpenCV not being found then you’ll want to double
check your sym-link and ensure it is pointing to a valid file.

10 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

Figure 4: Learn how to utilize Deep Learning and Convolutional


Neural Networks to classify the contents of images inside my book,
Deep Learning for Computer Vision with Python.

Now that you have Keras + TensorFlow for deep learning installed on your system the
obvious question is:

“What’s next?”

I’m glad you asked.

I would suggest:

Starting with this tutorial on creating a simple neural network with Python and Keras.
From there, try training your first Convolutional Neural Network (LeNet) for handwritten
digit recognition.
You might also be interested in trying this example on using pre-trained CNN
architectures on the ImageNet dataset to recognize 1,000 different common object
categories.

Finally, I would also recommend taking a look at my book, Deep Learning for Computer
Vision with Python.

Inside the book there are over 900 pages covering: Neural Network fundamentals,

11 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

bject detection networks.

To learn more about my deep learning book (and grab 3 FREE sample chapters plus the
entire table of contents), just click the button below:

Click here to grab your copy of DL4CV!

Summary
In today’s blog post I demonstrated how to install the Keras deep learning library using the
TensorFlow backend.

When it comes to choosing a backend for Keras you need to consider a few aspects.

The first is the popularity and therefore the probability that a given library will continue to be
updated and supported in the future. In this case, TensorFlow wins hands down — it is
currently the most popular numerical computation engine in the world used for machine
learning and deep learning.

Secondly, you need to consider the functionality of a given library. While Theano is just as
easy to use as TensorFlow out-of-the-box (in terms of Keras backends), TensorFlow allows
for a more architecture agnostic deployment. By using TensorFlow it becomes possible to
train distributed deep learning networks across CPUs, GPUs, and other devices all without
having to change a single line of code.

Since Theano development has officially ceased in September 2017, I have fully switched
to TensorFlow and I’m not looking back.

If you enjoyed this install tutorial and found it helpful be sure to leave a note in the
comments!

And if you would like to receive email updates when new blog posts are published
on the PyImageSearch blog, please enter your email address in the form below.

Resource Guide (it’s totally free).

Enter your email address below to get my free 17-


page Computer Vision, OpenCV, and Deep
Learning Resource Guide PDF. Inside you'll find my
hand-picked tutorials, books, courses, and Python
libraries to help you master computer vision and deep

12 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

Your email address

DOWNLOAD THE GUIDE!

 deep learning, install, keras, tensorflow

 Intersection over Union (IoU) for object detection Raspbian + OpenCV pre-configured and pre-installed. 

67 Responses to Installing Keras with TensorFlow backend

REPLY 
Keith Pridbrey November 14, 2016 at 1:03 pm #

Thank you, thank you. This post is both eye-opening and helpful.

REPLY 
Adrian Rosebrock November 14, 2016 at 2:05 pm #

Thanks Keith!

REPLY 
manuel November 14, 2016 at 2:44 pm #

Thank you Adrian!! Now I’m much more prone to try and play around with deep
learning!!

REPLY 
Chris Hanning November 14, 2016 at 9:30 pm #

I’m running OSX 10.11.6 with xcode-8.1 and using pyenv to create virtual
environments.
Creating a python-2.7.12 virtual environment with
pyenv virtualenv 2.7.12 keras_tf
mkdir keras_tf; cd keras_tf; pyenv local keras_tf
pip install –upgrade $TF_BINARY_URL

13 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

keras_tf $ python
>>> import tensorflow
fails to import. There is an odd import error to do with a swig-created _pywrap_tensorflow
module that it can’t find.

…some time later… it may have been due to my current default xcode being 8.1
In the meantime I managed to follow along but using python-3.5.2 instead.
Here is a link to my opencv3 install workflow for anyone interested:
https://gist.github.com/keevee09/e3ae7a72673ad036d480675c9c8aa2db

REPLY 
Adrian Rosebrock November 16, 2016 at 1:55 pm #

Thanks for sharing Chris. I’m not sure about this error message. If you haven’t
already, I would suggest opening an Issue on the official TensorFlow/Keras GitHub.

REPLY 
Shravan Kumar Parunandula November 14, 2016 at 10:14 pm #

You always a rock star… Thanks

REPLY 
Adrian Rosebrock November 16, 2016 at 1:53 pm #

Thank you Shravan �

REPLY 
Kapil November 14, 2016 at 10:38 pm #

It’s always handy to hear from you.


Thanks Adrian..!!

It is my absolute pleasure to learn from your tutorials. Please make tutorials for deep
machine learning as you did for OpenCV python…!

REPLY 
Adrian Rosebrock November 15, 2016 at 12:48 pm #

I’ll absolutely continue to do deep learning/machine learning tutorials on the


PyImageSearch blog. There are a number of them already that were published within

14 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

REPLY 
samjakar November 15, 2016 at 2:16 am #

Adrian,
I am getting started on this right away. Thanks for clear instructions as always.

Would the following scenario classify for a Machine learning/Neural Net + Computer Vision
Combo?
Programmatically, I need to be able to identify a specific region in a video. Say, for
example, the led displays on the perimeter of a Soccer game , or say the Giant overhead
LED in a NBA game. Would I be able to achieve this with NN/ML/DL+CV combo? Or would
this be just a pure CV effort where NN/ML/DL has probably no role to play? Would you care
to share your thoughts?

Thanks
Samjakar

REPLY 
Adrian Rosebrock November 16, 2016 at 1:51 pm #

I could traditional computer vision + machine learning along with deep


learning being used to solve this problem. I think either would work, it mainly just
depends if your camera is static or if you’ll be capturing images from a large variety of
viewing angles.

Keep in mind that DL methods require a lot of training data so regardless of which way
you go, make sure you collect a lot of data to work with.

REPLY 
Haydar November 15, 2016 at 7:30 am #

It’s a very helpful topic. Thank you Adrian.

REPLY 
Mike November 15, 2016 at 1:30 pm #

The only problem with TF is its installation on windows that its quite nontrivial
unlike the Theanos one.

15 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

REPLY 

I haven’t used Windows in a very long time — that’s good to know. My biggest
complaint with TensorFlow is that it allocates the entire GPU memory to the process
that spawned it rather than only allocating the necessary GPU memory like Theano
does.

REPLY 
Anastasios Tsiolakidis November 17, 2016 at 10:03 am #

Thanks for the guide again, you could rearrange something to indicate that
~/.keras/keras.json is not there until your first “import keras”

REPLY 
CK December 3, 2016 at 5:58 pm #

Guys, there is a pecularity regarding installing TF version > 0.10 with keras.
You have to set image_dim_ordering in keras.json to “th” even though you are using TF!
Caused me immense headache while setting up TF on AWS.

REPLY 
Adrian Rosebrock December 5, 2016 at 1:33 pm #

Hey CK — I just spun up an AWS instance three days ago and didn’t run into
this particular issue. I’ll be sure to make note of it though.

REPLY 
Samantha Adams December 4, 2016 at 7:47 am #

Great tutorial! Love the stuff you have been doing with Deep Learning!

Just to add that I had a problem using virtualenv for some reason – scipy did not install
properly. but using anaconda and an conda environment worked perfectly so that is a
viable alternative.

REPLY 
Vasilis December 20, 2016 at 5:05 pm #

You are the best !!!


I want to ask your advice if tensorflow and keras are optimal solutions for a face recogntion
?

16 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

thanks !!!

REPLY 
Adrian Rosebrock December 21, 2016 at 10:22 am #

In reality, it depends. For some datasets you can get away with Eigenfaces,
Fisherfaces, or LBPs for face recognition. More advanced solutions should use
OpenFace directly instead of trying to code a custom face recognition network.

REPLY 
Khairul Izwan December 27, 2016 at 10:05 pm #

If I am already had the virtual environment for OpenCV, can I just install/use the
keras in it?

REPLY 
Adrian Rosebrock December 31, 2016 at 1:32 pm #

Yes, you can certainly use a pre-existing virtual environment provided there
aren’t any package conflicts. If your previous one was just for OpenCV I don’t expect
there to be any.

17 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

REPLY 

Thanks for the guide Adrian!

I managed to configure keras (keras.json) depending on virtualenv, this manner I can use
keras with Theano or Tensorflow via virtualenv.

If somebody is interested, just change in “~/.virtualenvs//lib/python2.7/site-packages/keras


/backend” this line:

_keras_base_dir = os.path.expanduser(‘~’)

for:

if hasattr(sys, ‘real_prefix’):
_keras_base_dir = sys.prefix
else:
_keras_base_dir = os.path.expanduser(‘~’)

This use “.keras/keras.json” inside the virtualenv dir

REPLY 
Adrian Rosebrock December 31, 2016 at 1:21 pm #

Awesome, thanks for sharing Zauron!

REPLY 
Wonchul Kim January 6, 2017 at 3:45 pm #

Hi!
First of all, this post was really helpful to install keras with tensorflow!!

I have a question!
Now, I installed keras with tensorflow and theano.
And starting “workon keras_th”, when I import keras in python, this worked as theano
backend, eventhough I implemented it on ‘keras_tf’.

Do I need to change the kears.json file setting?


If I have to, why do we use virtualenv?

I thought by using virtualenv, when i execute ‘keras_tf’ and import keras, it works as
tensorflow backend, and when i execute ‘keras_th’ and import keras, it works as theano
backend .

REPLY 
Adrian Rosebrock January 7, 2017 at 9:29 am #

18 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

onflicting library versions.


the virtual environment
you will need to update your keras.json file if you want to switch between Theano
and TensorFlow.

An alternative would be to create a hook that automatically swaps your keras.json


file when you run the workon command.

REPLY 
Wonchul Kim January 6, 2017 at 4:13 pm #

I’ve followed your post and completed to install keras with tensorflow.

But, after I closed the terminal and start a new one.

then, I tried to use ‘workon keras_tf’, but it said ‘workon: command not found’….

Is there something I have to do before using it?

REPLY 
Adrian Rosebrock January 7, 2017 at 9:28 am #

It sounds like your ~/.bash_profile (or similar profile file) was not
updated correctly when you installed virtualenv and virtualenvwrapper. Which
operating system are you using?

REPLY 
bernd February 5, 2017 at 1:40 pm #

Thanks Adrian,
very helpfull

REPLY 
Raghunandan Palakodety February 15, 2017 at 7:11 am #

Awesome Adrian!. You give the best tutorials.

REPLY 
Adrian Rosebrock February 15, 2017 at 8:57 am #

Thanks Raghunandan — I’m happy you enjoyed the tutorial!

19 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

REPLY 

hello Adrian,

I have already installed GPU support TensorFlow in my system in docker. I can use tf in
jupyter notebook. Do I need to install tf in my virtual environment again?

REPLY 
Adrian Rosebrock March 4, 2017 at 9:42 am #

Yes, each virtual environment is 100% independent from your system install
— that is the way virtual environments are designed to avoid library versioning issues.
Simply install TensorFlow into your virtual environment.

REPLY 
soumen March 2, 2017 at 10:47 pm #

Hello Adrian, in my case, after installation of keras I can see the file ~/.keras
/keras.json is empty instead of above mentioned default content. Is there anything wrong? I
used just onething different from you like I used different TF_BINARY URL for my system
ubuntu 16.04 gpu .. Awaiting for your response.

REPLY 
Adrian Rosebrock March 4, 2017 at 9:41 am #

Fire up a Python shell and import the Keras library. This should populate the
.json configuration file.

REPLY 
Jason March 6, 2017 at 10:46 am #

Hi Adrian,

Very nice post!

I have a question. Why use tf as the backend and not use it directly? What is the
advantage of using keras?

REPLY 
Adrian Rosebrock March 6, 2017 at 3:34 pm #

Keras acts as a wrapper around Theano/TensorFlow and makes it much

20 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

optimizations and speed that

REPLY 
DJ March 12, 2017 at 3:13 am #

Hi Adrian,

Thanks again for your post, very insightful. I am using AWS EC2 (p2.xlarge) to run Keras
via Anaconda. I have installed all the correct drivers for the K80 GPU, somehow when I run
my model, it’s still defaulting to use the CPU and was wondering if you happen to know if
there’s a setting I can use to switch to always use GPU when running the Tensorflow
backend?

Thanks!

REPLY 
Parnia March 23, 2017 at 1:33 pm #

thank you Adrian for this great tutorial

REPLY 
Adrian Rosebrock March 25, 2017 at 9:33 am #

Thanks for the comment Parnia, I’m happy you enjoyed it! �

REPLY 
Ajay March 25, 2017 at 12:36 pm #

File “/usr/local/lib/python2.7/dist-packages/keras/backend
/tensorflow_backend.py”, line 2856, in conv2d
x = tf.nn.convolution(
AttributeError: ‘module’ object has no attribute ‘convolution’

I am getting this error could someone please help me to remove it.

REPLY 
Adrian Rosebrock March 28, 2017 at 1:11 pm #

Which version of Keras are you using? Is it >= 2.0? If so, make sure you
install TensorFlow >= 1.0.

21 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

REPLY 

I have the same issue as well.

File “/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py”,
line 2856, in conv2d
x = tf.nn.convolution(
AttributeError: ‘module’ object has no attribute ‘convolution’

I m using tensorflow 1.0 and keras 2.02

REPLY 
Adrian Rosebrock April 3, 2017 at 2:15 pm #

Uninstall both tensorflow and protobuf, then re-install TensorFlow


ensuring that it’s >= 1.0.

REPLY 
Shashank April 12, 2017 at 1:27 am #

Hey Adrian!
I was installing a CPU only version of Tensorflow on Ubuntu 16.04, Python 3.5 running on
Virtual Box using the tutorial and I got the following error.
tensorflow-1.0.1-cp35-cp35m-linux_x86_64.whl is not a supported wheel on this platform.
Kindly help.
Thanks in advance

REPLY 
Adrian Rosebrock April 12, 2017 at 1:02 pm #

It’s hard to say what the exact issue is. Make sure you are copying and
pasting the full TensorFlow URL correctly.

REPLY 
auraham April 17, 2017 at 12:20 pm #

Great post, Adrian! For those interested, replace the value of TF_BINARY_URL by

https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.0.1-cp35-cp35m-
linux_x86_64.whl

in order to use Tensorflow 1.0.1 (python 3.5, GPU-enabled). In other case, you will install a
previous version (0.11 in this post). This is recommended to use the latest API and avoid

22 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

For more options, check this page:

https://www.tensorflow.org/install/install_linux#TF_PYTHON_URL

REPLY 
Adrian Rosebrock April 19, 2017 at 12:59 pm #

Thanks for sharing Auraham!

REPLY 
PranavAgarwal May 27, 2017 at 7:18 am #

How do I install Tensor Flow on my Ubuntu 15.04 32 bit system ,using pip always
show error?

REPLY 
Adrian Rosebrock May 28, 2017 at 12:58 am #

What is the error message you are receiving? Without knowing the error, it’s
impossible to diagnose the problem.

REPLY 
Surya June 7, 2017 at 5:24 am #

Thanks Adrian Rosebrock. I just followed the steps and its working fine. I think
“image_data_format”: “channels_last” is also correct in addition to the
“image_data_format”: “tf”.

REPLY 
Adrian Rosebrock June 9, 2017 at 1:52 pm #

You can use image_data_format as either channels_last or


channels_first with Keras 2.0. The difference is that channels_last is faster
with TensorFlow and channels_first is faster with Theano.

REPLY 
Alessandro June 13, 2017 at 4:45 pm #

Hi,

23 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

I have create my virtualenv keras with tensorflow backend with python; when i am trying to
use matplotlib I have a problem because it does not work with virtual environment. Do you
have any suggestion how to solve this issue?

Thanks,
Alessandro

REPLY 
Adrian Rosebrock June 16, 2017 at 11:36 am #

Hi Alessandro — are you getting an error when trying to use matplotlib from
the virtual environment?

If so, try installing an older version of matplotlib:

pip install matplotlib==1.4.3

REPLY 
Igor Gallon September 25, 2017 at 7:14 pm #

Hi Adrian. Is it possible to install Keras+TensorFlow in my Raspberry Pi 3


(raspbian)?

REPLY 
Adrian Rosebrock September 26, 2017 at 8:12 am #

Yes. I would actually suggest following my Deep learning + Ubuntu tutorial.


Since both Ubuntu and Raspbian are Debian-based you can use the same
instructions.

REPLY 
jeremy rutman January 10, 2018 at 10:48 am #

in the keras.json file


image_data_format
can no longer be set to tf, rather one should leave it (or change it) as channels_last like the
below, which is actually default it seems.

{
“epsilon”: 1e-07,
“floatx”: “float32”,

24 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

REPLY 
Adrian Rosebrock January 10, 2018 at 12:39 pm #

You’re absolutely right, for the TensorFlow backend in new versions of Keras
the image_data_format should now be channels_last.

REPLY 
Cesar February 20, 2018 at 6:28 am #

Hi Adrian,

Do I need to repeat the whole process of pip install for every virtual environment that i will
create?

thanks

REPLY 
Adrian Rosebrock February 22, 2018 at 9:15 am #

Correct, you would need to repeat the pip install process for each new Python
virtual environment that you create.

REPLY 
Nihel April 10, 2018 at 5:51 pm #

Hi Adrian

File “test_network.py”, line 22, in


orig = image.copy()
AttributeError: ‘NoneType’ object has no attribute ‘copy’

I am getting this error could someone please help me to remove it.

REPLY 
nabila July 16, 2018 at 4:00 pm #

I think I’ve successfully got Keras installed by installing from the official keras repo
on github. However, I’ve run into another problem where my openCV2 bindings are not
being recognized within my keras_tf virtual environment.

25 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

pi@raspberrypi:~ $ sudo find . -name ‘*cv2.so*’


./.virtualenvs/keras_tf/lib/python3.5/site-packages/cv2.so
./.virtualenvs/cv/lib/python3.5/site-packages/cv2.so
./opencv-3.3.0/build/lib/cv2.so

Any suggestions? SO

REPLY 
Adrian Rosebrock July 17, 2018 at 7:15 am #

There are two possibilities here:

1. You are not in the “keras_tf” Python virtual environment when trying to import
OpenCV and Keras
2. Your “cv2.so” sym-link from the “keras_tf” virtual environment points to a file that
does not exist. Double-check that file path.

REPLY 
Charlie July 27, 2018 at 11:08 am #

Just wanted to let you know that I followed your instructions for Tensorflow
installation, which failed with cryptic errors. I found that the latest version of Tensorflow will
not run on my (older) desktop CPU, a Core 2 Quad Q9650. When I forced the installation
of (the older) v1.5 of Tensorflow, everything worked.

I also bought your starter bundle last night. In the introduction summary, you have the
sentence “Never in the history of machine learning and neural networks have the available
tools at our disposable been so exceptional.” You likely meant to use the word “disposal.”

In any case, my reading list is full, and thanks for your efforts.

REPLY 
Adrian Rosebrock July 31, 2018 at 12:02 pm #

Hey Charlie — which version of Python + TensorFlow did you originally try to
install? Thanks for sharing that downgrading to v1.5 fixed the issue. I’ll also get that
typo updated. Thank you for picking up a copy of the Starter Bundle, I hope you are
enjoying it so far!

REPLY 
hashir September 27, 2018 at 4:08 am #

26 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

w and keras for gpu support

REPLY 
Adrian Rosebrock October 8, 2018 at 12:34 pm #

To install TensorFlow with GPU support you would want to install


tensorflow-gpu instead of tensorflow:

$ pip install --upgrade tensorflow-gpu

Leave a Reply

Name (required)

Email (will not be published) (required)

Website

SUBMIT COMMENT

Search... 

Resource Guide (it’s totally free).

Get your FREE 17 page Computer Vision, OpenCV, and Deep Learning
Resource Guide PDF. Inside you'll find my hand-picked tutorials, books, courses,
and libraries to help you master CV and DL.

27 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

Deep Learning for Computer Vision with Python Book — OUT NOW!

You're interested in deep learning and computer vision, but you don't know how to get started. Let me
help. My new book will teach you all you need to know about deep learning.

CLICK HERE TO MASTER DEEP LEARNING

You can detect faces in images & video.

Are you interested in detecting faces in images & video? But tired of Googling for tutorials that
never work? Then let me help! I guarantee that my new book will turn you into a face detection ninja by
the end of this weekend. Click here to give it a shot yourself.

CLICK HERE TO MASTER FACE DETECTION

28 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

The PyImageSearch Gurus course is now enrolling! Inside the course you'll learn how to perform:

Automatic License Plate Recognition (ANPR)


Deep Learning
Face Recognition
and much more!

Click the button below to learn more about the course, take a tour, and get 10 (FREE) sample
lessons.

TAKE A TOUR & GET 10 (FREE) LESSONS

Hello! I’m Adrian Rosebrock.

I'm an entrepreneur and Ph.D who has launched two successful image search
engines, ID My Pill and Chic Engine. I'm here to share my tips, tricks, and hacks I've
learned along the way.

Learn computer vision in a single weekend.

29 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

Want to learn computer vision & OpenCV? I can teach you in a single weekend. I know. It sounds crazy,
but it’s no joke. My new book is your guaranteed, quick-start guide to becoming an OpenCV Ninja. So
why not give it a try? Click here to become a computer vision ninja.

CLICK HERE TO BECOME AN OPENCV NINJA

Subscribe via RSS

Never miss a post! Subscribe to the PyImageSearch RSS Feed and keep up to date with
my image search engine tutorials, tips, and tricks

POPULAR

Install guide: Raspberry Pi 3 + Raspbian Jessie + OpenCV 3


APRIL 18, 2016

Raspbian Stretch: Install OpenCV 3 + Python on your Raspberry Pi


SEPTEMBER 4, 2017

Install OpenCV and Python on your Raspberry Pi 2 and B+


FEBRUARY 23, 2015

Home surveillance and motion detection with the Raspberry Pi, Python, OpenCV, and Dropbox
JUNE 1, 2015

Ubuntu 16.04: How to install OpenCV


OCTOBER 24, 2016

How to install OpenCV 3 on Raspbian Jessie


OCTOBER 26, 2015

30 of 31 11/10/18, 2:55 PM
Installing Keras with TensorFlow backend - PyI... https://www.pyimagesearch.com/2016/11/14/inst...

Find me on Twitter, Facebook, Google+, and LinkedIn.


© 2018 PyImageSearch. All Rights Reserved.

31 of 31 11/10/18, 2:55 PM

Vous aimerez peut-être aussi