Train your own dataset for Object Detection with TensorFlow’s Object Detector API

Narendiran Krishnan
9 min readApr 27, 2020

--

Here I will walk you through the steps to create your own Custom Object Detector with the help of Google’s Tensorflow Object Detector API using Python3.

You can find the code in the entire code here in my GITHUB repo.

Motivation:

Taking advantage of Lock Down, Corona or COVID-19, that’s all and I just wanna get my hands dirty with TensorFlow.

We are going to use Google’s TensorFlow Object Detection API.

Steps to be followed to implement this project:

  1. Setting up Object Detection Directory
  2. Configure path variables
  3. Training Data
  4. Configuring Training
  5. Training your model
  6. Inference Graph
  7. Testing our model

I have refereed multiple tutorials and blogs and worked on this and special thanks for the author of pythonprogramming.net and Edje Electronics.

Setting up Object Detection Directory:

The first thing you need to download is the official Tensorflow Model repo.

Once you export it rename the folder to model and create a folder named tensorflow2 and put the entire models folder into this tensorflow2

i.e. it already has official TensorFlow codes and along with that you will have to copy my repo as shown below

extracted files in Tensorflow2 folder

Now, once you’re done with step, you need to do once again do a GITHUB repo, this time its my repo (narenltk) which actually has the required files.

Link: https://github.com/narenltk

Once you’re done with this, it will be having the following in it.

files in narenltk github repo

Now you need to copy paste all the files and folder of my repo into the tensorflow2 folder,

tensorflow2\models\research\object_detection

Now we need to download the Faster-RCNN-Inception-V2-COCO model from TensorFlow’s model zoo

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

For this you need to download faster rcnn inception v2 coco, it is not that you should download only this you can download any model but I have executed with this example

download the faster rcnn model

Once you’re done with this extract it and once again you need to put it in the same folder where you have already put my github repo i.e. into the following folder

extract Faster RCNN files and put it in Object_detection folder

Finally, it should contain all these files and folders as shown below.

after copying all files it should these files that i have marked

Here in the Images folder I have labelled in the dataset with LabelImg tool.

If you want to download LabelImg you need to type in the following command:

Pip install labelimg

You can find the details of this in by GITHUB repo page, where I would have explained you guys how to install and access it.

I will let you guys find out how doing the labelling using this LabelImg as I have already done this work and you can find all the details in the Images and xml files there.

Since we are going to train our dataset from scratch, you need to delete the following files in the folder,

  1. All the xml and image files (only if you want or you can use mine) \object_detection\images\train and \object_detection\images\test
  2. The “test_labels.csv” and “train_labels.csv” files in \object_detection\images
  3. All files in \object_detection\training
  4. All files in \object_detection\inference_graph

Now we have mostly done our setting.

Once you’re done with this you can proceed with the necessary installation along with the files that are to be deleted you need to install the following i.e. the Python3 modules that are required for this project. You can find all the necessary details in my GitHub repo.

Python modules files

These are the files that are required for the project.

If you’re using Tensorflow 2.x version then you may have to face a lot of issues and work on finding an equivalent code.

Configure path variables

You need to do the following command, if you don’t this properly, you will encounter an error,

set PYTHONPATH=C:\tensorflow2\models;C:\tensorflow2\models\research;C:\tensorflow2\models\research\slim

For some you guys will be encountering

No module named ‘deployment’ or No module named ‘nets’

Don’t bother with it, I have given solution for that in my Git repo, you can visit and solve it, if the above path doesn’t work.

Once you are done with it, you need to copy and paste the following command into the command line and press Enter make sure that you’re in object_detection folder:

protoc — python_out=. .\object_detection\protos\anchor_generator.proto .\object_detection\protos\argmax_matcher.proto .\object_detection\protos\bipartite_matcher.proto .\object_detection\protos\box_coder.proto .\object_detection\protos\box_predictor.proto .\object_detection\protos\eval.proto .\object_detection\protos\faster_rcnn.proto .\object_detection\protos\faster_rcnn_box_coder.proto .\object_detection\protos\grid_anchor_generator.proto .\object_detection\protos\hyperparams.proto .\object_detection\protos\image_resizer.proto .\object_detection\protos\input_reader.proto .\object_detection\protos\losses.proto .\object_detection\protos\matcher.proto .\object_detection\protos\mean_stddev_box_coder.proto .\object_detection\protos\model.proto .\object_detection\protos\optimizer.proto .\object_detection\protos\pipeline.proto .\object_detection\protos\post_processing.proto .\object_detection\protos\preprocessor.proto .\object_detection\protos\region_similarity_calculator.proto .\object_detection\protos\square_box_coder.proto .\object_detection\protos\ssd.proto .\object_detection\protos\ssd_anchor_generator.proto .\object_detection\protos\string_int_label_map.proto .\object_detection\protos\train.proto .\object_detection\protos\keypoint_box_coder.proto .\object_detection\protos\multiscale_anchor_generator.proto .\object_detection\protos\graph_rewriter.proto .\object_detection\protos\calibration.proto .\object_detection\protos\flexible_grid_anchor_generator.proto

