Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 08aaf5b

Browse files
Update README.md
1 parent fe1e17c commit 08aaf5b

File tree

1 file changed

+68
-9
lines changed

1 file changed

+68
-9
lines changed

‎README.md‎

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,101 @@
11
# Tensorflow_API-Custom_object_detection
22
A sample project to detect custom object using Tensorflow object detection API
33

4+
45
## Folder Structure
56
- Tensorflow_API-Custom_object_detection
7+
- pre_trained_model
8+
- *downloaded files for choosen pretrained model*
69
- dataset
710
- Annotations
8-
- *Annotations for your images comes here*
11+
- *Annotations for your training images comes here*
912
- JPEGImages
1013
- *all of your images for training comes here*
1114
- testImages
1215
- *all your images for testing comes here*
1316
- lable.pbtxt
1417
- train.record
15-
- output
18+
- IG
1619
- *inference graph will be saved here*
17-
- train
20+
- CP
1821
- *checkpoints will be saved here*
1922
- eval.ipynb
2023
- train.ipynb
24+
- *config file for the choosen model*
25+
2126

2227
## Steps
2328

2429
###### Create folders
2530
Create the folders following the structure given above
2631

27-
###### prepare train and test images
28-
This repository contains train and test images for detection of "UE Roll" blue bluetooth speaker but I will highly recommend you to create your own dataset. Pick up an object you want to detect and take some pics of it with varying backgrounds, angles and distances. Sample of images used in this sample project is given below:
32+
###### Prepare train and test images
33+
This repository contains train and test images for detection of "UE Roll" blue bluetooth speaker but I will highly recommend you to create your own dataset. Pick up an object you want to detect and take some pics of it with varying backgrounds, angles and distances. Some of the sample images used in this sample project is given below:
2934

