Installing Keras on Raspberry Pi 3

Introduction: Installing Keras on Raspberry Pi 3

I build neat stuff.

If you are interested in machine learning and have a Raspberry Pi with you but don't know how to install Keras, an open source neural network library written in Python, on the Raspberry Pi, then this instructable is for you. Having the ability to do machine learning on a device as small as the Pi is really cool and can bring your Raspberry Pi projects to a whole new level. We will be using Theano instead of TensorFlow for our backend because installing TensorFlow on the Raspberry Pi is a tedious task and can cause errors and frustration if not done properly. So, let's get started.

This project was done by me, Nikhil Raghavendra, a Diploma in Computer Engineering student from Singapore Polytechnic, School of Electrical and Electronic Engineering, under the guidance of my mentor Mr Teo Shin Jen.

Step 1: Install Numpy

NumPy is a mathematical library for the Python programming language, that adds support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays. Machine learning is all about numbers and Numpy is a must have dependency for machine learning libraries and scripts. So go ahead and install it using the following command in Bash.

sudo apt-get install python3-numpy

Step 2: Install Scipy

SciPy is an open source Python library used for scientific computing and technical computing. SciPy contains modules that allow you to carry out linear algebra, integration, interpolation, special functions, FFT and more. You can install Scipy using the following commands.

sudo apt-get install libblas-dev
sudo apt-get install liblapack-dev sudo apt-get install python3-dev # Possibly already installed sudo apt-get install libatlas-base-dev # Optional sudo apt-get install gfortran sudo apt-get install python3-setuptools # Possibly already installed sudo apt-get install python3-scipy

NOTE: The installation of Scipy takes a few hours (it took me almost 3 hours, so go ahead and watch a movie)

Step 3: Install Theano

Theano is a Python library that allows you to define, optimize, and evaluate mathematical expressions involving multi-dimensional arrays efficiently. Keras uses either Theano or Tensorflow (depending on your config file preference) to carry out machine learning. Install Theano using the following commands.

sudo pip3 install scikit-learn
sudo pip3 install pillow sudo apt-get install python3-h5py sudo pip3 install --upgrade --no-deps git+git://

Step 4: Install Keras

Now that we have installed all of our dependencies, you can finally install Keras.

sudo pip3 install keras

Because we want to have Theano instead of TensorFlow as our backend for Keras, we have an extra step to execute.

sudo pip3 install --upgrade six
cd .keras nano keras.json

Then in the in-console editor, insert the following lines:

"image_dim_ordering": "th"
"backend": "theano"

Save and close the file. Now open a python terminal and import Keras. If your installation was successful no errors should occur. Do note that it is usual to see deprecation warnings while you are installing the packages so don’t be afraid and think that it is not going to work.



    • Water Contest

      Water Contest
    • Fix It! Contest

      Fix It! Contest
    • Metalworking Contest

      Metalworking Contest

    5 Discussions

    1.como importo keras en un python???

    Thank you.

    After following the steps, i had no Folder "keras"

    Successfully installed six-1.11.0
    pi@raspberrypi:~ $ cd .keras
    -bash: cd: .keras: No such file or directory
    pi@raspberrypi:~ $

    1 more answer

    1.TEngo el mismo problema, no tengo el directorio .keras

    i went through all the steps and finally when install keras i was stuck.

    pi@raspberrypi:~ $ sudo pip3 install keras

    Looking in indexes:,

    Collecting keras

    Using cached

    Requirement already satisfied: numpy>=1.9.1 in /usr/lib/python3/dist-packages (from keras) (1.12.1)

    Requirement already satisfied: six>=1.9.0 in /usr/lib/python3/dist-packages (from keras) (1.10.0)

    Collecting keras-preprocessing==1.0.1 (from keras)

    Using cached

    Requirement already satisfied: scipy>=0.14 in /usr/lib/python3/dist-packages (from keras) (0.18.1)

    Collecting pyyaml (from keras)

    HTTP error 404 while getting (from

    Could not install requirement pyyaml from (from keras) because of error 404 Client Error: Not Found for url:

    Could not install requirement pyyaml from (from keras) because of HTTP error 404 Client Error: Not Found for url: for URL (from

    please help me!

    For installing tensorflow as backend what i need to do??