Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
iperov
GitHub Repository: iperov/deepfacelab
Path: blob/master/facelib/S3FDExtractor.py
628 views
1
import operator
2
from pathlib import Path
3
4
import cv2
5
import numpy as np
6
7
from core.leras import nn
8
9
class S3FDExtractor(object):
10
def __init__(self, place_model_on_cpu=False):
11
nn.initialize(data_format="NHWC")
12
tf = nn.tf
13
14
model_path = Path(__file__).parent / "S3FD.npy"
15
if not model_path.exists():
16
raise Exception("Unable to load S3FD.npy")
17
18
class L2Norm(nn.LayerBase):
19
def __init__(self, n_channels, **kwargs):
20
self.n_channels = n_channels
21
super().__init__(**kwargs)
22
23
def build_weights(self):
24
self.weight = tf.get_variable ("weight", (1, 1, 1, self.n_channels), dtype=nn.floatx, initializer=tf.initializers.ones )
25
26
def get_weights(self):
27
return [self.weight]
28
29
def __call__(self, inputs):
30
x = inputs
31
x = x / (tf.sqrt( tf.reduce_sum( tf.pow(x, 2), axis=-1, keepdims=True ) ) + 1e-10) * self.weight
32
return x
33
34
class S3FD(nn.ModelBase):
35
def __init__(self):
36
super().__init__(name='S3FD')
37
38
def on_build(self):
39
self.minus = tf.constant([104,117,123], dtype=nn.floatx )
40
self.conv1_1 = nn.Conv2D(3, 64, kernel_size=3, strides=1, padding='SAME')
41
self.conv1_2 = nn.Conv2D(64, 64, kernel_size=3, strides=1, padding='SAME')
42
43
self.conv2_1 = nn.Conv2D(64, 128, kernel_size=3, strides=1, padding='SAME')
44
self.conv2_2 = nn.Conv2D(128, 128, kernel_size=3, strides=1, padding='SAME')
45
46
self.conv3_1 = nn.Conv2D(128, 256, kernel_size=3, strides=1, padding='SAME')
47
self.conv3_2 = nn.Conv2D(256, 256, kernel_size=3, strides=1, padding='SAME')
48
self.conv3_3 = nn.Conv2D(256, 256, kernel_size=3, strides=1, padding='SAME')
49
50
self.conv4_1 = nn.Conv2D(256, 512, kernel_size=3, strides=1, padding='SAME')
51
self.conv4_2 = nn.Conv2D(512, 512, kernel_size=3, strides=1, padding='SAME')
52
self.conv4_3 = nn.Conv2D(512, 512, kernel_size=3, strides=1, padding='SAME')
53
54
self.conv5_1 = nn.Conv2D(512, 512, kernel_size=3, strides=1, padding='SAME')
55
self.conv5_2 = nn.Conv2D(512, 512, kernel_size=3, strides=1, padding='SAME')
56
self.conv5_3 = nn.Conv2D(512, 512, kernel_size=3, strides=1, padding='SAME')
57
58
self.fc6 = nn.Conv2D(512, 1024, kernel_size=3, strides=1, padding=3)
59
self.fc7 = nn.Conv2D(1024, 1024, kernel_size=1, strides=1, padding='SAME')
60
61
self.conv6_1 = nn.Conv2D(1024, 256, kernel_size=1, strides=1, padding='SAME')
62
self.conv6_2 = nn.Conv2D(256, 512, kernel_size=3, strides=2, padding='SAME')
63
64
self.conv7_1 = nn.Conv2D(512, 128, kernel_size=1, strides=1, padding='SAME')
65
self.conv7_2 = nn.Conv2D(128, 256, kernel_size=3, strides=2, padding='SAME')
66
67
self.conv3_3_norm = L2Norm(256)
68
self.conv4_3_norm = L2Norm(512)
69
self.conv5_3_norm = L2Norm(512)
70
71
72
self.conv3_3_norm_mbox_conf = nn.Conv2D(256, 4, kernel_size=3, strides=1, padding='SAME')
73
self.conv3_3_norm_mbox_loc = nn.Conv2D(256, 4, kernel_size=3, strides=1, padding='SAME')
74
75
self.conv4_3_norm_mbox_conf = nn.Conv2D(512, 2, kernel_size=3, strides=1, padding='SAME')
76
self.conv4_3_norm_mbox_loc = nn.Conv2D(512, 4, kernel_size=3, strides=1, padding='SAME')
77
78
self.conv5_3_norm_mbox_conf = nn.Conv2D(512, 2, kernel_size=3, strides=1, padding='SAME')
79
self.conv5_3_norm_mbox_loc = nn.Conv2D(512, 4, kernel_size=3, strides=1, padding='SAME')
80
81
self.fc7_mbox_conf = nn.Conv2D(1024, 2, kernel_size=3, strides=1, padding='SAME')
82
self.fc7_mbox_loc = nn.Conv2D(1024, 4, kernel_size=3, strides=1, padding='SAME')
83
84
self.conv6_2_mbox_conf = nn.Conv2D(512, 2, kernel_size=3, strides=1, padding='SAME')
85
self.conv6_2_mbox_loc = nn.Conv2D(512, 4, kernel_size=3, strides=1, padding='SAME')
86
87
self.conv7_2_mbox_conf = nn.Conv2D(256, 2, kernel_size=3, strides=1, padding='SAME')
88
self.conv7_2_mbox_loc = nn.Conv2D(256, 4, kernel_size=3, strides=1, padding='SAME')
89
90
def forward(self, inp):
91
x, = inp
92
x = x - self.minus
93
x = tf.nn.relu(self.conv1_1(x))
94
x = tf.nn.relu(self.conv1_2(x))
95
x = tf.nn.max_pool(x, [1,2,2,1], [1,2,2,1], "VALID")
96
97
x = tf.nn.relu(self.conv2_1(x))
98
x = tf.nn.relu(self.conv2_2(x))
99
x = tf.nn.max_pool(x, [1,2,2,1], [1,2,2,1], "VALID")
100
101
x = tf.nn.relu(self.conv3_1(x))
102
x = tf.nn.relu(self.conv3_2(x))
103
x = tf.nn.relu(self.conv3_3(x))
104
f3_3 = x
105
x = tf.nn.max_pool(x, [1,2,2,1], [1,2,2,1], "VALID")
106
107
x = tf.nn.relu(self.conv4_1(x))
108
x = tf.nn.relu(self.conv4_2(x))
109
x = tf.nn.relu(self.conv4_3(x))
110
f4_3 = x
111
x = tf.nn.max_pool(x, [1,2,2,1], [1,2,2,1], "VALID")
112
113
x = tf.nn.relu(self.conv5_1(x))
114
x = tf.nn.relu(self.conv5_2(x))
115
x = tf.nn.relu(self.conv5_3(x))
116
f5_3 = x
117
x = tf.nn.max_pool(x, [1,2,2,1], [1,2,2,1], "VALID")
118
119
x = tf.nn.relu(self.fc6(x))
120
x = tf.nn.relu(self.fc7(x))
121
ffc7 = x
122
123
x = tf.nn.relu(self.conv6_1(x))
124
x = tf.nn.relu(self.conv6_2(x))
125
f6_2 = x
126
127
x = tf.nn.relu(self.conv7_1(x))
128
x = tf.nn.relu(self.conv7_2(x))
129
f7_2 = x
130
131
f3_3 = self.conv3_3_norm(f3_3)
132
f4_3 = self.conv4_3_norm(f4_3)
133
f5_3 = self.conv5_3_norm(f5_3)
134
135
cls1 = self.conv3_3_norm_mbox_conf(f3_3)
136
reg1 = self.conv3_3_norm_mbox_loc(f3_3)
137
138
cls2 = tf.nn.softmax(self.conv4_3_norm_mbox_conf(f4_3))
139
reg2 = self.conv4_3_norm_mbox_loc(f4_3)
140
141
cls3 = tf.nn.softmax(self.conv5_3_norm_mbox_conf(f5_3))
142
reg3 = self.conv5_3_norm_mbox_loc(f5_3)
143
144
cls4 = tf.nn.softmax(self.fc7_mbox_conf(ffc7))
145
reg4 = self.fc7_mbox_loc(ffc7)
146
147
cls5 = tf.nn.softmax(self.conv6_2_mbox_conf(f6_2))
148
reg5 = self.conv6_2_mbox_loc(f6_2)
149
150
cls6 = tf.nn.softmax(self.conv7_2_mbox_conf(f7_2))
151
reg6 = self.conv7_2_mbox_loc(f7_2)
152
153
# max-out background label
154
bmax = tf.maximum(tf.maximum(cls1[:,:,:,0:1], cls1[:,:,:,1:2]), cls1[:,:,:,2:3])
155
156
cls1 = tf.concat ([bmax, cls1[:,:,:,3:4] ], axis=-1)
157
cls1 = tf.nn.softmax(cls1)
158
159
return [cls1, reg1, cls2, reg2, cls3, reg3, cls4, reg4, cls5, reg5, cls6, reg6]
160
161
e = None
162
if place_model_on_cpu:
163
e = tf.device("/CPU:0")
164
165
if e is not None: e.__enter__()
166
self.model = S3FD()
167
self.model.load_weights (model_path)
168
if e is not None: e.__exit__(None,None,None)
169
170
self.model.build_for_run ([ ( tf.float32, nn.get4Dshape (None,None,3) ) ])
171
172
def __enter__(self):
173
return self
174
175
def __exit__(self, exc_type=None, exc_value=None, traceback=None):
176
return False #pass exception between __enter__ and __exit__ to outter level
177
178
def extract (self, input_image, is_bgr=True, is_remove_intersects=False):
179
180
if is_bgr:
181
input_image = input_image[:,:,::-1]
182
is_bgr = False
183
184
(h, w, ch) = input_image.shape
185
186
d = max(w, h)
187
scale_to = 640 if d >= 1280 else d / 2
188
scale_to = max(64, scale_to)
189
190
input_scale = d / scale_to
191
input_image = cv2.resize (input_image, ( int(w/input_scale), int(h/input_scale) ), interpolation=cv2.INTER_LINEAR)
192
193
olist = self.model.run ([ input_image[None,...] ] )
194
195
detected_faces = []
196
for ltrb in self.refine (olist):
197
l,t,r,b = [ x*input_scale for x in ltrb]
198
bt = b-t
199
if min(r-l,bt) < 40: #filtering faces < 40pix by any side
200
continue
201
b += bt*0.1 #enlarging bottom line a bit for 2DFAN-4, because default is not enough covering a chin
202
detected_faces.append ( [int(x) for x in (l,t,r,b) ] )
203
204
#sort by largest area first
205
detected_faces = [ [(l,t,r,b), (r-l)*(b-t) ] for (l,t,r,b) in detected_faces ]
206
detected_faces = sorted(detected_faces, key=operator.itemgetter(1), reverse=True )
207
detected_faces = [ x[0] for x in detected_faces]
208
209
if is_remove_intersects:
210
for i in range( len(detected_faces)-1, 0, -1):
211
l1,t1,r1,b1 = detected_faces[i]
212
l0,t0,r0,b0 = detected_faces[i-1]
213
214
dx = min(r0, r1) - max(l0, l1)
215
dy = min(b0, b1) - max(t0, t1)
216
if (dx>=0) and (dy>=0):
217
detected_faces.pop(i)
218
219
return detected_faces
220
221
def refine(self, olist):
222
bboxlist = []
223
for i, ((ocls,), (oreg,)) in enumerate ( zip ( olist[::2], olist[1::2] ) ):
224
stride = 2**(i + 2) # 4,8,16,32,64,128
225
s_d2 = stride / 2
226
s_m4 = stride * 4
227
228
for hindex, windex in zip(*np.where(ocls[...,1] > 0.05)):
229
score = ocls[hindex, windex, 1]
230
loc = oreg[hindex, windex, :]
231
priors = np.array([windex * stride + s_d2, hindex * stride + s_d2, s_m4, s_m4])
232
priors_2p = priors[2:]
233
box = np.concatenate((priors[:2] + loc[:2] * 0.1 * priors_2p,
234
priors_2p * np.exp(loc[2:] * 0.2)) )
235
box[:2] -= box[2:] / 2
236
box[2:] += box[:2]
237
238
bboxlist.append([*box, score])
239
240
bboxlist = np.array(bboxlist)
241
if len(bboxlist) == 0:
242
bboxlist = np.zeros((1, 5))
243
244
bboxlist = bboxlist[self.refine_nms(bboxlist, 0.3), :]
245
bboxlist = [ x[:-1].astype(np.int) for x in bboxlist if x[-1] >= 0.5]
246
return bboxlist
247
248
def refine_nms(self, dets, thresh):
249
keep = list()
250
if len(dets) == 0:
251
return keep
252
253
x_1, y_1, x_2, y_2, scores = dets[:, 0], dets[:, 1], dets[:, 2], dets[:, 3], dets[:, 4]
254
areas = (x_2 - x_1 + 1) * (y_2 - y_1 + 1)
255
order = scores.argsort()[::-1]
256
257
keep = []
258
while order.size > 0:
259
i = order[0]
260
keep.append(i)
261
xx_1, yy_1 = np.maximum(x_1[i], x_1[order[1:]]), np.maximum(y_1[i], y_1[order[1:]])
262
xx_2, yy_2 = np.minimum(x_2[i], x_2[order[1:]]), np.minimum(y_2[i], y_2[order[1:]])
263
264
width, height = np.maximum(0.0, xx_2 - xx_1 + 1), np.maximum(0.0, yy_2 - yy_1 + 1)
265
ovr = width * height / (areas[i] + areas[order[1:]] - width * height)
266
267
inds = np.where(ovr <= thresh)[0]
268
order = order[inds + 1]
269
return keep
270
271