(十)mmdetection源码解读:build_detector

目录

  • 一、build_detector调用过程
  • 二、build_detector参数分析

一、build_detector调用过程

model = build_detector(
    cfg.model, train_cfg=cfg.get('train_cfg'), test_cfg=cfg.get('test_cfg'))
#build_detector函数中
def build_detector(cfg, train_cfg=None, test_cfg=None):
    return DETECTORS.build(
        cfg, default_args=dict(train_cfg=train_cfg, test_cfg=test_cfg))

DETECTORS就是一个注册器类

from mmcv.cnn import MODELS as MMCV_MODELS
from mmcv.utils import Registry

MODELS = Registry('models', parent=MMCV_MODELS)#指定父类MODELS

BACKBONES = MODELS
NECKS = MODELS
ROI_EXTRACTORS = MODELS
SHARED_HEADS = MODELS
HEADS = MODELS
LOSSES = MODELS
DETECTORS = MODELS   #注意在这里 

build_model_from_cfg最终还是通过build_from_cfg函数来进行类的实例化

def build_model_from_cfg(cfg, registry, default_args=None):
  
    if isinstance(cfg, list):
        modules = [
            build_from_cfg(cfg_, registry, default_args) for cfg_ in cfg
        ]
        return Sequential(*modules)
    else:
        return build_from_cfg(cfg, registry, default_args)

MODELS = Registry('model', build_func=build_model_from_cfg)

二、build_detector参数分析

我们看到在build_detector函数中有三个参数

model = build_detector(
    cfg.model, train_cfg=cfg.get('train_cfg'), test_cfg=cfg.get('test_cfg'))

在build_from_cfg函数调用时,train_cfg=cfg.get(‘train_cfg’), test_cfg=cfg.get(‘test_cfg’)这两个参数被封装在default_args中,进行调用。
这里需要注意,cfg.model已经包含了’train_cfg’和’test_cfg’, 那么后两个参数就是对配置参数进行重新的设置。

build_from_cfg(cfg, registry, default_args)

你可能感兴趣的:(mmdtection,python,pytorch,目标检测)