-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_stacked3d.py
164 lines (128 loc) · 6.33 KB
/
train_stacked3d.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
from model3d.stacked_unet3d import StackedUnet3D
from src.utils import *
from data_loader.brats15_3d import Brats15DataLoader
from torch.utils.data import DataLoader
from torch.autograd import Variable
import sys
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
# ********** Hyper Parameter **********
data_dir = '/home/haoyum/download/BRATS2015_Training'
conf_train = 'config/train15.conf'
conf_test = 'config/test15.conf'
learning_rate = 0.001
batch_size = 4
epochs = 200
save_dir = 'ckpt_'
device_ids = [0, 1, 2, 3] # multi-GPU
cuda_available = torch.cuda.is_available()
model = 'sunet'
if not os.path.exists(save_dir + model + '/'):
os.mkdir(save_dir + model + '/')
# ******************** build model ********************
net = StackedUnet3D(in_ch=1, out_ch=2, degree=16)
if cuda_available:
net = net.cuda()
net = nn.DataParallel(net, device_ids=device_ids)
# ******************** log file ********************
log_train = open(model + '_log_train.txt', 'w')
log_test = open(model + '_log_test.txt', 'w')
log_train_dice = open(model + '_log_train_dice.txt', 'w')
log_test_dice = open(model + '_log_test_dice.txt', 'w')
# ******************** data preparation ********************
print 'train data ....'
train_data = Brats15DataLoader(data_dir=data_dir, task_type='wt', conf=conf_train, is_train=True)
print 'test data .....'
test_data = Brats15DataLoader(data_dir=data_dir, task_type='wt', conf=conf_test, is_train=False)
# data loader
train_dataset = DataLoader(dataset=train_data, batch_size=batch_size, shuffle=True)
test_dataset = DataLoader(dataset=test_data, batch_size=batch_size, shuffle=True)
def run():
optimizer = optim.Adam(params=net.parameters(), lr=learning_rate, betas=(0.9, 0.999))
criterion = nn.CrossEntropyLoss()
best_dice = -1
best_epoch = -1
for epoch in range(1, epochs + 1):
print ('epoch....................................' + str(epoch))
train_loss = []
test_loss = []
train_dice = []
test_dice = []
# *************** train model ***************
print 'train ....'
net.train()
for step, (images_vol, labels_vol, subject) in enumerate(train_dataset):
for i in range(len(images_vol)): # 144/16 = 9
images = Variable(images_vol[i].cuda() if cuda_available else images_vol[i])
# 5D tensor Batch_Size * 4(modal) * 16 * 192 * 192
labels = Variable(labels_vol[i].cuda() if cuda_available else labels_vol[i])
# 5D tensor Batch_Size * 1 * 16 * 192 * 192
optimizer.zero_grad()
predicts = net(images)
# 6D tensor Batch_Size * 4 * 2 * 16(volume_size) * height * weight
loss_train = 0
for k in range(4):
loss_train += criterion(predicts[:, i, :, :, :, :], labels[:, 0, :, :, :].long())
train_loss.append(float(loss_train))
loss_train.backward()
optimizer.step()
predicts = F.softmax(predicts[:, 3, :, :, :, :], dim=1)
# 5D float Tensor Batch_Size * 2 * 16(volume_size) * height * weight
predicts = (predicts[:, 1, :, :, :] > 0.5).long()
# 4D Long Tensor Batch_Size * 16(volume_size) * height * weight
d = dice(predicts, labels[:, 0, :, :, :].long())
train_dice.append(d)
# ****** save image of step 0 for each epoch ******
if step == 0:
save_train_slice(images, predicts, labels[:, 0, :, :, :], epoch, save_dir=save_dir + model + '/')
# ***************** calculate test loss *****************
print 'test ....'
net.eval()
for step, (images_vol, labels_vol, subject) in enumerate(test_dataset):
for i in range(len(images_vol)): # 144/16 = 9
images = Variable(images_vol[i].cuda() if cuda_available else images_vol[i])
# 5D tensor Batch_Size * 4(modal) * 16 * 192 * 192
labels = Variable(labels_vol[i].cuda() if cuda_available else labels_vol[i])
# 5D tensor Batch_Size * 1 * 16 * 192 * 192
predicts = net(images)
# # 6D tensor Batch_Size * 4 * 2 * 16(volume_size) * height * weight
loss_test = 0
for k in range(4):
loss_test += criterion(predicts[:, i, :, :, :, :], labels[:, 0, :, :, :].long())
test_loss.append(float(loss_test))
predicts = F.softmax(predicts[:, 3, :, :, :, :], dim=1)
# 5D float Tensor Batch_Size * 2 * 16(volume_size) * height * weight
predicts = (predicts[:, 1, :, :, :] > 0.5).long()
# 4D Long Tensor Batch_Size * 16(volume_size) * height * weight
d = dice(predicts, labels[:, 0, :, :, :].long())
test_dice.append(d)
# **************** save loss for one batch ****************
print 'train_loss ' + str(sum(train_loss) / (len(train_loss) * 1.0))
print 'test_loss ' + str(sum(test_loss) / (len(test_loss) * 1.0))
print 'train_dice ' + str(sum(train_dice) / (len(train_dice) * 1.0))
print 'test_dice ' + str(sum(test_dice) / (len(test_dice) * 1.0))
log_train.write(str(sum(train_loss)/(len(train_loss) * 1.0)) + '\n')
log_train_dice.write(str(sum(train_dice) / (len(train_dice) * 1.0)) + '\n')
log_test.write(str(sum(test_loss) / (len(test_loss) * 1.0)) + '\n')
log_test_dice.write(str(sum(test_dice) / (len(test_dice) * 1.0)) + '\n')
if sum(test_dice) / (len(test_dice) * 1.0) > best_dice:
best_dice = sum(test_dice) / (len(test_dice) * 1.0)
best_epoch = epoch
# **************** save model ****************
if epoch % 10 == 0:
torch.save(net.state_dict(),
os.path.join(save_dir + model + '/', 'epoch_{:d}.pth'.format(epoch)))
print '***********************************************************'
print 'Best Dice coefficient is '
print best_dice
print 'Best epoch is '
print best_epoch
print '***********************************************************'
print ('done!')
if __name__ == '__main__':
run()
log_train.close()
log_test.close()
log_test_dice.close()