Bitz'nPieces

Home Tech Notes Blog Art Gallery About

Introduction to Object-Detection using Tensorflow

Machinelearning and AI is all the hype these days and a big percentage of students find themselves working towards implementing one or another usecase using one of the several tools available for dealing with ML problems. As far as I can see, object detection is the most common AI problems that one tries to solve when starting off on this path. In my opinion google has done a terrific job with TensorFlow, and the Object-Detection API. And setting up a custom object detector is relativley quite easy now. With this Object-Detection API in place, the only problem left to solve is preparing the data-set for your problem. This in it self could be a huge pain sometimes.

In this post I thought I would cover on how to get set up with TensorFlow and the TensorFlow Object Detection API, as I also had to go through this process which could at times be a little frustrating in itself. I mostly work on linux, so this post is for linux, but i will make an other one for Windows soon. Also this would be the first time I am going to be writing a tutorial of any kind so I am pretty sure at the end of it all there will be a huge room for improvement.

What is TensorFlow? It is an opensource library that is commonly used for machine learning applications and to qoute their web-page "TensorFlow makes it easy for beginners and experts to create machine learning models for desktop, mobile, web, and cloud". So pretty niffty and versatile huh? At the moment there are two variants of TF available depending on the hardware you plan you use, they are TensorFlow CPU and TensorFlow GPU. For the sake of this tutorial I will cover the CPU variant. The processing will be slower here as the GPU is not utilizes for training but there are less steps involved in setting it up. So lets dive right in...

Down the rabbit hole we go.

STEP-1: Setting up the workspace: I covered creating python virtual environments in an earlier tutorial, so If you have doubts go ahead and hit that tutorial up, otherwise now is the time to create a working directory and creating a dedicated virtual-environment for the project. For the sake of consistency I named the workspace as ObjectDetection and the virtual-environment as tfenv

STEP-2: TensorFlow Installation: Open a new terminal window and activate the tfenv environment. At this point the name of the environment should be visible in front of the path specifier in the active terminal.

(tfenv) (base) ahmad@bitznpieces:~/ObjectDetection$

Run the following commands to install TensorFlow, I am going to be using version 1.14 for this tutorial. The reason for this is the many scripts in ObjectDetection API still do not support Tf>=2.0 and this causes many problems during training and execution.

pip install --upgrade pip pip install tensorflow==1.14

STEP-3: Testing The Installation: For TF>=2.0, for testing the installation, fire up the python interpretor, and the following code snippet should print the famous "Hello World" phrase along with some debug messages.

python >>>import tensorflow as tf >>>message = "Hello world! I am running TensorFlow!" >>>tf.print(message)

Since we are now working with TF1.14, use the following code instead for testing the installation:

python >>>import tensorflow as tf >>>msg = tf.constant("Hello world! I am running TensorFlow!") >>>sess = tf.Session() >>>print(sess.run(msg)) b'Hello world! I am running TensorFlow!'

STEP-4: Installing Dependencies: Getting the object detecion up and running requires installation of several other packages that will be used for visualizations, conversions etc. Go ahead and make a requirements.txt file now with the following packages:

jupyter matplotlib opencv-python pathlib lxml pillow Cython contextlib2

With the file in place install the packages using pip:

pip install -r requirements.txt

STEP-5: TensorFlow Models: TensorFlow Models repository contains several implementations and utilities of ML models, some of which we will be using today for this tutorial. You can read more about the contents of the repo here. Go ahead and clone the repo in in the ObjectDetection directory. At the time of writing this tutorial version 2.1.0 is the latest release.

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

STEP-6: Protobuf Installation: Protobuf short for protocol buffers are language neutral mechanism developed by google for serializing data.TensorFlow Object Detection API uses protobufs for models' & training parameters' configurations. There are two ways to get the protobuf ready for your object detection needs:

- STEP-6(a):

sudo apt-get install protobuf-compiler python-pil python-lxml python-tk # cd to /tensorflow/models/research directory: protoc object_detection/protos/*.proto --python_out=.

- STEP-6(b): The alternative is manually getting the protoc 3.0 release and compiling it. Download the protoc ver 3.0.0 release from here and extract it to /tensorflow/models/research/ directory

Now for compiling the protoc, from /tensorflow/models/research/ directory run the following command in the terminal:

./bin/protoc object_detection/protos/*.proto --python_out=.

STEP-7: COCOAPI Installation(optional): To be done

STEP-8: Adding Libraries to PythonPath: The paths to the relevant directories need to be appended to PYTHONPATH, this is done by executing the following path from tensorflow/models/research directory:

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

The above command is needed to be run on every new terminal before object-detection could be run. If you want to avoid this hassle then add the above line to the end of your ~/.bashrc file

gedit ~/.bashrc # update and save the file. source ~/.bashrc

STEP-9: Testing The Installation(TF-ObjectDetection API): To verify the installation of the Tensorflow ObjectDetection API run the following command.

# From /tensorflow/models/research directory: python object_detection/builders/model_builder_test.py

STEP-10: Taking ObjectDetection API for a test-drive:Change the directory to /tensorflow/models/research/object_detection and run jupyter notebook like so:

tensorflow/models/research/object_detection$ jupyter notebook

Scroll down and find the jupyter notebook named object_detection_tutorial.ipynb and open it. Go to the Cell tab and click "Run All". This will start the kernel and once the execution is completed, you will see the processing results of some test images included in the library in models/research/object_detection/test_images directory. You can also put your own images here to test with this tutorial notebook or change the path to the directory containing your images in cell[11] for the variable named PATH_TO_TEST_IMAGES_DIR.

STEP-11: Give yourself a pat on the back for getting through this tutorial and successfully getting every thing up and running.

I will cover preparing a custom dataset and training a model on it in one of the upcomming tutorials so stay tuned and code on!

* ------ * ------ * ------ *