|
| 1 | +# Copyright (c) OpenMMLab. All rights reserved. |
| 2 | +from typing import List, Tuple, Union |
| 3 | + |
| 4 | +import torch.nn as nn |
| 5 | +from mmcv.cnn import ConvModule |
| 6 | +from mmdet.utils import ConfigType, OptMultiConfig |
| 7 | + |
| 8 | +from mmyolo.registry import MODELS |
| 9 | +from ..layers import ELANBlock, MaxPoolAndStrideConvBlock |
| 10 | +from .base_backbone import BaseBackbone |
| 11 | + |
| 12 | + |
| 13 | +@MODELS.register_module() |
| 14 | +class YOLOv7Backbone(BaseBackbone): |
| 15 | + """Backbone used in YOLOv7. |
| 16 | +
|
| 17 | + Args: |
| 18 | + arch (str): Architecture of YOLOv7, from {P5, P6}. |
| 19 | + Defaults to P5. |
| 20 | + deepen_factor (float): Depth multiplier, multiply number of |
| 21 | + blocks in CSP layer by this amount. Defaults to 1.0. |
| 22 | + widen_factor (float): Width multiplier, multiply number of |
| 23 | + channels in each layer by this amount. Defaults to 1.0. |
| 24 | + out_indices (Sequence[int]): Output from which stages. |
| 25 | + Defaults to (2, 3, 4). |
| 26 | + frozen_stages (int): Stages to be frozen (stop grad and set eval |
| 27 | + mode). -1 means not freezing any parameters. Defaults to -1. |
| 28 | + plugins (list[dict]): List of plugins for stages, each dict contains: |
| 29 | +
|
| 30 | + - cfg (dict, required): Cfg dict to build plugin. |
| 31 | + - stages (tuple[bool], optional): Stages to apply plugin, length |
| 32 | + should be same as 'num_stages'. |
| 33 | + norm_cfg (:obj:`ConfigDict` or dict): Dictionary to construct and |
| 34 | + config norm layer. Defaults to dict(type='BN', requires_grad=True). |
| 35 | + act_cfg (:obj:`ConfigDict` or dict): Config dict for activation layer. |
| 36 | + Defaults to dict(type='SiLU'). |
| 37 | + norm_eval (bool): Whether to set norm layers to eval mode, namely, |
| 38 | + freeze running stats (mean and var). Note: Effect on Batch Norm |
| 39 | + and its variants only. |
| 40 | + init_cfg (:obj:`ConfigDict` or dict or list[dict] or |
| 41 | + list[:obj:`ConfigDict`]): Initialization config dict. |
| 42 | + """ |
| 43 | + |
| 44 | + # From left to right: |
| 45 | + # in_channels, out_channels, ELAN mode |
| 46 | + arch_settings = { |
| 47 | + 'P5': [[64, 128, 'expand_channel_2x'], [256, 512, 'expand_channel_2x'], |
| 48 | + [512, 1024, 'expand_channel_2x'], |
| 49 | + [1024, 1024, 'no_change_channel']] |
| 50 | + } |
| 51 | + |
| 52 | + def __init__(self, |
| 53 | + arch: str = 'P5', |
| 54 | + plugins: Union[dict, List[dict]] = None, |
| 55 | + deepen_factor: float = 1.0, |
| 56 | + widen_factor: float = 1.0, |
| 57 | + input_channels: int = 3, |
| 58 | + out_indices: Tuple[int] = (2, 3, 4), |
| 59 | + frozen_stages: int = -1, |
| 60 | + norm_cfg: ConfigType = dict( |
| 61 | + type='BN', momentum=0.03, eps=0.001), |
| 62 | + act_cfg: ConfigType = dict(type='SiLU', inplace=True), |
| 63 | + norm_eval: bool = False, |
| 64 | + init_cfg: OptMultiConfig = None): |
| 65 | + super().__init__( |
| 66 | + self.arch_settings[arch], |
| 67 | + deepen_factor, |
| 68 | + widen_factor, |
| 69 | + input_channels=input_channels, |
| 70 | + out_indices=out_indices, |
| 71 | + plugins=plugins, |
| 72 | + frozen_stages=frozen_stages, |
| 73 | + norm_cfg=norm_cfg, |
| 74 | + act_cfg=act_cfg, |
| 75 | + norm_eval=norm_eval, |
| 76 | + init_cfg=init_cfg) |
| 77 | + |
| 78 | + def build_stem_layer(self) -> nn.Module: |
| 79 | + """Build a stem layer.""" |
| 80 | + stem = nn.Sequential( |
| 81 | + ConvModule( |
| 82 | + 3, |
| 83 | + int(self.arch_setting[0][0] * self.widen_factor // 2), |
| 84 | + 3, |
| 85 | + padding=1, |
| 86 | + stride=1, |
| 87 | + norm_cfg=self.norm_cfg, |
| 88 | + act_cfg=self.act_cfg), |
| 89 | + ConvModule( |
| 90 | + int(self.arch_setting[0][0] * self.widen_factor // 2), |
| 91 | + int(self.arch_setting[0][0] * self.widen_factor), |
| 92 | + 3, |
| 93 | + padding=1, |
| 94 | + stride=2, |
| 95 | + norm_cfg=self.norm_cfg, |
| 96 | + act_cfg=self.act_cfg), |
| 97 | + ConvModule( |
| 98 | + int(self.arch_setting[0][0] * self.widen_factor), |
| 99 | + int(self.arch_setting[0][0] * self.widen_factor), |
| 100 | + 3, |
| 101 | + padding=1, |
| 102 | + stride=1, |
| 103 | + norm_cfg=self.norm_cfg, |
| 104 | + act_cfg=self.act_cfg)) |
| 105 | + return stem |
| 106 | + |
| 107 | + def build_stage_layer(self, stage_idx: int, setting: list) -> list: |
| 108 | + """Build a stage layer. |
| 109 | +
|
| 110 | + Args: |
| 111 | + stage_idx (int): The index of a stage layer. |
| 112 | + setting (list): The architecture setting of a stage layer. |
| 113 | + """ |
| 114 | + in_channels, out_channels, elan_mode = setting |
| 115 | + |
| 116 | + in_channels = int(in_channels * self.widen_factor) |
| 117 | + out_channels = int(out_channels * self.widen_factor) |
| 118 | + |
| 119 | + stage = [] |
| 120 | + if stage_idx == 0: |
| 121 | + pre_layer = ConvModule( |
| 122 | + in_channels, |
| 123 | + out_channels, |
| 124 | + 3, |
| 125 | + stride=2, |
| 126 | + padding=1, |
| 127 | + norm_cfg=self.norm_cfg, |
| 128 | + act_cfg=self.act_cfg) |
| 129 | + elan_layer = ELANBlock( |
| 130 | + out_channels, |
| 131 | + mode=elan_mode, |
| 132 | + num_blocks=2, |
| 133 | + norm_cfg=self.norm_cfg, |
| 134 | + act_cfg=self.act_cfg) |
| 135 | + stage.extend([pre_layer, elan_layer]) |
| 136 | + else: |
| 137 | + pre_layer = MaxPoolAndStrideConvBlock( |
| 138 | + in_channels, |
| 139 | + mode='reduce_channel_2x', |
| 140 | + norm_cfg=self.norm_cfg, |
| 141 | + act_cfg=self.act_cfg) |
| 142 | + elan_layer = ELANBlock( |
| 143 | + in_channels, |
| 144 | + mode=elan_mode, |
| 145 | + num_blocks=2, |
| 146 | + norm_cfg=self.norm_cfg, |
| 147 | + act_cfg=self.act_cfg) |
| 148 | + stage.extend([pre_layer, elan_layer]) |
| 149 | + return stage |
0 commit comments