3035
![img-2861](https://user-images.githubusercontent.com/5885636/46312084-b2084500-c5e1-11e8-909d-b9946b63343e.jpg)
3136

32-
Once you have captured pictures, transfer it to your PC and resize it to smaller size (given images have size of 605 x 454) so that your training will go smoothly without running out of memory. Now divide your captured images in to two chunks, one chunk for training and other for testing. Finally move training images in to *JPEGImages* folder and testing images in to *testImages* folder.
37+
Once you have captured pictures, transfer it to your PC and resize it to smaller size (given images have size of 605 x 454) so that your training will go smoothly without running out of memory. Now reaname (for better referencing later) and divide your captured images in to two chunks, one chunk for training(80%) and other for testing(20%). Finally move training images in to *JPEGImages* folder and testing images in to *testImages* folder.
3338

3439
###### Label the data
35-
Now its time to label your training data. We will be doing it using labelImg library. To download this library use [THIS LINK](https://github.com/tzutalin/labelImg).
36-
Once you have labelImg library downloaded on your PC, run lableImg.py. Select *JPEGImages* directory by clicking on *Open Dir* and change your save directory to *Annotations* by clicking on *Change Save Dir*. Now all you need to do is to draw rectangles around the you are planning to detect after clicking on *Create RectBox* and save it so that Annotations will get saved as .xml file in *Annotations* folder.
40+
Now its time to label your training data. We will be doing it using [labelImg library](https://pypi.org/project/labelImg/). To download this library along with its dependencies use [THIS LINK](https://github.com/tzutalin/labelImg).
41+
Once you have labelImg library downloaded on your PC, run lableImg.py. Select *JPEGImages* directory by clicking on *Open Dir* and change your save directory to *Annotations* by clicking on *Change Save Dir*. Now all you need to do is to draw rectangles around the object you are planning to detect. You will need to click on *Create RectBox* and then you will get cursor to label the objects. After labeling image give the name for label and save it so that Annotations will get saved as .xml file in *Annotations* folder.
3742

3843
![screenshot 2018年10月02日 01 08 08](https://user-images.githubusercontent.com/5885636/46311801-eb8c8080-c5e0-11e8-8444-aa45e39b1414.png)
3944

40-
___________Under progress _____________
45+
###### Setup Tensorflow models repository
46+
Now it time when we will start using Tensorflow object detection API so go ahead and clone it using following command
47+
```
48+
git clone https://github.com/tensorflow/models.git
49+
```
50+
Once you have cloned this repository, change your present working directory to models/reserarch/ and it to your python path. It you want to add it permanently then you will have to make change in your .bashrc file or you could do same temproarily for current session using following command:
51+
```
52+
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
53+
```
54+
You also need to run following command in order to get rid of string_int_label_map_pb2 issue (more details [HERE](https://github.com/tensorflow/models/issues/1595))
55+
```
56+
protoc object_detection/protos/*.proto --python_out=.
57+
```
58+
59+
###### Convert you data to Tensorflow record format
60+
In order to use Tensorflow API you need to feed data in Tensorflow record format. Thankfully tensorflow gives python script to convert Pascal VOC format dataset to tensorflow record format. Path for the file I mentioned in last line is given below:
61+
*models/research/object_detection/dataset_tools/create_pascal_tf_record.py*
62+
Now you have two options, either follow pascal VOC dataset format or modify the tesorflow script as per your need. I modified the script and I have placed same in this repository inside the folder named as *extra*. All you need to do is to take this script and replace original script with this. If you do so, you dont need to follow any specific format.
63+
After doing all this circus, there is one last thing is still remaining before we get our Tensorflow record file. You need to create a file for label map, in this repo its label.pbtxt, with dictionaly of label and id of items. Check label.pbtxt given in this repository to undestand the format, its pretty simple (Note: name of label should be same as what you had given while labeling object using labelImg). Now it time to create record file. From models/research as present working directory run following command to create Tensorflow record:
64+
```
65+
python object_detection/dataset_tools/create_pascal_tf_record.py --data_dir=<path_to_your_data_directory> --annotations_dir=<name of annotations directory> --output_path=<path_where_you_want_record_file_to_be_saved> --label_map_path=<path_of_label_map_file>
66+
```
67+
For more help run following:
68+
```
69+
python object_detection/dataset_tools/create_pascal_tf_record.py -h
70+
```
71+
An example path will be:
72+
```
73+
Python object_detection/dataset_tools/create_pascal_tf_record.py --data_dir=/Users/vijendra1125/Documents/tensorflow/object_detection/speaker_detection/dataset --annotations_dir==Annotations --output_path=/Users/vijendra1125/Documents/tensorflow/object_detection/speaker_detection/dataset/train.record --label_map_path=/Users/vijendra1125/Documents/tensorflow/object_detection/speaker_detection/dataset/label.pbtxt
74+
```
75+
76+
###### Training
77+
Now that we have data in right format to feed, we could go ahead with training our model. First thing you to do is to select the pretrained you would like to use. You could check and download pretrained model from [Tensorflow detection model zoo Github page](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md). Once downloaded, extract all file to the folder you had created for saving pretrained model files. Next you need to copy to *models/research/sample/configs/<your_model_name.config>* and paste in your project repo. You need to configure 5 paths in this file. Just open this file and search for PATH_TO_BE_CONFIGURED and replace it with path of required file. I used pretrained faster RCNN trained on COCO dataset and I have added modified config file (along with PATH_TO_BE_CONFIGURED as comment above lines which has been modified) for same in this repo. Now you are all set to train your model, just run following comand with models/research as present working directory
78+
```
79+
python object_detection/legacy/train.py --train_dir=<path_to_the folder_for_saving_checkpoints> --pipeline_config_path=<path_to_config_file>
80+
```
81+
An example will be
82+
```
83+
python object_detection/legacy/train.py --train_dir=/Users/vijendra1125/Documents/tensorflow/object_detection/speaker_detection/CP --pipeline_config_path=/Users/vijendra1125/Documents/tensorflow/object_detection/speaker_detection/faster_rcnn_resnet101_coco.config
84+
```
85+
Let it train till loss will be below 0.1 or even lesser. once you she loss is as low as you want then give keyboard interupt. Checkpoints will be saved in CP folder. Now its time to generate inference graph from saved checkpoints
86+
```
87+
python object_detection/export_inference_graph.py --input_type=image_tensor --pipeline_config_path=<path_to_config_file> --trained_checkpoint_prefix=<path to saved checkpoint> --output_directory=<path_to_the_folder_for_saving_inference_graph>
88+
```
89+
An example will be
90+
```
91+
python object_detection/export_inference_graph.py --input_type=image_tensor --pipeline_config_path=/Users/vijendra1125/Documents/tensorflow/object_detection/speaker_detection/faster_rcnn_resnet101_coco.config --trained_checkpoint_prefix=/Users/vijendra1125/Documents/tensorflow/object_detection/speaker_detection/train/model.ckpt-1691 --output_directory=/Users/vijendra1125/Documents/tensorflow/object_detection/speaker_detection/IG
92+
```
93+
**Bonus:If you want to train your model using Google colab then check out the train.ipynb file**
94+
95+
###### Test the trained model
96+
Finaly its to time to check the result of all the hard work you did. All you need to do is to copy model/research/object_detection/object_detection_tutorial.ipynb and modfy it to work with you inference graph. A modified file is aready given as eval.ipynb with this repo, you just need to chnage path and the number of images you have given as test image. below is the result of the model trained for detecting "UE Roll" blue bluetooth speaker.
97+
98+
99+
41100

42101

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /