文章目录[隐藏]
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
实验需要将现有开源目标检测代码的backbone替换为ResNeSt,开源代码基于mmdetection v1开发,mmcv版本较低(0.2.16),随着版本更新, 目前mmdetection v2.7支持ResNeSt。由于版本更新较大,新旧版本的mmdetection代码架构及实现差别较大,仅仅按照官网教程已不能将旧版mmdetection的backbone替换为ResNeSt,需改动多处,本文记录低版本mmdetection的backbone的替换过程。
提示:以下是本篇文章正文内容,下面案例可供参考
一、基本环境配置
ubuntu18.04 python3.7 pytorch1.7.1 CUDA11.1 mmcv0.5.9
二、实现过程
整体步骤按照mmdetection官网教程
1.Define resnest
将resnest.py和resnet.py粘贴至开源代码(基于旧版mmdetection开发)
在新版mmdetection (v2.10)找到resnest.py(mmdet/models/backbones/resnest.py)
先直接粘贴至开源代码中(mmdet/models/backbones/resnest.py)
由于resnest.py依赖resnest.py中的一些package和函数,因此也需要将resnet.py替换掉。方法同上
2. Import the module
在mmdet/models/backbones/init.py添加resnest
from .resnet import ResNet
from .resnext import ResNeXt
from .ssd_vgg import SSDVGG
from .hrnet import HRNet
from .resnest import ResNeSt
__all__ = ['ResNet','ResNeSt','ResNeXt', 'SSDVGG', 'HRNet']
3. 适配resnest.py
1.ResLayer
resnest.py 导入了ResLayer
因此 需要将新版中的res_layer.py(mmdet/models/utils/res_layer.py)粘贴到对应位置。并修改对应的init(mmdet/models/utils/init.py),即将ResLayer导入
from .conv_ws import conv_ws_2d, ConvWS2d
from .conv_module import build_conv_layer, ConvModule
from .norm import build_norm_layer
from .scale import Scale
from .res_layer import ResLayer, SimplifiedBasicBlock
from .weight_init import (xavier_init, normal_init, uniform_init, kaiming_init,
bias_init_with_prob)
__all__ = [
'ResLayer','conv_ws_2d', 'ConvWS2d', 'build_conv_layer', 'ConvModule',
'build_norm_layer', 'xavier_init', 'normal_init', 'uniform_init',
'kaiming_init', 'bias_init_with_prob', 'Scale','SimplifiedBasicBlock'
]
4. 适配resnet.py
resnest.py调用了resnet.py中的ResNetV1d,旧版的resnet没有该函数,因此需要将新版中的resnet.py拿过来。这就需要对新版的resnet.py进行适配。
需适配的内容如下:
4.1ResLayer
from ..utils import ResLayer
在上步已经实现适配
4.2get_root_logger
from mmdet.utils import get_root_logger
将新版mmdetection的logger.py拿过来放在对应位置(mmdet/utils/logger.py)
并修改init(mmdet/utils/init.py)
rom .flops_counter import get_model_complexity_info
from .registry import Registry, build_from_cfg
from .logger import get_root_logger
__all__ = ['get_root_logger','Registry', 'build_from_cfg', 'get_model_complexity_info']
# def get_root_logger():
# return None
4.3build_plugin_layer
from mmcv.cnn import (build_conv_layer, build_norm_layer, build_plugin_layer,
constant_init, kaiming_init)
在anaconda虚拟环境中找到mmcv,并下载一个最新版本的mmcv。
将新版mmcv(1.2.7)中的plugin.py粘贴至旧版mmcv(0.5.9)对应位置(mmcv/cnn/bricks/plugin.py),并修改init(mmcv/cnn/bricks/init.py)
该处使用的url网络请求的数据。
5. Use the backbone in your config file
# pretrained='torchvision://resnest101',
# pretrained='modelzoo://resnet50',
# pretrained='open-mmlab://resnest101',
pretrained='https://download.openmmlab.com/pretrain/third_party/resnest101_d2-f3b931b2.pth',
# backbone=dict(
# type='ResNeSt',
# depth=50,
# num_stages=4,
# out_indices=(0, 1, 2, 3),
# frozen_stages=1,
# style='pytorch'),
backbone=dict(
type='ResNeSt',
depth=101,
num_stages=4,
out_indices=(0, 1, 2, 3),
frozen_stages=1,
norm_cfg=dict(type='BN', requires_grad=True),
norm_eval=False,
style='pytorch',
stem_channels=128,
radix=2,
reduction_factor=4,
avg_down_stride=True),
版权声明:本文为CSDN博主「qq_30612045」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_30612045/article/details/114668977
暂无评论