-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathblazeface.py
182 lines (152 loc) · 6.57 KB
/
blazeface.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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from blazebase import BlazeDetector, BlazeBlock, FinalBlazeBlock
class BlazeFace(BlazeDetector):
"""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/hollance/BlazeFace-PyTorch and
https://github.com/google/mediapipe/
"""
def __init__(self, back_model=False):
super(BlazeFace, self).__init__()
# These are the settings from the MediaPipe example graph
# mediapipe/graphs/face_detection/face_detection_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:
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
else:
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
self.min_suppression_threshold = 0.3
self.num_keypoints = 6
# These settings are for converting detections to ROIs which can then
# be extracted and feed into the landmark network
# use mediapipe/calculators/util/detections_to_rects_calculator.cc
self.detection2roi_method = 'box'
# mediapipe/modules/face_landmark/face_detection_front_detection_to_roi.pbtxt
self.kp1 = 1
self.kp2 = 0
self.theta0 = 0.
self.dscale = 1.5
self.dy = 0.
self._define_layers()
def _define_layers(self):
if self.back_model:
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.
x = F.pad(x, (1, 2, 1, 2), "constant", 0)
b = x.shape[0] # batch size, needed for reshaping later
if self.back_model:
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)
# 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)
c1 = c1.permute(0, 2, 3, 1) # (b, 16, 16, 2)
c1 = c1.reshape(b, -1, 1) # (b, 512, 1)
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)
c = torch.cat((c1, c2), dim=1) # (b, 896, 1)
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)
return [r, c]