foreword
I am learning mmdetect recently. Generally speaking, the framework of mmlab feels quite difficult to get started. I also combined the up masters of station b (the name of the up master: I am Tudui, OneShotLove, more expensive than flying birds_HKL ) and Zhihu The mmlab official watch and learn, it is really a nanny-level tutorial, highly recommended. But everyone, in order to prevent forgetting in the future, record it. If there is something wrong, everyone is welcome to criticize and correct.
For data pre-processing, please refer to the previous blog: mmdetect2d trains its own data set (1) - labelme data processing
1. Data preparation
Add the data folder under mmdetection, and put the prepared coco dataset into:
Second, modify the config file
1. Modify the size of the input image according to your own computer configuration. The main thing is to change the resize size in the corresponding files under
configs/ base
/models: here, because my computing power is still enough, I will not modify it.
In addition, I don't know why, the image path must be an absolute path during training so that no error will be reported. Here it can also be changed at the beginning of these four files.
2. Modify num_classes according to the number of types detected.
Modify the value of num_classes in the corresponding model in the configs/ base /models file. There are two places in total. If you want to detect 3 types, write 3. One thing to note here is that there is no need to classify the background into one category in mmdetect, that is, you can write several categories according to your own categories:
3. Modify the corresponding label value according to the label of the data set
Modify the label file in mmdet/core/evaluation/class_names
4. Start running
Modify the corresponding label in mmdet/datasets/coco.py
Here, it should be noted that it is a tuple, if there is only one label, remember to add a comma
5. Run the program
can Enter directly in the terminal
python tools/train.py configs/mask_rcnn/mask_rcnn_r50_fpn_2x_coco.py --gpus 2 --work-dir mywork_dir
I didn't specify checkpoints here, not because I forgot, but because I want to train myself, and the effect of using the pre-trained model is not very good, so I didn't add it. However, when I use commands in the terminal, there will be errors, so I directly modify the parameters in the menu bar run–>adit configurations, which also has the advantage that I don’t need to enter commands every time.
In addition, just follow the number of gpus you want to use directly after –gpus.
6. Modify the corresponding hyperparameters.
If you do not want to modify the hyperparameters, you can run it directly. But if you want to modify the hyperparameters, you can directly stop the program with ctrl+c after running, and then there will be a .py in your output path The configuration file, the name is the model you use, for example, mine is mask_rcnn_r50_fpn_2x_coco.py, here you can modify the relevant parameters of your model, I usually modify the following part of the runner to modify the number of iterations, by the way, generally
speaking Here, the model name 1× represents 12, and 2× is 24, which refers to the maximum number of iterations. You can also directly modify it to what you want.
The optimizer is an optimizer. SGD is used here, and the learning rate is set to 0.02. The official statement is that 8 gpus use 0.02. If you have a few gpus, you can scale them down or expand them. For example, I have two gpus, so I lr= 0.005.
Checkpoints_config is to set how many times you save the model, the default is 1, modify it according to personal needs, I use 2.
The interval in log_config is the same, and your training log is saved once a few times
. I haven’t used the following ones, but they are roughly breakpoint training and workflow settings. I haven’t figured it out yet, and I will add it later.
At this point, this model can be used to train your own data set!