【AI目标检测】MMROTATE踩坑记录

MMROTATE介绍

MMRotate 是一款基于 PyTorch 的旋转框检测的开源工具箱,是 OpenMMLab 项目的成员之一。

MMROTATE安装

mmrotate的安装同mmdet等类似
参考mmrotate安装

  1. 创建环境
conda create -n openmmlab python=3.7 -y
conda activate openmmlab
  1. 安装pytorch
conda install pytorch==1.7.0 torchvision==0.8.0 cudatoolkit=10.1 -c pytorch
  1. pip install 必要的包

首先是mmcv-full

pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu111/torch1.9.0/index.html

然后要安装mmdet

pip install mmdet

mmrotate建议从源码安装

git clone https://github.com/open-mmlab/mmrotate.git
cd mmrotate
pip install -r requirements/build.txt
pip install -v -e .  # or "python setup.py develop"

其中pip install -v -e .orpython setup.py develop将会安装为开发模式,所有对源码的修改都将生效。(pip install -v -e .最后的.表示当前路径,不要漏掉!)

训练测试

根据自己的要求写配置文件,修改为自己的数据集路径和类别以及类别数目

# the new config inherits the base configs to highlight the necessary modification
_base_ = '../r3det/r3det_r50_fpn_1x_dota_oc.py'

# 验证设置
evaluation = dict(interval=1, metric='mAP')
runner = dict(type='EpochBasedRunner', max_epochs=200)
checkpoint_config = dict(interval=100)

# 1. dataset settings
dataset_type = 'DOTADataset'
classes = ('comp', 'procl')
data = dict(
    samples_per_gpu=2,
    workers_per_gpu=2,
    train=dict(
        type=dataset_type,
        # explicitly add your class names to the field `classes`
        classes=classes,
        ann_file='/home/wd337/projects/mmrotate/data/dota/annotations/train',
        img_prefix='/home/wd337/projects/mmrotate/data/dota/images/train'),
    val=dict(
        type=dataset_type,
        # explicitly add your class names to the field `classes`
        classes=classes,
        ann_file='/home/wd337/projects/mmrotate/data/dota/annotations/val',
        img_prefix='/home/wd337/projects/mmrotate/data/dota/images/val'),
    test=dict(
        type=dataset_type,
        # explicitly add your class names to the field `classes`
        classes=classes,
        ann_file='/home/wd337/projects/mmrotate/data/dota/annotations/test',
        img_prefix='/home/wd337/projects/mmrotate/data/dota/images/test'))

# 2. model settings
model = dict(
    bbox_head=dict(
        # explicitly over-write all the `num_classes` field from default 15 to 5.
        num_classes=2))

训练(单卡):

python tools/train.py configs/my_config/my_config.py --work-dir=work_dirs/my_model

测试:

python ./tools/test.py \
  configs/my_config/my_config.py \
  work_dirs/my_model/my_model_pth.pth --show-dir work_dirs/vis

出现的错误

error 1

ValueError: could not convert string to float: 'imagesource:null'

原因:由于在查看DOTA数据集的时候发现有标注文件都有imagesource和gsd信息,在转换时加上了这两行信息,去掉即可

error 2

