Wednesday, June 8, 2022

[Raspberry Pi] Install TensorFlow 2.2 and OpenCV 4.4.0 on Raspberry Pi 4 and use Neural Compute Stick 2


So far as we have known that Raspberry Pi 4, which is based on ARMv7 Processor rev 3 (v7l), has much more CPU computing power than previous generations. Neural Compute Stick 2(NCS 2) is a Plug and Play Development Kit for AI Inferencing via USB. Fortunately, I have both of them. Because of that, I was just wondering how to use them at the same time. The first idea coming to my mind is about AI inferencing. If we can have Tensorflow installed on it and do inferencing using NCS 2, that will be great. 



But, during the period of time trying, I found that it is not easy to have Tensorflow on Raspberry Pi 4. After several hours of experiments, I summarized the following instructions to do so. That is also my memo which helps me to keep the records.


Install Tensorflow on Raspberry Pi 4

I found this post is very good resource for installing Tensorflow on Raspberry Pi 4. Here you go:
https://qengineering.eu/install-tensorflow-2.2.0-on-raspberry-pi-4.html

# get a fresh start
$ sudo apt-get update
$ sudo apt-get upgrade

# install the dependencies (if not already onboard)
$ sudo apt-get install gfortran
$ sudo apt-get install libhdf5-dev libc-ares-dev libeigen3-dev
$ sudo apt-get install libatlas-base-dev libopenblas-dev libblas-dev
$ sudo apt-get install openmpi-bin libopenmpi-dev
$ sudo apt-get install liblapack-dev cython
$ sudo pip3 install keras_applications==1.0.8 --no-deps
$ sudo pip3 install keras_preprocessing==1.1.0 --no-deps
$ sudo pip3 install -U --user six wheel mock
$ sudo -H pip3 install pybind11
$ sudo -H pip3 install h5py==2.10.0
# upgrade setuptools 40.8.0 -> 52.0.0
$ sudo -H pip3 install --upgrade setuptools
# install gdown to download from Google drive
sudo -H pip install gdown
# download the wheel
$ gdown https://drive.google.com/uc?id=11mujzVaFqa7R1_lB7q0kVPW22Ol51MPg
# install TensorFlow

$ sudo -H pip3 install tensorflow-2.2.0-cp37-cp37m-linux_armv7l.whl wrapt --upgrade --ignore-installed
$ sudo apt install libopencv-dev
#$ sudo pip3 install opencv-contrib-python
$ sudo pip3 install numpy==1.19.1

# install matplotlib and related
$ sudo pip3 install python3-matplotlib
# Or if pip3 for matplotlib doesn't work
$ sudo apt install python3-matplotlib
# and complete the installation by rebooting
$ sudo reboot 
$ cd Downloads/


Install OpenVINO™ toolkit for Raspbian* OS

Once we have installed Tensorflow ready, then we can try to use NCS 2 via OpenVINO. I just follow the offical web site to do so:
https://docs.openvino.ai/2020.4/openvino_docs_install_guides_installing_openvino_raspbian.html#install-package

$ sudo mkdir -p /opt/intel/openvino
$ wget https://download.01.org/opencv/2019/openvinotoolkit/l_openvino_toolkit_raspbi_p_2019.1.094.tgz
$ sudo tar -xf  l_openvino_toolkit_raspbi_p_2019.1.094.tgz.tgz --strip 1 -C /opt/intel/openvino
$ sudo apt install cmake
$ source /opt/intel/openvino/bin/setupvars.sh

#Add USB Rules
$ sudo usermod -a -G users "$(whoami)"
$ source /opt/intel/openvino/bin/setupvars.sh
$ sh /opt/intel/openvino/install_dependencies/install_NCS_udev_rules.sh

#Plug in your Intel® Neural Compute Stick 2
#Build and Run Object Detection Sample
$ mkdir build && cd build
$ cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS="-march=armv7-a" /opt/intel/openvino/deployment_tools/inference_engine/samples/cpp/
$ make -j2 object_detection_sample_ssd

