Tensorflow with Raspberry Pi. If you stumble with pip, solve it with virtualenv!

It's about the virtual environment of Python. We will derail from the middle and start building Tensorflow.

Introduction

virtualenv is one of the packages that builds a lightweight virtual environment within Python.

When developing with Python, you tend to stumble when building an environment. pip or pip or pip. For me as a beginner, environment construction is always the last boss. Really scary.

This is where you talk about yourself, and as one of the uses of this virtualenv, you can manage the packages introduced by the interpreter and pip at the same time. It's a very useful module.

This time I would like to talk about the difficulties when introducing TensorFlow to Raspberry Pi. Rather than a difficult story, is it a series of flows that can be built smoothly with virtualenv?

The goal is to build an environment to move the sample.

Preparation

First of all, prepare the Raspberry Pi that everyone loves. It is assumed that the initial settings have been completed. It doesn't matter if it's SSH or VNC, so for the time being, just open the terminal.

Tool Ver
Python 3.5.3
Tensorflow 1.11.0

Introducing Tensorflow

I think you can install it without any problems.

sudo apt install libatlasbase-dev 
pip install tensorflow 

I can't say I stumbled at this point at the time.

Preparing virtualenv

I wish I could install it without any problems, but in my case, I couldn't install only a specific package.

Here is the main issue. It was a mess until I got to know virtualenv.

Introduction of virtualenv

Install virtualenv.

pip install virtualenv

Build an environment in the current directory

Execute the following in the current directory to create an environment called hoge.

virtualenv --system-site-packages -p python3 ./hoge

Enable virtual environment

Activate the built environment.

source ./hoge/bin/activate

When enabled, the beginning of the terminal

(hoge)$

So that's it.

Preparation of modules, etc.

Install the required modules for Tensorflow. Let's install it individually. If Python2 and Python3 coexist, you can install it with pip3.

Module introduction

Install Cython, contextlib2, pillow, lxml, jupyter, matplotlib.

pip install --user Cython
pip install --user contextlib2
pip install --user pillow
pip install --user lxml
pip install --user jupyter
pip install --user matplotlib2

Install these with the virtual environment enabled. Also, the explanation of each module is omitted.

Introduction of Object Detection API

In order to detect an object, it is necessary to install "Object Detection API". I will omit the details, but if you want to detect objects right away, please introduce it. It is also needed to train your own dataset. This time, I will try to build the environment until the sample is executed.

Preparing to move the sample

This time I will rely on the sample. I'm sorry. First, clone this.

git clone https://github.com/tensorflow/models.git

When the clone is finished

models/research

Please move to.

Therefore

#Compiler installation
sudo apt-get install protobuf-compiler
#compile
protoc object_detection/protos/*.proto --python_out=.a

When you do these, the protbuf library will be compiled without any response.

Next you need to specify the environment variables.

export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim

As a caveat, it may be better to write to the bash file in advance because it needs to be rewritten every time the terminal is started.

Finally, clone the following trained data.

https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/d
etection_model_zoo.md

Now you are ready. After that, I think it's a good idea to execute the code written in Python.

I still can't!

After all it is quick to put it back in.

When re-installing Python and Tensorflow

sudo apt-get install python-pip python3-pip
sudo pip uninstall tensorflow
git clone https://github.com/PINTO0309/Tensorflow-bin.git
cd Tensorflow-bin
sudo pip install tensorflow-1.11.0-cp35-cp35m-linux_armv7l.whl

When introducing Tensorflow individually

https://www.piwheels.org/simple/tensorflow/

I think you should refer to here.

Finally

This time, it is an experience story that I escaped from the predicament of environment construction using virtualenv. Without virtualenv, I would probably have been frustrated. Thank you virtualenv.

I also used these to create a simple thing to figure out how many people are there. I won't write it because it's not particularly attractive, but at that time I was working on an experiment to see if it could be operated with a small hardware called Raspberry Pi, so I'd like to talk about that later, but probably not.

References

-github tensorflow https://github.com/tensorflow/models.git

-piwheels https://www.piwheels.org/

Recommended Posts

Tensorflow with Raspberry Pi. If you stumble with pip, solve it with virtualenv!
[TensorFlow] If you want to run TensorBoard, install it with pip
Build a Tensorflow environment with Raspberry Pi [2020]
GPGPU with Raspberry Pi
DigitalSignage with Raspberry Pi
Mutter plants with Raspberry Pi
Install TensorFlow 1.15.0 on Raspberry Pi
Don't write Python if you want to speed it up with Python
What to do if you can't install pyaudio with pip #Python
Make a thermometer with Raspberry Pi and make it viewable with a browser Part 4
What to do if you get a UnicodeDecodeError with pip install
Read this if you get SSL related errors with pip install! !! !!
[Raspberry Pi] Stepping motor control with Raspberry Pi
Use vl53l0x with Raspberry Pi (python)
Servo motor control with Raspberry Pi
Serial communication with Raspberry Pi + PySerial
OS setup with Raspberry Pi Imager
Try L Chika with raspberry pi
Uninstall everything you installed with pip
VPN server construction with Raspberry Pi
Don't you know it? pip command
Try moving 3 servos with Raspberry Pi
Using a webcam with Raspberry Pi
Get GrovePi + sensor value with Raspberry Pi and store it in kintone
If you can't import even though it is already installed by pip
What to do if you get Could not fetch URL 443 with pip