Build normalization layer. Args: cfg (dict): The norm layer config, which should contain: - type (str): Layer type. - layer args: Args needed to instantiate a norm layer. - requires_grad (bool, optional): Whether stop gradient updates. num_fe
(cfg, num_features, postfix='')
| 100 | return 'norm_layer' |
| 101 | |
| 102 | def build_norm_layer(cfg, num_features, postfix=''): |
| 103 | """Build normalization layer. |
| 104 | |
| 105 | Args: |
| 106 | cfg (dict): The norm layer config, which should contain: |
| 107 | |
| 108 | - type (str): Layer type. |
| 109 | - layer args: Args needed to instantiate a norm layer. |
| 110 | - requires_grad (bool, optional): Whether stop gradient updates. |
| 111 | num_features (int): Number of input channels. |
| 112 | postfix (int | str): The postfix to be appended into norm abbreviation |
| 113 | to create named layer. |
| 114 | |
| 115 | Returns: |
| 116 | tuple[str, nn.Module]: The first element is the layer name consisting |
| 117 | of abbreviation and postfix, e.g., bn1, gn. The second element is the |
| 118 | created norm layer. |
| 119 | """ |
| 120 | if not isinstance(cfg, dict): |
| 121 | raise TypeError('cfg must be a dict') |
| 122 | if 'type' not in cfg: |
| 123 | raise KeyError('the cfg dict must contain the key "type"') |
| 124 | cfg_ = cfg.copy() |
| 125 | |
| 126 | layer_type = cfg_.pop('type') |
| 127 | if layer_type not in NORM_LAYERS: |
| 128 | raise KeyError(f'Unrecognized norm type {layer_type}') |
| 129 | |
| 130 | norm_layer = eval(layer_type)('') |
| 131 | abbr = infer_abbr(norm_layer) |
| 132 | |
| 133 | assert isinstance(postfix, (int, str)) |
| 134 | name = abbr + str(postfix) |
| 135 | |
| 136 | requires_grad = cfg_.pop('requires_grad', True) |
| 137 | cfg_.setdefault('eps', 1e-5) |
| 138 | if layer_type != 'GN': |
| 139 | layer = norm_layer(num_features, **cfg_) |
| 140 | if layer_type == 'SyncBN' and hasattr(layer, '_specify_ddp_gpu_num'): |
| 141 | layer._specify_ddp_gpu_num(1) |
| 142 | else: |
| 143 | assert 'num_groups' in cfg_ |
| 144 | layer = norm_layer(num_channels=num_features, **cfg_) |
| 145 | |
| 146 | for param in layer.parameters(): |
| 147 | param.requires_grad = requires_grad |
| 148 | |
| 149 | return name,layer |
| 150 | |
| 151 | def build_activation_layer(cfg): |
| 152 | """Build activation layer. |
no test coverage detected