Deep Learning on Pi: Install TensorFlow on Raspberry Pi 3
‘AI is the new electricity’, says Prof. Andrew Ng, a pioneer in the field of machine learning. It’s no surprise to see the camera in our smartphones becoming more intelligent. Now-a-days, they are getting better to tell us our apparent age from face detection. Salute to the growing computer vision! It is believed that we may be seeing more and more artificial intelligence inside portable devices in upcoming years. So, why should we leave a pace behind to exploit the machine learning algorithms on portable ones.
Today, we will start with the aim of implementing a suite of specialized machine learning, deep learning. We aim implementation on a wonderful, cheap, yet-powerful credit-card size computer, the Raspberry Pi 3. Hence, I will guide you how to install tensorflow on raspberry pi. TensorFlow is a well-known state-of-the-art and ever-updating library of deep learning.
Assumptions
- We have latest version of Raspbian Stretch Desktop installed on your Pi. If you haven’t setup your Pi yet, follow the setup guide in my previous post.
- We are able to access your Pi with keyboard and monitor, or remotely via ssh or remote desktop connection.
- And, Pi is connected to the Internet.
With that being said, let’s quickly start installing TensorFlow release 1.8 on Raspbian Stretch desktop. Complementary to this blog post, you are welcome to watch the tutorial in this YouTube video.
Open up the terminal and follow the steps given as follows.
Step 1: Install dependencies
As we will be installing tensorflow with Python 3.5.x, we need to install certain dependencies prior install.
First of all, install pip using command
sudo apt-get install python3-pip
As a result, this will install the latest version of pip for Python 3.
In addition, setup Python 3 development tools using
sudo apt-get install python3-dev
Step 2: Install virtual env
Now, we are planning to install tensoflow on a virtual environment, that will be isolated from other python installations. This keeps us safe from bothering about inter-package compatibility issues. In a way, we will be having our own installed-libraries’ world, separate from the rest of installations on Raspbian.
Therefore, type in the following command in terminal
sudo apt-get install python-virtualenv
Consequently, this will query for the associated packages over the repositories and prompt you with
Do you want to continue? [Y/n]
Press ‘y’ and hit Enter to continue. The installation will be completed in 2 minutes approx.
Step 3: Create a new virtual environment
Type in
vitutalenv --system-site-packages -p python3 tensorflow
This will create a new environment named ‘tensorflow’ with Python 3 installed.
Step 4: Activate virtual environment
In order to enter into the newly created environment, type in the following command
source ~/tensorflow/bin/activate
Press Enter, and you will be migrated to tensorflow environment; and the command line must look like as below.
Step 5: Access TensorFlow wheel archive
Now comes the crucial part.
Actually, in the official website of tensorflow, we are having instructions for installing inside Ubuntu Linux, which are applicable for setup of the library in x86 or x64 systems, that won’t be compatible for our Raspberry Pi which uses ARMv7 CPU.
Hence, we need to find the appropriate binaries for Pi’s hardware architecture. Luckily, there is a repository in GitHub which gives us the precompiled binaries for latest versions of tensorflow.
Thanks to their contributors.
Going to the GitHub page, you will see the latest tensorflow release wheel files available for download.
We will be downloading the wheel archive for Raspberry Pi 3 with Python 3.5. Copy the url of the appropriate wheel file from here.
Step 6: Install Tensorflow using pip
After copying the url of wheel file, head over to the terminal, still inside the tensorflow environment, and type in
pip3 install 'the url of the wheel file'
This will download the wheel file from the repository and install it as well. The download process will take time depending upon the internet connection. It will download all the necessary packages for tensorflow itself. This will take time, around 5-10 minutes. For the time being, just sit back and relax.
After pip installation completes, we will be back to command prompt.
Step 7: Verify the setup
In order to verify the installation, quickly open up the Python interpreter inside the terminal, by typing in
python
Now, type in the following small code
>>import tensorflow as tf >>tf.__version__
The above code results in display of the version of tensorflow. Since, at this time, I am writing this post, the latest version from Git I installed is tensorflow r1.8, hence it outputs
'1.8.0'
Furthermore, feed in the following code, line-by-line
>>hello=tf.constant('hello world') # this creates a constant named hello tensorflow >>with tf.Session() as sess: ... print(sess.run(hello)) ... #press Enter
Consequently, the code will result in output
b'hello world'
Hence, this is the expected output which verifies the success of setup.
Thanks for reading this blog post to install TensorFlow on raspberry pi.
Happy DEEP LEARNING !
vitutalenv –system-site-packages -p python3 tensorflow
should read
virtualenv –system-site-packages -p python3 tensorflow
source ~/tensorflow/bin/activate
should read (I think)
source ./tensorflow/bin/activate
Thank you for your tutorial. Very helpful.
vitutalenv –system-site-packages -p python3 tensorflow
should read
virtualenv –system-site-packages -p python3 tensorflow
Hi,
The content is very useful.
Step 3: There is a typo error
vitutalenv –system-site-packages -p python3 tensorflow
virtualenv spell check