-
Notifications
You must be signed in to change notification settings - Fork 4
/
model_cnn.py
174 lines (138 loc) · 7.34 KB
/
model_cnn.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
164
165
166
167
168
169
170
171
172
173
174
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import read_data
import numpy as np
import tensorflow as tf
NUM_TRAIN_EXAMPLES = read_data.NUM_TRAIN_EXAMPLES
NUM_CLASSES = read_data.NUM_CLASSES
DROP_PROB = 0.5
REG_STRENGTH = 0.001
INITIAL_LEARNING_RATE = 1e-3
LR_DECAY_FACTOR = 0.5
EPOCHS_PER_LR_DECAY = 5
MOVING_AVERAGE_DECAY = 0.9999
BATCH_SIZE = 64
def _activation_summary(x):
tf.histogram_summary(x.op.name + '/activations', x)
tf.scalar_summary(x.op.name + '/sparsity', tf.nn.zero_fraction(x))
# Use tf.get_variable() instead of tf.Variable() to be able to reuse variables for evaluation run
# ^This was necessary when sharing variables between train and eval run.
# Not necessary now as eval run is based off saved checkpoints, which have moving average of the variables
def _variable_with_weight_decay(name, shape, stddev, wd):
var = tf.get_variable(name, shape, initializer=tf.truncated_normal_initializer(stddev=stddev))
if wd is not None:
weight_decay = tf.mul(tf.nn.l2_loss(var), wd, name='reg_loss')
tf.add_to_collection('losses', weight_decay)
return var
def inference(images):
# conv 1
with tf.variable_scope('conv1') as scope:
weights = _variable_with_weight_decay('weights', shape=[5, 5, 3, 32], stddev=1/np.sqrt(5*5*3), wd=0.00)
biases = tf.get_variable('biases', shape = [32], initializer=tf.constant_initializer(0.0))
conv = tf.nn.conv2d(images, weights, [1, 1, 1, 1], padding='SAME')
bias = tf.nn.bias_add(conv, biases)
conv1 = tf.nn.relu(bias, name=scope.name)
_activation_summary(conv1)
# conv 2
with tf.variable_scope('conv2') as scope:
weights = _variable_with_weight_decay('weights', shape=[5, 5, 32, 64], stddev=1/np.sqrt(5*5*32), wd=0.00)
biases = tf.get_variable('biases', shape = [64], initializer=tf.constant_initializer(0.0))
conv = tf.nn.conv2d(conv1, weights, [1, 1, 1, 1], padding='SAME')
bias = tf.nn.bias_add(conv, biases)
conv2 = tf.nn.relu(bias, name=scope.name)
_activation_summary(conv2)
# pool 1
with tf.variable_scope('pool1') as scope:
pool1 = tf.nn.max_pool(conv2, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
# conv 3
with tf.variable_scope('conv3') as scope:
weights = _variable_with_weight_decay('weights', shape=[3, 3, 64, 64], stddev=1/np.sqrt(3*3*64), wd=0.00)
biases = tf.get_variable('biases', shape = [64], initializer=tf.constant_initializer(0.0))
conv = tf.nn.conv2d(pool1, weights, [1, 1, 1, 1], padding='SAME')
bias = tf.nn.bias_add(conv, biases)
conv3 = tf.nn.relu(bias, name=scope.name)
_activation_summary(conv3)
# conv 4
with tf.variable_scope('conv4') as scope:
weights = _variable_with_weight_decay('weights', shape=[3, 3, 64, 64], stddev=1/np.sqrt(3*3*64), wd=0.00)
biases = tf.get_variable('biases', shape = [64], initializer=tf.constant_initializer(0.0))
conv = tf.nn.conv2d(conv3, weights, [1, 1, 1, 1], padding='SAME')
bias = tf.nn.bias_add(conv, biases)
conv4 = tf.nn.relu(bias, name=scope.name)
_activation_summary(conv4)
# pool 2
with tf.variable_scope('pool2') as scope:
pool2 = tf.nn.max_pool(conv4, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
# conv 5
with tf.variable_scope('conv5') as scope:
weights = _variable_with_weight_decay('weights', shape=[3, 3, 64, 64], stddev=1/np.sqrt(3*3*64), wd=0.00)
biases = tf.get_variable('biases', shape = [64], initializer=tf.constant_initializer(0.0))
conv = tf.nn.conv2d(pool2, weights, [1, 1, 1, 1], padding='SAME')
bias = tf.nn.bias_add(conv, biases)
conv5 = tf.nn.relu(bias, name=scope.name)
_activation_summary(conv5)
# conv 6
with tf.variable_scope('conv6') as scope:
weights = _variable_with_weight_decay('weights', shape=[3, 3, 64, 64], stddev=1/np.sqrt(3*3*64), wd=0.00)
biases = tf.get_variable('biases', shape = [64], initializer=tf.constant_initializer(0.0))
conv = tf.nn.conv2d(conv5, weights, [1, 1, 1, 1], padding='SAME')
bias = tf.nn.bias_add(conv, biases)
conv6 = tf.nn.relu(bias, name=scope.name)
_activation_summary(conv6)
# pool 3
with tf.variable_scope('pool3') as scope:
pool3 = tf.nn.max_pool(conv6, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
# fully connected 1
with tf.variable_scope('fc1') as scope:
batch_size = images.get_shape()[0].value
pool3_flat = tf.reshape(pool3, [batch_size, -1])
dim = pool3_flat.get_shape()[1].value
weights = _variable_with_weight_decay('weights', shape=[dim, 384], stddev=1/np.sqrt(dim), wd=REG_STRENGTH)
biases = tf.get_variable('biases', shape = [384], initializer=tf.constant_initializer(0.0))
fc1 = tf.nn.relu(tf.matmul(pool3_flat, weights) + biases, name=scope.name)
_activation_summary(fc1)
# fully connected 2
with tf.variable_scope('fc2') as scope:
weights = _variable_with_weight_decay('weights', shape=[384, 192], stddev=1/np.sqrt(384), wd=REG_STRENGTH)
biases = tf.get_variable('biases', shape = [192], initializer=tf.constant_initializer(0.0))
fc2 = tf.nn.relu(tf.matmul(fc1, weights) + biases, name=scope.name)
_activation_summary(fc2)
# dropout
fc2_drop = tf.nn.dropout(fc2, DROP_PROB)
# Softmax
with tf.variable_scope('softmax_linear') as scope:
weights = _variable_with_weight_decay('weights', shape=[192, NUM_CLASSES], stddev=1/np.sqrt(192), wd=0.000)
biases = tf.get_variable('biases', shape = [NUM_CLASSES], initializer=tf.constant_initializer(0.0))
logits = tf.add(tf.matmul(fc2_drop, weights), biases, name=scope.name)
_activation_summary(logits)
return logits
def loss(logits, labels):
labels = tf.cast(labels, tf.int64)
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels, name='xentropy')
data_loss = tf.reduce_mean(cross_entropy, name='xentropy_mean')
tf.add_to_collection('losses', data_loss)
total_loss = tf.add_n(tf.get_collection('losses'), name='total_loss')
return total_loss
def _loss_summaries(total_loss):
losses = tf.get_collection('losses')
for l in losses + [total_loss]:
tf.scalar_summary(l.op.name, l)
def training(total_loss):
global_step = tf.Variable(0, name='global_step', trainable=False)
decay_steps = int(EPOCHS_PER_LR_DECAY * NUM_TRAIN_EXAMPLES / BATCH_SIZE)
learning_rate = tf.train.exponential_decay(INITIAL_LEARNING_RATE, global_step, decay_steps, LR_DECAY_FACTOR, staircase=True)
tf.scalar_summary('learning_rate', learning_rate)
_loss_summaries(total_loss)
optimizer = tf.train.AdamOptimizer(learning_rate)
opt_op = optimizer.minimize(total_loss, global_step=global_step)
for var in tf.trainable_variables():
tf.histogram_summary(var.op.name, var)
mov_average_object = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
moving_average_op = mov_average_object.apply(tf.trainable_variables())
with tf.control_dependencies([opt_op]):
train_op = tf.group(moving_average_op)
return train_op
def evaluation(logits, true_labels):
correct_pred = tf.nn.in_top_k(logits, true_labels, 1)
return tf.reduce_mean(tf.cast(correct_pred, tf.float32))*100