Skip to content

Commit d219340

Browse files
DONE: Preparing your custom dataset for training
1 parent 79cd9d5 commit d219340

File tree

417 files changed

+5836
-11
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

417 files changed

+5836
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
.idea/
3+
data/raccoon_dataset
34

45
# Byte-compiled / optimized / DLL files
56
__pycache__/

README.md

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Python 3.6](https://img.shields.io/badge/Python-3.6-3776AB)](https://www.python.org/downloads/release/python-360/)
44
[![TensorFlow 2.2](https://img.shields.io/badge/TensorFlow-2.2-FF6F00?logo=tensorflow)](https://github.com/tensorflow/tensorflow/releases/tag/v2.2.0)
55

6-
### Introduction
6+
## Introduction
77

88

99
With the [announcement](https://blog.tensorflow.org/2020/07/tensorflow-2-meets-object-detection-api.html) that [Object Detection API](https://github.com/tensorflow/models/tree/master/research/object_detection) is now compatible with Tensorflow 2, I tried to test the new models published in the [TF2 model zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md), and train them with my custom data.
@@ -14,16 +14,18 @@ This tutorial should be useful for those who have experience with the API but ca
1414
However, I will add all the details and working examples for the new comers who are trying to use the object detection api for the first time, so hopefully this tutorial will make it easy for beginners to get started and run their object detection models easily.
1515

1616

17-
### Roadmap
17+
## Roadmap
1818

1919
This tutorial should take you from installation, to running pre-trained detection model, and training/evaluation your models with a custom dataset.
2020

2121
1. [Installation](#installation)
2222
2. [Inference with pre-trained models](#inference-with-pre-trained-models)
23-
3. Preparing your custom dataset for training
24-
4. Training with your custom data, and exporting trained models for inference
23+
3. [Preparing your custom dataset for training](#preparing-your-custom-dataset-for-training)
24+
4. Training object detction model with your custom dataset
25+
5. Exporting your trained model for inference
2526

26-
### Installation
27+
28+
## Installation
2729

2830
The examples in this repo is tested with python 3.6 and Tensorflow 2.2.0, but it is expected to work with other Tensorflow 2.x versions with python version 3.5 or higher.
2931

@@ -96,8 +98,15 @@ python object_detection/builders/model_builder_tf2_test.py
9698

9799
For more installation options, please refer to the original [installation guide](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2.md).
98100

101+
To run the examples in this repo, you will need some more dependencies:
102+
103+
```bash
104+
# install OpenCV python package
105+
pip install opencv-python
106+
pip install opencv-contrib-python
107+
```
99108

100-
### Inference with pre-trained models
109+
## Inference with pre-trained models
101110

102111
To go through the tutorial, clone this repo and follow the instructions step by step.
103112

@@ -132,7 +141,7 @@ python detect_objects.py --model_path models/efficientdet_d0_coco17_tpu-32/saved
132141

133142
Sample output from the detection with EfficientDet D0 model:
134143

135-
![alt-txt-1](data/output/1.jpg) ![alt-txt-2](data/output/4.jpg)
144+
![detcetion-output1](data/samples/output/1.jpg)
136145

137146

138147
You can also select set of classes to be detected by passing their labels to the argument `--class_ids` as a string with the "," delimiter. For example, using `--class_ids "1,3" ` will do detection for the classes "person" and "car" only as they have id 1 and 3 respectively (you can check the id and labels from the [coco labelmap](models/mscoco_label_map.pbtxt)). Not using this argument will lead to detecting all objects in the provided labelmap.
@@ -144,15 +153,59 @@ python detect_objects.py --video_input --class_ids "1" --threshold 0.3 --video_
144153
```
145154

146155

156+
## Preparing your custom dataset for training
147157

148-
### Preparing your custom dataset for training
158+
In this tutorial, I am going to use the interesting [raccoon dataset](https://github.com/datitran/raccoon_dataset) collected by [Dat Tran](https://dat-tran.com/).
159+
The raccoon dataset contains a total of 200 images with 217 raccoons, which is suitable to use in tutorial examples.
160+
161+
The original [dataset repo](https://github.com/datitran/raccoon_dataset) provides many scripts to deal with the dataset and randomly select train and test splits with 160 and 40 images respectively.
162+
However, just for convenience, and to decrease the efforts needed, I have included the dataset images and annotation in this repo (in [data/raccoon_data/](data/raccoon_data/) ), and split them manually, taking the first 160 images for training, and the last 40 images for testing.
163+
I recommend checking the original [dataset repo](https://github.com/datitran/raccoon_dataset), along with this [article](https://towardsdatascience.com/how-to-train-your-own-object-detector-with-tensorflows-object-detector-api-bec72ecfe1d9) written by the author of the dataset.
149164

150-
TODO
151165

152-
### Training with your custom data
166+
First step to start training your model is to generate [TFRecord](https://www.tensorflow.org/tutorials/load_data/tfrecord) file from the dataset annotations.
167+
TFRecord is a binary file format that makes dealing with large datasets more efficient, you can read more about TFRecords in this [article](https://medium.com/mostly-ai/tensorflow-records-what-they-are-and-how-to-use-them-c46bc4bbb564).
168+
The Tensorflow Object Detection API provides examples to generate tfrecords from annotations that have the same shape as pascal voc or oxford pet dataset (you can see the instructions [here](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/preparing_inputs.md)).
169+
But generally, you may have your data annotations in any format, so let's generate intermediate format as csv file, then use it to generate our tfrecords.
170+
171+
First we need to convert the xml annotations files to csv, which was [provided in the raccoon dataset repo](https://github.com/datitran/raccoon_dataset/blob/master/xml_to_csv.py). I just took this file and refined it a little, and used argparse package to input our pathes as arguments, you can find the refined file in [data_gen/xml_to_csv.py](data_gen/xml_to_csv.py).
172+
You can use this file as follow:
173+
174+
```bash
175+
python xml_to_csv.py --annot_dir ../data/raccoon_data/train/annotations --out_csv_path ../data/raccoon_data/train_labels.csv
176+
```
177+
178+
After generating the csv file, use it to genrate the tfrecord file.
179+
In the tensorflow detection repo they provide a good [tutorial](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/using_your_own_dataset.md) to deal with your custon data and generate tfrecords.
180+
I have used the examples provided, and solved some issues to make it work with TF2, and also used argparse to make it easier to use for any data in the future.
181+
You can find my file in [data_gen/generate_tfrecord.py](data_gen/generate_tfrecord.py), and you can use it as follows:
182+
183+
```bash
184+
python generate_tfrecord.py --path_to_images $DATASET_PATH/train/images --path_to_annot ../data/raccoon_data/train_labels.csv \
185+
--path_to_label_map ../models/raccoon_labelmap.pbtxt \
186+
--path_to_save_tfrecords ../data/raccoon_data/train.record
187+
```
188+
189+
For convenience, I have added all these steps in a shell file that you can run to generate the csv files and use them to generate the tfrecords.
190+
So simply run this shell file as follows:
191+
192+
```bash
193+
cd data_gen/
194+
bash gen_data.sh
195+
```
196+
197+
After running this command, you will find the generated csv and tfrecords (.record) files located in [data/raccoon_data](data/raccoon_data).
198+
Et voila, we have the tfrecord files generated, and we can use it in next steps for training.
199+
200+
201+
## Training object detection model with your custom dataset
153202

154203
TODO
155204

156205

206+
## Exporting your trained model for inference
207+
208+
TODO
209+
157210

158211

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<annotation verified="yes">
2+
<folder>images</folder>
3+
<filename>raccoon-161.jpg</filename>
4+
<path>/Users/datitran/Desktop/raccoon/images/raccoon-161.jpg</path>
5+
<source>
6+
<database>Unknown</database>
7+
</source>
8+
<size>
9+
<width>500</width>
10+
<height>347</height>
11+
<depth>1</depth>
12+
</size>
13+
<segmented>0</segmented>
14+
<object>
15+
<name>raccoon</name>
16+
<pose>Unspecified</pose>
17+
<truncated>0</truncated>
18+
<difficult>0</difficult>
19+
<bndbox>
20+
<xmin>209</xmin>
21+
<ymin>73</ymin>
22+
<xmax>385</xmax>
23+
<ymax>186</ymax>
24+
</bndbox>
25+
</object>
26+
</annotation>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<annotation verified="yes">
2+
<folder>images</folder>
3+
<filename>raccoon-162.jpg</filename>
4+
<path>/Users/datitran/Desktop/raccoon/images/raccoon-162.jpg</path>
5+
<source>
6+
<database>Unknown</database>
7+
</source>
8+
<size>
9+
<width>259</width>
10+
<height>194</height>
11+
<depth>3</depth>
12+
</size>
13+
<segmented>0</segmented>
14+
<object>
15+
<name>raccoon</name>
16+
<pose>Unspecified</pose>
17+
<truncated>0</truncated>
18+
<difficult>0</difficult>
19+
<bndbox>
20+
<xmin>45</xmin>
21+
<ymin>34</ymin>
22+
<xmax>161</xmax>
23+
<ymax>184</ymax>
24+
</bndbox>
25+
</object>
26+
</annotation>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<annotation verified="yes">
2+
<folder>images</folder>
3+
<filename>raccoon-163.jpg</filename>
4+
<path>/Users/datitran/Desktop/raccoon/images/raccoon-163.jpg</path>
5+
<source>
6+
<database>Unknown</database>
7+
</source>
8+
<size>
9+
<width>248</width>
10+
<height>203</height>
11+
<depth>3</depth>
12+
</size>
13+
<segmented>0</segmented>
14+
<object>
15+
<name>raccoon</name>
16+
<pose>Unspecified</pose>
17+
<truncated>0</truncated>
18+
<difficult>0</difficult>
19+
<bndbox>
20+
<xmin>6</xmin>
21+
<ymin>7</ymin>
22+
<xmax>240</xmax>
23+
<ymax>157</ymax>
24+
</bndbox>
25+
</object>
26+
</annotation>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<annotation verified="yes">
2+
<folder>images</folder>
3+
<filename>raccoon-164.jpg</filename>
4+
<path>/Users/datitran/Desktop/raccoon/images/raccoon-164.jpg</path>
5+
<source>
6+
<database>Unknown</database>
7+
</source>
8+
<size>
9+
<width>274</width>
10+
<height>184</height>
11+
<depth>3</depth>
12+
</size>
13+
<segmented>0</segmented>
14+
<object>
15+
<name>raccoon</name>
16+
<pose>Unspecified</pose>
17+
<truncated>1</truncated>
18+
<difficult>0</difficult>
19+
<bndbox>
20+
<xmin>10</xmin>
21+
<ymin>27</ymin>
22+
<xmax>178</xmax>
23+
<ymax>184</ymax>
24+
</bndbox>
25+
</object>
26+
</annotation>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<annotation verified="yes">
2+
<folder>images</folder>
3+
<filename>raccoon-165.jpg</filename>
4+
<path>/Users/datitran/Desktop/raccoon/images/raccoon-165.jpg</path>
5+
<source>
6+
<database>Unknown</database>
7+
</source>
8+
<size>
9+
<width>199</width>
10+
<height>253</height>
11+
<depth>3</depth>
12+
</size>
13+
<segmented>0</segmented>
14+
<object>
15+
<name>raccoon</name>
16+
<pose>Unspecified</pose>
17+
<truncated>0</truncated>
18+
<difficult>0</difficult>
19+
<bndbox>
20+
<xmin>27</xmin>
21+
<ymin>11</ymin>
22+
<xmax>194</xmax>
23+
<ymax>228</ymax>
24+
</bndbox>
25+
</object>
26+
</annotation>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<annotation verified="yes">
2+
<folder>images</folder>
3+
<filename>raccoon-166.jpg</filename>
4+
<path>/Users/datitran/Desktop/raccoon/images/raccoon-166.jpg</path>
5+
<source>
6+
<database>Unknown</database>
7+
</source>
8+
<size>
9+
<width>328</width>
10+
<height>154</height>
11+
<depth>3</depth>
12+
</size>
13+
<segmented>0</segmented>
14+
<object>
15+
<name>raccoon</name>
16+
<pose>Unspecified</pose>
17+
<truncated>0</truncated>
18+
<difficult>0</difficult>
19+
<bndbox>
20+
<xmin>108</xmin>
21+
<ymin>31</ymin>
22+
<xmax>208</xmax>
23+
<ymax>120</ymax>
24+
</bndbox>
25+
</object>
26+
</annotation>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<annotation verified="yes">
2+
<folder>images</folder>
3+
<filename>raccoon-167.jpg</filename>
4+
<path>/Users/datitran/Desktop/raccoon/images/raccoon-167.jpg</path>
5+
<source>
6+
<database>Unknown</database>
7+
</source>
8+
<size>
9+
<width>259</width>
10+
<height>195</height>
11+
<depth>3</depth>
12+
</size>
13+
<segmented>0</segmented>
14+
<object>
15+
<name>raccoon</name>
16+
<pose>Unspecified</pose>
17+
<truncated>1</truncated>
18+
<difficult>0</difficult>
19+
<bndbox>
20+
<xmin>1</xmin>
21+
<ymin>5</ymin>
22+
<xmax>175</xmax>
23+
<ymax>195</ymax>
24+
</bndbox>
25+
</object>
26+
</annotation>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<annotation verified="yes">
2+
<folder>images</folder>
3+
<filename>raccoon-168.jpg</filename>
4+
<path>/Users/datitran/Desktop/raccoon/images/raccoon-168.jpg</path>
5+
<source>
6+
<database>Unknown</database>
7+
</source>
8+
<size>
9+
<width>628</width>
10+
<height>314</height>
11+
<depth>3</depth>
12+
</size>
13+
<segmented>0</segmented>
14+
<object>
15+
<name>raccoon</name>
16+
<pose>Unspecified</pose>
17+
<truncated>0</truncated>
18+
<difficult>0</difficult>
19+
<bndbox>
20+
<xmin>98</xmin>
21+
<ymin>88</ymin>
22+
<xmax>374</xmax>
23+
<ymax>303</ymax>
24+
</bndbox>
25+
</object>
26+
<object>
27+
<name>raccoon</name>
28+
<pose>Unspecified</pose>
29+
<truncated>1</truncated>
30+
<difficult>0</difficult>
31+
<bndbox>
32+
<xmin>173</xmin>
33+
<ymin>1</ymin>
34+
<xmax>471</xmax>
35+
<ymax>309</ymax>
36+
</bndbox>
37+
</object>
38+
</annotation>

0 commit comments

Comments
 (0)