-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcnn-latefusion.py
286 lines (206 loc) · 11.9 KB
/
cnn-latefusion.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
import input_data
import tensorflow as tf
import time
import numpy as np
import network_settings as ns
nowtime = str(time.time())
# -----------------------
print('start')
def cnn_model_1D(features, labels, mode):
x_image1 = tf.reshape(features["x1"], [-1, ns.IMAGE_SHAPE1[0], ns.IMAGE_SHAPE1[1]])
x_image2 = tf.reshape(features["x2"], [-1, ns.IMAGE_SHAPE2[0], ns.IMAGE_SHAPE2[1]])
conv1_1 = tf.layers.conv1d(inputs=x_image1, filters=ns.C1_LAYER_SIZE, kernel_size=conv_shape, padding='SAME',
activation=tf.nn.relu)
pool1_1 = tf.layers.max_pooling1d(inputs=conv1_1, pool_size=ns.MPOOL_SHAPE, strides=ns.MPOOL_SHAPE, padding='SAME')
# second conv layer
conv2_1 = tf.layers.conv1d(inputs=pool1_1, filters=ns.C2_LAYER_SIZE, kernel_size=conv_shape, padding='SAME',
activation=tf.nn.relu)
pool2_1 = tf.layers.max_pooling1d(inputs=conv2_1, pool_size=ns.MPOOL_SHAPE, strides=ns.MPOOL_SHAPE, padding='SAME')
# third conv layer
conv3_1 = tf.layers.conv1d(inputs=pool2_1, filters=ns.C3_LAYER_SIZE, kernel_size=conv_shape, padding='SAME',
activation=tf.nn.relu)
pool3_1 = tf.layers.max_pooling1d(inputs=conv3_1, pool_size=ns.MPOOL_SHAPE, strides=ns.MPOOL_SHAPE, padding='SAME')
# first conv layer
conv1_2 = tf.layers.conv1d(inputs=x_image2, filters=ns.C1_LAYER_SIZE, kernel_size=conv_shape, padding='SAME',
activation=tf.nn.relu)
pool1_2 = tf.layers.max_pooling1d(inputs=conv1_2, pool_size=ns.MPOOL_SHAPE, strides=ns.MPOOL_SHAPE, padding='SAME')
# second conv layer
conv2_2 = tf.layers.conv1d(inputs=pool1_2, filters=ns.C2_LAYER_SIZE, kernel_size=conv_shape, padding='SAME',
activation=tf.nn.relu)
pool2_2 = tf.layers.max_pooling1d(inputs=conv2_2, pool_size=ns.MPOOL_SHAPE, strides=ns.MPOOL_SHAPE, padding='SAME')
# third conv layer
conv3_2 = tf.layers.conv1d(inputs=pool2_2, filters=ns.C3_LAYER_SIZE, kernel_size=conv_shape, padding='SAME',
activation=tf.nn.relu)
pool3_2 = tf.layers.max_pooling1d(inputs=conv3_2, pool_size=ns.MPOOL_SHAPE, strides=ns.MPOOL_SHAPE, padding='SAME')
# densely connected layer
pool3_flat_1 = tf.reshape(pool3_1, [-1, ns.CONV_OUTPUT_SHAPE1 * ns.C3_LAYER_SIZE])
dense1_1 = tf.layers.dense(inputs=pool3_flat_1, units=ns.FC_LAYER_SIZE, activation=tf.nn.relu)
dropout1_1 = tf.layers.dropout(inputs=dense1_1, rate=ns.DROPOUT_RATE, training=mode == tf.estimator.ModeKeys.TRAIN)
# densely connected layer
dense2_1 = tf.layers.dense(inputs=dropout1_1, units=ns.FC_LAYER_SIZE, activation=tf.nn.relu)
dropout2_1 = tf.layers.dropout(inputs=dense2_1, rate=ns.DROPOUT_RATE, training=mode == tf.estimator.ModeKeys.TRAIN)
# densely connected layer
pool3_flat_2 = tf.reshape(pool3_2, [-1, ns.CONV_OUTPUT_SHAPE2 * ns.C3_LAYER_SIZE])
dense1_2 = tf.layers.dense(inputs=pool3_flat_2, units=ns.FC_LAYER_SIZE, activation=tf.nn.relu)
dropout1_2 = tf.layers.dropout(inputs=dense1_2, rate=ns.DROPOUT_RATE, training=mode == tf.estimator.ModeKeys.TRAIN)
# densely connected layer
dense2_2 = tf.layers.dense(inputs=dropout1_2, units=ns.FC_LAYER_SIZE, activation=tf.nn.relu)
dropout2_2 = tf.layers.dropout(inputs=dense2_2, rate=ns.DROPOUT_RATE, training=mode == tf.estimator.ModeKeys.TRAIN)
# combine
dropout2 = tf.concat([dropout2_1, dropout2_2], 1)
logits = tf.layers.dense(inputs=dropout2, units=ns.NUM_CLASSES)
predictions = {
# Generate predictions (for PREDICT and EVAL mode)
"classes": tf.argmax(input=logits, axis=1),
# Add `softmax_tensor` to the graph. It is used for PREDICT and by the
# `logging_hook`.
"probabilities": tf.nn.softmax(logits, name="softmax_tensor")
}
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)
# Calculate Loss (for both TRAIN and EVAL modes)
loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
# Configure the Training Op (for TRAIN mode)
if mode == tf.estimator.ModeKeys.TRAIN:
# optimizer = tf.train.GradientDescentOptimizer(learning_rate=LEARNING_RATE)
optimizer = tf.train.AdamOptimizer(learning_rate=ns.LEARNING_RATE)
train_op = optimizer.minimize(
loss=loss,
global_step=tf.train.get_global_step())
return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)
# Add evaluation metrics (for EVAL mode)
eval_metric_ops = {
"accuracy": tf.metrics.accuracy(
labels=labels, predictions=predictions["classes"])}
return tf.estimator.EstimatorSpec(
mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)
def cnn_model_2D(features, labels, mode):
x_image1 = tf.reshape(features["x1"], [-1, ns.IMAGE_SHAPE1[0], ns.IMAGE_SHAPE1[1], ns.IMAGE_SHAPE1[2]])
x_image2 = tf.reshape(features["x2"], [-1, ns.IMAGE_SHAPE2[0], ns.IMAGE_SHAPE2[1], ns.IMAGE_SHAPE2[2]])
conv1_1 = tf.layers.conv2d(inputs=x_image1, filters=ns.C1_LAYER_SIZE, kernel_size=conv_shape, padding='SAME',
activation=tf.nn.relu)
pool1_1 = tf.layers.max_pooling2d(inputs=conv1_1, pool_size=ns.MPOOL_SHAPE, strides=ns.MPOOL_SHAPE, padding='SAME')
# second conv layer
conv2_1 = tf.layers.conv2d(inputs=pool1_1, filters=ns.C2_LAYER_SIZE, kernel_size=conv_shape, padding='SAME',
activation=tf.nn.relu)
pool2_1 = tf.layers.max_pooling2d(inputs=conv2_1, pool_size=ns.MPOOL_SHAPE, strides=ns.MPOOL_SHAPE, padding='SAME')
# third conv layer
conv3_1 = tf.layers.conv2d(inputs=pool2_1, filters=ns.C3_LAYER_SIZE, kernel_size=conv_shape, padding='SAME',
activation=tf.nn.relu)
pool3_1 = tf.layers.max_pooling2d(inputs=conv3_1, pool_size=ns.MPOOL_SHAPE, strides=ns.MPOOL_SHAPE, padding='SAME')
# first conv layer
conv1_2 = tf.layers.conv2d(inputs=x_image2, filters=ns.C1_LAYER_SIZE, kernel_size=conv_shape, padding='SAME',
activation=tf.nn.relu)
pool1_2 = tf.layers.max_pooling2d(inputs=conv1_2, pool_size=ns.MPOOL_SHAPE, strides=ns.MPOOL_SHAPE, padding='SAME')
# second conv layer
conv2_2 = tf.layers.conv2d(inputs=pool1_2, filters=ns.C2_LAYER_SIZE, kernel_size=conv_shape, padding='SAME',
activation=tf.nn.relu)
pool2_2 = tf.layers.max_pooling2d(inputs=conv2_2, pool_size=ns.MPOOL_SHAPE, strides=ns.MPOOL_SHAPE, padding='SAME')
# third conv layer
conv3_2 = tf.layers.conv2d(inputs=pool2_2, filters=ns.C3_LAYER_SIZE, kernel_size=conv_shape, padding='SAME',
activation=tf.nn.relu)
pool3_2 = tf.layers.max_pooling2d(inputs=conv3_2, pool_size=ns.MPOOL_SHAPE, strides=ns.MPOOL_SHAPE, padding='SAME')
# densely connected layer
pool3_flat_1 = tf.reshape(pool3_1, [-1, ns.CONV_OUTPUT_SHAPE1 * ns.C3_LAYER_SIZE])
dense1_1 = tf.layers.dense(inputs=pool3_flat_1, units=ns.FC_LAYER_SIZE, activation=tf.nn.relu)
dropout1_1 = tf.layers.dropout(inputs=dense1_1, rate=ns.DROPOUT_RATE, training=mode == tf.estimator.ModeKeys.TRAIN)
# densely connected layer
dense2_1 = tf.layers.dense(inputs=dropout1_1, units=ns.FC_LAYER_SIZE, activation=tf.nn.relu)
dropout2_1 = tf.layers.dropout(inputs=dense2_1, rate=ns.DROPOUT_RATE, training=mode == tf.estimator.ModeKeys.TRAIN)
# densely connected layer
pool3_flat_2 = tf.reshape(pool3_2, [-1, ns.CONV_OUTPUT_SHAPE2 * ns.C3_LAYER_SIZE])
dense1_2 = tf.layers.dense(inputs=pool3_flat_2, units=ns.FC_LAYER_SIZE, activation=tf.nn.relu)
dropout1_2 = tf.layers.dropout(inputs=dense1_2, rate=ns.DROPOUT_RATE, training=mode == tf.estimator.ModeKeys.TRAIN)
# densely connected layer
dense2_2 = tf.layers.dense(inputs=dropout1_2, units=ns.FC_LAYER_SIZE, activation=tf.nn.relu)
dropout2_2 = tf.layers.dropout(inputs=dense2_2, rate=ns.DROPOUT_RATE, training=mode == tf.estimator.ModeKeys.TRAIN)
# combine
dropout2 = tf.concat([dropout2_1, dropout2_2], 1)
logits = tf.layers.dense(inputs=dropout2, units=ns.NUM_CLASSES)
predictions = {
# Generate predictions (for PREDICT and EVAL mode)
"classes": tf.argmax(input=logits, axis=1),
# Add `softmax_tensor` to the graph. It is used for PREDICT and by the
# `logging_hook`.
"probabilities": tf.nn.softmax(logits, name="softmax_tensor")
}
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)
# Calculate Loss (for both TRAIN and EVAL modes)
loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
# Configure the Training Op (for TRAIN mode)
if mode == tf.estimator.ModeKeys.TRAIN:
# optimizer = tf.train.GradientDescentOptimizer(learning_rate=LEARNING_RATE)
optimizer = tf.train.AdamOptimizer(learning_rate=ns.LEARNING_RATE)
train_op = optimizer.minimize(
loss=loss,
global_step=tf.train.get_global_step())
return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)
# Add evaluation metrics (for EVAL mode)
eval_metric_ops = {
"accuracy": tf.metrics.accuracy(
labels=labels, predictions=predictions["classes"])}
return tf.estimator.EstimatorSpec(
mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)
def main(argv):
if len(argv) < 5:
print("Error, Syntax: {0} [train/test] [dataset] [conv dim] [conv width]".format(argv[0]))
exit()
global conv_shape
conv_shape = int(argv[4])
dataset = argv[2]
conv_dim = argv[3]
test = argv[1]
ns.load_settings_late(dataset, conv_dim)
run_name = "latefusion-fc1024-lr{0}-adam-{1}conv-{2}".format(ns.LEARNING_RATE, conv_shape, dataset) # +"-"+nowtime
print(run_name)
# Create the Estimator
if conv_dim == "1d":
classifier = tf.estimator.Estimator(model_fn=cnn_model_1D, model_dir="models/" + run_name)
else:
classifier = tf.estimator.Estimator(model_fn=cnn_model_2D, model_dir="models/" + run_name)
if test == "train":
# train
# Set up logging for predictions
# Log the values in the "Softmax" tensor with label "probabilities"
tensors_to_log = {"probabilities": "softmax_tensor"}
logging_hook = tf.train.LoggingTensorHook(
tensors=tensors_to_log, every_n_iter=100)
# Train the model
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x1": train_data1, "x2": train_data2},
y=train_labels,
batch_size=ns.BATCH_SIZE,
num_epochs=None,
shuffle=True)
classifier.train(
input_fn=train_input_fn,
steps=ns.NUM_ITER,
hooks=[logging_hook])
# Evaluate the model and print results
eval_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x1": eval_data1, "x2": eval_data2},
y=eval_labels,
num_epochs=1,
shuffle=False)
eval_results = classifier.evaluate(input_fn=eval_input_fn)
print(run_name)
print(eval_results)
else:
# test
# Evaluate the model and print results
eval_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x1": eval_data1, "x2": eval_data2},
y=eval_labels,
num_epochs=1,
shuffle=False)
# eval_results = classifier.evaluate(input_fn=eval_input_fn)
labels = eval_labels
predictions = list(classifier.predict(input_fn=eval_input_fn))
predicted_classes = [p["classes"] for p in predictions]
from sklearn.metrics import confusion_matrix, classification_report
print(run_name)
print(confusion_matrix(labels, predicted_classes))
print(classification_report(labels, predicted_classes))
if __name__ == "__main__":
tf.app.run()