/machine-learning-rpi

Setup ML for Raspberry Pi

Primary LanguageDockerfileMIT LicenseMIT

Machine Learning with JupyterLab on a Raspberry Pi

platform version

Run Jupyter Lab with Tensorflow on a Raspberry Pi as a service or within a docker container.

Setup environment

Install Tensorflow

Install Jupyter Lab

Install R and IRkernel (experimental!)

System service

Docker container

Links

Setup environment

Install packages

sudo apt-get update && sudo apt-get upgrade

apt-get install -y --no-install-recommends \
    build-essential \
    libc-ares-dev \
    libeigen3-dev \
    libffi-dev \
    libfreetype6-dev \
    libopenmpi-dev \
    libpng-dev \
    openmpi-bin \
    openssl \
    wget \
    && \
    apt-get clean

Switch to Python 3

apt-get install -y --no-install-recommends \
    python3 \
    python3-dev \
    python3-pip \
    python3-setuptools \
    python3-wheel
pip3 install --upgrade pip
sudo rm /usr/bin/python 
sudo ln -s /usr/bin/python3 /usr/bin/python

Install additional python modules

pip3 install \
    Cython==0.29.24 \
    matplotlib==3.0.2 \
    numpy==1.19.5 \
    pandas==1.0 \
    scikit-learn=0.20.2

Install Tensorflow

Since there are Python wheels available for ARM architecture at https://github.com/lhelontra/tensorflow-on-arm/releases or https://github.com/bitsy-ai/tensorflow-arm-bin we don't need to build it.

Install packages

apt-get install -y --no-install-recommends \
    build-essential \
    gfortran \
    libatlas-base-dev \
    libhdf5-103 \
    libhdf5-dev \
    libhdf5-serial-dev
pip3 install \
    h5py==2.10.0 \
    keras_applications==1.0.8 \
    keras_preprocessing==1.1.2

Build and install binaries

wget https://github.com/lhelontra/tensorflow-on-arm/releases/download/v2.4.0/tensorflow-2.4.0-cp37-none-linux_armv7l.whl
pip3 uninstall tensorflow
pip3 install tensorflow-2.4.0-cp37-none-linux_armv7l.whl

Install Jupyter Lab

Install packages

sudo apt-get install -y --no-install-recommends \
    npm \
    nodejs
sudo npm install -g configurable-http-proxy
pip3 install \
    notebook==6.4.5 \
    jupyterlab==3.2.1

Create a configuration

jupyter notebook --generate-config

Create a password

jupyter notebook password

Modify the settings

~/.jupyter/jupyter_notebook_config.py

c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False
c.NotebookApp.port = 8888
c.NotebookApp.allow_remote_access = True
c.NotebookApp.token = ''
c.NotebookApp.password_required = True
c.NotebookApp.notebook_dir = '<your_notebook_folder>'
c.NotebookApp.default_url = '/lab'

~/.jupyter/jupyter_notebook_config.json

{
    "NotebookApp": {
        "nbserver_extensions": {
            "jupyterlab": true,
            "jupyter_extensions_configurator": true
        }
    }
}

Install R and IRkernel

Install packages

sudo apt remove r-base

sudo apt-get install -y --no-install-recommends \
    libbz2-dev \
    libcurl4-openssl-dev \
    liblzma-dev \
    libreadline-dev \
    libgit2-dev \
    libxml2-dev \
    libpcre3 \
    libpcre3-dev

Build and install binaries

wget https://ftp.fau.de/cran/src/base/R-4/R-4.1.1.tar.gz
tar -xvf R-4.1.1.tar.gz
rm R-4.1.1.tar.gz
cd R-4.1.1
./configure --with-x=no --disable-java --with-pcre1 --prefix=<r_home_directory>
make && make install
cd ..
rm R-4.1.1

Create soft links

ln -s <r_home_directory>/bin/R /usr/local/bin/R
ln -s <r_home_directory>/bin/Rscript /usr/local/bin/Rscript

Install IRkernel

install.packages('IRkernel', repos='http://cran.rstudio.com/')
IRkernel::installspec()

Use system service

Create the service file /lib/systemd/system/jupyterlab.service.

[Unit] 
Description=JupyterLab Service 
After=multi-user.target  

[Service] 
User=<user_name> 
ExecStart=/usr/local/bin/jupyter notebook
Restart=on-failure

[Install] 
WantedBy=multi-user.target

Start the service.

sudo systemctl daemon-reload 
sudo systemctl start jupyterlab
sudo systemctl enable jupyterlab 
sudo systemctl status jupyterlab.service

If the status command shows "active (running)" the Jupyter Lab should be reachable by http://<server_ip_address>:8888/lab.

Use Docker container

The docker container is based on Debian Buster for arm32v7 and installs

Environment variables

  • JUPYTER_PASSWORD = jupyter
  • TINI_VERSION = 0.19.0 (used for build only)
  • TENSORFLOW_VERSION = 2.4.0 (used for build only)

Install packages

curl -sSL https://get.docker.com | sh
sudo usermod -aG docker <user_name>
sudo pip3 install docker-compose
sudo systemctl enable docker

Build and start container

docker-compose build
docker-compose up -d

Links

https://towardsdatascience.com/setup-your-home-jupyterhub-on-a-raspberry-pi-7ad32e20eed

https://github.com/kleinee/jns (MIT License)

https://github.com/armindocachada/raspberrypi-docker-tensorflow-opencv/blob/main/Dockerfile_tensorflow

https://raspberrypi.stackexchange.com/questions/107483/error-installing-tensorflow-cannot-find-libhdfs-so

https://github.com/kidig/rpi-jupyter-lab

https://github.com/ml-tooling/ml-workspace (Apache-2.0 License)