Custom Object Detection using TensorFlow — (From Scratch)

Image for post
Image for post
Image by Author (Harshil, 2020)

1. Installation

1.1 Tensorflow

1.2 Other dependencies

1.3 Clone the Tensorflow models repository

# For example: ~/anaconda/envs/<your_env_name>/lib/python3.6/site-packages/tensorflow$ cd <path_to_your_tensorflow_installation>

1.4 Setting up the environment

$ cd <path_to_your_tensorflow_installation>/models/research/$ protoc object_detection/protos/*.proto --python_out=.$ export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim

2. Images

Image for post
Image for post
Image by Author (Harshil, 2020)
object_detection
├── data
|
├── training
|
├── images
| ├── test
| ├── train
| ├── ...

2.1 Image Labeling

C:\Users\path\labelImg> python labelImg.py
Image for post
Image for post
Screenshot by Author (Harshil, 2020)
object_detection
├── images
├── test
| ├── cmimage1.jpg
| ├── cmimage1.xml
├── cmimage2.jpg
| ├── cmimage2.xml
├── train
| ├── ...
xml to csv python by author (Harshil, 2020)
Tfreorcrd python by Author(Harshil, 2020)
path/object_detection> python xml_to_csv.py

2.2 Generate_tfrecord

Commands by Author(Harshil, 2020)

1. TensorFlow Detection Model

object_detection
├── data
├── object_detection.pbtxt
├── train.record
├── test.record
├── train_lables.csv
├── test_labels.csv
├── training
├── object_detection.pbtxt
├── pipline.config
├── ssd_mobilenet_v1_pets.config

2. PBTX File

item { id: 1 name: 'cloth_mask'}

3. Training

set PYTHONPATH=$PYTHONPATH:"pwd":"pwd"/slim'
python train.py --train_dir=training/ --pipeline_config_path=training/ssd_mobilenet_v1_pets.config --logtostderr
I1030 12:52:00.296686  3708 learning.py:768] Starting Queues.
INFO:tensorflow:global_step/sec: 0
I1030 12:52:09.401392 30180 supervisor.py:1099] global_step/sec: 0
INFO:tensorflow:Recording summary at step 3217.
I1030 12:52:21.126251 16976 supervisor.py:1050] Recording summary at step 3217.
INFO:tensorflow:global step 3218: loss = 1.7246 (21.576 sec/step)
I1030 12:52:22.335144 3708 learning.py:507] global step 3218: loss = 1.7246 (21.576 sec/step)
INFO:tensorflow:global step 3219: loss = 1.6730 (3.673 sec/step)
I1030 12:52:26.409953 3708 learning.py:507] global step 3219: loss = 1.6730 (3.673 sec/step)
INFO:tensorflow:global step 3220: loss = 1.8269 (4.190 sec/step)
I1030 12:52:30.603331 3708 learning.py:507] global step 3220: loss = 1.8269 (4.190 sec/step)
INFO:tensorflow:global step 3221: loss = 1.9480 (4.277 sec/step)
I1030 12:52:34.883605 3708 learning.py:507] global step 3221: loss = 1.9480 (4.277 sec/step)
INFO:tensorflow:global step 3222: loss = 1.5400 (5.243 sec/step)
I1030 12:52:40.129421 3708 learning.py:507] global step 3222: loss = 1.5400 (5.243 sec/step)
INFO:tensorflow:global step 3223: loss = 1.3421 (5.265 sec/step)
I1030 12:52:45.397172 3708 learning.py:507] global step 3223: loss = 1.3421 (5.265 sec/step)

4. EXPORT GRAPH

python export_inference_graph.py --input_type image_tensor --pipeline_config_path training/ssd_mobilenet_v1_pets.config --trained_checkpoint_prefix training/model.ckpt-3217 --output_directory new_graph

5. Web Cam Detection

Custom Webcam python by Author(Harshil, 2020)
Image for post
Image for post
Image by Author (Harshil, 2020)

6. Google Colab

7. TENSORBORAD (Optional )

tensorboard — logdir=training/
Image for post
Image for post
Image for post
Image for post
Screenshot by Author (Harshil, 2020)

Now practice with more objects and more images, Happy Coding !!!

Written by

Tech Lead and Founder at @XenonStudio. #Mobile #AI. Visit :- https://xenonstudio.in

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store