-
Notifications
You must be signed in to change notification settings - Fork 26
/
mtcnn.go
441 lines (359 loc) · 11.1 KB
/
mtcnn.go
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package goface
import (
"io/ioutil"
// "log"
"math"
tf "github.com/tensorflow/tensorflow/tensorflow/go"
"github.com/tensorflow/tensorflow/tensorflow/go/op"
)
type MtcnnDetector struct {
modelFile string
graph *tf.Graph
session *tf.Session
minSize float64
scaleFactor float64
scoreThresholds []float32
}
func NewMtcnnDetector(modelFile string) (*MtcnnDetector, error) {
det := &MtcnnDetector{modelFile: modelFile, minSize: 20.0, scaleFactor: 0.709, scoreThresholds: []float32{0.6, 0.7, 0.7}}
model, err := ioutil.ReadFile(modelFile)
if err != nil {
return nil, err
}
graph := tf.NewGraph()
if err := graph.Import(model, ""); err != nil {
return nil, err
}
session, err := tf.NewSession(graph, nil)
if err != nil {
return nil, err
}
det.graph = graph
det.session = session
return det, nil
}
func (det *MtcnnDetector) Close() {
if det.session != nil {
det.session.Close()
det.session = nil
}
}
func (det *MtcnnDetector) Config(scaleFactor, minSize float64, scoreThresholds []float32) {
if scaleFactor > 0 {
det.scaleFactor = scaleFactor
}
if minSize > 0 {
det.minSize = minSize
}
if scoreThresholds != nil {
det.scoreThresholds = scoreThresholds
}
}
func (det *MtcnnDetector) DetectFaces(tensor *tf.Tensor) ([][]float32, error) {
session := det.session
graph := det.graph
var err error
var total_bbox, total_reg [][]float32
var total_score []float32
h := float32(tensor.Shape()[1])
w := float32(tensor.Shape()[2])
scales := scales(float64(h), float64(w), det.scaleFactor, det.minSize)
// log.Println("scales:", scales)
// stage 1
for _, scale := range scales {
img, err := resizeImage(tensor, scale)
if err != nil {
return nil, err
}
output, err := session.Run(
map[tf.Output]*tf.Tensor{
graph.Operation("pnet/input").Output(0): img,
},
[]tf.Output{
graph.Operation("pnet/conv4-2/BiasAdd").Output(0),
graph.Operation("pnet/prob1").Output(0),
},
nil)
if err != nil {
return nil, err
}
// log.Println("pnet:", img.Shape(), "=>", output[0].Shape(), ",", output[1].Shape())
out0, _ := transpose(output[0], []int32{0, 2, 1, 3})
out1, _ := transpose(output[1], []int32{0, 2, 1, 3})
xreg := out0.Value().([][][][]float32)[0]
xscore := out1.Value().([][][][]float32)[0]
bbox, reg, score := generateBbox(xscore, xreg, scale, det.scoreThresholds[0])
if len(bbox) == 0 {
continue
}
bbox, reg, score, err = nms(bbox, reg, score, 0.5)
if len(bbox) > 0 {
total_bbox = append(total_bbox, bbox...)
total_reg = append(total_reg, reg...)
total_score = append(total_score, score...)
}
}
// log.Println("stage 1 bbox:", len(total_bbox))
if len(total_bbox) == 0 {
return nil, nil
}
total_bbox, total_reg, total_score, err = nms(total_bbox, total_reg, total_score, 0.7)
// log.Println("stage 1 nms bbox:", len(total_bbox), err)
if len(total_bbox) == 0 {
return nil, nil
}
//calibrate & square
for i, box := range total_bbox {
total_bbox[i] = square(adjustBbox(box, total_reg[i]))
}
// stage 2
imgs, err := cropResizeImage(tensor, normalizeBbox(total_bbox, w, h), []int32{24, 24}, true)
output, err := session.Run(
map[tf.Output]*tf.Tensor{
graph.Operation("rnet/input").Output(0): imgs,
},
[]tf.Output{
graph.Operation("rnet/conv5-2/conv5-2").Output(0),
graph.Operation("rnet/prob1").Output(0),
},
nil)
if err != nil {
return nil, err
}
// log.Println("rnet:", imgs.Shape(), "=>", output[0].Shape(), ",", output[1].Shape())
//filter
reg := output[0].Value().([][]float32)
score := output[1].Value().([][]float32)
total_bbox, total_reg, total_score = filterBbox(total_bbox, reg, score, det.scoreThresholds[1])
// log.Println("stage 2, filter bbox: ", len(total_bbox))
if len(total_bbox) == 0 {
return nil, nil
}
total_bbox, total_reg, total_score, err = nms(total_bbox, total_reg, total_score, 0.7)
// log.Println("stage 2, nms bbox: ", len(total_bbox), err)
if len(total_bbox) == 0 {
return nil, nil
}
//calibrate, square
for i, box := range total_bbox {
total_bbox[i] = square(adjustBbox(box, total_reg[i]))
}
// stage 3
imgs, err = cropResizeImage(tensor, normalizeBbox(total_bbox, w, h), []int32{48, 48}, true)
output, err = session.Run(
map[tf.Output]*tf.Tensor{
graph.Operation("onet/input").Output(0): imgs,
},
[]tf.Output{
graph.Operation("onet/conv6-2/conv6-2").Output(0),
graph.Operation("onet/conv6-3/conv6-3").Output(0),
graph.Operation("onet/prob1").Output(0),
},
nil)
if err != nil {
return nil, err
}
// log.Println("onet:", imgs.Shape(), "=>", output[0].Shape(), ",", output[1].Shape(), ",", output[2].Shape())
reg = output[0].Value().([][]float32)
score = output[2].Value().([][]float32)
total_bbox, total_reg, total_score = filterBbox(total_bbox, reg, score, det.scoreThresholds[2])
// log.Println("stage 3, filter bbox: ", len(total_bbox))
if len(total_bbox) == 0 {
return nil, nil
}
for i, box := range total_bbox {
total_bbox[i] = adjustBbox(box, total_reg[i])
}
total_bbox, _, total_score, err = nms(total_bbox, total_reg, total_score, 0.7)
// log.Println("stage 3, nms bbox: ", len(total_bbox), err)
return total_bbox, nil
}
func adjustBbox(bbox []float32, reg []float32) []float32 {
if len(bbox) == 4 && len(reg) == 4 {
w := bbox[2] - bbox[0] + 1.0
h := bbox[3] - bbox[1] + 1.0
bbox[0] += reg[0] * w
bbox[1] += reg[1] * h
bbox[2] += reg[2] * w
bbox[3] += reg[3] * h
}
return bbox
}
func square(bbox []float32) []float32 {
if len(bbox) != 4 {
return bbox
}
w := bbox[2] - bbox[0]
h := bbox[3] - bbox[1]
l := w
if l < h {
l = h
}
bbox[0] = bbox[0] + 0.5*w - 0.5*l
bbox[1] = bbox[1] + 0.5*h - 0.5*l
bbox[2] = bbox[0] + l
bbox[3] = bbox[1] + l
return bbox
}
func normalizeBbox(bbox [][]float32, w, h float32) [][]float32 {
out := make([][]float32, len(bbox))
for i, box := range bbox {
ibox := make([]float32, 4)
//NOTE: y1, x1, y2, x2
ibox[0] = box[1] / h
ibox[1] = box[0] / w
ibox[2] = box[3] / h
ibox[3] = box[2] / w
out[i] = ibox
}
return out
}
func filterBbox(bbox, reg [][]float32, score [][]float32, threshold float32) (nbbox, nreg [][]float32, nscore []float32) {
for i, x := range score {
if x[1] > threshold {
nbbox = append(nbbox, bbox[i])
nreg = append(nreg, reg[i])
nscore = append(nscore, x[1])
}
}
return
}
func generateBbox(imap [][][]float32, reg [][][]float32, scale float64, threshold float32) (bbox, nreg [][]float32, score []float32) {
const (
Stride = 2.0
CellSize = 12.0
)
for i, x := range imap {
for j, y := range x {
if y[1] > threshold {
n := []float32{float32(math.Floor((Stride*float64(j)+1.0)/scale + 0.5)),
float32(math.Floor((Stride*float64(i)+1.0)/scale + 0.5)),
float32(math.Floor((Stride*float64(j)+1.0+CellSize)/scale + 0.5)),
float32(math.Floor((Stride*float64(i)+1.0+CellSize)/scale + 0.5)),
}
bbox = append(bbox, n)
nreg = append(nreg, reg[i][j])
score = append(score, y[1])
}
}
}
return
}
func nms(bbox, reg [][]float32, score []float32, threshold float32) (nbbox, nreg [][]float32, nscore []float32, err error) {
tbbox, _ := tf.NewTensor(bbox)
tscore, _ := tf.NewTensor(score)
s := op.NewScope()
pbbox := op.Placeholder(s.SubScope("bbox"), tf.Float, op.PlaceholderShape(tf.MakeShape(-1, 4)))
pscore := op.Placeholder(s.SubScope("score"), tf.Float, op.PlaceholderShape(tf.MakeShape(-1)))
out := op.NonMaxSuppression(s, pbbox, pscore, op.Const(s.SubScope("max_len"), int32(len(bbox))), op.NonMaxSuppressionIouThreshold(threshold))
outs, err := runScope(s, map[tf.Output]*tf.Tensor{pbbox: tbbox, pscore: tscore}, []tf.Output{out})
if err != nil {
return
}
pick := outs[0]
if pick != nil {
if idx, ok := pick.Value().([]int32); ok {
for _, i := range idx {
nbbox = append(nbbox, bbox[i])
nreg = append(nreg, reg[i])
nscore = append(nscore, score[i])
}
}
}
return
}
func CropResizeImage(img *tf.Tensor, bbox [][]float32, size []int32) (*tf.Tensor, error) {
h := float32(img.Shape()[1])
w := float32(img.Shape()[2])
return cropResizeImage(img, normalizeBbox(bbox, w, h), size, false)
}
func cropResizeImage(img *tf.Tensor, bbox [][]float32, size []int32, normalize bool) (*tf.Tensor, error) {
tbbox, _ := tf.NewTensor(bbox)
s := op.NewScope()
pimg := op.Placeholder(s.SubScope("img"), tf.Float, op.PlaceholderShape(tf.MakeShape(1, -1, -1, 3)))
pbbox := op.Placeholder(s.SubScope("bbox"), tf.Float, op.PlaceholderShape(tf.MakeShape(-1, 4)))
ibidx := op.Const(s.SubScope("bidx"), make([]int32, len(bbox)))
isize := op.Const(s.SubScope("size"), size)
// log.Println("cropResize", img.Shape(), ",", tbbox.Shape())
out := op.CropAndResize(s, pimg, pbbox, ibidx, isize)
if normalize {
out = normalizeImage(s, out)
}
outs, err := runScope(s, map[tf.Output]*tf.Tensor{pimg: img, pbbox: tbbox}, []tf.Output{out})
if err != nil {
return nil, err
}
return outs[0], nil
}
func resizeImage(img *tf.Tensor, scale float64) (*tf.Tensor, error) {
h := int32(math.Ceil(float64(img.Shape()[1]) * scale))
w := int32(math.Ceil(float64(img.Shape()[2]) * scale))
s := op.NewScope()
pimg := op.Placeholder(s, tf.Float, op.PlaceholderShape(tf.MakeShape(1, -1, -1, 3)))
out := op.ResizeBilinear(s, pimg, op.Const(s.SubScope("size"), []int32{h, w}))
out = normalizeImage(s, out)
outs, err := runScope(s, map[tf.Output]*tf.Tensor{pimg: img}, []tf.Output{out})
if err != nil {
return nil, err
}
return outs[0], nil
}
func normalizeImage(s *op.Scope, input tf.Output) tf.Output {
out := op.Mul(s, op.Sub(s, input, op.Const(s.SubScope("mean"), float32(127.5))),
op.Const(s.SubScope("scale"), float32(0.0078125)))
out = op.Transpose(s, out, op.Const(s.SubScope("perm"), []int32{0, 2, 1, 3}))
return out
}
func transpose(img *tf.Tensor, perm []int32) (*tf.Tensor, error) {
s := op.NewScope()
in := op.Placeholder(s, tf.Float, op.PlaceholderShape(tf.MakeShape(-1, -1, -1, -1)))
out := op.Transpose(s, in, op.Const(s, perm))
outs, err := runScope(s, map[tf.Output]*tf.Tensor{in: img}, []tf.Output{out})
if err != nil {
return nil, err
}
return outs[0], nil
}
func runScope(s *op.Scope, inputs map[tf.Output]*tf.Tensor, outputs []tf.Output) ([]*tf.Tensor, error) {
graph, err := s.Finalize()
if err != nil {
return nil, err
}
session, err := tf.NewSession(graph, nil)
if err != nil {
return nil, err
}
defer session.Close()
return session.Run(inputs, outputs, nil)
}
func scales(h, w float64, factor, minSize float64) []float64 {
minl := h
if minl > w {
minl = w
}
m := 12.0 / minSize
minl = minl * m
var scales []float64
for count := 0; minl > 12.0; {
scales = append(scales, m*math.Pow(factor, float64(count)))
minl = minl * factor
count += 1
}
return scales
}
func TensorFromJpeg(bytes []byte) (*tf.Tensor, error) {
tensor, err := tf.NewTensor(string(bytes))
if err != nil {
return nil, err
}
s := op.NewScope()
input := op.Placeholder(s, tf.String)
out := op.ExpandDims(s,
op.Cast(s, op.DecodeJpeg(s, input, op.DecodeJpegChannels(3)), tf.Float),
op.Const(s.SubScope("make_batch"), int32(0)))
outs, err := runScope(s, map[tf.Output]*tf.Tensor{input: tensor}, []tf.Output{out})
if err != nil {
return nil, err
}
return outs[0], nil
}