Tensorflow Object Detection API
Part 1- Installation with tensorflow — CPU : Windows 10
Tensorflow object detection API configuring can be one of the most complex and equally rewarding tasks if you want to leverage power of plug and play already trained deep learning models and quickly train and with some little enhancements, deploy it. While working in my organization, tensorflow object detection api served to be very helpful to few of my colleagues from other department for object detection, segmentation in their projects. Then concerned project was transferred to our department and I was made one of the contributor for that project. In the beginning it gave me a lot of frustration because of different error popups and most of the tutorials were based on Linux environment, So I decided to document the whole process of windows 10 properly so that you don’t have to bang your head against the wall for the similar problems that I faced.
In this article I will be talking about how to install and configure tensorflow object detection api with tensorflow cpu in windows 10. I have written an another article for configuring tensorflow with GPU as well in windows 10, If you want to start with tensorflow gpu, Please go ahead and click this link.
One of the good habit while working with python is to create a separate environment for all your projects, because even if some of your packages got mistakenly installed (incorrect version) which might lead to environment corruption, which in turn can increase your rework by many folds as you have to again uninstall and reinstall anaconda base version which is same as reinstalling anaconda again.
Before proceeding for setting up the environment download models folder using the following git command from git bash in windows, or you can download it manually and unzip it
git clone https://github.com/tensorflow/models.git
Step 1 : Setting up a new environment in anaconda for tensorflow-cpu based object detection api
Launch anaconda command prompt (python3, 64 bit) and type the following command and press enter, type ‘y’ when prompted for permission. I have used name xyz_cpu, you can change it accordingly
base $$ conda create --name xyz_cpu python==3.6
after new environment is installed, you have to launch it with following command
base $$ conda activate xyz_cpuoutput :
xyz_cpu $$
Optional : In case you want to check how many environments you already have, or in case you forget environment names, you can check it using following command
conda env list
Now after activating the environment xyz_cpu, we need to install tensorflow-cpu version and check if its running correctly. To install tensorflow, activate environment xyz_cpu and run following command
pip install tensorflow==1.15
For checking installation run this code
import tensorflow as tf
def main():
hello = tf.constant('hello TF')
sess = tf.Session()
print(sess.run(hello))
if __name__ == '__main__':
main() Output >> hello TF
Usually when setting up the environment from scratch you donot face any issues with tensorflow cpu installation unless there is version mismatch.
Now since tensorflow is up and running its time to install other required packages:
- Protobuf 3.0.0
- Python-tk
- Pillow 1.0
- lxml
- tf Slim (which is included in the “tensorflow/models/research/” checkout)
- Jupyter notebook
- Matplotlib
- Tensorflow (>=1.12.0)
- Cython
- contextlib2
- cocoapi
- numpy
- scikit-learn
pip install protobuf protobuf-compiler lxml cython pillow contextlib2 jupyter matplotlib numpy scikit-learn
Now except, Cocoapi we have installed everything.
Step 2 : Installing and setup pycocotools
Tensorflow object detection API has models trained on various dataset. In this tutorials, I am demonstrating object detection trained on COCO dataset. For doing so, we need to install pycocotools.
COCO is a large image dataset designed for object detection, segmentation, person keypoints detection, stuff segmentation, and caption generation. COCO API package provides Python APIs that assists in loading, parsing, and visualizing the annotations in COCO, and will be present in your system as pycocotools
2.1 First open a git bash and run or download the same from github and extract it
git clone https://github.com/pdollar/coco.git
Then move to this directory
cd coco/PythonAPI
And then edit the setup.py file in the coco/PythonAPI directory from this
extra_compile_args=[‘-Wno-cpp’, ‘-Wno-unused-function’, ‘-std=c99’],
to this
extra_compile_args=[‘-std=c99’], then save it. And then in the coco/PythonAPI directory, run
python setup.py install
If you get an output : Finished processing dependencies for pycocotools==2.0 the it means you successfully finished it
Step 3 : Protobuf compilation
The Tensorflow Object Detection API uses Protobufs to configure model and training parameters. Before the framework can be used, the Protobuf libraries must be compiled. This should be done by running the following command from the models/research/ directory:
For this step download protoc zip file suitable for your system from this link
Extract protoc.exe from bin folder and paste it into script directory for your environment. For example in my system:
C:\Users\windows_user\AppData\Local\Continuum\anaconda3\envs\autoveh\Scripts\
Now run the following command from inside models/research/ directory
protoc object_detection/protos/*.proto --python_out=.
If it runs without any errors, then that means you are good to go to the next step
Step 4 : Adding Libraries to python path
When running locally, the models/research/ and slim directories should be appended to PYTHONPATH.
To check current paths in your python path, run the following one liner from the concerned environment
python -c"import sys; print('\n'.join(sys.path))"
Now we have to add research and research/slim folder to the path as well. For that, run this
#first run >> cd , and check current directory
For ex D:/models/research
#the run
set PYTHONPATH=D:/models/research;D:/models/research/slim
Now if you run, python -c”import sys………))” you can see that research and slim folders are added to python path.
Note : Every time you run tensorflow object detection api, you have to append research/ and research/slim to PYHTONPATH
Testing the installation
You can test that you have correctly installed the Tensorflow Object Detection
API by running the following command:
python object_detection/builders/model_builder_test.py
If you can run it without any errors and get for all then Tensorflow Object Detection API is correctly installed and configured.
Ending Note : In this first article, I described on how to install and configure tensorflow object detection api. In the upcoming posts, I will write about on how to use this object detection api for hand on object detection on real life data sets.
Please give me claps if you liked this article.
About Me
I work as a Data Scientist in Bangalore, India. My interest lies in solving problem statements related to Computer Vision, Image Processing, Machine Learning and Deep Learning. Feel free to connect with me on Linkedin.