-
Notifications
You must be signed in to change notification settings - Fork 41
/
biformer_mm.py
67 lines (54 loc) · 2.27 KB
/
biformer_mm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import torch
import torch.nn as nn
from mmcv_custom import load_checkpoint
from mmdet.models.builder import BACKBONES
from mmdet.utils import get_root_logger
from models_cls.biformer import BiFormer
from timm.models.layers import LayerNorm2d
@BACKBONES.register_module()
class BiFormer_mm(BiFormer):
def __init__(self, pretrained=None, norm_eval=True, disable_bn_grad=False, **kwargs):
super().__init__(**kwargs)
# step 1: remove unused segmentation head & norm
del self.head # classification head
del self.norm # head norm
# step 2: add extra norms for dense tasks
self.extra_norms = nn.ModuleList()
for i in range(4):
self.extra_norms.append(LayerNorm2d(self.embed_dim[i]))
# step 3: initialization & load ckpt
self.apply(self._init_weights)
self.init_weights(pretrained=pretrained)
# step 4: freeze bn
self.norm_eval = norm_eval
if disable_bn_grad:
for m in self.modules():
if isinstance(m, nn.BatchNorm2d):
for param in m.parameters():
param.requires_grad = False
def init_weights(self, pretrained):
if isinstance(pretrained, str):
logger = get_root_logger()
logger.info(f'Load pretrained model from {pretrained}')
load_checkpoint(self, pretrained, map_location='cpu', strict=False, logger=logger)
def forward_features(self, x: torch.Tensor):
out = []
for i in range(4):
x = self.downsample_layers[i](x)
x = self.stages[i](x)
# NOTE: in the version before submission:
# x = self.extra_norms[i](x)
# out.append(x)
out.append(self.extra_norms[i](x).contiguous())
return tuple(out)
def forward(self, x:torch.Tensor):
return self.forward_features(x)
def train(self, mode=True):
"""Convert the model into training mode while keep normalization layer
freezed."""
super().train(mode)
if mode and self.norm_eval:
for m in self.modules():
# trick: eval have effect on BatchNorm only
if isinstance(m, nn.BatchNorm2d):
m.eval()