Initially the Object detection folder in a Tensorflow models folder has protos folder where which contains the following,

before execution of protoc

In order to generate the python files for this .proto files we need to run the above code and we will be having the following now once we run it,

after execution of protoc command

Run the following commands from the

C:\tensorflow2\models\research directory:

C:\tensorflow2\models\research> python setup.py build

C:\tensorflow2\models\research> python setup.py install

You should have the following output

build command
install command

Training Data

Since I have already labelled the training images all we need to do is only to generate the TFRecords that serve as input data to the TensorFlow training model. We are going to work with xml_to_csv.py and generate_tfrecord.py scripts from Dat Tran’s Raccoon Detector dataset, with our directory.

To create the csv of train and test files .xml data are being used from \object_detection folder,

xml to csv file
test and train labels created

Now we are going to create our own label map, in order to do so, open the generate_tfrecord.py file in any text editor. There will a #TO-DO at line 31 you need to customize it accordingly, here in our dataset, there are 5 classes and make the changes in the code as follows,

generate_tfrecord.py
  1. Bottleopener
  2. Spoon
  3. Fork
  4. Peeler
  5. Whisker

For generating the TFRecord files, type the following commands,

check my git repo for this code
train record output
test record output

You can verify that it has been created from the following,

verify in object_detection folder

Another last thing to do before training is to create the label map and editing the configuration file.

In the Training folder you can see a faster rcnn config file and you can also see the labelmap_narenltk.pbtxt file.

labelmap_narenltk.pbtxt file

You need to open and make the same changes that you did in the generate_tfrecord.py file.

Here I have already done the needful if you need to do the modification you can add up your own classes by removing my classes.

Now we are going to configure the object detection training pipeline, which will define what are the parameters that’s going to be used for training.

Move to C:\tensorflow2\models\research\object_detection\samples\configs

and copy the

faster_rcnn_inception_v2_pets.config

file into the \object_detection\training directory.

Now choose any text editor and do the needful, as there is going to be several changes that are supposed to be done properly,

changes in config file

Well, make the changes in the marked place; just put 5 if your using kitchen utilities, which I annotated, if you use your own dataset you need use your total number of classes.

Next is defining the path, if in any case you encounter a problem you can look into my config file that I have in my repo

fine tune checkpoint path

I have marked the path, so you guys too type the same path.

test and train record paths with labelmap_narenltk.pbtxt path

Kindly look into the image above and type them as given in the image or you can also refer the repo.

You need to look into the num_examples in line 130 where you need to specify the exact number of testing images that you have given for the model.

You can follow up where to make changes by looking into the line of code.

Training your model

Now get back to the \object_detection directory and type in the following command,

python train.py — logtostderr — train_dir=training/ — pipeline_config_path=training/faster_rcnn_inception_v2_pets.config

You can refer my git repo too, I have pasted the code for this too.

If you’re using a CPU, then it will take around more than 2 days or 48 hrs to make sure that losses reach below 0.0500 i.e. to be like constantly in this range.

Once in every 5 min Tensorflow saves the checkpoint, once the training is over we need to check the checkpoint with the highest number.

Inference Graph

Now we need to generate the Inference Graph and to do that we need to make note of the model.ckpt-xxxx number as shown,

make note of number 1721

Here the highest number is 1721.

Then type in the following command,

python export_inference_graph.py — input_type image_tensor — pipeline_config_path training/faster_rcnn_inception_v2_pets.config — trained_checkpoint_prefix training/model.ckpt-1721 — output_directory inference_graph

Testing our model

Now open the Object_detection_webcam.py and do the following changes, nothing big, it’s just NUM of classes to 5 or based on the classes you need have added,

make changes as 5

Once you do this and run the program, well yeahh…!!!

Now you have your own Object Detector, check the output which I took from my lappy(less configuration),

bottle opener output
fork output

Anytime anywhere if you’re struck with some error, you can also refer to my GITHUB repo.

Increasing you dataset:

In any case, if you feel like you have less number of images with you which is not going to make your training any better, then you can use the data_argumentation.py file.

data_augmentation.py

Here you can see 3 boxes,

  1. File_path → just give your dataset input path
  2. Resize_file_path → output i.e. destination directory
  3. Extension → output in what extension you want, jpg / jpeg / png file format.

you can read my other blog where I have displayed how to train on Google Colab freely.

If you wish to stay connected,

you can just google “ narenltk / narendiran krishnan ” or just drop a mail to → narenltk@gmail.com → Happy to help..!!!

--

--

Narendiran Krishnan
Narendiran Krishnan

Written by Narendiran Krishnan

AI blogger. Inspiring & working towards a better future through technology & Artificial Intelligence. Join me in the quest ..!!

No responses yet