#Download the pre-trained Face Detection model
$ wget --no-check-certificate https://download.01.org/opencv/2020/openvinotoolkit/2020.1/open_model_zoo/models_bin/1/face-detection-adas-0001/FP16/face-detection-adas-0001.bin
$ wget --no-check-certificate https://download.01.org/opencv/2020/openvinotoolkit/2020.1/open_model_zoo/models_bin/1/face-detection-adas-0001/FP16/face-detection-adas-0001.xml

#Test OpenVINO example using NCS2
$ sudo apt install eog
$ ./armv7l/Release/object_detection_sample_ssd -m face-detection-adas-0001.xml -d MYRIAD -i faces.png
$ eog out_0.bmp

$ sudo apt install eog
$ ./armv7l/Release/object_detection_sample_ssd -m face-detection-adas-0001.xml -d MYRIAD -i faces.png
$ eog out_0.bmp

#Intel's Deep Learning Inference Engine (DL IE) is a part of Intel® OpenVINO™ toolkit. You can use it as a computational backend for OpenCV deep learning module.
# Install OpenvCV 4.4.0
$ sudo apt install build-essential cmake git pkg-config libgtk-3-dev \libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev \gfortran openexr libatlas-base-dev python3-dev python3-numpy \
libtbb2 libtbb-dev libdc1394-22-dev libopenexr-dev \
libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev

$ sudo dpkg --add-architecture armhf && \
apt-get update && \
apt-get install -y --no-install-recommends \
crossbuild-essential-armhf \
xz-utils \
libgtk2.0-dev:armhf \
libpython-dev:armhf \
libpython3-dev:armhf \
python-numpy \
python3-numpy \
libgstreamer1.0-dev:armhf \
libgstreamer-plugins-base1.0-dev:armhf

#Clone the OpenCV’s and OpenCV contrib repositories:

$ mkdir ~/opencv_build && cd ~/opencv_build
$ git clone https://github.com/opencv/opencv.git
$ git clone https://github.com/opencv/opencv_contrib.git

#Once the download is complete, create a temporary build directory, and navigate to it:
$ cd ~/opencv_build/opencv
$ mkdir -p build && cd build

#Setup environment variables to detect Inference Engine:
$ source /opt/intel/openvino/bin/setupvars.sh
$ export ngraph_DIR=/opt/intel/openvino/deployment_tools/ngraph/cmake/

#Set up the OpenCV build with CMake:
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D WITH_IPP=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D OPENCV_ENABLE_PKG_CONFIG=ON \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D CMAKE_INSTALL_PREFIX=$(python3 -c "import sys; print(sys.prefix)") \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D ENABLE_NEON=ON \
-D CPU_BASELINE="NEON" \
-D WITH_INF_ENGINE=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
-D OPENCV_PYTHON3_INSTALL_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \
-D PYTHON_EXECUTABLE=$(which python3) \
-D BUILD_EXAMPLES=ON ..

#Start the compilation process and install it
$ make -j4 && sudo make install

#Fixed the error of import cv2 on Python3
$ sudo pip3 install numpy==1.19.1
$ cd lib/python3/
$ sudo cp cv2.cpython-37m-arm-linux-gnueabihf.so /usr/lib/python3/dist-packages/

#To verify the installation, type the following commands and you should see the OpenCV version.
#C++ bindings:
$ pkg-config --modversion opencv4

#Python bindings:
$ python3 -c "import cv2; print(cv2.__version__)"


Test all of them together

Building a frontend for a Raspberry Pi Machine Learning Server with Intel Movidius NCS
https://towardsdatascience.com/building-a-frontend-for-a-raspberry-pi-machine-learning-server-with-intel-movidius-ncs-cb940d75103



Reference

https://bbs.huaweicloud.com/forum/thread-82599-1-1.html
https://linuxize.com/post/how-to-install-opencv-on-ubuntu-20-04/
https://github.com/opencv/opencv/wiki/Intel's-Deep-Learning-Inference-Engine-backend#linux










No comments: