TensorPi – Machine Learning in the Go!
Do Machine Learning on your Pi, from anywhere and everywhere.
“The illiterate of the future are not those who cant read or write but those who cannot Learn, UnLearn and Relearn”— Alvin Toffer
Wouldn't it be awesome if you had your own pocket machine learning server, that you could take with and setup everywhere or access from anywhere in the world…?
This article aims to teach you just that, so read along!
Table of content
- The Story
- Powering the Pi
- Setting up the tools
- Getting online
The Backstory….
I started my Data Science and Machine Learning journey last year, and have been using my MacBook Pro as the primary machine but the problem is that not all of my resources are being used for model training and stuff.
Most of the resources are tied up in other programs like my browser and Spotify, so I thought about getting a server for myself that will be dedicated for my machine learning needs. Now I had two options either get a cloud server or buy a machine to setup as a server.
The problems with those options were :
- Expensive
- I have security and privacy issues with cloud services
- Not very portable
One night I was sitting at my table, and I heard my door bell ring. Finally! my new Raspberry Pi 4 had arrived, and I knew in that instant this baby was gonna go places.
Powering Up The Pi
Raspberry Pi is a credit card sized computer with WiFi, Gigabit Ethernet and now Packing power of upto 8GB RAM it was the perfect solution for my needs.
Fair Warning : I dont work on extremely large datasets so 8GB is enough for my Machine Learning needs.
I prefer using less cables and stuff so I setup my Pi Headless, you can follow my headless setup guide here.
Once your done setting up headover to the next section.
Setting Up Machine Learning Tools
Now that the boring stuff is out of the way, lets get to cookin!
My goto Machine Learning and Deep Learning Framework is Tensorflow, and that is what we will be using today.
Prerequisites :
Before we can install Tensorflow we need to install some more packages, enter the following commands in your terminal to setup.
Virtual Environment Setup
sudo apt install virtualenv
cd Desktop
mkdir TensorPi
cd TensorPi
virtualenv python3 tensorflow
source tensorflow/bin/activate
Installing Libraries :
apt install python3-matplotlib python3-scipy
pip3 install — upgrade pip
# Reboot your pi and then activate your virtualenv again
sudo apt install libatlas-base-dev
pip3 install numpy pandas
sudo pip3 install jupyter
# Reboot your pi and then activate your virtualenv again
You can test your libraries and jupyter by issuing the following command in the terminal whilst your virtualenv is activated.
jupyter-notebook
Installing Tensorflow 2.0
To install Tensorflow yourself you can follow the guide on this GitHub.
Or reactivate your virtualenv and type in the following commands :
sudo apt install -y libhdf5-dev libc-ares-dev libeigen3-dev gcc gfortran python-dev libgfortran5 libatlas3-base libatlas-base-dev libopenblas-dev libopenblas-base libblas-dev liblapack-dev cython openmpi-bin libopenmpi-dev libatlas-base-dev python3-dev
sudo pip3 install keras_applications==1.0.8 — no-deps
sudo pip3 install keras_preprocessing==1.1.0 — no-deps
sudo pip3 install h5py==2.9.0
sudo pip3 install pybind11
pip3 install -U — user six wheel mock
sudo pip3 uninstall tensorflow
wget https://github.com/lhelontra/tensorflow-on-arm/releases/download/v2.0.0/tensorflow-2.0.0-cp37-none-linux_armv7l.whl
sudo -H pip3 install tensorflow-2.0.0-cp37-none-linux_armv7l.whl
Note : We have installed Tensorflow version 2.0.0 since the other versions were giving errors.
After this you can fire up your jupyter notebook and check.
import tensorflow
tensorflow.__version__
Going Online
If you prefer to use your Pi only at home using SSH or VNC then your done.
But if your a traveller like me and prefer to travel as light as possible, then you can expose your pi to the internet with my latest favorite tool NGROK.
Installing Ngrok
To install ngrok on your Pi, you need to :
- Goto ngrok.com > Create an account > Download Ngrok
- unzip the downloaded file
- cd into the directory
install ngrok using > ./ngrok authtoken <your_auth_token>
Now goto your TensorPi directory, activate your virtualenv and fire up your jupyter notebook.
After that open a new terminal window and type : ./ngrok http 8888
This will give you a URL which you can use to access your notebook anywhere on the internet, while all your processing power is handled by the Raspberry Pi.
Enjoy!