Fully documented training and testing on homemade datasets in mmrotate! Nanny level tutorial!

For the installation tutorial, see another blog on my homepage, Microsoft Planetary Computer (MFC): a complete record of the demo that runs through mmrotate! _I want to be an astronaut! Mom and Dad can be happy blog - CSDN blog ! By default, you have installed all the packages, and the demo can run through!

1. Import the required packages.

from mmdet.apis import init_detector, inference_detector, show_result_pyplot
import mmrotate

2. Put the data set where it should be placed.

If you are using juputer, you need to upload zip. It can be decompressed like this.

# 解压
import zipfile
import os
"""
src_path:压缩包所在文件路径
target_path:压缩后文件存放路径
"""
src_path="/home/jovyan/mmrotate/data/DOTA2.zip"
target_path="/home/jovyan/mmrotate/data/DOTA2"
if(not os.path.isdir(target_path)):
    z = zipfile.ZipFile(src_path, 'r')
    z.extractall(path=target_path)
    z.close()

## 下面开始用split! 先trainval再test!

         The file I uploaded is called DOTA2.zip, I will change the name later.

 

        For the convenience of the picture, I named my data set DOTA, so that I don't need to change the json file. It is recommended that everyone change this. Whether it is test, train or val, it is like this anyway, according to the official website:

 mmrotate
├── mmrotate
├── tools
├── configs
├── data
│   ├── DOTA
│   │   ├── train
│   │   ├── val
│   │   ├── test

At this point, your original dataset is ready!

3. Dataset cropping

Please refer to my blog for grammar: mmrotate: data set cropping_I want to be an astronaut! Mom and Dad are happy blog-CSDN Blog

Because I use a cloud server, I can't modify the json file, so I need to add -ann-dirs data/DOTA/test/labelTxt/. But you don't have to! You can change it directly in the json file!

Attention everyone! The original ms_test.json has no label path! You have to fill in or change it yourself!

         So the official code is not particularly accurate! It didn't tell us to change the json!

         paste the code

python tools/data/dota/split/img_split.py --base-json \
  tools/data/dota/split/split_configs/ms_trainval.json

python tools/data/dota/split/img_split.py --base-json \
  tools/data/dota/split/split_configs/ms_test.json

         It will put the cropped data in mmrotate/data/split_ms_dota.

 4. Download the model and weights

Download like this: What I downloaded here is rotated_retinanet. You can find all config here.

!mim download mmrotate --config rotated_retinanet_obb_r50_fpn_1x_dota_le90 --dest .

         In general, removing py is the name of the model. But sometimes it doesn't, but it will also list the names of all the models. It should be possible to see the details of the error report by yourself.

 5. Modify the configuration file

 This step is much bigger.

        First, create a new config file under the path where you put the model. My new one is called my_config.py. Please read the official configuration file explanation document in detail! Seriously look! Tutorial 1: Learning Configuration Files — After reading the mmrotate documentation, modify your own configuration files!

# 新配置继承了基础配置用于突出显示必要的修改
_base_ = './oriented_rcnn_r50_fpn_1x_dota_le90.py' # 修改这里的模型名称!
data_root = 'data/split_ms_dota/' # 修改这里的路径!
# 1. 数据集的设置
classes = ('ship',) # 修改这里的类别名称!
data = dict(
    samples_per_gpu=2,
    workers_per_gpu=2,
    train=dict(
        #注意将你的类名添加到字段 `classes`
        classes=classes,
        ann_file='data/split_ms_dota/trainval/annfiles/', #修改路径*6!
        img_prefix='data/split_ms_dota/trainval/images/'),
    val=dict(
        #注意将你的类名添加到字段 `classes`
        classes=classes,
        ann_file='data/split_ms_dota/trainval/annfiles/',
        img_prefix='data/split_ms_dota/trainval/images/'),
    test=dict(
        #注意将你的类名添加到字段 `classes`
        classes=classes,
        ann_file='data/split_ms_dota/test/annfiles',
        img_prefix='/data/split_ms_dota/test/images'))

# # 2. 模型设置
model = dict(
    roi_head=dict(
        bbox_head=dict(
        # 显式将所有 `num_classes` 字段从 15 重写为 1。
            num_classes=1))) # 修改这里!

# 我们可以使用预训练的权重来获取更好的性能
load_from = 'oriented_rcnn_r50_fpn_1x_dota_le90-6d2b2ce0.pth' #修改这里!

        Also need to modify the second line of dotav1.py!

        There are also classes that modify dota.py!

 Then run train.py!

!python tools/train.py my_config.py

 The official code given below doesn't work for me! It is recommended to use the code above.

!mim train mmdet oriented_rcnn_r50_fpn_1x_dota_le90.py # 这个不好用!

Then you can practice alchemy happily!

Guess you like

Origin blog.csdn.net/weixin_46812066/article/details/128586211