File "/home/wd337/projects/mmrotate/mmrotate/datasets/dota.py", line 199, in evaluate
    mean_ap, _ = eval_map(
TypeError: eval_map() got an unexpected keyword argument 'version'

原因:
在mean_ap传参时的version=self.version在构建eval_map时并未使用,将其注释掉

mean_ap, _ = eval_map(
                results,
                annotations,
                scale_ranges=scale_ranges,
                iou_thr=iou_thr,
                dataset=self.CLASSES,
                # version=self.version,
                logger=logger,
                nproc=nproc)
                
def eval_map(det_results,
             annotations,
             scale_ranges=None,
             iou_thr=0.5,
             dataset=None,
             logger=None,
             nproc=4):

后续0.1.1版本官方版本已经解决

error 3

  File "/home/wd337/projects/mmrotate/mmrotate/datasets/dota.py", line 199, in evaluate
    mean_ap, _ = eval_map(
  File "/home/wd337/projects/mmrotate/mmrotate/datasets/dota.py", line 454, in eval_map
    print_map_summary(
  File "/home/wd337/projects/mmrotate/mmrotate/datasets/dota.py", line 516, in print_map_summary
    label_names[j], num_gts[i, j], results[j]['num_dets'],
IndexError: tuple index out of range

配置文件中的class没有改完全,使用的r3det配置文件中在model中的refine_heads中也有num_class参数,修改为自己的class数即可

error 4

Traceback (most recent call last):
  File "tools/train.py", line 182, in <module>
    main()
  File "tools/train.py", line 171, in main
    train_detector(
  File "/home/wd337/projects/mmrotate/mmrotate/apis/train.py", line 156, in train_detector
    runner.run(data_loaders, cfg.workflow)
  File "/home/wd337/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmcv/runner/epoch_based_runner.py", line 127, in run
    epoch_runner(data_loaders[i], **kwargs)
  File "/home/wd337/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmcv/runner/epoch_based_runner.py", line 47, in train
    for i, data_batch in enumerate(self.data_loader):
  File "/home/wd337/anaconda3/envs/openmmlab/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 359, in __iter__
    return self._get_iterator()
  File "/home/wd337/anaconda3/envs/openmmlab/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 305, in _get_iterator
    return _MultiProcessingDataLoaderIter(self)
  File "/home/wd337/anaconda3/envs/openmmlab/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 944, in __init__
    self._reset(loader, first_iter=True)
  File "/home/wd337/anaconda3/envs/openmmlab/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 975, in _reset
    self._try_put_index()
  File "/home/wd337/anaconda3/envs/openmmlab/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 1209, in _try_put_index
    index = self._next_index()
  File "/home/wd337/anaconda3/envs/openmmlab/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 512, in _next_index
    return next(self._sampler_iter)  # may raise StopIteration
  File "/home/wd337/anaconda3/envs/openmmlab/lib/python3.8/site-packages/torch/utils/data/sampler.py", line 226, in __iter__
    for idx in self.sampler:
  File "/home/wd337/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmdet/datasets/samplers/group_sampler.py", line 36, in __iter__
    indices = np.concatenate(indices)
  File "<__array_function__ internals>", line 180, in concatenate
ValueError: need at least one array to concatenate

原因:
配置文件中的数据集路径写错了,一定要仔细检查

error 5

Traceback (most recent call last):
  File "/home/wd337/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmcv/utils/registry.py", line 52, in build_from_cfg
    return obj_cls(**args)
  File "/home/wd337/projects/mmrotate/mmrotate/models/detectors/r3det.py", line 48, in __init__
    self.refine_head.append(build_head(refine_head))
  File "/home/wd337/projects/mmrotate/mmrotate/models/builder.py", line 37, in build_head
    return ROTATED_HEADS.build(cfg)
  File "/home/wd337/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmcv/utils/registry.py", line 212, in build
    return self.build_func(*args, **kwargs, registry=self)
  File "/home/wd337/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmcv/cnn/builder.py", line 27, in build_model_from_cfg
    return build_from_cfg(cfg, registry, default_args)
  File "/home/wd337/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmcv/utils/registry.py", line 24, in build_from_cfg
    raise KeyError(
KeyError: '`cfg` or `default_args` must contain the key "type", but got {\'num_classes\': 2, \'train_cfg\': {\'assigner\': {\'type\': \'MaxIoUAssigner\', \'pos_iou_thr\': 0.6, \'neg_iou_thr\': 0.5, \'min_pos_iou\': 0, \'ignore_iof_thr\': -1, \'iou_calculator\': {\'type\': \'RBboxOverlaps2D\'}}, \'allowed_border\': -1, \'pos_weight\': -1, \'debug\': False}, \'test_cfg\': {\'nms_pre\': 2000, \'min_bbox_size\': 0, \'score_thr\': 0.05, \'nms\': {\'iou_thr\': 0.1}, \'max_per_img\': 2000}}\nNone'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "tools/train.py", line 182, in 
    main()
  File "tools/train.py", line 152, in main
    model = build_detector(
  File "/home/wd337/projects/mmrotate/mmrotate/models/builder.py", line 55, in build_detector
    return ROTATED_DETECTORS.build(
  File "/home/wd337/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmcv/utils/registry.py", line 212, in build
    return self.build_func(*args, **kwargs, registry=self)
  File "/home/wd337/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmcv/cnn/builder.py", line 27, in build_model_from_cfg
    return build_from_cfg(cfg, registry, default_args)
  File "/home/wd337/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmcv/utils/registry.py", line 55, in build_from_cfg
    raise type(e)(f'{obj_cls.__name__}: {e}')
KeyError: 'R3Det: \'`cfg` or `default_args` must contain the key "type", but got {\\\'num_classes\\\': 2, \\\'train_cfg\\\': {\\\'assigner\\\': {\\\'type\\\': \\\'MaxIoUAssigner\\\', \\\'pos_iou_thr\\\': 0.6, \\\'neg_iou_thr\\\': 0.5, \\\'min_pos_iou\\\': 0, \\\'ignore_iof_thr\\\': -1, \\\'iou_calculator\\\': {\\\'type\\\': \\\'RBboxOverlaps2D\\\'}}, \\\'allowed_border\\\': -1, \\\'pos_weight\\\': -1, \\\'debug\\\': False}, \\\'test_cfg\\\': {\\\'nms_pre\\\': 2000, \\\'min_bbox_size\\\': 0, \\\'score_thr\\\': 0.05, \\\'nms\\\': {\\\'iou_thr\\\': 0.1}, \\\'max_per_img\\\': 2000}}\\nNone\''

原因,配置文件的写法不规范

error 6

最后的结果都是歪的,但是mAP却很高
原因:
在确认框的方向的时候将坐标系搞错,从而在转换数据集的时候缺少了一个负号,因此框全都少了同样的角度

error 7

修改backbone中的ReResNet不起作用
原因:
默认的配置是使用的ResNet和FPN,并不是仓库中用e2cnn重写的ReResNet和ReFPN

你可能感兴趣的:(python,计算机视觉,目标检测,深度学习)