MMDetection target detection example

MMDetection target detection example

MMDetection is based on the cooc data set, Faster-rcnn method of target recognition training



Preface

I have been in contact with the deep learning target detection toolbox MMDetection for nearly a week. The learning process is very difficult. It only took three days to install and configure the environment, but it looks really beautiful to slowly overcome the suffering!
Since this toolbox is jointly open sourced by SenseTime (2018 COCO Target Detection Challenge Champion) and the Chinese University of Hong Kong in 2018, it is relatively new and does not have very detailed tutorials for us to learn, so I would like to thank those big guys very much. Share your experience on the platform.
The following is a small example I want to share with you (use environment of MMDetection toolkit: Ubuntu+Pycharm ), I hope you can also use MMDetection to feel the unique charm of deep learning target detection.


One, MMDetection installation

For installation, my experience is simply:
1. Hardware configuration ( GPU )
2. Linux is easier to install than Windows
3. Refer to the instructions on Github , some versions of dependent libraries must correspond

The following is my recommended video and blog:
mmdetection collection of
deep learning target detection toolbox mmdetection, train your own data

2. Introduction to Data Set

I made this example based on this video , but there are many steps that are not the same. It may be that the toolbox is updated too fast. The py files may appear joyfully or fall quietly, but the general process is still OK. .

I have the data set in the network disk. You can download it directly to jku4 . The target detection is composed of "pots" and "instructions". Let me show you a picture:

Insert picture description here
The dataset opens into three folders:

Insert picture description here

There are two JSON files in the annotations folder:
Insert picture description here

In Instances_train2017, the types are'hu' and'shu' (should be the meaning of pot and book):

Insert picture description here

2. Create and change related py files

Note that the mmdetection-master I used here was downloaded on Github . This toolbox is updated very quickly, and the corresponding py files may be updated, but the general process is almost the same.

1. Modify the CLASSES=('hu','shu') under coco.py, don't reverse the order, because the JSON format (see the figure above) is in the order of'hu','shu'.

class CocoDataset(CustomDataset):
	CLASSES=('hu','shu')

2. Modify configs/faster-rcnn/faster_rcnn_r50_fpn_1x_coco.py to find the 4 py files it depends on.

Insert picture description here

2.1 Modify configs/ base /models/faster_rcnn_r50_fpn.py.

Step1: Find num_classes=3, there is only one num_classes in my py file (in fact, I have been wondering whether it is 2 or 3, originally it was 81. I counted the number of categories to be 80, so the background should be considered. But the first time you try to set num_classes=2 can also be trained, update: I tried a new data set, 2 categories, but an error was reported when num_classes=3, you can try both methods) Step2 : Pretrained=None (at the top) but I don’t understand why this is the case. You can watch that video.

Insert picture description here
2.2 Modify configs/ base /datasets/coco_detection.py to
modify the relative path data_root and image_scale() of the file.

2.3 Modify schedule_1x.py and default_runtime.py

Step1: First check the number of your GPUs through torch.cuda.device_count(). When the number of GPUs is 8, lr=0.02; when the number of GPUs is 4, lr=0.01; I only need one GPU, so I set lr=0.0025. Here you can also modify total_epochs (the total number of iterations).
Insert picture description here
Insert picture description here

Step2: The interval=11 here is because there are a total of 11 photos in Val2017. [Update: In other words, the gap between each segment, as shown in the figure below, there are a total of 4000 photos in val, but if I set it to 12, the training process This is shown in the figure below]
Insert picture description here

load_from='faster_rcnn_r50_fpn_1x_3.pth' (corresponding to the following)
Insert picture description here

3. Modify coco_classes() in mmdet/evaluation/class_names.py.

Insert picture description here

4. Create a py file named changefasterrcnn in the mmdetection-master directory.

Here you need to create a checkpoints in this directory, and download the corresponding pth, this is the officially trained weight file, you can find the link to download in the readme of the corresponding method folder, like this is in the faster-rcnn folder Download from the readme here is the link extraction code I provided : 9iq1 (may be unavailable due to official updates, an Error will occur, such as "pth is not a checkpoint").
Insert picture description here

Three, compile training

Xshell (terminal) input:

python changefasterrcnn.py

After running, faster_rcnn_r50_fpn_1x_3.pth will be generated in the mmdetection-master directory, which corresponds to the pth file mentioned in Step 2 of 2.3 (load_from='faster_rcnn_r50_fpn_1x_3.pth').
Insert picture description here

This is the training process, the process is about 5 minutes (here you need to create a work_dirs folder in advance, what logs in the training process, the json file and the pth file of each iteration, here pay attention to latest.pth, it is a link and Not a pth file, you can use epoch_10.pth to test at last):

python tools/train.py configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py

Insert picture description here
The accuracy and recall rate of the tenth iteration training:
Insert picture description here

Fourth, run the Demo instance

1. Modify image_demo.py

Because the Xshell I use is not good for interface display (Pycharm community version seems to be allergic to pyplot), that is, the last show_result_pyplot method cannot be used. If you can, don't change it.
Insert picture description here
The specific content of show_result_pyplot() is in this place:
Insert picture description here

Change plt.show() to plt.savefig('myfigure'), save the picture and open it for viewing, so that it does not jump out:
Insert picture description here

2. Terminal operation

Put a photo (00000.bmp) into the demo folder first, and put the epoch_10.pth obtained from the previous training into the checkpoints folder.

python demo/image_demo.py demo/00000.bmp configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py checkpoints/epoch_10.pth

Finally, when clang clang clang, you can get this hard-won myfigure , and the detection effect is still good:
Insert picture description here


to sum up

Although this MMDetection toolkit is very useful, no matter the installation configuration of the environment or the modification of the subsequent py file content, our patience and care are greatly tested. I hope everyone can exercise their abilities from it. If you encounter a bug, you can also Leave a message below, if I also encounter the same bug, I will exchange and discuss with you. If you are interested in target detection, you can also participate in Kaggle, Tianchi and special coco data competitions.

Guess you like

Origin blog.csdn.net/weixin_45734379/article/details/112725000