-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_result.py
executable file
·249 lines (224 loc) · 9.99 KB
/
test_result.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
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
'''Test function using trained models.
Author: Haiyang Kong
'''
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import sys
from os import walk
import numpy as np
import csv
import time
from scipy.ndimage.interpolation import zoom
from six.moves import urllib
from six.moves import xrange
import tensorflow as tf
from spectroscopy import FLAGS
from spectroscopy import readCSV, save3DSlice
from spectroscopy import normalizePlanes, worldToVoxelCoord
from spectroscopy import interpolatefilter, createImageBorder
FLAGS.USE_OFFICIAL = False
XAVIER_INIT = tf.contrib.layers.xavier_initializer(seed=FLAGS.SEED)
Wb = {
'W1': tf.get_variable('W1', [3, 3, 3, FLAGS.CHANNEL_NUMBER, 16], tf.float32, XAVIER_INIT),
'b1': tf.Variable(tf.zeros([16])),
'W2': tf.get_variable('W2', [3, 3, 3, 16, 32], tf.float32, XAVIER_INIT),
'b2': tf.Variable(tf.zeros([32])),
'W3': tf.get_variable('W3', [3, 3, 3, 32, 64], tf.float32, XAVIER_INIT),
'b3': tf.Variable(tf.zeros([64])),
'W4': tf.get_variable('W4', [3, 3, 3, 64, 128], tf.float32, XAVIER_INIT),
'b4': tf.Variable(tf.zeros([128])),
'W5': tf.get_variable('W5', [3, 3, 3, 128, 256], tf.float32, XAVIER_INIT),
'b5': tf.Variable(tf.zeros([256])),
'fcw1': tf.get_variable('fcw1', [2**3 * 256, 32], tf.float32, XAVIER_INIT),
'fcb1': tf.Variable(tf.zeros([32])),
'fcw2': tf.get_variable('fcw2', [32, FLAGS.LABEL_NUMBER], tf.float32, XAVIER_INIT),
'fcb2': tf.Variable(tf.zeros([FLAGS.LABEL_NUMBER]))
}
def model(data, keep_prob):
with tf.variable_scope('conv1') as scope:
conv = tf.nn.conv3d(data, Wb['W1'], strides=[1, 1, 1, 1, 1], padding='SAME')
relu = tf.nn.relu(tf.nn.bias_add(conv, Wb['b1']))
with tf.variable_scope('conv2') as scope:
conv = tf.nn.conv3d(relu, Wb['W2'], strides=[1, 1, 1, 1, 1], padding='SAME')
relu = tf.nn.relu(tf.nn.bias_add(conv, Wb['b2']))
pool = tf.nn.max_pool3d(relu, ksize=[1, 2, 2, 2, 1],
strides=[1, 2, 2, 2, 1], padding='VALID')
with tf.variable_scope('conv3') as scope:
conv = tf.nn.conv3d(pool, Wb['W3'], strides=[1, 1, 1, 1, 1], padding='SAME')
relu = tf.nn.relu(tf.nn.bias_add(conv, Wb['b3']))
pool = tf.nn.max_pool3d(relu, ksize=[1, 2, 2, 2, 1],
strides=[1, 2, 2, 2, 1], padding='VALID')
with tf.variable_scope('conv4') as scope:
conv = tf.nn.conv3d(pool, Wb['W4'], strides=[1, 1, 1, 1, 1], padding='SAME')
relu = tf.nn.relu(tf.nn.bias_add(conv, Wb['b4']))
pool = tf.nn.max_pool3d(relu, ksize=[1, 2, 2, 2, 1],
strides=[1, 2, 2, 2, 1], padding='VALID')
with tf.variable_scope('conv5') as scope:
conv = tf.nn.conv3d(pool, Wb['W5'], strides=[1, 1, 1, 1, 1], padding='SAME')
relu = tf.nn.relu(tf.nn.bias_add(conv, Wb['b5']))
pool = tf.nn.max_pool3d(relu, ksize=[1, 2, 2, 2, 1],
strides=[1, 2, 2, 2, 1], padding='VALID')
with tf.variable_scope('reshape'):
ps = pool.get_shape().as_list()
reshape = tf.reshape(pool, [-1, ps[1] * ps[2] * ps[3] * ps[4]])
with tf.variable_scope('fc1'):
hidden = tf.nn.relu(tf.matmul(reshape, Wb['fcw1']) + Wb['fcb1'])
with tf.variable_scope('dropout'):
hidden = tf.nn.dropout(hidden, keep_prob, seed=FLAGS.SEED)
with tf.variable_scope('fc2'):
out = tf.matmul(hidden, Wb['fcw2']) + Wb['fcb2']
return out
def readSpecialLine(csvLines, uid):
coor = []
oriAxis = []
label = []
for line in csvLines:
if line[0] == uid:
coor.append(np.array(line[-2:0:-1], np.float))
oriAxis.append(line[1:-1])
label.append(int(line[-1]))
return coor, label, oriAxis
def readSpecialLine_own(csvLines, uid):
coor = []
oriAxis = []
label = []
for line in csvLines:
if line[0] == uid:
coor.append(np.array(line[3:0:-1], np.float))
oriAxis.append(line[1:-1])
label.append(1 if float(line[-1]) > 0.5 else 0)
return coor, label, oriAxis
def error_rate(predictions, labels):
"""Return the error rate based on dense predictions and sparse labels."""
return 100.0 - (100.0 * np.sum(np.argmax(predictions, 1) == labels) /
predictions.shape[0])
def LUNAtest(image, sess, test_prediction, test_data_node):
def test_in_batches(data):
size = data.shape[0]
predictions = np.ndarray(shape=(size, FLAGS.LABEL_NUMBER), dtype=np.float32)
for begin in xrange(0, size, FLAGS.EVAL_BATCH_SIZE):
end = begin + FLAGS.EVAL_BATCH_SIZE
if end <= size:
predictions[begin:end, :] = sess.run(
test_prediction,
feed_dict={test_data_node: data[begin:end, ...]})
else:
batch_predictions = sess.run(
test_prediction,
feed_dict={test_data_node: data[-FLAGS.EVAL_BATCH_SIZE:, ...]})
predictions[begin:, :] = batch_predictions[begin - size:, :]
return predictions
predictions = test_in_batches(image)
return predictions
def test_official(mhdOriginalPath, uid, csvLines, count, sess, test_prediction, test_data_node):
print('Processing No. {}...'.format(count))
axis, label, oriAxis = readSpecialLine(csvLines, uid)
interpolatedImage, outputsize, _, outputspacing, origin = interpolatefilter(
mhdOriginalPath)
BackImage = createImageBorder(interpolatedImage, outputsize)
ccList = []
for a in axis:
cutCenter = worldToVoxelCoord(a, origin[::-1], outputspacing)
cutCenter += (FLAGS.BACK_SIZE / 2 - np.array(outputsize[::-1]) / 2)
ccList.append(cutCenter)
ccList = np.round(ccList).astype(np.int)
image = np.empty([len(ccList), FLAGS.SAVE_SIZE, FLAGS.SAVE_SIZE, FLAGS.SAVE_SIZE, 1])
for index, cc in enumerate(ccList):
cutTemp = BackImage[cc[0] - int(FLAGS.CUT_SIZE / 2):cc[0] + int(FLAGS.CUT_SIZE / 2),
cc[1] - int(FLAGS.CUT_SIZE / 2):cc[1] + int(FLAGS.CUT_SIZE / 2),
cc[2] - int(FLAGS.CUT_SIZE / 2):cc[2] + int(FLAGS.CUT_SIZE / 2)]
# save3DSlice(BackImage, cc, 'test1/')
cutTemp = zoom(cutTemp, float(FLAGS.SAVE_SIZE) / FLAGS.CUT_SIZE)
image[index, ..., 0] = cutTemp
results = LUNAtest(image, sess, test_prediction, test_data_node)
return results, oriAxis, label
def test_own(mhdOriginalPath, uid, csvLines, count, sess, test_prediction, test_data_node):
print('Processing No. {}...'.format(count))
axis, label, oriAxis = readSpecialLine_own(csvLines, uid)
interpolatedImage, outputsize, spacing, _, _ = interpolatefilter(mhdOriginalPath)
BackImage = createImageBorder(interpolatedImage, outputsize)
ccList = []
for a in axis:
cutCenter = np.array(a, np.float) * spacing[::-1] / spacing[0]
cutCenter += (FLAGS.BACK_SIZE / 2 - np.array(outputsize[::-1]) / 2)
ccList.append(cutCenter)
ccList = np.round(ccList).astype(np.int)
image = np.empty([len(ccList), FLAGS.SAVE_SIZE, FLAGS.SAVE_SIZE, FLAGS.SAVE_SIZE, 1])
for index, cc in enumerate(ccList):
cutTemp = BackImage[cc[0] - int(FLAGS.CUT_SIZE / 2):cc[0] + int(FLAGS.CUT_SIZE / 2),
cc[1] - int(FLAGS.CUT_SIZE / 2):cc[1] + int(FLAGS.CUT_SIZE / 2),
cc[2] - int(FLAGS.CUT_SIZE / 2):cc[2] + int(FLAGS.CUT_SIZE / 2)]
# save3DSlice(BackImage, cc, 'test1/')
cutTemp = zoom(cutTemp, float(FLAGS.SAVE_SIZE) / FLAGS.CUT_SIZE)
image[index, ..., 0] = cutTemp
results = LUNAtest(image, sess, test_prediction, test_data_node)
return results, oriAxis, label
def genPath(originalPath):
'''generate a dict with the uid as keys
and the subset number as values.
'''
pathDict = {}
for (dirpath, dirnames, filenames) in walk(originalPath):
dirnames.sort()
filenames.sort()
if(len(filenames) < 20):
continue
else:
for filename in filenames:
if filename.endswith('.mhd'):
pathDict.setdefault(filename[:-4], dirpath[-1])
return pathDict
def main(_):
filePath = '/home/kong/4T/nodule_project'
pathDict = genPath(filePath)
csvHeader = ['seriesuid', 'coordX', 'coordY', 'coordZ', 'probability']
csvName = '/home/kong/4T/official_extended/submissionCSV-model-012345678.csv'
csvLines = readCSV(csvName)[1:]
with open('results_submit_voxel.csv', 'wb') as f:
csvwriter = csv.writer(f)
csvwriter.writerow(csvHeader)
count = 0
test_data_node = tf.placeholder(
tf.float32, (None, FLAGS.IMAGE_SIZE, FLAGS.IMAGE_SIZE, FLAGS.IMAGE_SIZE, FLAGS.CHANNEL_NUMBER))
test_prediction = tf.nn.softmax(model(test_data_node, 1))
saver = tf.train.Saver()
for ssNo in range(10):
modelPath = '/home/kong/4T/official_extended/Cross{}'.format(ssNo)
with tf.Session() as sess:
ckpt = tf.train.get_checkpoint_state(modelPath)
ckpt.model_checkpoint_path = os.path.join(modelPath, ckpt.model_checkpoint_path)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
print('Model of Subset {} Restored Successfully...'.format(ssNo))
first = True
for uid in pathDict:
if str(ssNo) != pathDict[uid]:
continue
mhdFileName = '{}/subset{}/{}.mhd'.format(filePath, ssNo, uid)
if FLAGS.USE_OFFICIAL:
result, axis, label = test_official(mhdFileName, uid, csvLines, count,
sess, test_prediction, test_data_node)
else:
result, axis, label = test_own(mhdFileName, uid, csvLines, count,
sess, test_prediction, test_data_node)
if first:
allResult = result
allLabel = label
first = False
else:
allResult = np.append(allResult, result, 0)
allLabel = np.append(allLabel, label, 0)
for i in range(result.shape[0]):
writeContent = [uid]
writeContent.extend(axis[i])
writeContent.append(result[i, 1])
csvwriter.writerow(writeContent)
count += 1
print('Error Rate of Model {} is: {}'.format(
ssNo, error_rate(allResult, allLabel)))
if __name__ == '__main__':
tf.app.run()