Train your own dataset for Object Detection with TensorFlow’s Object Detector API
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:
- Setting up Object Detection Directory
- Configure path variables
- Training Data
- Configuring Training
- Training your model
- Inference Graph
- 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
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.
Once you’re done with this, it will be having the following in it.
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
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
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
Finally, it should contain all these files and folders as shown below.
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,
- All the xml and image files (only if you want or you can use mine) \object_detection\images\train and \object_detection\images\test
- The “test_labels.csv” and “train_labels.csv” files in \object_detection\images
- All files in \object_detection\training
- 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.
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,
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,
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
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,
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,
- Bottleopener
- Spoon
- Fork
- Peeler
- Whisker
For generating the TFRecord files, type the following commands,
You can verify that it has been created from the following,
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.
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,
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
I have marked the path, so you guys too type the same 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,
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,
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),
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.
Here you can see 3 boxes,
- File_path → just give your dataset input path
- Resize_file_path → output i.e. destination directory
- 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..!!!