-
Notifications
You must be signed in to change notification settings - Fork 0
/
stent_SImpleNewModel.py
443 lines (341 loc) · 13.6 KB
/
stent_SImpleNewModel.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# -*- coding: utf-8 -*-
import time
import torch
from torch import nn, optim
import sys
sys.path.append("..")
#import d2lzh_pytorch as d2l
# import dataloader1
import dataloader1
import os
import numpy as np
import torch
import torchvision
import torchvision.transforms as transforms
import sys
from IPython import display
from matplotlib import pyplot as plt
import pylab
from torch import nn
import time
import torch.nn.functional as F
#from unet_parts import *
# import torchvision.models as models
from torchsummary import summary
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
os.environ["CUDA_VISIBLE_DEVICES"] = "2"
def use_svg_display():
"""Use svg format to display plot in jupyter"""
display.set_matplotlib_formats('svg')
def set_figsize(figsize=(3.5, 2.5)):
use_svg_display()
# 设置图的尺寸
plt.rcParams['figure.figsize'] = figsize
def semilogy(x_vals, y_vals, x_label, y_label, x2_vals=None, y2_vals=None,
legend=None, figsize=(3.5, 2.5)):
set_figsize(figsize)
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.semilogy(x_vals, y_vals)
if x2_vals and y2_vals:
plt.semilogy(x2_vals, y2_vals, linestyle=':')
plt.legend(legend)
plt.savefig('/media/mmlab/dataset/mengya/StentDetectionDataset/Dataset/D8/loss_curve.png', format='png')
plt.show()
pylab.show()
# train data path
train_xdir = '/media/mmlab/dataset/mengya/StentDetectionDataset/Dataset/D8/Data/'
train_ypath = '/media/mmlab/dataset/mengya/StentDetectionDataset/Dataset/D8/filelist.txt'
train_data_list = "/media/mmlab/dataset/mengya/StentDetectionDataset/Dataset/D8/train.txt"
# test data path
test_xdir = '/media/mmlab/dataset/mengya/StentDetectionDataset/Dataset/D8/Data/'
test_ypath = '/media/mmlab/dataset/mengya/StentDetectionDataset/Dataset/D8/filelist.txt'
test_data_list= "/media/mmlab/dataset/mengya/StentDetectionDataset/Dataset/D8/test.txt"
#dataloader
train_dataset = dataloader1.Dataset(img_list = train_data_list, img_dir=train_xdir, file_path=train_ypath)
test_dataset = dataloader1.Dataset(img_list = test_data_list, img_dir=test_xdir, file_path=test_ypath)
feature, label = train_dataset[4]
print(feature.shape, label)
batch_size = 40
if sys.platform.startswith('win'):
num_workers = 0
else:
num_workers = 2
train_iter = torch.utils.data.DataLoader(train_dataset, batch_size = batch_size, shuffle = True, num_workers = num_workers)
test_iter = torch.utils.data.DataLoader(test_dataset, batch_size = batch_size, shuffle = True, num_workers = num_workers)
'''
# Define model for 4 dimensional data
class LeNet(nn.Module):
def __init__(self):
super(LeNet, self).__init__()
self.conv = nn.Sequential(
nn.Conv3d(1, 6, 5), # in_channels, out_channels, kernel_size
nn.ReLU(),
nn.MaxPool3d(2, 2), # kernel_size, stride
nn.Conv3d(6, 16, 5),
nn.ReLU(),
nn.MaxPool3d(2, 2)
)
self.fc = nn.Sequential(
# nn.Linear(16*203*232, 120), # num_inputs, num_outputs
nn.Linear(16 * 5 * 5 * 4, 200), # num_inputs, num_outputs
nn.ReLU(),
nn.Linear(200, 120),
nn.ReLU(),
nn.Linear(120, 120),
nn.ReLU(),
nn.Linear(120, 84),
nn.ReLU(),
nn.Linear(84, 4) # Before: 4 classes in total, Now 5 classes in total
)
def forward(self, img):
feature = self.conv(img)
output = self.fc(feature.view(img.shape[0], -1)) # do the flatten
return output
'''
'''
class UNet(nn.Module):
def __init__(self, n_channels, n_classes, bilinear=True):
super(UNet, self).__init__()
self.n_channels = n_channels
self.n_classes = n_classes
self.bilinear = bilinear
self.inc = DoubleConv(n_channels, 64)
self.down1 = Down(64, 128)
self.down2 = Down(128, 256)
self.down3 = Down(256, 512)
self.down4 = Down(512, 512)
self.up1 = Up(1024, 256, bilinear)
self.up2 = Up(512, 128, bilinear)
self.up3 = Up(256, 64, bilinear)
self.up4 = Up(128, 64, bilinear)
self.outc = OutConv(64, n_classes)
def forward(self, x):
x1 = self.inc(x)
x2 = self.down1(x1)
x3 = self.down2(x2)
x4 = self.down3(x3)
x5 = self.down4(x4)
x = self.up1(x5, x4)
x = self.up2(x, x3)
x = self.up3(x, x2)
x = self.up4(x, x1)
logits = self.outc(x)
return logits
'''
class DoubleConv(nn.Module):
"""(convolution => [BN] => ReLU) * 2"""
def __init__(self, in_channels, out_channels):
super().__init__()
self.double_conv = nn.Sequential(
nn.Conv3d(in_channels, out_channels, kernel_size=3, padding=1),
nn.BatchNorm3d(out_channels),
nn.ReLU(inplace=True),
nn.Conv3d(out_channels, out_channels, kernel_size=3, padding=1),
nn.BatchNorm3d(out_channels),
nn.ReLU(inplace=True)
)
def forward(self, x):
return self.double_conv(x)
class Down(nn.Module):
"""Downscaling with maxpool then double conv"""
def __init__(self, in_channels, out_channels):
super().__init__()
self.maxpool_conv = nn.Sequential(
nn.MaxPool3d(2),
DoubleConv(in_channels, out_channels)
)
def forward(self, x):
return self.maxpool_conv(x)
############################ Complicated network #################################################
# class UNet(nn.Module):
# def __init__(self, n_channels, n_classes, bilinear=True):
# super(UNet, self).__init__()
# self.n_channels = n_channels
# self.n_classes = n_classes
# self.bilinear = bilinear
# self.inc = DoubleConv(n_channels, 10)
# self.down1 = Down(10, 20)
# self.down2 = Down(20, 30)
# self.down3 = Down(30, 40)
# #self.down4 = Down(256, 512)
# self.linear1 = nn.Linear(86700, 256) #flatten layer size of down1
# self.linear2 = nn.Linear(13440, 256) #flatten layer size of down2
# self.linear3 = nn.Linear(1920, 256) #flatten layer size of down3
# #self.linear4 = nn.Linear(, 256) #flatten layer size of down4
# self.linear5 = nn.Linear(768, 128)
# self.linear6 = nn.Linear(128, 64)
# self.linear7 = nn.Linear(64, n_classes) # classes number
# self.activation1 = nn.ReLU()
# self.activation2 = nn.ReLU()
# self.activation3 = nn.ReLU()
# def forward(self, x):
# # print(x.size())
# x = self.inc(x)
# x = self.down1(x)
# x1 = self.linear1(x.view(x.shape[0], -1))
# # print("first1")
# # print(x.size())
# x = self.down2(x)
# x2 = self.linear2(x.view(x.shape[0], -1))
# # print("first2")
# # print(x.size())
# x = self.down3(x)
# x3 = self.linear3(x.view(x.shape[0], -1))
# # print("first3")
# full_fea_x = torch.cat((x1,x2,x3), 1)
# # print(full_fea_x.size())
# full_fea_x = self.linear5(full_fea_x)
# full_fea_x = self.activation1(full_fea_x)
# full_fea_x = self.linear6 (full_fea_x)
# full_fea_x = self.activation2(full_fea_x)
# full_fea_x = self.linear7(full_fea_x)
# return full_fea_x
############################ Simpler network #################################################
class UNet(nn.Module):
def __init__(self, n_channels, n_classes, bilinear=True):
super(UNet, self).__init__()
self.n_channels = n_channels
self.n_classes = n_classes
self.bilinear = bilinear
self.inc = DoubleConv(n_channels, 10)
self.down1 = Down(10, 20)
# self.down2 = Down(20, 30)
# self.down3 = Down(30, 40)
# #self.down4 = Down(256, 512)
self.linear1 = nn.Linear(86700, 1024) #flatten layer size of down1
# self.linear2 = nn.Linear(13440, 256) #flatten layer size of down2
# self.linear3 = nn.Linear(1920, 256) #flatten layer size of down3
# #self.linear4 = nn.Linear(, 256) #flatten layer size of down4
self.linear5 = nn.Linear(1024, 512)
self.linear6 = nn.Linear(512, 64)
self.linear7 = nn.Linear(64, n_classes) # classes number
self.activation1 = nn.ReLU()
self.activation2 = nn.ReLU()
self.activation3 = nn.ReLU()
def forward(self, x):
# print(x.size())
x = self.inc(x)
x = self.down1(x)
x = self.linear1(x.view(x.shape[0], -1))
# print("first1")
# print(x.size())
# x = self.down2(x)
# x2 = self.linear2(x.view(x.shape[0], -1))
# # print("first2")
# # print(x.size())
# x = self.down3(x)
# x3 = self.linear3(x.view(x.shape[0], -1))
# print("first3")
# full_fea_x = torch.cat((x1,x2,x3), 1)
# print(full_fea_x.size())
full_fea_x = self.linear5(x)
full_fea_x = self.activation1(full_fea_x)
full_fea_x = self.linear6 (full_fea_x)
full_fea_x = self.activation2(full_fea_x)
full_fea_x = self.linear7(full_fea_x)
return full_fea_x
global net
# net = LeNet()
# net = UNet(1, 3) # classes number
net = UNet(1, 4) # classes number
net = net.to(device)
summary(net, (1, 31, 34, 34))
print(net)
# ######## Test the shape of con block #######
# X = torch.rand(1, 31, 34, 34)
# X = X.to(device)
# for name, blk in net.named_children():
# X = blk(X)
# print(name, 'output shape', X.shape)
pre_label = open("/media/mmlab/dataset/mengya/StentDetectionDataset/Dataset/D8/pre_label.txt", "w+")
model_label = open("/media/mmlab/dataset/mengya/StentDetectionDataset/Dataset/D8/re_label.txt", "w+")
# train the model
def evaluate_accuracy(data_iter, device, epoch):
global net
#if device is None and isinstance(net, torch.nn.Module):
# device = list(net.parameters())[0].device
acc_sum, n = 0.0, 0
m = 0
batch_count = 0
loss = torch.nn.CrossEntropyLoss()
test_l_sum = 0
pre_label.write("epoch: "+str(epoch) +"\n")
model_label.write("epoch: "+str(epoch) +"\n")
with torch.no_grad():
for X, y in data_iter:
net.eval()
y_hat = net(X.to(device))
y = y.to(device)
l = loss(y_hat, y)
y_pred = y_hat.argmax(dim=1).cpu().numpy()
y_real = y.cpu().numpy() # if y = , the original y will be covered, so change into another name
for item in y_pred:
pre_label.write(item.astype(str) +"\n") #.astype(str) is necessary
# model_label.write(y_real.astype(str) +"\n")
for item in y_real:
# print(item)
model_label.write(item.astype(str) +"\n") #.astype(str) is necessary
# pre_label.close() # have some error
# print(y_pred)
# print(y_real)
# print('-'*10)
acc_sum += (net(X.to(device)).argmax(dim=1) == y.to(device)).float().sum().cpu().item()
n += y.shape[0]
batch_count += 1
test_l_sum += l.cpu().item() #
test_loss = test_l_sum/batch_count
return (acc_sum /n, test_loss)
def train_ch5(train_iter, test_iter, batch_size, device, lr, w_d, num_epochs):
global net
train_ls = []
test_ls = []
########## find the best epoch ##########
# Best_Dice = 0
# Best_epoch=0
# avg_dice = []
###########################################
#net = net.to(device)
#print("training on ", device)
loss = torch.nn.CrossEntropyLoss()
for epoch in range(num_epochs):
optimizer = torch.optim.Adam(net.parameters(), lr=lr, weight_decay = w_d)
net.train()
train_l_sum, train_acc_sum, n, batch_count, start = 0.0, 0.0, 0, 0, time.time()
for X, y in train_iter:
X = X.to(device)
y = y.to(device)
y_hat = net(X)
l = loss(y_hat, y)
optimizer.zero_grad()
l.backward()
optimizer.step()
train_l_sum += l.cpu().item() #
train_acc_sum += (y_hat.argmax(dim=1) == y).sum().cpu().item()
n += y.shape[0]
batch_count += 1 #
test_acc, test_l = evaluate_accuracy(test_iter,device, epoch)
train_loss = train_l_sum / batch_count #
train_ls.append(train_loss)
test_ls.append(test_l)
print('epoch %d, train_loss %.4f, test_loss %.4f, overfit %.4f,train acc %.3f, test acc %.3f, time %.1f sec'
% (epoch + 1, train_loss, test_l, test_l-train_loss, train_acc_sum / n, test_acc, time.time() - start))
########## find the best epoch ##########
# if np.mean(avg_dice) > Best_Dice:
# Best_Dice = np.mean(avg_dice)
# Best_epoch = epochs
#3############################################
PATH = '/media/mmlab/dataset/mengya/StentDetectionDataset/Dataset/D8/checkpoint/'
if not os.path.exists(PATH):
os.makedirs(PATH)
# 'ckpt_dir': '/media/mmlab/data/mengya/miccai_2018_SurgicalScene/trained_model'
# torch.save(net.state_dict(), PATH)
snapshot_name = 'epoch_' + str(epoch)
torch.save(net.state_dict(), os.path.join(PATH, snapshot_name + '.pt'))
semilogy(range(1, num_epochs + 1), train_ls, 'epochs', 'loss', range(1, num_epochs + 1), test_ls, ['train', 'test'])
# lr = 0.00005
# w_d = 0.05
learning_rate = 0.00005
weight_decay = 0.4
num_epochs = 150
train_ch5(train_iter, test_iter, batch_size, device, learning_rate, weight_decay, num_epochs)