-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeep_cnn_model.py
335 lines (302 loc) · 12.9 KB
/
deep_cnn_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
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
import sys
import numpy as np
import tensorflow as tf
from configs import DeepConfig
from utils.prepare_data import load_data, prepare_train_data
class DeepModel(object):
def __init__(self, config, session, graph):
print("Init Model object")
self.graph = graph
self.sess = session
self.log_path = '/tmp/tensorboard/'
self.config = config
self.learning_rate = self.config.lr
self.batch_size = self.config.batch_size
self.image_size = self.config.image_size
self.epochs = self.config.epochs
sys.stdout.write('<log>Building Graph')
# build computation graph
self.build_graph()
sys.stdout.write('</log>\n')
def init_model(self, images, training):
# CNN Model with tf.layers
initializer = tf.contrib.layers.xavier_initializer()
regularizer = tf.contrib.layers.l2_regularizer(self.config.l2)
# Input Layer
input_layer = tf.reshape(
images, [-1,
self.config.image_size,
self.config.image_size,
self.config.channels])
# Convolutional Layer #1
conv1 = tf.layers.conv2d(
inputs=input_layer,
filters=32,
kernel_size=[3, 3],
padding="same",
kernel_initializer=initializer,
kernel_regularizer=regularizer,
use_bias=True,
bias_initializer=initializer,
bias_regularizer=regularizer,
activation=tf.nn.relu)
conv1 = tf.layers.conv2d(
inputs=conv1,
filters=32,
kernel_size=[3, 3],
padding="same",
kernel_initializer=initializer,
kernel_regularizer=regularizer,
use_bias=True,
bias_initializer=initializer,
bias_regularizer=regularizer,
activation=tf.nn.relu)
pool1 = tf.layers.max_pooling2d(
inputs=conv1, pool_size=[2, 2], strides=(2, 2))
# Convolutional Layer #2
conv2 = tf.layers.conv2d(
inputs=pool1,
filters=64,
kernel_size=[3, 3],
padding="same",
kernel_initializer=initializer,
kernel_regularizer=regularizer,
use_bias=True,
bias_initializer=initializer,
bias_regularizer=regularizer,
activation=tf.nn.relu)
conv2 = tf.layers.conv2d(
inputs=conv2,
filters=64,
kernel_size=[3, 3],
padding="same",
kernel_initializer=initializer,
kernel_regularizer=regularizer,
use_bias=True,
bias_initializer=initializer,
bias_regularizer=regularizer,
activation=tf.nn.relu)
pool2 = tf.layers.max_pooling2d(
inputs=conv2, pool_size=[2, 2], strides=(2, 2))
# Convolutional Layer #3
conv3 = tf.layers.conv2d(
inputs=pool2,
filters=64,
kernel_size=[3, 3],
padding="same",
kernel_initializer=initializer,
kernel_regularizer=regularizer,
use_bias=True,
bias_initializer=initializer,
bias_regularizer=regularizer,
activation=tf.nn.relu)
conv3 = tf.layers.conv2d(
inputs=conv3,
filters=64,
kernel_size=[3, 3],
padding="same",
kernel_initializer=initializer,
kernel_regularizer=regularizer,
use_bias=True,
bias_initializer=initializer,
bias_regularizer=regularizer,
activation=tf.nn.relu)
pool3 = tf.layers.max_pooling2d(
inputs=conv3, pool_size=[2, 2], strides=(2, 2))
# return pool3
# Dense Layer
# Flatten for 64*64 : 8,8,128
flatten = tf.reshape(pool3, [-1, 8 * 8 * 64])
# Flatten for 150*150 : 18,18,128
# flatten = tf.reshape(pool3, [-1, 18 * 18 * 128])
# Flatten for 224*224 : 28,28,128
# flatten = tf.reshape(pool4, [-1, 28 * 28 * 128])
# Dense Layer
fc1 = tf.layers.dense(
inputs=flatten,
units=256,
activation=tf.nn.relu,
kernel_initializer=initializer,
kernel_regularizer=regularizer,
use_bias=True,
bias_initializer=initializer,
bias_regularizer=regularizer)
fc1 = tf.layers.dropout(
inputs=fc1,
rate=self.config.dropout,
training=training)
fc2 = tf.layers.dense(
inputs=fc1,
units=256,
activation=tf.nn.relu,
kernel_initializer=initializer,
kernel_regularizer=regularizer,
use_bias=True,
bias_initializer=initializer,
bias_regularizer=regularizer)
fc2 = tf.layers.dropout(
inputs=fc2,
rate=self.config.dropout,
training=training)
# fc3 = tf.layers.dense(
# inputs=fc2,
# units=64,
# activation=tf.nn.relu,
# kernel_initializer=initializer,
# kernel_regularizer=regularizer,
# use_bias=True,
# bias_initializer=initializer,
# bias_regularizer=regularizer)
# fc3 = tf.layers.dropout(
# inputs=fc3,
# rate=self.config.dropout,
# training=training)
# One output: Confidence score of being a dog
logits = tf.layers.dense(inputs=fc2, units=1, activation=tf.nn.sigmoid)
return logits
# build the graph
def build_graph(self):
with self.graph.as_default():
with self.sess:
with tf.device('/gpu:0'):
# Input images
self.images = tf.placeholder(shape=[None,
self.config.image_size,
self.config.image_size,
self.config.channels],
dtype=tf.float32,
name='Images')
# self.val_images = tf.placeholder(shape=[None,
# self.config.image_size,
# self.config.image_size,
# self.config.channels],
# dtype=tf.float32,
# name='Images')
# Input labels that represent the real outputs
self.labels = tf.placeholder(shape=[None, 1],
dtype=tf.float32,
name='Labels')
# self.val_labels = tf.placeholder(shape=[None, 1],
# dtype=tf.float32,
# name='Labels')
# Is Training?
self.training = tf.placeholder(dtype=tf.bool)
self.model = self.init_model(self.images, self.training)
# self.preds = tf.nn.sigmoid(self.model)
thresholds = tf.fill(
[self.config.batch_size], self.config.threshold)
self.predictions = tf.greater_equal(
self.model, thresholds)
correct_prediction = tf.equal(
self.predictions, tf.cast(self.labels, tf.bool))
self.accuracy = tf.reduce_mean(
tf.cast(correct_prediction, tf.float32))
self.loss = tf.losses.log_loss(
labels=self.labels, predictions=self.model)
# Validation
# self.val_model = self.init_model(
# self.val_images, self.training)
# self.val_predictions = tf.greater_equal(
# self.val_model, thresholds)
# val_correct_prediction = tf.equal(
# self.val_predictions, tf.cast(self.val_labels, tf.bool))
# self.val_accuracy = tf.reduce_mean(
# tf.cast(correct_prediction, tf.float32))
# self.val_loss = tf.losses.log_loss(
# labels=self.labels, predictions=self.model)
# self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(
# labels=self.labels, logits=self.model))
# self.accuracy = tf.constant(1)
# self.loss = tf.constant(1)
self.optimizer = tf.train.RMSPropOptimizer(
learning_rate=self.learning_rate).minimize(self.loss)
# TensorBoard Summary
tf.summary.scalar("log_loss", self.loss)
tf.summary.scalar("accuracy", self.accuracy)
# tf.summary.scalar("val_loss", self.val_loss)
# tf.summary.scalar("val_accuracy", self.val_accuracy)
self.summary = tf.summary.merge_all()
self.init = tf.global_variables_initializer()
self.writer = tf.summary.FileWriter(
self.log_path, graph=self.sess.graph_def)
with tf.device('/cpu:0'):
self.saver = tf.train.Saver(tf.trainable_variables())
def generate_feed_dict(self, batch_images, batch_labels, training=False):
return {
self.images: batch_images,
self.labels: batch_labels,
self.training: training
}
def predict(self, batch_images, batch_labels):
feed_dict = self.generate_feed_dict(batch_images, batch_labels, False)
pred, loss, acc = self.sess.run(
[self.model, self.loss, self.accuracy], feed_dict=feed_dict)
return pred, loss, acc
def train_eval_batch(self, batch_images, batch_labels, training=True):
feed_dict = self.generate_feed_dict(
batch_images, batch_labels, training)
loss, acc, _ = self.sess.run(
[self.loss, self.accuracy, self.optimizer], feed_dict=feed_dict)
return loss, acc
def eval_batch(self, batch_images, batch_labels, training=False):
feed_dict = self.generate_feed_dict(
batch_images, batch_labels, training)
summary, loss, acc = self.sess.run(
[self.summary, self.loss, self.accuracy], feed_dict=feed_dict)
return summary, loss, acc
def test_batch(self, batch_images, batch_labels, training=False):
feed_dict = self.generate_feed_dict(
batch_images, batch_labels, training)
pred = self.sess.run(
[self.model], feed_dict=feed_dict)
return pred
def save(self, step):
self.saver.save(self.sess, self.config.ckpt_path +
'.ckpt', global_step=step)
def restore(self, path=None):
# get checkpoint state
if path:
ckpt = tf.train.get_checkpoint_state(path)
print(ckpt)
print(ckpt)
print(ckpt)
print(ckpt)
else:
ckpt = tf.train.get_checkpoint_state('./ckpt')
# restore session
if ckpt and ckpt.model_checkpoint_path:
self.sess.run(self.init)
print("\nGlobal Variables Initialized")
self.saver = tf.train.import_meta_graph(
ckpt.model_checkpoint_path + '.meta')
print("\nRestoring model")
self.saver.restore(self.sess, ckpt.model_checkpoint_path)
else:
self.sess.run(self.init)
print("\nGlobal Variables Initialized")
if __name__ == '__main__':
graph = tf.Graph()
sess_config = tf.ConfigProto(
allow_soft_placement=True, log_device_placement=True)
sess_config.gpu_options.allow_growth = True
sess = tf.Session(config=sess_config)
config = DeepConfig()
model = DeepModel(config, sess, graph)
# model.sess.run(model.init)
# print("\nGlobal Variables Initialized")
model.restore()
train_dogs, train_cats = load_data(config.image_size)
train_batches = prepare_train_data(
train_dogs, train_cats, config.batch_size)
# train_batch = next_batch(train_batches)
batch_images, batch_labels = map(list, zip(*train_batches[0]))
batch_images = np.array(batch_images)
batch_labels = np.array(batch_labels).reshape(-1, 1)
pred, loss, acc = model.predict(batch_images, batch_labels)
# zeros = np.zeros(
# (8, 150, 150, 3), dtype=np.int)
# pred, loss, acc = model.predict(
# zeros, np.array([1, 1, 1, 1, 1, 1, 1, 1]).reshape(-1, 1))
print(pred)
print(loss)
print(acc)