-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathnew_model.py
320 lines (279 loc) · 15.1 KB
/
new_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
import tensorflow as tf
import numpy as np
import time
from data_prepare import get_all_filename,get_train_batch,get_test_batch
import random
DTYPE = tf.float32
learning_rate = 0.3
epoch = 30
cubic_shape = [[6, 20, 20], [10, 30, 30], [26, 40, 40]]
alpha1= 0.3
alpha2 = 0.4
alpha3 = 0.3
gpu_options1 = tf.GPUOptions(per_process_gpu_memory_fraction=0.3)
sess1 = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options1))
gpu_options2 = tf.GPUOptions(per_process_gpu_memory_fraction=0.3)
sess2 = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options2))
gpu_options3 = tf.GPUOptions(per_process_gpu_memory_fraction=0.3)
sess3 = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options3))
def weight_variable(shape,name=None):
return tf.get_variable(name=name,shape=shape,initializer=tf.truncated_normal_initializer(stddev=0.001))
def bias_variable(shape,name=None):
return tf.get_variable(name=name,shape=shape,initializer=tf.constant_initializer(value=0.01))
def arch1(inputs,_keep_prob):
'''
network structure for cube shape 20x20x6 ,cover 58% nodule
:param inputs:
:return:
'''
in_filters = 1
pre_layer = inputs
with tf.name_scope("arch-1") as scope:
# shape of input for arch-1 is cube 20x20x6
with tf.variable_scope("conv_1") as con_scope:
out_filters = 64
kernel = weight_variable([3,5,5,in_filters,out_filters],name="weight")
# the output size is batch_size x 6x20x20x64 ([batch_size,in_deep,width,height,output_deep])
conv = tf.nn.conv3d(pre_layer,kernel,strides=[1,1,1,1,1],padding='SAME')
bias = bias_variable([out_filters],name="biases")
bias = tf.nn.bias_add(conv,bias)
conv1 = tf.nn.relu(bias,name=con_scope.name)
pre_layer = conv1
in_filters = out_filters
pre_layer = tf.nn.dropout(pre_layer,keep_prob=_keep_prob)
# pooling don't change feature shape at all
pool1 = tf.nn.max_pool3d(pre_layer,ksize=[1,1,1,1,1],strides=[1,1,1,1,1],padding='SAME')
pre_layer= pool1
with tf.variable_scope("conv_2") as con_scope:
out_filters = 64
kernel = weight_variable([3,5,5, in_filters, out_filters],name="weight")
conv = tf.nn.conv3d(pre_layer, kernel, strides=[1, 1, 1, 1, 1], padding='SAME')
bias = bias_variable( [out_filters],name="biases")
bias = tf.nn.bias_add(conv, bias)
conv2 = tf.nn.relu(bias, name=con_scope.name)
pre_layer = conv2
in_filters = out_filters
pre_layer = tf.nn.dropout(pre_layer, keep_prob=_keep_prob)
with tf.variable_scope("conv_3") as con_scope:
out_filters = 64
kernel = weight_variable([1,5,5, in_filters, out_filters],name="weight")
conv = tf.nn.conv3d(pre_layer, kernel, strides=[1, 1, 1, 1, 1], padding='SAME')
bias = bias_variable([out_filters],name="biases")
bias = tf.nn.bias_add(conv, bias)
conv3 = tf.nn.relu(bias, name=con_scope.name)
pre_layer = conv3
in_filters = out_filters
pre_layer = tf.nn.dropout(pre_layer, keep_prob=_keep_prob)
# output shape of all above is 6x20x20x64,maxpooling kernel is 1x1x1, and convolutional padding are same,no shape decrease
out_conv3 = tf.reshape(pre_layer, [-1, 6 * 20 * 20 * 64])
w_fc1 = weight_variable([6 * 20 * 20 * 64, 150],name='w_fc1')
out_fc1 = tf.nn.relu(tf.add(tf.matmul(out_conv3, w_fc1), bias_variable([150],name="fc1_biases")))
out_fc1 = tf.nn.dropout(out_fc1, keep_prob=_keep_prob)
w_fc2 = weight_variable([150,2],name="w_fc2")
out_fc2 = tf.nn.relu(tf.add(tf.matmul(out_fc1,w_fc2), bias_variable([2], name="fc2_biases")))
out_fc2 = tf.nn.dropout(out_fc2, keep_prob=_keep_prob)
return out_fc2
def arch2(inputs,_keep_prob):
'''
network structure for cube shape 30x30x10 ,cover 85% nodule
:param inputs:
:return:
'''
in_filters = 1
pre_layer = inputs
with tf.name_scope("arch-2") as scope:
# shape of input for arch-1 is cube 30x30x10
with tf.variable_scope("conv_1") as con_scope:
out_filters = 64
kernel = weight_variable([3,5,5,in_filters,out_filters],name="weight")
# the output size is batch_size x 6x20x20x64 ([batch_size,in_deep,width,height,output_deep])
conv = tf.nn.conv3d(pre_layer,kernel,strides=[1,1,1,1,1],padding='SAME')
bias = bias_variable([out_filters],name="biases")
bias = tf.nn.bias_add(conv,bias)
conv1 = tf.nn.relu(bias,name=con_scope.name)
pre_layer = conv1
in_filters = out_filters
pre_layer = tf.nn.dropout(pre_layer,keep_prob=_keep_prob)
# pooling make shape into 10x15x15x64
pool1 = tf.nn.max_pool3d(pre_layer,ksize=[1,1,2,2,1],strides=[1,1,1,1,1],padding='SAME')
pre_layer= pool1
with tf.variable_scope("conv_2") as con_scope:
out_filters = 64
kernel = weight_variable( [3,5,5, in_filters, out_filters],name="weight")
conv = tf.nn.conv3d(pre_layer, kernel, strides=[1, 1, 1, 1, 1], padding='SAME')
bias = bias_variable([out_filters],name="biases")
bias = tf.nn.bias_add(conv, bias)
conv2 = tf.nn.relu(bias, name=con_scope.name)
pre_layer = conv2
in_filters = out_filters
pre_layer = tf.nn.dropout(pre_layer, keep_prob=_keep_prob)
with tf.variable_scope("conv_3") as con_scope:
out_filters = 64
kernel = weight_variable([3,5,5, in_filters, out_filters],name="weight")
conv = tf.nn.conv3d(pre_layer, kernel, strides=[1, 1, 1, 1, 1], padding='SAME')
bias = bias_variable([out_filters],name="biases")
bias = tf.nn.bias_add(conv, bias)
conv3 = tf.nn.relu(bias, name=con_scope.name)
pre_layer = conv3
in_filters = out_filters
pre_layer = tf.nn.dropout(pre_layer, keep_prob=_keep_prob)
# output shape of all above is 10x15x15x64, convolutional padding are SAME
out_conv3 = tf.reshape(pre_layer, [-1, 10 * 15 * 15 * 64])
w_fc1 = weight_variable([10 * 15 * 15 * 64, 250],name='w_fc1')
out_fc1 = tf.nn.relu(tf.add(tf.matmul(out_conv3, w_fc1), bias_variable([250],name="fc1_biases")))
out_fc1 = tf.nn.dropout(out_fc1, keep_prob=_keep_prob)
w_fc2 = weight_variable([250,2],name="w_fc2")
out_fc2 = tf.nn.relu(tf.add(tf.matmul(out_fc1,w_fc2), bias_variable([2], name="fc2_biases")))
out_fc2 = tf.nn.dropout(out_fc2, keep_prob=_keep_prob)
return out_fc2
def arch3(inputs,_keep_prob):
'''
network structure for cube shape 40x40x26 ,cover 99% nodule
:param inputs:
:return:
'''
in_filters = 1
pre_layer = inputs
with tf.name_scope("arch-3") as scope:
# shape of input for arch-1 is cube 40x40x26
with tf.variable_scope("conv_1") as con_scope:
out_filters = 64
kernel = weight_variable([3,5,5,in_filters,out_filters],name="weight")
# the output size is batch_size x 6x20x20x64 ([batch_size,in_deep,width,height,output_deep])
conv = tf.nn.conv3d(pre_layer,kernel,strides=[1,1,1,1,1],padding='SAME')
bias = bias_variable([out_filters],name="biases")
bias = tf.nn.bias_add(conv,bias)
conv1 = tf.nn.relu(bias,name=con_scope.name)
pre_layer = conv1
in_filters = out_filters
pre_layer = tf.nn.dropout(pre_layer,keep_prob=_keep_prob)
# pooling make shape into 13x20x20x64
pool1 = tf.nn.max_pool3d(pre_layer,ksize=[1,2,2,2,1],strides=[1,1,1,1,1],padding='SAME')
pre_layer= pool1
with tf.variable_scope("conv_2") as con_scope:
out_filters = 64
kernel = weight_variable([3,5,5, in_filters, out_filters],name="weight")
conv = tf.nn.conv3d(pre_layer, kernel, strides=[1, 1, 1, 1, 1], padding='SAME')
bias = bias_variable([out_filters],name="biases")
bias = tf.nn.bias_add(conv, bias)
conv2 = tf.nn.relu(bias, name=con_scope.name)
pre_layer = conv2
in_filters = out_filters
pre_layer = tf.nn.dropout(pre_layer, keep_prob=_keep_prob)
with tf.variable_scope("conv_3") as con_scope:
out_filters = 64
kernel = weight_variable([3,5,5, in_filters, out_filters],name="weight")
conv = tf.nn.conv3d(pre_layer, kernel, strides=[1, 1, 1, 1, 1], padding='SAME')
bias = bias_variable([out_filters],name="biases")
bias = tf.nn.bias_add(conv, bias)
conv3 = tf.nn.relu(bias, name=con_scope.name)
pre_layer = conv3
in_filters = out_filters
pre_layer = tf.nn.dropout(pre_layer, keep_prob=_keep_prob)
# output shape of all above is 13x20x20x64, convolutional padding are SAME
out_conv3 = tf.reshape(pre_layer, [-1, 13 * 20 * 20 * 64])
w_fc1 = weight_variable([13 * 20 * 20 * 64, 250],name='w_fc1')
out_fc1 = tf.nn.relu(tf.add(tf.matmul(out_conv3, w_fc1), bias_variable([250],name="fc1_biases")))
out_fc1 = tf.nn.dropout(out_fc1, keep_prob=_keep_prob)
w_fc2 = weight_variable([250,2],name="w_fc2")
out_fc2 = tf.nn.relu(tf.add(tf.matmul(out_fc1,w_fc2), bias_variable([2], name="fc2_biases")))
out_fc2 = tf.nn.dropout(out_fc2, keep_prob=_keep_prob)
return out_fc2
def train_model(arch_index,npy_path,test_path,batch_size = 256):
highest_acc = 0.0
highest_iterator = 1
initial_learning_rate = 0.9
all_train_filenames = get_all_filename(npy_path,cubic_shape[arch_index][1])
# ensure that number of real nodule sample and fake nodule are equal
real_file_list = []
fake_file_list = []
real_num = 0
for file in all_train_filenames:
if "real" in file:
real_num = real_num +1
real_file_list.append(file)
elif "fake" in file:
fake_file_list.append(file)
print("size of real file ",len(real_file_list))
print("size of fake file ", len(fake_file_list))
fake_file_list = random.sample(fake_file_list,real_num)
print("size of fake file(after random choice) ", len(fake_file_list))
all_train_filenames = real_file_list + fake_file_list
all_test_filenames = get_all_filename(test_path,cubic_shape[arch_index][1])
all_train_filenames = all_train_filenames[:20000]
print("file size is :\t",len(all_train_filenames))
# how many time should one epoch should loop to feed all data
times = int(len(all_train_filenames) / batch_size)
if (len(all_train_filenames) % batch_size) != 0:
times = times + 1
# keep_prob used for dropout
keep_prob = tf.placeholder(tf.float32)
# take placeholder as input
x = tf.placeholder(tf.float32, [None, cubic_shape[arch_index][0], cubic_shape[arch_index][1], cubic_shape[arch_index][2]])
x_image = tf.reshape(x, [-1, cubic_shape[arch_index][0], cubic_shape[arch_index][1], cubic_shape[arch_index][2], 1])
if arch_index == 0:
net = arch1(x_image,keep_prob)
elif arch_index == 1:
net = arch2(x_image,keep_prob)
elif arch_index == 2:
net = arch3(x_image,keep_prob)
else:
print("model architecture index must be 0 or 1 or 2,current is %s which is not supported"%(str(arch_index)))
return
saver = tf.train.Saver() # default to save all variable,save mode or restore from path
# softmax layer
real_label = tf.placeholder(tf.float32, [None, 2])
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=net, labels=real_label)
#cross_entropy = tf.reduce_mean(-tf.reduce_sum(real_label * tf.log(net), axis=1))
net_loss = tf.reduce_mean(cross_entropy)
tf.summary.scalar('net loss', net_loss)
global_step = tf.Variable(0, trainable=False)
# decayed_learning_rate = learning_rate *decay_rate^ (global_step / decay_steps)
learning_rate = tf.train.exponential_decay(initial_learning_rate,
global_step=global_step,
decay_steps=5000, decay_rate=0.95)
train_step = tf.train.MomentumOptimizer(learning_rate,momentum=0.9).minimize(net_loss)
tf.summary.scalar("learing_rate",learning_rate)
correct_prediction = tf.equal(tf.argmax(net, 1), tf.argmax(real_label, 1))
accruacy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
tf.summary.scalar("training accuracy",accruacy)
merged = tf.summary.merge_all()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
train_writer = tf.summary.FileWriter('./arch-%d-tensorboard/'%(arch_index), sess.graph)
# loop epoches
for i in range(epoch):
epoch_start =time.time()
# the data will be shuffled by every epoch
random.shuffle(all_train_filenames)
random.shuffle(all_test_filenames)
all_test_filenames = all_test_filenames[:1000]
for t in range(times):
batch_files = all_train_filenames[t*batch_size:(t+1)*batch_size]
batch_data, batch_label = get_train_batch(batch_files)
# print("training data ...")
# print(batch_data.shape)
# print("training label...")
# print(batch_label.shape)
feed_dict = {x: batch_data, real_label: batch_label,keep_prob:0.8,global_step:i*times+t}
summary,_ = sess.run([merged,train_step],feed_dict =feed_dict)
train_writer.add_summary(summary, i*times+t)
saver.save(sess, './arch-%d-ckpt/arch-%d'%(arch_index,arch_index), global_step=i + 1)
#print("training in epoch:%d of %d,times %d in %d "%(i,epoch,t,times))
epoch_end = time.time()
test_batch,test_label = get_test_batch(all_test_filenames[0:1000])
print("type of test batch ,label",type(test_batch),type(test_label))
print("length of test batch data:",test_batch.shape)
print("length of test batch label:\t", test_label.shape)
test_dict = {x: test_batch, real_label: test_label, keep_prob:1.0}
acc_test,loss = sess.run([accruacy,net_loss],feed_dict=test_dict)
if acc_test>highest_acc:
highest_acc = acc_test
print('accuracy is %f' % acc_test)
print("loss is ", loss)
print(" epoch %d time consumed %f seconds"%(i,(epoch_end-epoch_start)))
print("training finshed..highest accuracy is %f,the iterator is %d " % (highest_acc, highest_iterator))
arch_index = 0
npy_path = "H:/data/luna2016/cubic_normalization_npy/"
test_path = "H:/data/luna2016/cubic_normalization_test/"
train_model(arch_index,npy_path,test_path,batch_size = 256)