-
Notifications
You must be signed in to change notification settings - Fork 0
/
tfcnn.py
305 lines (269 loc) · 12.4 KB
/
tfcnn.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
import tensorflow as tf
import numpy as np
from extract_data_from_trees import extract_imagedata
from sklearn.metrics import roc_auc_score
from sklearn.metrics import roc_curve
from sklearn.metrics import auc
import matplotlib.pyplot as plt
import sys
from matplotlib.backends.backend_pdf import PdfPages
#Iterate through the dataset
def data_iterator(orig_X, orig_htsoft, orig_y=None, batch_size=32, label_size=2, shuffle=False):
# Optionally shuffle the data before training
if shuffle:
indices = np.random.permutation(len(orig_X))
data_X = orig_X[indices]
data_htsoft = orig_htsoft[indices]
data_y = orig_y[indices] if np.any(orig_y) else None
else:
data_X = orig_X
data_htsoft = orig_htsoft
data_y = orig_y
###
total_processed_examples = 0
total_steps = int(np.ceil(len(data_X) / float(batch_size)))
for step in xrange(total_steps):
# Create the batch by selecting up to batch_size elements
batch_start = step * batch_size
x = data_X[batch_start:batch_start + batch_size]
ht = data_htsoft[:, batch_start:batch_start + batch_size]
# Convert our target from the class index to a one hot vector
y = None
if np.any(data_y):
y = data_y[batch_start:batch_start + batch_size] #None
# y_indices = data_y[batch_start:batch_start + batch_size]
# y = np.zeros((len(x), label_size), dtype=np.int32)
# y[np.arange(len(y_indices)), y_indices] = 1
###
yield x, y, ht
total_processed_examples += len(x)
# Sanity check to make sure we iterated over all the dataset as intended
assert total_processed_examples == len(data_X), 'Expected {} and processed {}'.format(len(data_X), total_processed_examples)
class Config(object):
"""Holds model hyperparams and data information.
The config class is used to store various hyperparameters and dataset
information parameters. Model objects are passed a Config() object at
instantiation.
"""
DIM_ETA = 52
DIM_PHI = 64
num_channels = 1
num_classes = 2
dropout = 0.9
lr = 1e-4
final_size = 54
batch_size = 128
frac_train = 0.7
max_epoch = 20
class_ratio = 0.2
early_stopping = 2
class HEPModel(object):
def load_data(self):
"""Loads data from disk and stores it in memory.
Feel free to add instance variables to Model object that store loaded data.
"""
data_samples, labels, htsoft = extract_imagedata(normalization=0)
aug_data = data_samples.reshape((data_samples.shape[0], data_samples.shape[1], data_samples.shape[2], 1))
total_batches = np.floor(data_samples.shape[0]/self.config.batch_size)
num_train = np.floor(self.config.frac_train*total_batches) * self.config.batch_size
num_train = int(num_train)
self.X_train = aug_data[:num_train, :, :, :]
self.Y_train = labels[:num_train, :]
self.htsoft_train = htsoft[:, :num_train]
self.X_test = aug_data[num_train:int(total_batches*self.config.batch_size), :, :, :]
self.htsoft_test = htsoft[:, num_train:int(total_batches*self.config.batch_size)]
self.Y_test = labels[num_train:int(total_batches*self.config.batch_size) , :]
def add_placeholders(self):
"""Adds placeholder variables to tensorflow computational graph.
Tensorflow uses placeholder variables to represent locations in a
computational graph where data is inserted. These placeholders are used as
inputs by the rest of the model building code and will be fed data during
training.
See for more information:
https://www.tensorflow.org/versions/r0.7/api_docs/python/io_ops.html#placeholders
"""
self.input_placeholder = tf.placeholder(tf.float32, shape=(None, self.config.DIM_PHI, self.config.DIM_ETA, self.config.num_channels))
self.labels_placeholder = tf.placeholder(tf.float32, shape=(None, self.config.num_classes))
self.htsoft_placeholder = tf.placeholder(tf.float32, shape=(1, None))
self.dropout_placeholder = tf.placeholder(tf.float32)
def create_feed_dict(self, input_batch, dropout, htsoft, label_batch=None):
"""Creates the feed_dict for training the given step.
A feed_dict takes the form of:
feed_dict = {
<placeholder>: <tensor of values to be passed for placeholder>,
....
}
If label_batch is None, then no labels are added to feed_dict.
Hint: The keys for the feed_dict should be a subset of the placeholder
tensors created in add_placeholders.
Args:
input_batch: A batch of input data.
label_batch: A batch of label data.
Returns:
feed_dict: The feed dictionary mapping from placeholders to values.
"""
feed_dict = {
self.input_placeholder : input_batch,
self.dropout_placeholder : dropout,
self.htsoft_placeholder : htsoft
}
if label_batch is not None:
feed_dict[self.labels_placeholder] = label_batch
return feed_dict
def add_model(self, input_data):
with tf.variable_scope("FirstConv") as CLayer1:
w_conv1 = tf.get_variable("w_conv1", (11, 11, 1, 32), initializer=tf.truncated_normal_initializer(stddev=0.1))
b_conv1 = tf.get_variable("b_conv1", (32), initializer=tf.constant_initializer(0.1))
conv1 = tf.nn.conv2d(input_data, w_conv1, strides=[1, 1, 1, 1], padding='VALID')
hconv1 = tf.nn.relu(conv1 + b_conv1)
h_pool1 = tf.nn.max_pool(hconv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
with tf.variable_scope("SecondConv") as CLayer2:
w_conv2 = tf.get_variable("w_conv2", (11 , 11, 32, 64), initializer=tf.truncated_normal_initializer(stddev=0.1))
b_conv2 = tf.get_variable("b_conv2", (64), initializer=tf.constant_initializer(0.1))
conv2 = tf.nn.conv2d(h_pool1, w_conv2, strides=[1, 1, 1, 1], padding='VALID')
hconv2 = tf.nn.relu(conv2 + b_conv2)
h_pool2 = tf.nn.max_pool(hconv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
with tf.variable_scope("FullyConnected") as FC:
flattend_input = tf.reshape(input_data, [self.config.batch_size, -1])
w_input = tf.get_variable("w_input", (self.config.DIM_ETA*self.config.DIM_PHI, 32), initializer=tf.truncated_normal_initializer(stddev=0.1))
wfc1 = tf.get_variable("wfc1", (self.config.final_size*64, 32), initializer=tf.truncated_normal_initializer(stddev=0.1))
#bfc1 = tf.get_variable("bfc1", (32), initializer=tf.constant_initializer(0.1))
h_pool2_flat = tf.reshape(h_pool2, [-1, self.config.final_size*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, wfc1) + tf.matmul(flattend_input, w_input))#+ bfc1)
h_fc1_drop = tf.nn.dropout(h_fc1, self.dropout_placeholder)
with tf.variable_scope("ReadoutLayer") as RL:
wfc2 = tf.get_variable("wfc2", (32, self.config.num_classes), initializer=tf.truncated_normal_initializer(stddev=0.1))
bfc2 = tf.get_variable("bfc2", (self.config.num_classes), initializer=tf.constant_initializer(0.1))
y_conv = tf.matmul(h_fc1_drop, wfc2) + bfc2
return y_conv
def add_loss_op(self, pred):
"""Adds ops for loss to the computational graph.
Args:
pred: A tensor of shape (batch_size, n_classes)
Returns:
loss: A 0-d tensor (scalar) output
"""
#Hinge Loss
#pred = tf.nn.softmax(pred)
#loss = tf.reduce_mean(tf.maximum( 0.0, (1.0 - self.labels_placeholder)*pred - self.labels_placeholder*pred + 1.0 ))
#class_weights = tf.constant([1.0 - self.config.class_ratio, self.config.class_ratio])
#weighted_logits = tf.mul(pred, class_weights)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, self.labels_placeholder))
return loss
def add_training_op(self, loss):
"""Sets up the training Ops.
Creates an optimizer and applies the gradients to all trainable variables.
The Op returned by this function is what must be passed to the
`sess.run()` call to cause the model to train. See
https://www.tensorflow.org/versions/r0.7/api_docs/python/train.html#Optimizer
for more information.
Args:
loss: Loss tensor, from cross_entropy_loss.
Returns:
train_op: The Op for training.
"""
### YOUR CODE HERE
optimizer = tf.train.AdamOptimizer(learning_rate=self.config.lr)
train_op = optimizer.minimize(loss)
return train_op
def __init__(self, config):
self.config = config
self.load_data()
self.add_placeholders()
output = self.add_model(self.input_placeholder)
self.loss = self.add_loss_op(output)
self.predictions = tf.nn.softmax(output)
one_hot_prediction = tf.argmax(self.predictions, 1)
correct_prediction = tf.equal(tf.argmax(self.labels_placeholder, 1), one_hot_prediction)
self.correct_predictions = tf.reduce_sum(tf.cast(correct_prediction, 'int32'))
self.train_op = self.add_training_op(self.loss)
def run_epoch(self, sess, input_data, input_labels, htsoft):
orig_X, orig_y, orig_htsoft = input_data, input_labels, htsoft
dp = self.config.dropout
total_loss = []
total_correct_examples = 0
total_processed_examples = 0
total_steps = len(orig_X) / self.config.batch_size
for step, (x, y, ht) in enumerate(data_iterator(orig_X, orig_htsoft, orig_y, batch_size=self.config.batch_size,
label_size=self.config.num_classes)):
feed = self.create_feed_dict(input_batch=x, dropout=dp, htsoft=ht, label_batch=y)
loss, total_correct, _ = session.run(
[self.loss, self.correct_predictions, self.train_op],
feed_dict=feed)
total_processed_examples += len(x)
total_correct_examples += total_correct
total_loss.append(loss)
sys.stdout.write('\r{} / {} : loss = {}'.format(
step, total_steps, np.mean(total_loss)))
sys.stdout.flush()
sys.stdout.write('\r')
sys.stdout.flush()
return np.mean(total_loss), total_correct_examples / float(total_processed_examples)
def predict(self, sess, input_data, input_classes, htsoft):
"""Make predictions from the provided model.
Args:
sess: tf.Session()
input_data: np.ndarray of shape (n_samples, n_features)
input_labels: np.ndarray of shape (n_samples, n_classes)
Returns:
average_loss: Average loss of model.
predictions: Predictions of model on input_data
"""
dp = 1
losses = []
results = []
predictions_scores = []
data = data_iterator(input_data, htsoft, batch_size=self.config.batch_size,
label_size=self.config.num_classes)
first = True
for step, (x, y, ht) in enumerate(data):
feed = self.create_feed_dict(input_batch=x, dropout=dp, htsoft=ht)
preds = session.run(self.predictions, feed_dict=feed)
preds = np.array(preds)
if first:
predictions_scores = preds
first = False
else:
predictions_scores = np.vstack((predictions_scores, preds))
predicted_indices = preds.argmax(axis=1)
results.extend(predicted_indices)
flattened_classes = np.array(input_classes.argmax(axis=1))
accuracy = sum(np.array(results) == flattened_classes)/float(len(flattened_classes))
print 'Accuracy on Test'
print accuracy
corresponding_scores = predictions_scores[:, 1]
fpr, tpr , thresholds = roc_curve(flattened_classes, corresponding_scores)
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
area = auc(fpr, tpr)
print 'Test Area Under Curve'
print area
plt.plot(fpr, tpr)
title = "Tensor Flow CNN"
plt.figtext(.4, .5, "AUC : " + str(area))
pp = PdfPages(title + ".pdf")
plt.savefig(pp, format="pdf")
pp.close()
plt.show()
return np.mean(losses), results
config = Config()
tf.reset_default_graph()
with tf.Graph().as_default():
model = HEPModel(config)
init = tf.initialize_all_variables()
with tf.Session() as session:
best_val_loss = float('inf')
best_val_epoch = 0
session.run(init)
for epoch in xrange(config.max_epoch):
print 'Epoch {}'.format(epoch)
train_loss, train_acc = model.run_epoch(session, model.X_train,
model.Y_train, model.htsoft_train)
print 'Training loss: {}'.format(train_loss)
print 'Training acc: {}'.format(train_acc)
if train_loss < best_val_loss:
best_val_loss = train_loss
best_val_epoch = epoch
if epoch - best_val_epoch > config.early_stopping:
break
val_loss, predictions = model.predict(session, model.X_test, model.Y_test, model.htsoft_test)