Custom Object Detection using TensorFlow — Part 2 (Train a Custom Model) Local + Colab
--
Part 1 — https://harshilp.medium.com/custom-object-detection-using-tensorflow-part-1-from-scratch
In this part we will train a custom model and will test it on web cam. We have generated train.record, test.record and csv files in part 1.
1. TensorFlow Detection Model
Open the above repo and download any model you want and place the folder into models>research>object_detection. I am using ssd_mobilenet_v1_coco
Now add https://github.com/Bengemon825/TF_Object_Detection2020/blob/master/masks/training/ssd_mobilenet_v1_pets.config pet file into ‘training’ folder.
Make changes to the PATHs and num of objects you have
Now copy pipline.conifg file in ssd_mobilenet_v1_coco and paste it into training folder. It should look something like this
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
In training folder create a .txt file and paste the below code
item { id: 1 name: 'cloth_mask'}
Now click on Save As, Name it ‘object-detection.pbtxt’ and select all files and Save it. Make n item if you have n label for object detection, make sure you use same label name used in csv files. Now copy this pbtxt file and paste it into data folder.
3. Training
Now that you have added pbtxt file in data and training folder. Open anaconda cmd cd to models\research\object_Detection and run -
set PYTHONPATH=$PYTHONPATH:"pwd":"pwd"/slim'
then
python train.py --train_dir=training/ --pipeline_config_path=training/ssd_mobilenet_v1_pets.config --logtostderr
if you get error saying no module found, then just copy that file and paste in into object_detection. Make sure you just copy it, don't delete it from main dir.
If any other error is showing make sure you check your .record file, generate_tfrecord file. Make sure they are not empty or have any error in them.
Once you successfully run the cmd you will be able to see that model is getting trained with losses and time. It will look something like this-
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)
make sure you are generating losses, once your loss rate is equal or less than 1, press CTRL+C to stop training.
4. EXPORT GRAPH
Now that your training is over head to object_Detection folder and open training folder. You will see data,index,meta file generated with same name. Check the latest files like — model.ckpt-3217
now run, and edit the ckpt file name in below cmd with your latest file.
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
Now go to object_detection folder, new_graph folder will be generated.
5. Web Cam Detection
Create a custom web cam detection python file in object_detection dir. , paste below code —
Now open pycharm and run the above file. Your web cam will pop-up and you will be detect face mask with label and accuracy. You can even run this on your cmd prompt.
If you are not able to run any cmd —
- check with the version code
- check the path
- check .record, .csv, .config etc files are there
- lxml, slim and other are installed
- check label name it should be same in csv, generate tf record file and pbtxt file.
- use about 100+ images for each object
6. TENSORBORAD (Optional )
TensorBoard provides the visualization and tooling needed for machine learning experimentation:
- Tracking and visualizing metrics such as loss and accuracy
- Visualizing the model graph (ops and layers)
- Viewing histograms of weights, biases, or other tensors as they change over time
- Projecting embeddings to a lower dimensional space
- Displaying images, text, and audio data
- Profiling TensorFlow programs
Now open another anaconda cmd and run in object_detection
tensorboard — logdir=training/
It will provide link to your local host, like this http://localhost:6006/#
I will write separate blog on how to Train model on Google Colab !
Now practice with more objects and more images, Happy Coding !!!
Imp Links-
Part 1 — https://harshilp.medium.com/custom-object-detection-using-tensorflow-part-1-from-scratch
https://gist.github.com/its-harshil/7c91148289b93975c822b223bc99975f