forked from yinguobing/cnn-facial-landmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
163 lines (138 loc) · 5.26 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
import tensorflow as tf
class LandmarkModel(object):
def __init__(self, output_size):
self.output_size = output_size
def __call__(self, input_tensor):
# |== Layer 0: input layer ==|
# Input feature x should be of shape (batch_size, image_width, image_height,
# color_channels). As we will directly using the decoded image tensor of
# data type int8, a convertion should be performed.
inputs = tf.cast(input_tensor, tf.float32)
# |== Layer 1 ==|
with tf.variable_scope('layer1'):
# Convolutional layer.
# Computes 32 features using a 3x3 filter with ReLU activation.
conv1 = tf.layers.conv2d(
inputs=inputs,
filters=32,
kernel_size=[3, 3],
strides=(1, 1),
padding='valid',
activation=tf.nn.relu)
# Pooling layer.
# First max pooling layer with a 2x2 filter and stride of 2.
pool1 = tf.layers.max_pooling2d(
inputs=conv1,
pool_size=[2, 2],
strides=(2, 2),
padding='valid')
# |== Layer 2 ==|
with tf.variable_scope('layer2'):
# Convolutional layer
# Computes 64 features using a 3x3 filter with ReLU activation.
conv2 = tf.layers.conv2d(
inputs=pool1,
filters=64,
kernel_size=[3, 3],
strides=(1, 1),
padding='valid',
activation=tf.nn.relu)
# Convolutional layer
# Computes 64 features using a 3x3 filter with ReLU activation.
conv3 = tf.layers.conv2d(
inputs=conv2,
filters=64,
kernel_size=[3, 3],
strides=(1, 1),
padding='valid',
activation=tf.nn.relu)
# Pooling layer
# Second max pooling layer with a 2x2 filter and stride of 2.
pool2 = tf.layers.max_pooling2d(
inputs=conv3,
pool_size=[2, 2],
strides=(2, 2),
padding='valid')
# |== Layer 3 ==|
with tf.variable_scope('layer3'):
# Convolutional layer
# Computes 64 features using a 3x3 filter with ReLU activation.
conv4 = tf.layers.conv2d(
inputs=pool2,
filters=64,
kernel_size=[3, 3],
strides=(1, 1),
padding='valid',
activation=tf.nn.relu)
# Convolutional layer
# Computes 64 features using a 3x3 filter with ReLU activation.
conv5 = tf.layers.conv2d(
inputs=conv4,
filters=64,
kernel_size=[3, 3],
strides=(1, 1),
padding='valid',
activation=tf.nn.relu)
# Pooling layer
# Third max pooling layer with a 2x2 filter and stride of 2.
pool3 = tf.layers.max_pooling2d(
inputs=conv5,
pool_size=[2, 2],
strides=(2, 2),
padding='valid')
# |== Layer 4 ==|
with tf.variable_scope('layer4'):
# Convolutional layer
# Computes 128 features using a 3x3 filter with ReLU activation.
conv6 = tf.layers.conv2d(
inputs=pool3,
filters=128,
kernel_size=[3, 3],
strides=(1, 1),
padding='valid',
activation=tf.nn.relu)
# Convolutional layer
# Computes 128 features using a 3x3 filter with ReLU activation.
conv7 = tf.layers.conv2d(
inputs=conv6,
filters=128,
kernel_size=[3, 3],
strides=(1, 1),
padding='valid',
activation=tf.nn.relu)
# Pooling layer
# Fourth max pooling layer with a 2x2 filter and stride of 2.
pool4 = tf.layers.max_pooling2d(
inputs=conv7,
pool_size=[2, 2],
strides=(1, 1),
padding='valid')
# |== Layer 5 ==|
with tf.variable_scope('layer5'):
# Convolutional layer
conv8 = tf.layers.conv2d(
inputs=pool4,
filters=256,
kernel_size=[3, 3],
strides=(1, 1),
padding='valid',
activation=tf.nn.relu)
# |== Layer 6 ==|
with tf.variable_scope('layer6'):
# Flatten tensor into a batch of vectors
flatten = tf.layers.flatten(inputs=conv8)
# Dense layer 1, a fully connected layer.
dense1 = tf.layers.dense(
inputs=flatten,
units=1024,
activation=tf.nn.relu,
use_bias=True)
# Dense layer 2, also known as the output layer.
logits = tf.layers.dense(
inputs=dense1,
units=self.output_size,
activation=None,
use_bias=True,
name="logits")
logits = tf.identity(logits, 'final_dense')
return logits