-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathmodel.py
executable file
·132 lines (119 loc) · 7.42 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
"""
@AmineHorseman
Sep, 1st, 2016
"""
import tensorflow as tf
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.merge_ops import merge_outputs, merge
from tflearn.layers.normalization import local_response_normalization, batch_normalization
from tflearn.layers.estimator import regression
from tflearn.optimizers import Momentum, Adam
from parameters import NETWORK, HYPERPARAMS
def build_model(optimizer=HYPERPARAMS.optimizer, optimizer_param=HYPERPARAMS.optimizer_param,
learning_rate=HYPERPARAMS.learning_rate, keep_prob=HYPERPARAMS.keep_prob,
learning_rate_decay=HYPERPARAMS.learning_rate_decay, decay_step=HYPERPARAMS.decay_step):
if NETWORK.model == 'A':
return build_modelA(optimizer, optimizer_param, learning_rate, keep_prob, learning_rate_decay, decay_step)
elif NETWORK.model == 'B':
return build_modelB(optimizer, optimizer_param, learning_rate, keep_prob, learning_rate_decay, decay_step)
else:
print( "ERROR: no model " + str(NETWORK.model))
exit()
def build_modelB(optimizer=HYPERPARAMS.optimizer, optimizer_param=HYPERPARAMS.optimizer_param,
learning_rate=HYPERPARAMS.learning_rate, keep_prob=HYPERPARAMS.keep_prob,
learning_rate_decay=HYPERPARAMS.learning_rate_decay, decay_step=HYPERPARAMS.decay_step):
images_network = input_data(shape=[None, NETWORK.input_size, NETWORK.input_size, 1], name='input1')
images_network = conv_2d(images_network, 64, 3, activation=NETWORK.activation)
#images_network = local_response_normalization(images_network)
if NETWORK.use_batchnorm_after_conv_layers:
images_network = batch_normalization(images_network)
images_network = max_pool_2d(images_network, 3, strides = 2)
images_network = conv_2d(images_network, 128, 3, activation=NETWORK.activation)
if NETWORK.use_batchnorm_after_conv_layers:
images_network = batch_normalization(images_network)
images_network = max_pool_2d(images_network, 3, strides = 2)
images_network = conv_2d(images_network, 256, 3, activation=NETWORK.activation)
if NETWORK.use_batchnorm_after_conv_layers:
images_network = batch_normalization(images_network)
images_network = max_pool_2d(images_network, 3, strides = 2)
images_network = dropout(images_network, keep_prob=keep_prob)
images_network = fully_connected(images_network, 4096, activation=NETWORK.activation)
images_network = dropout(images_network, keep_prob=keep_prob)
images_network = fully_connected(images_network, 1024, activation=NETWORK.activation)
if NETWORK.use_batchnorm_after_fully_connected_layers:
images_network = batch_normalization(images_network)
if NETWORK.use_landmarks or NETWORK.use_hog_and_landmarks:
if NETWORK.use_hog_sliding_window_and_landmarks:
landmarks_network = input_data(shape=[None, 2728], name='input2')
elif NETWORK.use_hog_and_landmarks:
landmarks_network = input_data(shape=[None, 208], name='input2')
else:
landmarks_network = input_data(shape=[None, 68, 2], name='input2')
landmarks_network = fully_connected(landmarks_network, 1024, activation=NETWORK.activation)
if NETWORK.use_batchnorm_after_fully_connected_layers:
landmarks_network = batch_normalization(landmarks_network)
landmarks_network = fully_connected(landmarks_network, 128, activation=NETWORK.activation)
if NETWORK.use_batchnorm_after_fully_connected_layers:
landmarks_network = batch_normalization(landmarks_network)
images_network = fully_connected(images_network, 128, activation=NETWORK.activation)
network = merge([images_network, landmarks_network], 'concat', axis=1)
else:
network = images_network
network = fully_connected(network, NETWORK.output_size, activation='softmax')
if optimizer == 'momentum':
optimizer = Momentum(learning_rate=learning_rate, momentum=optimizer_param,
lr_decay=learning_rate_decay, decay_step=decay_step)
elif optimizer == 'adam':
optimizer = Adam(learning_rate=learning_rate, beta1=optimizer_param, beta2=learning_rate_decay)
else:
print( "Unknown optimizer: {}".format(optimizer))
network = regression(network, optimizer=optimizer, loss=NETWORK.loss, learning_rate=learning_rate, name='output')
return network
def build_modelA(optimizer=HYPERPARAMS.optimizer, optimizer_param=HYPERPARAMS.optimizer_param,
learning_rate=HYPERPARAMS.learning_rate, keep_prob=HYPERPARAMS.keep_prob,
learning_rate_decay=HYPERPARAMS.learning_rate_decay, decay_step=HYPERPARAMS.decay_step):
images_network = input_data(shape=[None, NETWORK.input_size, NETWORK.input_size, 1], name='input1')
images_network = conv_2d(images_network, 64, 5, activation=NETWORK.activation)
#images_network = local_response_normalization(images_network)
if NETWORK.use_batchnorm_after_conv_layers:
images_network = batch_normalization(images_network)
images_network = max_pool_2d(images_network, 3, strides = 2)
images_network = conv_2d(images_network, 64, 5, activation=NETWORK.activation)
if NETWORK.use_batchnorm_after_conv_layers:
images_network = batch_normalization(images_network)
images_network = max_pool_2d(images_network, 3, strides = 2)
images_network = conv_2d(images_network, 128, 4, activation=NETWORK.activation)
if NETWORK.use_batchnorm_after_conv_layers:
images_network = batch_normalization(images_network)
images_network = dropout(images_network, keep_prob=keep_prob)
images_network = fully_connected(images_network, 1024, activation=NETWORK.activation)
if NETWORK.use_batchnorm_after_fully_connected_layers:
images_network = batch_normalization(images_network)
if NETWORK.use_landmarks or NETWORK.use_hog_and_landmarks:
if NETWORK.use_hog_sliding_window_and_landmarks:
landmarks_network = input_data(shape=[None, 2728], name='input2')
elif NETWORK.use_hog_and_landmarks:
landmarks_network = input_data(shape=[None, 208], name='input2')
else:
landmarks_network = input_data(shape=[None, 68, 2], name='input2')
landmarks_network = fully_connected(landmarks_network, 1024, activation=NETWORK.activation)
if NETWORK.use_batchnorm_after_fully_connected_layers:
landmarks_network = batch_normalization(landmarks_network)
landmarks_network = fully_connected(landmarks_network, 40, activation=NETWORK.activation)
if NETWORK.use_batchnorm_after_fully_connected_layers:
landmarks_network = batch_normalization(landmarks_network)
images_network = fully_connected(images_network, 40, activation=NETWORK.activation)
network = merge([images_network, landmarks_network], 'concat', axis=1)
else:
network = images_network
network = fully_connected(network, NETWORK.output_size, activation='softmax')
if optimizer == 'momentum':
optimizer = Momentum(learning_rate=learning_rate, momentum=optimizer_param,
lr_decay=learning_rate_decay, decay_step=decay_step)
elif optimizer == 'adam':
optimizer = Adam(learning_rate=learning_rate, beta1=optimizer_param, beta2=learning_rate_decay)
else:
print( "Unknown optimizer: {}".format(optimizer))
network = regression(network, optimizer=optimizer, loss=NETWORK.loss, learning_rate=learning_rate, name='output')
return network