-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathtorchvision_model.py
executable file
·254 lines (202 loc) · 9.38 KB
/
torchvision_model.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import torch
import torch.nn as nn
import torchvision.models.detection.backbone_utils as backbone_utils
import torchvision.models.resnet as resnet
import torchvision.models._utils as _utils
import torch.nn.functional as F
from collections import OrderedDict
from anchors import Anchors
from utils import RegressionTransform
import losses
class ContextModule(nn.Module):
def __init__(self,in_channels=256):
super(ContextModule,self).__init__()
self.det_conv1 = nn.Sequential(
nn.Conv2d(in_channels,in_channels,kernel_size=3,stride=1,padding=1),
nn.BatchNorm2d(in_channels)
)
self.det_context_conv1 = nn.Sequential(
nn.Conv2d(in_channels,in_channels//2,kernel_size=3,stride=1,padding=1),
nn.BatchNorm2d(in_channels//2),
nn.ReLU(inplace=True)
)
self.det_context_conv2 = nn.Sequential(
nn.Conv2d(in_channels//2,in_channels//2,kernel_size=3,stride=1,padding=1),
nn.BatchNorm2d(in_channels//2)
)
self.det_context_conv3_1 = nn.Sequential(
nn.Conv2d(in_channels//2,in_channels//2,kernel_size=3,stride=1,padding=1),
nn.BatchNorm2d(in_channels//2),
nn.ReLU(inplace=True)
)
self.det_context_conv3_2 = nn.Sequential(
nn.Conv2d(in_channels//2,in_channels//2,kernel_size=3,stride=1,padding=1),
nn.BatchNorm2d(in_channels//2)
)
self.det_concat_relu = nn.ReLU(inplace=True)
def forward(self,x):
x1 = self.det_conv1(x)
x_ = self.det_context_conv1(x)
x2 = self.det_context_conv2(x_)
x3_ = self.det_context_conv3_1(x_)
x3 = self.det_context_conv3_2(x3_)
out = torch.cat((x1,x2,x3),1)
act_out = self.det_concat_relu(out)
return act_out
class FeaturePyramidNetwork(nn.Module):
def __init__(self,in_channels_list,out_channels):
super(FeaturePyramidNetwork,self).__init__()
self.lateral_blocks = nn.ModuleList()
self.context_blocks = nn.ModuleList()
self.aggr_blocks = nn.ModuleList()
for i, in_channels in enumerate(in_channels_list):
if in_channels == 0:
continue
lateral_block_module = nn.Sequential(
nn.Conv2d(in_channels,out_channels,kernel_size=1,stride=1,padding=0),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)
)
aggr_block_module = nn.Sequential(
nn.Conv2d(out_channels,out_channels,kernel_size=3,stride=1,padding=1),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)
)
context_block_module = ContextModule(out_channels)
self.lateral_blocks.append(lateral_block_module)
self.context_blocks.append(context_block_module)
if i > 0 :
self.aggr_blocks.append(aggr_block_module)
# initialize params of fpn layers
for m in self.modules():
if isinstance(m,nn.Conv2d):
nn.init.kaiming_uniform_(m.weight, a=1)
nn.init.constant_(m.bias, 0)
def forward(self,x):
names = list(x.keys())
x = list(x.values())
last_inner = self.lateral_blocks[-1](x[-1])
results = []
results.append(self.context_blocks[-1](last_inner))
for feature, lateral_block, context_block, aggr_block in zip(
x[:-1][::-1], self.lateral_blocks[:-1][::-1], self.context_blocks[:-1][::-1], self.aggr_blocks[::-1]
):
if not lateral_block:
continue
lateral_feature = lateral_block(feature)
feat_shape = lateral_feature.shape[-2:]
inner_top_down = F.interpolate(last_inner, size=feat_shape, mode="nearest")
last_inner = lateral_feature + inner_top_down
last_inner = aggr_block(last_inner)
results.insert(0, context_block(last_inner))
# make it back an OrderedDict
out = OrderedDict([(k, v) for k, v in zip(names, results)])
return out
class ClassHead(nn.Module):
def __init__(self,inchannels=512,num_anchors=3):
super(ClassHead,self).__init__()
self.num_anchors = num_anchors
self.conv1x1 = nn.Conv2d(inchannels,self.num_anchors*2,kernel_size=(1,1),stride=1,padding=0)
self.output_act = nn.LogSoftmax(dim=-1)
def forward(self,x):
out = self.conv1x1(x)
out = out.permute(0,2,3,1)
b, h, w, c = out.shape
out = out.view(b, h, w, self.num_anchors, 2)
out = self.output_act(out)
return out.contiguous().view(out.shape[0], -1, 2)
class BboxHead(nn.Module):
def __init__(self,inchannels=512,num_anchors=3):
super(BboxHead,self).__init__()
self.conv1x1 = nn.Conv2d(inchannels,num_anchors*4,kernel_size=(1,1),stride=1,padding=0)
def forward(self,x):
out = self.conv1x1(x)
out = out.permute(0,2,3,1)
return out.contiguous().view(out.shape[0], -1, 4)
class LandmarkHead(nn.Module):
def __init__(self,inchannels=512,num_anchors=3):
super(LandmarkHead,self).__init__()
self.conv1x1 = nn.Conv2d(inchannels,num_anchors*10,kernel_size=(1,1),stride=1,padding=0)
def forward(self,x):
out = self.conv1x1(x)
out = out.permute(0,2,3,1)
return out.contiguous().view(out.shape[0], -1, 10)
class RetinaFace(nn.Module):
def __init__(self,backbone,return_layers,anchor_nums=3):
super(RetinaFace,self).__init__()
# if backbone_name == 'resnet50':
# self.backbone = resnet.resnet50(pretrained)
# self.backbone = resnet.__dict__[backbone_name](pretrained=pretrained)
# self.return_layers = {'layer1': 0, 'layer2': 1, 'layer3': 2, 'layer4': 3}
assert backbone,'Backbone can not be none!'
assert len(return_layers)>0,'There must be at least one return layers'
self.body = _utils.IntermediateLayerGetter(backbone, return_layers)
in_channels_stage2 = 256
# in_channels_stage2 = 64
in_channels_list = [
#in_channels_stage2,
in_channels_stage2 * 2,
in_channels_stage2 * 4,
in_channels_stage2 * 8,
]
out_channels = 256
self.fpn = FeaturePyramidNetwork(in_channels_list,out_channels)
# self.ClassHead = ClassHead()
# self.BboxHead = BboxHead()
# self.LandmarkHead = LandmarkHead()
self.ClassHead = self._make_class_head()
self.BboxHead = self._make_bbox_head()
self.LandmarkHead = self._make_landmark_head()
self.anchors = Anchors()
self.regressBoxes = RegressionTransform()
self.losslayer = losses.LossLayer()
def _make_class_head(self,fpn_num=3,inchannels=512,anchor_num=3):
classhead = nn.ModuleList()
for i in range(fpn_num):
classhead.append(ClassHead(inchannels,anchor_num))
return classhead
def _make_bbox_head(self,fpn_num=3,inchannels=512,anchor_num=3):
bboxhead = nn.ModuleList()
for i in range(fpn_num):
bboxhead.append(BboxHead(inchannels,anchor_num))
return bboxhead
def _make_landmark_head(self,fpn_num=3,inchannels=512,anchor_num=3):
landmarkhead = nn.ModuleList()
for i in range(fpn_num):
landmarkhead.append(LandmarkHead(inchannels,anchor_num))
return landmarkhead
def freeze_bn(self):
'''Freeze BatchNorm layers.'''
for layer in self.modules():
if isinstance(layer, nn.BatchNorm2d):
layer.eval()
def forward(self,inputs):
if self.training:
img_batch, annotations = inputs
else:
img_batch = inputs
out = self.body(img_batch)
features = self.fpn(out)
# bbox_regressions = torch.cat([self.BboxHead(feature) for feature in features.values()], dim=1)
# ldm_regressions = torch.cat([self.LandmarkHead(feature) for feature in features.values()], dim=1)
# classifications = torch.cat([self.ClassHead(feature) for feature in features.values()],dim=1)
bbox_regressions = torch.cat([self.BboxHead[i](feature) for i, feature in enumerate(features.values())], dim=1)
ldm_regressions = torch.cat([self.LandmarkHead[i](feature) for i, feature in enumerate(features.values())], dim=1)
classifications = torch.cat([self.ClassHead[i](feature) for i, feature in enumerate(features.values())],dim=1)
anchors = self.anchors(img_batch)
if self.training:
return self.losslayer(classifications, bbox_regressions,ldm_regressions, anchors, annotations)
else:
bboxes, landmarks = self.regressBoxes(anchors, bbox_regressions, ldm_regressions, img_batch)
return classifications, bboxes, landmarks
def create_retinaface(return_layers,backbone_name='resnet50',anchors_num=3,pretrained=True):
backbone = resnet.__dict__[backbone_name](pretrained=pretrained)
# freeze layer1
for name, parameter in backbone.named_parameters():
# if 'layer2' not in name and 'layer3' not in name and 'layer4' not in name:
# parameter.requires_grad_(False)
if name == 'conv1.weight':
# print('freeze first conv layer...')
parameter.requires_grad_(False)
model = RetinaFace(backbone,return_layers,anchor_nums=3)
return model