forked from thibo73800/capsnet-traffic-sign-classifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
337 lines (287 loc) · 13.1 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
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
336
337
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import numpy as np
from model_base import ModelBase
from caps_net import conv_caps_layer, fully_connected_caps_layer
import tensorflow as tf
class ModelTrafficSign(ModelBase):
"""
ModelTrafficSign.
This class is used to create the conv graph using:
Dynamic Routing Between Capsules
"""
# Numbers of label to predict
NB_LABELS = 43
def __init__(self, model_name, output_folder):
"""
**input:
*model_name: (Integer) Name of this model
*output_folder: Output folder to saved data (tensorboard, checkpoints)
"""
ModelBase.__init__(self, model_name, output_folder=output_folder)
def _build_inputs(self):
"""
Build tensorflow inputs
(Placeholder)
**return: **
*tf_images: Images Placeholder
*tf_labels: Labels Placeholder
"""
# Images 32*32*3
tf_images = tf.placeholder(tf.float32, [None, 32, 32, 3], name='images')
# Labels: [0, 1, 6, 20, ...]
tf_labels = tf.placeholder(tf.int64, [None], name='labels')
return tf_images, tf_labels
def _build_main_network(self, images, conv_2_dropout):
"""
This method is used to create the two convolutions and the CapsNet on the top
**input:
*images: Image PLaceholder
*conv_2_dropout: Dropout value placeholder
**return: **
*Caps1: Output of first Capsule layer
*Caps2: Output of second Capsule layer
"""
# First BLock:
# Layer 1: Convolution.
shape = (self.h.conv_1_size, self.h.conv_1_size, 3, self.h.conv_1_nb)
conv1 = self._create_conv(self.tf_images, shape, relu=True, max_pooling=False, padding='VALID')
# Layer 2: Convolution.
#shape = (self.h.conv_2_size, self.h.conv_2_size, self.h.conv_1_nb, self.h.conv_2_nb)
#conv2 = self._create_conv(conv1, shape, relu=True, max_pooling=False, padding='VALID')
conv1 = tf.nn.dropout(conv1, keep_prob=conv_2_dropout)
# Create the first capsules layer
caps1 = conv_caps_layer(
input_layer=conv1,
capsules_size=self.h.caps_1_vec_len,
nb_filters=self.h.caps_1_nb_filter,
kernel=self.h.caps_1_size)
# Create the second capsules layer used to predict the output
caps2 = fully_connected_caps_layer(
input_layer=caps1,
capsules_size=self.h.caps_2_vec_len,
nb_capsules=self.NB_LABELS,
iterations=self.h.routing_steps)
return caps1, caps2
def _build_decoder(self, caps2, one_hot_labels, batch_size):
"""
Build the decoder part from the last capsule layer
**input:
*Caps2: Output of second Capsule layer
*one_hot_labels
*batch_size
"""
labels = tf.reshape(one_hot_labels, (-1, self.NB_LABELS, 1))
# squeeze(caps2): [?, len_v_j, capsules_nb]
# labels: [?, NB_LABELS, 1] with capsules_nb == NB_LABELS
mask = tf.matmul(tf.squeeze(caps2), labels, transpose_a=True)
# Select the good capsule vector
capsule_vector = tf.reshape(mask, shape=(batch_size, self.h.caps_2_vec_len))
# capsule_vector: [?, len_v_j]
# Reconstruct image
fc1 = tf.contrib.layers.fully_connected(capsule_vector, num_outputs=400)
fc1 = tf.reshape(fc1, shape=(batch_size, 5, 5, 16))
upsample1 = tf.image.resize_nearest_neighbor(fc1, (8, 8))
conv1 = tf.layers.conv2d(upsample1, 4, (3,3), padding='same', activation=tf.nn.relu)
upsample2 = tf.image.resize_nearest_neighbor(conv1, (16, 16))
conv2 = tf.layers.conv2d(upsample2, 8, (3,3), padding='same', activation=tf.nn.relu)
upsample3 = tf.image.resize_nearest_neighbor(conv2, (32, 32))
conv6 = tf.layers.conv2d(upsample3, 16, (3,3), padding='same', activation=tf.nn.relu)
# 3 channel for RGG
logits = tf.layers.conv2d(conv6, 3, (3,3), padding='same', activation=None)
decoded = tf.nn.sigmoid(logits, name='decoded')
tf.summary.image('reconstruction_img', decoded)
return decoded
def init(self):
"""
Init the graph
"""
# Get graph inputs
self.tf_images, self.tf_labels = self._build_inputs()
# Dropout inputs
self.tf_conv_2_dropout = tf.placeholder(tf.float32, shape=(), name='conv_2_dropout')
# Dynamic batch size
batch_size = tf.shape(self.tf_images)[0]
# Translate labels to one hot array
one_hot_labels = tf.one_hot(self.tf_labels, depth=self.NB_LABELS)
# Create the first convolution and the CapsNet
self.tf_caps1, self.tf_caps2 = self._build_main_network(self.tf_images, self.tf_conv_2_dropout)
# Build the images reconstruction
self.tf_decoded = self._build_decoder(self.tf_caps2, one_hot_labels, batch_size)
# Build the loss
_loss = self._build_loss(
self.tf_caps2, one_hot_labels, self.tf_labels, self.tf_decoded, self.tf_images)
(self.tf_loss_squared_rec, self.tf_margin_loss_sum, self.tf_predicted_class,
self.tf_correct_prediction, self.tf_accuracy, self.tf_loss, self.tf_margin_loss,
self.tf_reconstruction_loss) = _loss
# Build optimizer
optimizer = tf.train.AdamOptimizer(learning_rate=self.h.learning_rate)
self.tf_optimizer = optimizer.minimize(self.tf_loss, global_step=tf.Variable(0, trainable=False))
# Log value into tensorboard
tf.summary.scalar('margin_loss', self.tf_margin_loss)
tf.summary.scalar('accuracy', self.tf_accuracy)
tf.summary.scalar('total_loss', self.tf_loss)
tf.summary.scalar('reconstruction_loss', self.tf_reconstruction_loss)
self.tf_test = tf.random_uniform([2], minval=0, maxval=None, dtype=tf.float32, seed=None, name="tf_test")
self.init_session()
def _build_loss(self, caps2, one_hot_labels, labels, decoded, images):
"""
Build the loss of the graph
"""
# Get the length of each capsule
capsules_length = tf.sqrt(tf.reduce_sum(tf.square(caps2), axis=2, keep_dims=True))
max_l = tf.square(tf.maximum(0., 0.9 - capsules_length))
max_l = tf.reshape(max_l, shape=(-1, self.NB_LABELS))
max_r = tf.square(tf.maximum(0., capsules_length - 0.1))
max_r = tf.reshape(max_r, shape=(-1, self.NB_LABELS))
t_c = one_hot_labels
m_loss = t_c * max_l + 0.5 * (1 - t_c) * max_r
margin_loss_sum = tf.reduce_sum(m_loss, axis=1)
margin_loss = tf.reduce_mean(margin_loss_sum)
# Reconstruction loss
loss_squared_rec = tf.square(decoded - images)
reconstruction_loss = tf.reduce_mean(loss_squared_rec)
# 3. Total loss
loss = margin_loss + (0.0005 * reconstruction_loss)
# Accuracy
predicted_class = tf.argmax(capsules_length, axis=1)
predicted_class = tf.reshape(predicted_class, [tf.shape(capsules_length)[0]])
correct_prediction = tf.equal(predicted_class, labels)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
return (loss_squared_rec, margin_loss_sum, predicted_class, correct_prediction, accuracy,
loss, margin_loss, reconstruction_loss)
def optimize(self, images, labels, tb_save=True):
"""
Train the model
**input: **
*images: Image to train the model on
*labels: True classes
*tb_save: (Boolean) Log this optimization in tensorboard
**return: **
Loss: The loss of the model on this batch
Acc: Accuracy of the model on this batch
"""
tensors = [self.tf_optimizer, self.tf_margin_loss, self.tf_accuracy, self.tf_tensorboard]
_, loss, acc, summary = self.sess.run(tensors,
feed_dict={
self.tf_images: images,
self.tf_labels: labels,
self.tf_conv_2_dropout: self.h.conv_2_dropout
})
if tb_save:
# Write data to tensorboard
self.train_writer.add_summary(summary, self.train_writer_it)
self.train_writer_it += 1
return loss, acc
def evaluate(self, images, labels, tb_train_save=False, tb_test_save=False):
"""
Evaluate dataset
**input: **
*images: Image to train the model on
*labels: True classes
*tb_train_save: (Boolean) Log this optimization in tensorboard under the train part
*tb_test_save: (Boolean) Log this optimization in tensorboard under the test part
**return: **
Loss: The loss of the model on this batch
Acc: Accuracy of the model on this batch
"""
tensors = [self.tf_margin_loss, self.tf_accuracy, self.tf_tensorboard]
loss, acc, summary = self.sess.run(tensors,
feed_dict={
self.tf_images: images,
self.tf_labels: labels,
self.tf_conv_2_dropout: 1.
})
if tb_test_save:
# Write data to tensorboard
self.test_writer.add_summary(summary, self.test_writer_it)
self.test_writer_it += 1
if tb_train_save:
# Write data to tensorboard
self.train_writer.add_summary(summary, self.train_writer_it)
self.train_writer_it += 1
return loss, acc
def predict(self, images):
"""
Method used to predict a class
Return a softmax
**input: **
*images: Image to train the model on
**return:
*softmax: Softmax between all capsules
"""
tensors = [self.tf_caps2]
caps2 = self.sess.run(tensors,
feed_dict={
self.tf_images: images,
self.tf_conv_2_dropout: 1.
})[0]
# tf.sqrt(tf.reduce_sum(tf.square(caps2), axis=2, keep_dims=True))
caps2 = np.sqrt(np.sum(np.square(caps2), axis=2, keepdims=True))
caps2 = np.reshape(caps2, (len(images), self.NB_LABELS))
# softmax
softmax = np.exp(caps2) / np.sum(np.exp(caps2), axis=1, keepdims=True)
return softmax
def reconstruction(self, images, labels):
"""
Method used to get the reconstructions given a batch
Return the result as a softmax
**input: **
*images: Image to train the model on
*labels: True classes
"""
tensors = [self.tf_decoded]
decoded = self.sess.run(tensors,
feed_dict={
self.tf_images: images,
self.tf_labels: labels,
self.tf_conv_2_dropout: 1.
})[0]
return decoded
def evaluate_dataset(self, images, labels, batch_size=10):
"""
Evaluate a full dataset
This method is used to fully evaluate the dataset batch per batch. Useful when
the dataset can't be fit inside to the GPU.
*input: **
*images: Image to train the model on
*labels: True classes
*return: **
*loss: Loss overall your dataset
*accuracy: Accuracy overall your dataset
*predicted_class: Predicted class
"""
tensors = [self.tf_loss_squared_rec, self.tf_margin_loss_sum, self.tf_correct_prediction,
self.tf_predicted_class]
loss_squared_rec_list = None
margin_loss_sum_list = None
correct_prediction_list = None
predicted_class = None
b = 0
for batch in self.get_batches([images, labels], batch_size, shuffle=False):
images_batch, labels_batch = batch
loss_squared_rec, margin_loss_sum, correct_prediction, classes = self.sess.run(tensors,
feed_dict={
self.tf_images: images_batch,
self.tf_labels: labels_batch,
self.tf_conv_2_dropout: 1.
})
if loss_squared_rec_list is not None:
predicted_class = np.concatenate((predicted_class, classes))
loss_squared_rec_list = np.concatenate((loss_squared_rec_list, loss_squared_rec))
margin_loss_sum_list = np.concatenate((margin_loss_sum_list, margin_loss_sum))
correct_prediction_list = np.concatenate((correct_prediction_list, correct_prediction))
else:
predicted_class = classes
loss_squared_rec_list = loss_squared_rec
margin_loss_sum_list = margin_loss_sum
correct_prediction_list = correct_prediction
b += batch_size
margin_loss = np.mean(margin_loss_sum_list)
reconstruction_loss = np.mean(loss_squared_rec_list)
accuracy = np.mean(correct_prediction_list)
loss = margin_loss
return loss, accuracy, predicted_class
if __name__ == '__main__':
model_traffic_sign = ModelTrafficSign("test", output_folder=None)
model_traffic_sign.init()