Saturday, March 24, 2018

How to use Tensorflow Object Detection API

1. How to use Tensorflow Object Detection API
2. How to train for Tensorflow Object Detection API
3. How to use Tensorboard
4. How to use a trained model of TF Detect in Android

I am using Linux Mint.

At first, you need tensorflow:
$ python3.6 -m pip install tensorflow

If your pc has GPU, install tensorflow-gpu:
$ python3.6 -m pip install tensorflow-gpu
(These instructions might help you if you have problem:
Install Keras on Linux
Install Keras on windows)

Then you need to install tensorflow object detection:
$ cd ~/Documents
$ git clone https://github.com/tensorflow/models.git 
Then you can read the official start guide from
/models/research/object_detection/g3doc/installation.md

Install dependencies:
$ sudo apt-get install protobuf-compiler python-pil python-lxml python-tk
$ python3.6 -m pip install jupyter
$ python3.6 -m pip install matplotlib
$ python3.6 -m pip install pillow
$ python3.6 -m pip install lxml

Make sure you have the latest cython:
$ python3.6 -m pip install --upgrade --user cython

COCO API installation:
git clone https://github.com/cocodataset/cocoapi.git
cd cocoapi/PythonAPI

We must rewrite the "PythonAPI/Makefile".
all:
    # install pycocotools locally
python3.6 setup.py build_ext --inplace #Change the python version!
rm -rf build

install:
# install pycocotools to the Python site-packages #Change the python version!
python3.6 setup.py build_ext install
rm -rf build

Then do the make command.
make
cp -r pycocotools ~/Documents/models/research/

Protobuf Compilation:
$ cd ~/Documents/models/research/
$ protoc object_detection/protos/*.proto --python_out=.
$ export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
Note: you need to export the PYTHONPATH every time you open the console. It's just temporarily adding it to the path.

Check if you successfully installed everything:
$ cd ~/Documents/models/research/
$ python3.6 object_detection/builders/model_builder_test.py

If it says it's OK, finally you can use the object detection API now!
Go to the research directory and start jupyter.
$ cd ~/Documents/models/research/object_detection/
$ jupyter notebook
If vscode opens instead of the browser, try "export BROWSER=/usr/bin/firefox" and "jupyter notebook".

You will see this:

Click on "object_detection_tutorial.ipynb":

Click on "Run". Every time you click on it, the program execution will proceed one by one.





If you want to close the notebook, hit "ctrl + c" on your console.