在mmdetection框架中自定义模型并注册(registry)

我已经在conda虚拟环境中安装好了mmdet库,自定义模型的模型如下:

import torch
import torch.nn as nn
from mmdet.models import DETECTORS

@DETECTORS.register_module()
class A(nn.Module):
    def __init__(self, train_cfg, test_cfg):
        super(A, self).__init__()
        self.a = nn.Parameter(torch.randn(3, 4))
        self.train_cfg = train_cfg
        self.test_cfg = test_cfg
        
    def forward(self):
        return self.a

通过调用DETECTORS.register_module()方法实现模型的注册,

我的配置文件如下,

model = dict(
    type='B',
    train_cfg=dict(),
    test_cfg=dict()
)

在主函数中通过配置文件实例化model,

from mmcv import Config
from mmdet3d.models import build_model
import os

if __name__ == '__main__':
    
    cfg = Config.fromfile('cfg.py')
    model = build_model(cfg.model)

此时mmcv库报错:

KeyError: 'A is not in the models registry'

修改后的代码如下:

from mmcv import Config
from mmdet3d.models import build_model
import os
from temp.model import A

if __name__ == '__main__':
    cfg = Config.fromfile('cfg.py')
    model = build_model(cfg.model)

也就是说在文件中导入class A时,装饰器函数才会生效,模块注册才完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值