-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathmtcnn.py
126 lines (97 loc) · 4.62 KB
/
mtcnn.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
#! /usr/bin/env python
# coding=utf-8
#================================================================
# Copyright (C) 2019 * Ltd. All rights reserved.
#
# Editor : VIM
# File name : mtcnn.py
# Author : YunYang1994
# Created date: 2020-02-26 19:22:04
# Description :
#
#================================================================
import numpy as np
import tensorflow as tf
from utils import load_weights
class PNet(tf.keras.Model):
def __init__(self):
super().__init__()
self.conv1 = tf.keras.layers.Conv2D(10, 3, 1, name='conv1')
self.prelu1 = tf.keras.layers.PReLU(shared_axes=[1,2], name="PReLU1")
self.conv2 = tf.keras.layers.Conv2D(16, 3, 1, name='conv2')
self.prelu2 = tf.keras.layers.PReLU(shared_axes=[1,2], name="PReLU2")
self.conv3 = tf.keras.layers.Conv2D(32, 3, 1, name='conv3')
self.prelu3 = tf.keras.layers.PReLU(shared_axes=[1,2], name="PReLU3")
self.conv4_1 = tf.keras.layers.Conv2D(2, 1, 1, name='conv4-1')
self.conv4_2 = tf.keras.layers.Conv2D(4, 1, 1, name='conv4-2')
def call(self, x, training=False):
out = self.prelu1(self.conv1(x))
out = tf.nn.max_pool2d(out, 2, 2, padding="SAME")
out = self.prelu2(self.conv2(out))
out = self.prelu3(self.conv3(out))
score = tf.nn.softmax(self.conv4_1(out), axis=-1)
boxes = self.conv4_2(out)
return boxes, score
class RNet(tf.keras.Model):
def __init__(self):
super().__init__()
self.conv1 = tf.keras.layers.Conv2D(28, 3, 1, name='conv1')
self.prelu1 = tf.keras.layers.PReLU(shared_axes=[1,2], name="prelu1")
self.conv2 = tf.keras.layers.Conv2D(48, 3, 1, name='conv2')
self.prelu2 = tf.keras.layers.PReLU(shared_axes=[1,2], name="prelu2")
self.conv3 = tf.keras.layers.Conv2D(64, 2, 1, name='conv3')
self.prelu3 = tf.keras.layers.PReLU(shared_axes=[1,2], name="prelu3")
self.dense4 = tf.keras.layers.Dense(128, name='conv4')
self.prelu4 = tf.keras.layers.PReLU(shared_axes=None, name="prelu4")
self.dense5_1 = tf.keras.layers.Dense(2, name="conv5-1")
self.dense5_2 = tf.keras.layers.Dense(4, name="conv5-2")
self.flatten = tf.keras.layers.Flatten()
def call(self, x, training=False):
out = self.prelu1(self.conv1(x))
out = tf.nn.max_pool2d(out, 3, 2, padding="SAME")
out = self.prelu2(self.conv2(out))
out = tf.nn.max_pool2d(out, 3, 2, padding="VALID")
out = self.prelu3(self.conv3(out))
out = self.flatten(out)
out = self.prelu4(self.dense4(out))
score = tf.nn.softmax(self.dense5_1(out), -1)
boxes = self.dense5_2(out)
return boxes, score
class ONet(tf.keras.Model):
def __init__(self):
super().__init__()
self.conv1 = tf.keras.layers.Conv2D(32, 3, 1, name="conv1")
self.prelu1 = tf.keras.layers.PReLU(shared_axes=[1,2], name="prelu1")
self.conv2 = tf.keras.layers.Conv2D(64, 3, 1, name="conv2")
self.prelu2 = tf.keras.layers.PReLU(shared_axes=[1,2], name="prelu2")
self.conv3 = tf.keras.layers.Conv2D(64, 3, 1, name="conv3")
self.prelu3 = tf.keras.layers.PReLU(shared_axes=[1,2], name="prelu3")
self.conv4 = tf.keras.layers.Conv2D(128, 2, 1, name="conv4")
self.prelu4 = tf.keras.layers.PReLU(shared_axes=[1,2], name="prelu4")
self.dense5 = tf.keras.layers.Dense(256, name="conv5")
self.prelu5 = tf.keras.layers.PReLU(shared_axes=None, name="prelu5")
self.dense6_1 = tf.keras.layers.Dense(2 , name="conv6-1")
self.dense6_2 = tf.keras.layers.Dense(4 , name="conv6-2")
self.dense6_3 = tf.keras.layers.Dense(10 , name="conv6-3")
self.flatten = tf.keras.layers.Flatten()
def call(self, x, training=False):
out = self.prelu1(self.conv1(x))
out = tf.nn.max_pool2d(out, 3, 2, padding="SAME")
out = self.prelu2(self.conv2(out))
out = tf.nn.max_pool2d(out, 3, 2, padding="VALID")
out = self.prelu3(self.conv3(out))
out = tf.nn.max_pool2d(out, 2, 2, padding="SAME")
out = self.prelu4(self.conv4(out))
out = self.dense5(self.flatten(out))
out = self.prelu5(out)
score = tf.nn.softmax(self.dense6_1(out))
boxes = self.dense6_2(out)
lamks = self.dense6_3(out)
return boxes, lamks, score
pnet, rnet, onet = PNet(), RNet(), ONet()
pnet(tf.ones(shape=[1, 12, 12, 3]))
rnet(tf.ones(shape=[1, 24, 24 ,3]))
onet(tf.ones(shape=[1, 48, 48, 3]))
load_weights(pnet, "./weights/det1.npy")
load_weights(rnet, "./weights/det2.npy")
load_weights(onet, "./weights/det3.npy")