-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcnn_3d.py
179 lines (143 loc) · 6.68 KB
/
cnn_3d.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
import tensorflow as tf
import numpy as np
import h5py
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from nn_utils import conv3d, conv3d_transpose
import sys
import time
slim = tf.contrib.slim
DATA_DIR = 'data'
class Config():
lr = 0.001
l2 = 0.01
dropout = 0.8
kernel_size = 3
num_filters = 8
downsample_every = 2
num_layers = 4
num_layers_to_train = num_layers
num_layers_to_restore = 0
downsample_every = 2
downsample_factor = 2
use_sex_labels = False
mode = 'pretrain'
class CNN_3D(object):
def _save_images(self, images, outputs, name):
if len(outputs.shape) == 4:
outputs = np.expand_dims(outputs, 4)
im_depth = images.shape[1]/2
out_depth = outputs.shape[1]/2
fig = plt.figure()
a = fig.add_subplot(1,2,1)
imgplot = plt.imshow(images[0,im_depth,:,:])
a.set_title('Original')
a = fig.add_subplot(1,2,2)
imgplot = plt.imshow(outputs[0,out_depth,:,:,0])
a.set_title('Reconstructed')
fig.savefig('figures/' + name + ".png")
plt.close()
def make_predictions(self, output):
"""Get answer predictions from output"""
preds = tf.nn.softmax(output)
pred = tf.argmax(preds, 1)
return pred
def calc_accuracy(self, predictions, labels):
correct_prediction = tf.equal(predictions, labels)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
return accuracy
def calc_loss(self, outputs, labels):
# in pretraining outputs and labels are the full 3d MRIs
if self.config.mode == 'pretrain':
loss = tf.reduce_sum(tf.square(outputs - labels))
else:
loss = tf.reduce_sum(tf.nn.sparse_softmax_cross_entropy_with_logits(outputs, labels))
loss += tf.reduce_sum(tf.pack(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)))
return loss
def add_train_op(self, loss):
train_op = tf.train.AdamOptimizer(learning_rate=self.config.lr).minimize(loss)
return train_op
def correlation_inference(self, correlation):
flattened = slim.flatten(correlation)
layer_1 = slim.fully_connected(flattened, 1000, weights_regularizer=slim.l2_regularizer(self.config.l2))
output = slim.fully_connected(layer_1, 2, activation_fn=None, weights_regularizer=slim.l2_regularizer(self.config.l2))
#output = slim.fully_connected(flattened, 2, activation_fn=None, weights_regularizer=slim.l2_regularizer(self.config.l2))
return output
def image_inference(self, images, train=True):
images = tf.expand_dims(images, -1)
# for convenience
num_filters = self.config.num_filters
kernel_size = self.config.kernel_size
downsample_every = self.config.downsample_every
num_layers = self.config.num_layers
num_layers_to_train = self.config.num_layers_to_train
# conv
if num_layers > 0:
trainable = True if num_layers_to_train >= num_layers else False
forward = conv3d(images, kernel_size, 1, num_filters,
scope='conv_1', trainable=trainable)
else:
forward = images
for i in range(num_layers - 1):
stride = 2 if i % downsample_every == 0 else 1
trainable = True if i+2 > num_layers - num_layers_to_train else False
forward = conv3d(forward, kernel_size, num_filters, num_filters,
scope='conv_' + str(i+2), stride=stride, trainable=trainable)
if stride == 2:
forward = slim.dropout(forward, keep_prob=self.config.dropout, is_training=train)
if self.config.mode == 'pretrain':
backward = forward
# deconv
for i in range(num_layers - 1):
stride = 1 if i % downsample_every == 0 else 2
backward = conv3d_transpose(backward, kernel_size, num_filters, num_filters,
scope='conv_' + str(num_layers - i), stride=stride)
backward = conv3d_transpose(backward, kernel_size, num_filters, 1, scope='conv_1', stride=2)
output = tf.squeeze(backward)
else:
flattened = slim.flatten(forward)
with tf.variable_scope('fully_connected'):
# fully connected layer
output = slim.fully_connected(flattened, 2000, weights_regularizer=slim.l2_regularizer(self.config.l2))
output = slim.fully_connected(output, 500, weights_regularizer=slim.l2_regularizer(self.config.l2))
output = slim.fully_connected(output, 2, activation_fn=None, weights_regularizer=slim.l2_regularizer(self.config.l2))
return output, forward
def inference(self, images, correlation, train):
if self.config.use_correlation == 2:
corr_outputs = self.correlation_inference(correlation)
return corr_outputs
image_outputs, self.filt = self.image_inference(images, train)
# pool correlation and image results
if self.config.use_correlation == 1:
corr_outputs = self.correlation_inference(correlation)
outputs = tf.concat(1, [image_outputs, corr_outputs])
outputs = slim.fully_connected(outputs, 2, activation_fn=None)
return outputs
return image_outputs
def add_summaries(self, images, train):
dataset = 'train' if train else 'validation'
tf.scalar_summary('loss', self.loss)
tf.scalar_summary('accuracy', self.accuracy)
if self.config.use_correlation != 2:
filt_depth = int(self.filt.get_shape()[1])/2
im_depth = int(images.get_shape()[1])/2
filt = tf.squeeze(
tf.slice(self.filt, [0, filt_depth, 0, 0, 0], [-1, 1, -1, -1, 1])
, [1])
image = tf.squeeze(
tf.slice(images, [0, im_depth, 0, 0], [-1, 1, -1, -1])
)
tf.image_summary('filter', filt)
tf.image_summary('image', tf.expand_dims(image, -1))
def __init__(self, config, image_batch, label_batch, corr_batch, train=True):
self.config = config
label_batch = image_batch if config.mode == 'pretrain' else label_batch
self.outputs = self.inference(image_batch, corr_batch, train)
self.loss = self.calc_loss(self.outputs, label_batch)
self.predictions = self.make_predictions(self.outputs)
self.accuracy = self.calc_accuracy(self.predictions, label_batch)
self.train_op = self.add_train_op(self.loss)
self.add_summaries(image_batch, train)
self.merged = tf.merge_all_summaries()