Introduction: Installing Keras on Raspberry Pi 3
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://github.com/Theano/Theano.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:
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.