-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
269 lines (215 loc) · 9.28 KB
/
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import torch
import torch.nn as nn
import torchvision
import torch.nn.functional as F
class BlazeBlock(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1):
super(BlazeBlock, self).__init__()
self.stride = stride
self.channel_pad = out_channels - in_channels
# TFLite uses slightly different padding than PyTorch
# on the depthwise conv layer when the stride is 2.
if stride == 2:
self.max_pool = nn.MaxPool2d(kernel_size=stride, stride=stride)
padding = 0
else:
padding = (kernel_size - 1) // 2
self.convs = nn.Sequential(
nn.Conv2d(in_channels=in_channels, out_channels=in_channels,
kernel_size=kernel_size, stride=stride, padding=padding,
groups=in_channels, bias=True),
nn.Conv2d(in_channels=in_channels, out_channels=out_channels,
kernel_size=1, stride=1, padding=0, bias=True),
)
self.act = nn.ReLU(inplace=True)
def forward(self, x):
if self.stride == 2:
h = F.pad(x, (0, 2, 0, 2), "constant", 0)
x = self.max_pool(x)
else:
h = x
if self.channel_pad > 0:
x = F.pad(x, (0, 0, 0, 0, 0, self.channel_pad), "constant", 0)
return self.act(self.convs(h) + x)
class FinalBlazeBlock(nn.Module):
def __init__(self, channels, kernel_size=3):
super(FinalBlazeBlock, self).__init__()
# TFLite uses slightly different padding than PyTorch
# on the depthwise conv layer when the stride is 2.
self.convs = nn.Sequential(
nn.Conv2d(in_channels=channels, out_channels=channels,
kernel_size=kernel_size, stride=2, padding=0,
groups=channels, bias=True),
nn.Conv2d(in_channels=channels, out_channels=channels,
kernel_size=1, stride=1, padding=0, bias=True),
)
self.act = nn.ReLU(inplace=True)
def forward(self, x):
h = F.pad(x, (0, 2, 0, 2), "constant", 0)
return self.act(self.convs(h))
class BlazeNet(nn.Module):
"""The BlazeFace face detection model from MediaPipe.
The version from MediaPipe is simpler than the one in the paper;
it does not use the "double" BlazeBlocks.
Because we won't be training this model, it doesn't need to have
batchnorm layers. These have already been "folded" into the conv
weights by TFLite.
The conversion to PyTorch is fairly straightforward, but there are
some small differences between TFLite and PyTorch in how they handle
padding on conv layers with stride 2.
This version works on batches, while the MediaPipe version can only
handle a single image at a time.
Based on code from https://github.com/tkat0/PyTorch_BlazeFace/ and
https://github.com/google/mediapipe/
"""
def __init__(self, back_model=0):
super(BlazeNet, self).__init__()
# These are the settings from the MediaPipe example graphs
# mediapipe/graphs/face_detection/face_detection_mobile_gpu.pbtxt
# and mediapipe/graphs/face_detection/face_detection_back_mobile_gpu.pbtxt
self.num_classes = 1
self.num_anchors = 896
self.num_coords = 16
self.score_clipping_thresh = 100.0
self.back_model = back_model
if back_model==1:
self.x_scale = 256.0
self.y_scale = 256.0
self.h_scale = 256.0
self.w_scale = 256.0
self.min_score_thresh = 0.65
elif back_model==0:
self.x_scale = 128.0
self.y_scale = 128.0
self.h_scale = 128.0
self.w_scale = 128.0
self.min_score_thresh = 0.75
elif back_model==2:
self.x_scale = 64.0
self.y_scale = 64.0
self.h_scale = 64.0
self.w_scale = 64.0
self.min_score_thresh = 0.75
self.min_suppression_threshold = 0.3
self._define_layers()
self.drop = nn.Dropout(p=0.5, inplace=False)
if self.back_model>0:
self.fc1 = nn.Linear(in_features=224, out_features=2, bias=True)
else:
self.fc1 = nn.Linear(in_features=896, out_features=2, bias=True)
def _define_layers(self):
if self.back_model==1:
self.backbone = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=24, kernel_size=5, stride=2, padding=0, bias=True),
nn.ReLU(inplace=True),
BlazeBlock(24, 24),
BlazeBlock(24, 24),
BlazeBlock(24, 24),
BlazeBlock(24, 24),
BlazeBlock(24, 24),
BlazeBlock(24, 24),
BlazeBlock(24, 24),
BlazeBlock(24, 24, stride=2),
BlazeBlock(24, 24),
BlazeBlock(24, 24),
BlazeBlock(24, 24),
BlazeBlock(24, 24),
BlazeBlock(24, 24),
BlazeBlock(24, 24),
BlazeBlock(24, 24),
BlazeBlock(24, 48, stride=2),
BlazeBlock(48, 48),
BlazeBlock(48, 48),
BlazeBlock(48, 48),
BlazeBlock(48, 48),
BlazeBlock(48, 48),
BlazeBlock(48, 48),
BlazeBlock(48, 48),
BlazeBlock(48, 96, stride=2),
BlazeBlock(96, 96),
BlazeBlock(96, 96),
BlazeBlock(96, 96),
BlazeBlock(96, 96),
BlazeBlock(96, 96),
BlazeBlock(96, 96),
BlazeBlock(96, 96),
)
self.final = FinalBlazeBlock(96)
self.classifier_8 = nn.Conv2d(96, 2, 1, bias=True)
self.classifier_16 = nn.Conv2d(96, 6, 1, bias=True)
#self.regressor_8 = nn.Conv2d(96, 32, 1, bias=True)
#self.regressor_16 = nn.Conv2d(96, 96, 1, bias=True)
else:
self.backbone1 = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=24, kernel_size=5, stride=2, padding=0, bias=True),
nn.ReLU(inplace=True),
BlazeBlock(24, 24),
BlazeBlock(24, 28),
BlazeBlock(28, 32, stride=2),
BlazeBlock(32, 36),
BlazeBlock(36, 42),
BlazeBlock(42, 48, stride=2),
BlazeBlock(48, 56),
BlazeBlock(56, 64),
BlazeBlock(64, 72),
BlazeBlock(72, 80),
BlazeBlock(80, 88),
)
self.backbone2 = nn.Sequential(
BlazeBlock(88, 96, stride=2),
BlazeBlock(96, 96),
BlazeBlock(96, 96),
BlazeBlock(96, 96),
BlazeBlock(96, 96),
)
self.classifier_8 = nn.Conv2d(88, 2, 1, bias=True)
self.classifier_16 = nn.Conv2d(96, 6, 1, bias=True)
#self.regressor_8 = nn.Conv2d(88, 32, 1, bias=True)
#self.regressor_16 = nn.Conv2d(96, 96, 1, bias=True)
def forward(self, x):
# TFLite uses slightly different padding on the first conv layer
# than PyTorch, so do it manually.
debug = False
if debug:
print(x.shape)
x = F.pad(x, (1, 2, 1, 2), "constant", 0)
if debug:
print(x.shape)
b = x.shape[0] # batch size, needed for reshaping later
if self.back_model==1:
x = self.backbone(x) # (b, 16, 16, 96)
h = self.final(x) # (b, 8, 8, 96)
else:
x = self.backbone1(x) # (b, 88, 16, 16)
h = self.backbone2(x) # (b, 96, 8, 8)
if debug:
print(x.shape,h.shape)
# Note: Because PyTorch is NCHW but TFLite is NHWC, we need to
# permute the output from the conv layers before reshaping it.
c1 = self.classifier_8(x) # (b, 2, 16, 16)
if debug:
print(c1.shape)
c1 = c1.permute(0, 2, 3, 1) # (b, 16, 16, 2)
c1 = c1.reshape(b, -1, 1) # (b, 512, 1)
if debug:
print(c1.shape)
c2 = self.classifier_16(h) # (b, 6, 8, 8)
c2 = c2.permute(0, 2, 3, 1) # (b, 8, 8, 6)
c2 = c2.reshape(b, -1, 1) # (b, 384, 1)
if debug:
print(c2.shape)
c = torch.cat((c1, c2), dim=1) # (b, 896, 1)
#print(c.shape)
'''
r1 = self.regressor_8(x) # (b, 32, 16, 16)
r1 = r1.permute(0, 2, 3, 1) # (b, 16, 16, 32)
r1 = r1.reshape(b, -1, 16) # (b, 512, 16)
r2 = self.regressor_16(h) # (b, 96, 8, 8)
r2 = r2.permute(0, 2, 3, 1) # (b, 8, 8, 96)
r2 = r2.reshape(b, -1, 16) # (b, 384, 16)
r = torch.cat((r1, r2), dim=1) # (b, 896, 16)
'''
output = self.drop(c)
output = output.view(-1, output.shape[1])
output = self.fc1(output)
return output