Skip to content

Commit d87d97a

Browse files
authored
Update Readme
1 parent c62c1a2 commit d87d97a

File tree

1 file changed

+281
-0
lines changed

1 file changed

+281
-0
lines changed

README_Vietnamese.md

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,284 @@ Khi bạn đã chạy tất cả script trong notebook, bạn sẽ thấy hai h
191191
<img src="doc/jupyter_notebook_dogs.jpg">
192192
</p>
193193

194+
### 3. Thu thập và gán nhãn hình ảnh
195+
Sau khi TensorFlow Object Detection API đã thiết lập thành công và sẵn sàng chạy, chúng ta cần cung cần cung cấp các hình ảnh mà sẽ dùng để đào tạo một detection classifier mới.
196+
197+
#### 3a. Thu thập hình ảnh
198+
TensorFlow cần lượng lớn hình ảnh của một object để có thể đào tạo một detection classifier model tốt. Để đào tạo một bộ phân loại mạnh mẽ, các hình ảnh đào tạo cần có ngẫu nhiên các objects trong ảnh cùng với các objects mong muốn, và nên có đa dạng về nền và điều kiện ánh sáng khác nhau. Cần có một số hình ảnh mà trong đó object mong muốn bị che khuất một phần, chồng chéo với một thứ khác, hoặc chỉ ở giữa bức ảnh.
199+
200+
Với Pinochle Card Detection classifier của tôi, tôi cso 6 objects khác nhau mà tôi muốn phát hiện (Thứ tự các quân bài chín, mười Ri, Q, K và Xì - Đây chỉ là thứ tự, tôi không nhận diện quân Xì). Tôi đã sử dụng iPhone để chụp 40 bức ảnh, với nhiều các vật thể không muốn cũng có trong ảnh. Sau đó, tôi chụp khoảng 100 bức khác cùng với đa dạng các quân bài cùng trong một bức ảnh. Tôi muốn phát hiện các thể khi chúng trùng lên nhau, vì vậy tôi chắc chắn rằng các thẻ có thể chồng lên nhau trong nhiều hình ảnh.
201+
202+
<p align="center">
203+
<img src="doc/collage.jpg">
204+
</p>
205+
206+
Bạn có thể sử dụng điện thoại của bạn để chụp các hình ảnh về vật thể hoặc bạn có thể tải chúng từ Google Image Search. Tôi khuyến nghị bạn nên có ít nhất 200 ảnh tất cả. Tôi sử dụng 311 ảnh để đào tạo bộ phát hiện các quân bài của mình.
207+
208+
Đảm bảo rằng các hình ảnh không quá lớn. Chúng nên nhỏ hơn 200KB mỗi bức, và độ phân giải của chúng không lớn hơn 720x1280. Hình ảnh càng lớn thì càng mất nhiều thời gian để đào tạo. Bạn có thể sử dụng resizer.py script trong repository để giảm kích thước của các hình ảnh xuống.
209+
210+
Sau khi bạn có tất cả các hình ảnh bạn cần, chia chúng ra 20% đến thư mục \object_detection\images\test, và 80% còn lại vào thư mục \object_detection\images\train. Đảm bảo rằng có đa dạng hình ảnh trong cả hai thư mục \test và \train.
211+
212+
#### 3b. Label Pictures
213+
Here comes the fun part! With all the pictures gathered, it’s time to label the desired objects in every picture. LabelImg is a great tool for labeling images, and its GitHub page has very clear instructions on how to install and use it.
214+
215+
[LabelImg GitHub link](https://github.com/tzutalin/labelImg)
216+
217+
[LabelImg download link](https://www.dropbox.com/s/tq7zfrcwl44vxan/windows_v1.6.0.zip?dl=1)
218+
219+
Download and install LabelImg, point it to your \images\train directory, and then draw a box around each object in each image. Repeat the process for all the images in the \images\test directory. This will take a while!
220+
221+
<p align="center">
222+
<img src="doc/labels.jpg">
223+
</p>
224+
225+
LabelImg saves a .xml file containing the label data for each image. These .xml files will be used to generate TFRecords, which are one of the inputs to the TensorFlow trainer. Once you have labeled and saved each image, there will be one .xml file for each image in the \test and \train directories.
226+
227+
### 4. Generate Training Data
228+
With the images labeled, it’s time to generate the TFRecords that serve as input data to the TensorFlow training model. This tutorial uses the xml_to_csv.py and generate_tfrecord.py scripts from [Dat Tran’s Raccoon Detector dataset](https://github.com/datitran/raccoon_dataset), with some slight modifications to work with our directory structure.
229+
230+
First, the image .xml data will be used to create .csv files containing all the data for the train and test images. From the \object_detection folder, issue the following command in the Anaconda command prompt:
231+
```
232+
(tensorflow1) C:\tensorflow1\models\research\object_detection> python xml_to_csv.py
233+
```
234+
This creates a train_labels.csv and test_labels.csv file in the \object_detection\images folder.
235+
236+
Next, open the generate_tfrecord.py file in a text editor. Replace the label map starting at line 31 with your own label map, where each object is assigned an ID number. This same number assignment will be used when configuring the labelmap.pbtxt file in Step 5b.
237+
238+
For example, say you are training a classifier to detect basketballs, shirts, and shoes. You will replace the following code in generate_tfrecord.py:
239+
```
240+
# TO-DO replace this with label map
241+
def class_text_to_int(row_label):
242+
if row_label == 'nine':
243+
return 1
244+
elif row_label == 'ten':
245+
return 2
246+
elif row_label == 'jack':
247+
return 3
248+
elif row_label == 'queen':
249+
return 4
250+
elif row_label == 'king':
251+
return 5
252+
elif row_label == 'ace':
253+
return 6
254+
else:
255+
None
256+
```
257+
With this:
258+
```
259+
# TO-DO replace this with label map
260+
def class_text_to_int(row_label):
261+
if row_label == 'basketball':
262+
return 1
263+
elif row_label == 'shirt':
264+
return 2
265+
elif row_label == 'shoe':
266+
return 3
267+
else:
268+
None
269+
```
270+
Then, generate the TFRecord files by issuing these commands from the \object_detection folder:
271+
```
272+
python generate_tfrecord.py --csv_input=images\train_labels.csv --image_dir=images\train --output_path=train.record
273+
python generate_tfrecord.py --csv_input=images\test_labels.csv --image_dir=images\test --output_path=test.record
274+
```
275+
These generate a train.record and a test.record file in \object_detection. These will be used to train the new object detection classifier.
276+
277+
### 5. Create Label Map and Configure Training
278+
The last thing to do before training is to create a label map and edit the training configuration file.
279+
280+
#### 5a. Label map
281+
The label map tells the trainer what each object is by defining a mapping of class names to class ID numbers. Use a text editor to create a new file and save it as labelmap.pbtxt in the C:\tensorflow1\models\research\object_detection\training folder. (Make sure the file type is .pbtxt, not .txt !) In the text editor, copy or type in the label map in the format below (the example below is the label map for my Pinochle Deck Card Detector):
282+
```
283+
item {
284+
id: 1
285+
name: 'nine'
286+
}
287+
288+
item {
289+
id: 2
290+
name: 'ten'
291+
}
292+
293+
item {
294+
id: 3
295+
name: 'jack'
296+
}
297+
298+
item {
299+
id: 4
300+
name: 'queen'
301+
}
302+
303+
item {
304+
id: 5
305+
name: 'king'
306+
}
307+
308+
item {
309+
id: 6
310+
name: 'ace'
311+
}
312+
```
313+
The label map ID numbers should be the same as what is defined in the generate_tfrecord.py file. For the basketball, shirt, and shoe detector example mentioned in Step 4, the labelmap.pbtxt file will look like:
314+
```
315+
item {
316+
id: 1
317+
name: 'basketball'
318+
}
319+
320+
item {
321+
id: 2
322+
name: 'shirt'
323+
}
324+
325+
item {
326+
id: 3
327+
name: 'shoe'
328+
}
329+
```
330+
331+
#### 5b. Configure training
332+
Finally, the object detection training pipeline must be configured. It defines which model and what parameters will be used for training. This is the last step before running training!
333+
334+
Navigate to C:\tensorflow1\models\research\object_detection\samples\configs and copy the faster_rcnn_inception_v2_pets.config file into the \object_detection\training directory. Then, open the file with a text editor. There are several changes to make to the .config file, mainly changing the number of classes and examples, and adding the file paths to the training data.
335+
336+
Make the following changes to the faster_rcnn_inception_v2_pets.config file. Note: The paths must be entered with single forward slashes (NOT backslashes), or TensorFlow will give a file path error when trying to train the model! Also, the paths must be in double quotation marks ( " ), not single quotation marks ( ' ).
337+
338+
- Line 9. Change num_classes to the number of different objects you want the classifier to detect. For the above basketball, shirt, and shoe detector, it would be num_classes : 3 .
339+
- Line 106. Change fine_tune_checkpoint to:
340+
- fine_tune_checkpoint : "C:/tensorflow1/models/research/object_detection/faster_rcnn_inception_v2_coco_2018_01_28/model.ckpt"
341+
342+
- Lines 123 and 125. In the train_input_reader section, change input_path and label_map_path to:
343+
- input_path : "C:/tensorflow1/models/research/object_detection/train.record"
344+
- label_map_path: "C:/tensorflow1/models/research/object_detection/training/labelmap.pbtxt"
345+
346+
- Line 130. Change num_examples to the number of images you have in the \images\test directory.
347+
348+
- Lines 135 and 137. In the eval_input_reader section, change input_path and label_map_path to:
349+
- input_path : "C:/tensorflow1/models/research/object_detection/test.record"
350+
- label_map_path: "C:/tensorflow1/models/research/object_detection/training/labelmap.pbtxt"
351+
352+
Save the file after the changes have been made. That’s it! The training job is all configured and ready to go!
353+
354+
### 6. Run the Training
355+
**UPDATE 9/26/18:**
356+
*As of version 1.9, TensorFlow has deprecated the "train.py" file and replaced it with "model_main.py" file. I haven't been able to get model_main.py to work correctly yet (I run in to errors related to pycocotools). Fortunately, the train.py file is still available in the /object_detection/legacy folder. Simply move train.py from /object_detection/legacy into the /object_detection folder and then continue following the steps below.*
357+
358+
Here we go! From the \object_detection directory, issue the following command to begin training:
359+
```
360+
python train.py --logtostderr --train_dir=training/ --pipeline_config_path=training/faster_rcnn_inception_v2_pets.config
361+
```
362+
If everything has been set up correctly, TensorFlow will initialize the training. The initialization can take up to 30 seconds before the actual training begins. When training begins, it will look like this:
363+
364+
<p align="center">
365+
<img src="doc/training.jpg">
366+
</p>
367+
368+
Each step of training reports the loss. It will start high and get lower and lower as training progresses. For my training on the Faster-RCNN-Inception-V2 model, it started at about 3.0 and quickly dropped below 0.8. I recommend allowing your model to train until the loss consistently drops below 0.05, which will take about 40,000 steps, or about 2 hours (depending on how powerful your CPU and GPU are). Note: The loss numbers will be different if a different model is used. MobileNet-SSD starts with a loss of about 20, and should be trained until the loss is consistently under 2.
369+
370+
You can view the progress of the training job by using TensorBoard. To do this, open a new instance of Anaconda Prompt, activate the tensorflow1 virtual environment, change to the C:\tensorflow1\models\research\object_detection directory, and issue the following command:
371+
```
372+
(tensorflow1) C:\tensorflow1\models\research\object_detection>tensorboard --logdir=training
373+
```
374+
This will create a webpage on your local machine at YourPCName:6006, which can be viewed through a web browser. The TensorBoard page provides information and graphs that show how the training is progressing. One important graph is the Loss graph, which shows the overall loss of the classifier over time.
375+
376+
<p align="center">
377+
<img src="doc/loss_graph.JPG">
378+
</p>
379+
380+
The training routine periodically saves checkpoints about every five minutes. You can terminate the training by pressing Ctrl+C while in the command prompt window. I typically wait until just after a checkpoint has been saved to terminate the training. You can terminate training and start it later, and it will restart from the last saved checkpoint. The checkpoint at the highest number of steps will be used to generate the frozen inference graph.
381+
382+
### 7. Export Inference Graph
383+
Now that training is complete, the last step is to generate the frozen inference graph (.pb file). From the \object_detection folder, issue the following command, where “XXXX” in “model.ckpt-XXXX” should be replaced with the highest-numbered .ckpt file in the training folder:
384+
```
385+
python export_inference_graph.py --input_type image_tensor --pipeline_config_path training/faster_rcnn_inception_v2_pets.config --trained_checkpoint_prefix training/model.ckpt-XXXX --output_directory inference_graph
386+
```
387+
This creates a frozen_inference_graph.pb file in the \object_detection\inference_graph folder. The .pb file contains the object detection classifier.
388+
389+
### 8. Use Your Newly Trained Object Detection Classifier!
390+
The object detection classifier is all ready to go! I’ve written Python scripts to test it out on an image, video, or webcam feed.
391+
392+
Before running the Python scripts, you need to modify the NUM_CLASSES variable in the script to equal the number of classes you want to detect. (For my Pinochle Card Detector, there are six cards I want to detect, so NUM_CLASSES = 6.)
393+
394+
To test your object detector, move a picture of the object or objects into the \object_detection folder, and change the IMAGE_NAME variable in the Object_detection_image.py to match the file name of the picture. Alternatively, you can use a video of the objects (using Object_detection_video.py), or just plug in a USB webcam and point it at the objects (using Object_detection_webcam.py).
395+
396+
To run any of the scripts, type “idle” in the Anaconda Command Prompt (with the “tensorflow1” virtual environment activated) and press ENTER. This will open IDLE, and from there, you can open any of the scripts and run them.
397+
398+
If everything is working properly, the object detector will initialize for about 10 seconds and then display a window showing any objects it’s detected in the image!
399+
400+
<p align="center">
401+
<img src="doc/detector2.jpg">
402+
</p>
403+
404+
If you encounter errors, please check out the Appendix: it has a list of errors that I ran in to while setting up my object detection classifier. You can also trying Googling the error. There is usually useful information on Stack Exchange or in TensorFlow’s Issues on GitHub.
405+
406+
## Appendix: Common Errors
407+
It appears that the TensorFlow Object Detection API was developed on a Linux-based operating system, and most of the directions given by the documentation are for a Linux OS. Trying to get a Linux-developed software library to work on Windows can be challenging. There are many little snags that I ran in to while trying to set up tensorflow-gpu to train an object detection classifier on Windows 10. This Appendix is a list of errors I ran in to, and their resolutions.
408+
409+
#### 1. ModuleNotFoundError: No module named 'deployment' or No module named 'nets'
410+
411+
This error occurs when you try to run object_detection_tutorial.ipynb or train.py and you don’t have the PATH and PYTHONPATH environment variables set up correctly. Exit the virtual environment by closing and re-opening the Anaconda Prompt window. Then, issue “activate tensorflow1” to re-enter the environment, and then issue the commands given in Step 2e.
412+
413+
You can use “echo %PATH%” and “echo %PYTHONPATH%” to check the environment variables and make sure they are set up correctly.
414+
415+
Also, make sure you have run these commands from the \models\research directory:
416+
```
417+
setup.py build
418+
setup.py install
419+
```
420+
421+
#### 2. ImportError: cannot import name 'preprocessor_pb2'
422+
423+
#### ImportError: cannot import name 'string_int_label_map_pb2'
424+
425+
#### (or similar errors with other pb2 files)
426+
427+
This occurs when the protobuf files (in this case, preprocessor.proto) have not been compiled. Re-run the protoc command given in Step 2f. Check the \object_detection\protos folder to make sure there is a name_pb2.py file for every name.proto file.
428+
429+
#### 3. object_detection/protos/.proto: No such file or directory
430+
431+
This occurs when you try to run the
432+
```
433+
“protoc object_detection/protos/*.proto --python_out=.”
434+
```
435+
command given on the TensorFlow Object Detection API installation page. Sorry, it doesn’t work on Windows! Copy and paste the full command given in Step 2f instead. There’s probably a more graceful way to do it, but I don’t know what it is.
436+
437+
#### 4. Unsuccessful TensorSliceReader constructor: Failed to get "file path" … The filename, directory name, or volume label syntax is incorrect.
438+
439+
This error occurs when the filepaths in the training configuration file (faster_rcnn_inception_v2_pets.config or similar) have not been entered with backslashes instead of forward slashes. Open the .config file and make sure all file paths are given in the following format:
440+
```
441+
“C:/path/to/model.file”
442+
```
443+
444+
#### 5. ValueError: Tried to convert 't' to a tensor and failed. Error: Argument must be a dense tensor: range(0, 3) - got shape [3], but wanted [].
445+
446+
The issue is with models/research/object_detection/utils/learning_schedules.py Currently it is
447+
```
448+
rate_index = tf.reduce_max(tf.where(tf.greater_equal(global_step, boundaries),
449+
range(num_boundaries),
450+
[0] * num_boundaries))
451+
```
452+
Wrap list() around the range() like this:
453+
454+
```
455+
rate_index = tf.reduce_max(tf.where(tf.greater_equal(global_step, boundaries),
456+
list(range(num_boundaries)),
457+
[0] * num_boundaries))
458+
```
459+
460+
[Ref: Tensorflow Issue#3705](https://github.com/tensorflow/models/issues/3705#issuecomment-375563179)
461+
462+
#### 6. ImportError: DLL load failed: The specified procedure could not be found. (or other DLL-related errors)
463+
This error occurs because the CUDA and cuDNN versions you have installed are not compatible with the version of TensorFlow you are using. The easiest way to resolve this error is to use Anaconda's cudatoolkit package rather than manually installing CUDA and cuDNN. If you ran into these errors, try creating a new Anaconda virtual environment:
464+
```
465+
conda create -n tensorflow2 pip python=3.5
466+
```
467+
Then, once inside the environment, install TensorFlow using CONDA rather than PIP:
468+
```
469+
conda install tensorflow-gpu
470+
```
471+
Then restart this guide from Step 2 (but you can skip the part where you install TensorFlow in Step 2d).
472+
473+
#### 7. In Step 2g, the Jupyter Notebook runs all the way through with no errors, but no pictures are displayed at the end.
474+
If you run the full Jupyter Notebook without getting any errors, but the labeled pictures still don't appear, try this: go in to object_detection/utils/visualization_utils.py and comment out the import statements around lines 29 and 30 that include matplotlib. Then, try re-running the Jupyter notebook. (The visualization_utils.py script changes quite a bit, so it might not be exactly line 29 and 30.)

0 commit comments

Comments
 (0)