You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+18-20Lines changed: 18 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
# How to train a custom object detection model with the Tensorflow Object Detection API
2
2
(ReadME inspired by [EdjeElectronics](https://github.com/EdjeElectronics))
3
3
4
-
> Update: This README and Repository is now fully updated for Tensorflow 2. If you want to use Tensorflow 1 instead check out [my article](https://gilberttanner.com/blog/creating-your-own-objectdetector). If you want to train your model in Google Colab check out [my notebook](Tensorflow_2_Object_Detection_Train_model.ipynb).
4
+
> Update: This README and Repository is now fully updated for Tensorflow 2. If you want to use Tensorflow 1 instead check out [my article](https://gilberttanner.com/blog/creating-your-own-objectdetector). If you want to train your model in Google Colab check out [the Tensorflow_2_Object_Detection_Train_model notebook](Tensorflow_2_Object_Detection_Train_model.ipynb).
5
5
6
6

7
7
@@ -93,11 +93,9 @@ OK (skipped=1)
93
93
94
94
Now that the Tensorflow Object Detection API is ready to go, we need to gather the images needed for training.
95
95
96
-
To train a robust model, we need lots of pictures that should vary as much as possible from each other. That means that they should have different lighting conditions, different backgrounds and lots of random objects in them.
96
+
To train a robust model, the pictures should be as diverse as possible. So they should have different backgrounds, varying lighting conditions, and unrelated random objects in them.
97
97
98
-
You can either take the pictures yourself or you can download them from the internet. For my microcontroller detector, I have four different objects I want to detect (Arduino Nano, ESP8266, Raspberry Pi 3, Heltect ESP32 Lora).
99
-
100
-
I took about 25 pictures of each individual microcontroller and 25 pictures containing multiple microcontrollers using my smartphone. After taking the pictures make sure to transform them to a resolution suitable for training (I used 800x600).
98
+
You can either take pictures yourself, or you can download pictures from the internet. For my microcontroller detector, I took about 25 pictures of each individual microcontroller and 25 pictures containing multiple microcontrollers.
101
99
102
100

103
101
@@ -107,13 +105,13 @@ You can use the [resize_images script](resize_images.py) to resize the image to
107
105
python resize_images.py -d images/ -s 800 600
108
106
```
109
107
110
-
After you have all the images move about 80% to the object_detection/images/train directory and the other 20% to the object_detection/images/test directory. Make sure that the images in both directories have a good variety of classes.
108
+
After you have all the images, move about 80% to the object_detection/images/train directory and the other 20% to the object_detection/images/test directory. Make sure that the images in both directories have a good variety of classes.
111
109
112
110
### 3. Labeling data
113
111
114
112
With all the pictures gathered, we come to the next step - labeling the data. Labeling is the process of drawing bounding boxes around the desired objects.
115
113
116
-
LabelImg is a great tool for creating a object detection data-set.
114
+
LabelImg is a great tool for creating an object detection data-set.
@@ -123,13 +121,13 @@ Download and install LabelImg. Then point it to your images/train and images/tes
123
121
124
122

125
123
126
-
LabelImg supports two formats, PascalVOC and Yolo. For this tutorial make sure to select PascalVOC. LabelImg saves a xml file containing the label data for each image. These files will be used to create a tfrecord file, which can be used to train the model.
124
+
LabelImg supports two formats, PascalVOC and Yolo. For this tutorial, make sure to select PascalVOC. LabelImg saves a xml file containing the label data for each image. These files will be used to create a tfrecord file, which can be used to train the model.
127
125
128
126
### 4. Generating Training data
129
127
130
-
With the images labeled, we need to create TFRecords that can be served as input data for training of the object detector. In order to create the TFRecords we will use two scripts from [Dat Tran’s raccoon detector](https://github.com/datitran/raccoon_dataset). Namely the xml_to_csv.py and generate_tfrecord.py files.
128
+
With the images labeled, we need to create TFRecords that can be served as input data for training the object detector. To create the TFRecords, we will use two scripts from [Dat Tran’s raccoon detector](https://github.com/datitran/raccoon_dataset). Namely, the xml_to_csv.py and generate_tfrecord.py files.
131
129
132
-
After downloading both scripts we can first of change the main method in the xml_to_csv file so we can transform the created xml files to csv correctly.
130
+
After downloading both scripts, we first change the main method in the xml_to_csv file to transform the created xml files to csv correctly.
133
131
134
132
```python
135
133
# Old:
@@ -153,7 +151,7 @@ Now we can transform our xml files to csvs by opening the command line and typin
153
151
python xml_to_csv.py
154
152
```
155
153
156
-
These creates two files in the images directory. One called test_labels.csv and another one called train_labels.csv.
154
+
The above command creates two files in the images directory. One called test_labels.csv and another one called train_labels.csv.
157
155
158
156
Next, open the generate_tfrecord.py file and replace the labelmap inside the class_text_to_int method with your own label map.
These two commands generate a train.record and a test.record file which can be used to train our object detector.
194
+
These two commands generate a train.record and a test.record file, which can be used to train our object detector.
197
195
198
196
### 5. Getting ready for training
199
197
@@ -226,7 +224,7 @@ The id number of each item should match the id of specified in the generate_tfre
226
224
227
225
#### 5.2 Creating the training configuration
228
226
229
-
Lastly we need to create a training configuration file. As a base model I will use EfficientDet – a recent family of SOTA models discovered with the help of Neural Architecture Search. The Tensorflow OD API provides a lot of different models. For more information check out the [Tensorflow 2 Detection Model Zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md)
227
+
Lastly, we need to create a training configuration file. As a base model, I will use EfficientDet – a recent family of SOTA models discovered with the help of Neural Architecture Search. The Tensorflow OD API provides a lot of different models. For more information check out the [Tensorflow 2 Detection Model Zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md)
230
228
231
229
The [base config](https://github.com/tensorflow/models/blob/master/research/object_detection/configs/tf2/ssd_efficientdet_d0_512x512_coco17_tpu-8.config) for the model can be found inside the [configs/tf2 folder](https://github.com/tensorflow/models/tree/master/research/object_detection/configs/tf2).
232
230
@@ -252,35 +250,35 @@ Copy the config file to the training directory. Then open it inside a text edito
252
250
253
251
*```label_map_path: "<path>/labelmap.pbtxt"```
254
252
255
-
* Line 144 and 189: change batch_size to a number appropriate for your hardware, like 4, 8 or 16.
253
+
* Line 144 and 189: change batch_size to a number appropriate for your hardware, like 4, 8, or 16.
256
254
257
255
### 6. Training the model
258
256
259
-
To train the model execute the following command in the command line:
257
+
To train the model, execute the following command in the command line:
If everything was setup correctly the training should begin shortly and you should see something like the following:
263
+
If everything was setup correctly, the training should begin shortly, and you should see something like the following:
266
264
267
265

268
266
269
-
Every few minutes the current loss gets logged to Tensorboard. Open Tensorboard by opening a second command line, navigating to the object_detection folder and typing:
267
+
Every few minutes, the current loss gets logged to Tensorboard. Open Tensorboard by opening a second command line, navigating to the object_detection folder and typing:
270
268
271
269
```tensorboard --logdir=training/train```
272
270
273
271
This will open a webpage at localhost:6006.
274
272
275
273

276
274
277
-
The training scrips saves checkpoints about every five minutes. Train the model until it reaches a satisfying loss then you can terminat the training process by pressing Ctrl+C.
275
+
The training script saves checkpoints about every five minutes. Train the model until it reaches a satisfying loss, then you can terminate the training process by pressing Ctrl+C.
278
276
279
277
### 7. Exporting the inference graph
280
278
281
-
Now that we have a trained model we need to generate an inference graph, which can be used to run the model.
279
+
Now that we have a trained model, we need to generate an inference graph that can be used to run the model.
282
280
283
-
> Note: There is currently a[issue](https://github.com/tensorflow/models/issues/8841) that occurs when you're trying to export the model. As a temprary fix Github user [Jacobsolawetz](https://github.com/Jacobsolawetz)discoverd that you can add ```if not isinstance(x, str):``` add line 140 of the ```https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/keras/utils/tf_utils.py``` script. For more information check out [his comment to the issue](https://github.com/tensorflow/models/issues/8841#issuecomment-657647648).
281
+
> Note: There is currently an[issue](https://github.com/tensorflow/models/issues/8841) that occurs when you're trying to export the model. As a temporary fix, Github user [Jacobsolawetz](https://github.com/Jacobsolawetz)discovered that you can add ```if not isinstance(x, str):``` add line 140 of the ```https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/keras/utils/tf_utils.py``` script. For more information, check out [his comment on the issue](https://github.com/tensorflow/models/issues/8841#issuecomment-657647648).
0 commit comments