builder.py 959 Bytes
Newer Older
Kai Chen's avatar
Kai Chen committed
1
2
from torch import nn

3
from mmdet.utils import build_from_cfg
myownskyW7's avatar
myownskyW7 committed
4
from .registry import (BACKBONES, NECKS, ROI_EXTRACTORS, SHARED_HEADS, HEADS,
Jiangmiao Pang's avatar
Jiangmiao Pang committed
5
                       LOSSES, DETECTORS)
Kai Chen's avatar
Kai Chen committed
6
7
8


def build(cfg, registry, default_args=None):
Kai Chen's avatar
Kai Chen committed
9
    if isinstance(cfg, list):
10
11
12
        modules = [
            build_from_cfg(cfg_, registry, default_args) for cfg_ in cfg
        ]
Kai Chen's avatar
Kai Chen committed
13
14
        return nn.Sequential(*modules)
    else:
15
        return build_from_cfg(cfg, registry, default_args)
Kai Chen's avatar
Kai Chen committed
16
17
18


def build_backbone(cfg):
Kai Chen's avatar
Kai Chen committed
19
    return build(cfg, BACKBONES)
Kai Chen's avatar
Kai Chen committed
20
21
22


def build_neck(cfg):
Kai Chen's avatar
Kai Chen committed
23
    return build(cfg, NECKS)
Kai Chen's avatar
Kai Chen committed
24
25
26


def build_roi_extractor(cfg):
Kai Chen's avatar
Kai Chen committed
27
    return build(cfg, ROI_EXTRACTORS)
Kai Chen's avatar
Kai Chen committed
28
29


myownskyW7's avatar
myownskyW7 committed
30
31
32
33
def build_shared_head(cfg):
    return build(cfg, SHARED_HEADS)


Kai Chen's avatar
Kai Chen committed
34
35
def build_head(cfg):
    return build(cfg, HEADS)
Kai Chen's avatar
Kai Chen committed
36
37


Jiangmiao Pang's avatar
Jiangmiao Pang committed
38
39
40
41
def build_loss(cfg):
    return build(cfg, LOSSES)


Kai Chen's avatar
Kai Chen committed
42
def build_detector(cfg, train_cfg=None, test_cfg=None):
Kai Chen's avatar
Kai Chen committed
43
    return build(cfg, DETECTORS, dict(train_cfg=train_cfg, test_cfg=test_cfg))