Hi, This is about custom object detector using tensorflow object detection API. In this project trying to detect batman apperance. Please refer step by step.
- Download the images of the object what you want to detect.
- Download the tensorflow models repo and do installation steps in which is given at object detection.
-
Labeling image is main the process of object detection. It is the process of adding bounding boxes to the objects in the images.
-
In this project Colabeler used for image labeling.
-
After Labeled images the folder structure images have all the images. The subdirectory train and test have the xml files. The folder structure was given below. Create folder named data also in main folder.
Pre Processing | └───/images │ │ // All images present() │ │ │ └───/train │ │ // .xml files of train images │ │ │ └───/test │ // .xml files of test images | └───/data │ // The exported data folder │ └─── xml_to_csv.py │ └─── generate_tfrecord.py -
Execute the xml_to_csv.py file. It will create the test.csv and train.csv inside the data folder.
-
Before creating tf record change the lable row_label to repected class at line 30 in generate_tfrecord.py. Here I have one object so I changed the label as 'batman'
-
After changing line execute generate_tfrecord.py. It will create the test.record and train.record inside the data folder.
The label map file will contain the labels and ids of the object. In this case the object was only one. So the pbtxt file have label for only one object.
-
The creation of .config file for explained in this link.
-
To complete this process the .ckpt file also needed. In this case .ckpt inherited from ssd_mobilenet_v1_coco_2017_11_17. Download the model and move it to object_detection folder
-
Please also refer batman.config file. Here paths need to be changed for "input_path" and "label_map_path" for both test and train and "fine_tune_checkpoint" path to .ckpt file path".
-
Open tensorflow models directory and execute following commands.
# From tensorflow/models/research/ protoc object_detection/protos/*.proto --python_out=. # From tensorflow/models/research/ export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim -
Now move the .pbtxt, .records and .csv files to /research/object_detection/data
-
split images as train and evol folder and move it to /research/object_detection/models/model folder. Also move .config file also to this folder.
-
The final folder structure as follows.
object_detection | └───/data │ │ │ └─── .pbtxt file │ └─── test.record │ └─── train.record │ └─── test.csv │ └─── train.csv | └───/models │ │ │ └───/model │ │ │ │ │ └───/train │ │ │ // Trainning Images │ │ └───/eval │ │ │ // Test Images │ │ └─── .config file │ │ │ └─── Other Files │ └───/ssd_mobilenet_v1_coco_2017_11_17 │ │ │ └─── model.ckpt │ │ │ └─── Other files │ └─── Other folders and files
Move to object_detection folder in terminal and execute following command in terminal.
python model_main.py --pipeline_config_path=models/model/{NAME_OF_CONFIG_FILE}.config --model_dir=models/model --num_train_steps={NUMBER_OF_TRAINNING_STEP} --num_eval_steps={NUMBER_OF_TEST_STEP} --alsologtostderr
Above command will train model locally. If want to train in cloud please refer docs
The trainned model will be available at "object_detection/models/model" folder.
To export frozen_inference_graph.pb file execute following commad.
python export_inference_graph.py --input_type image_tensor --pipeline_config_path models/model/{CONFIG_FILE_NAME}.config --trained_checkpoint_prefix models/model/model.ckpt-{NUMBER} --output_directory {EXPORT_DIRECTORY_PATH}
-
Open jupyter notebook.
-
Open "object_detection_tutorial.ipynb".
-
Change MODEL_NAME and PATH_TO_FROZEN_GRAPH. .
-
Remove download codes for DOWNLOAD_BASE
-
Add images for test in the "test_images" folder and chand TEST_IMAGE_PATHS
-
Run cells.

