-
Notifications
You must be signed in to change notification settings - Fork 3
/
train_ACDC.py
240 lines (201 loc) · 9.76 KB
/
train_ACDC.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
import os
import logging
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.nn.modules.loss import CrossEntropyLoss
import torchvision
from torchvision import transforms
from torch.utils.data import DataLoader
import torch.backends.cudnn as cudnn
import random
import time
import numpy as np
from tqdm import tqdm
from medpy.metric import dc,hd95
from scipy.ndimage import zoom
from utils.utils import powerset
from utils.utils import DiceLoss, calculate_dice_percase, val_single_volume
from utils.dataset_ACDC import ACDCdataset, RandomGenerator
from test_ACDC import inference
from lib.networks import PVT_GCASCADE, MERIT_GCASCADE
parser = argparse.ArgumentParser()
parser.add_argument('--encoder', default='PVT', help='Name of encoder: PVT or MERIT')
parser.add_argument('--skip_aggregation', default='additive', help='Type of skip-aggregation: additive or concatenation')
parser.add_argument("--batch_size", default=12, help="batch size")
parser.add_argument("--lr", default=0.0001, help="learning rate")
parser.add_argument("--max_epochs", default=400)
parser.add_argument("--img_size", default=224)
parser.add_argument("--save_path", default="./model_pth/ACDC")
parser.add_argument("--n_gpu", default=1)
parser.add_argument("--checkpoint", default=None)
parser.add_argument("--list_dir", default="./data/ACDC/lists_ACDC")
parser.add_argument("--root_dir", default="./data/ACDC/")
parser.add_argument("--volume_path", default="./data/ACDC/test")
parser.add_argument("--z_spacing", default=10)
parser.add_argument("--num_classes", default=4)
parser.add_argument('--test_save_dir', default='./predictions', help='saving prediction as nii!')
parser.add_argument('--deterministic', type=int, default=1,
help='whether use deterministic training')
parser.add_argument('--seed', type=int,
default=2222, help='random seed')
args = parser.parse_args()
if not args.deterministic:
cudnn.benchmark = True
cudnn.deterministic = False
else:
cudnn.benchmark = False
cudnn.deterministic = True
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
args.is_pretrain = True
args.exp = 'PVT_GCASCADE_MUTATION_w3_7_Run1_' + str(args.img_size)
snapshot_path = "{}/{}/{}".format(args.save_path, args.exp, 'PVT_GCASCADE_MUTATION_w3_7_Run1')
snapshot_path = snapshot_path + '_pretrain' if args.is_pretrain else snapshot_path
snapshot_path = snapshot_path + '_epo' +str(args.max_epochs) if args.max_epochs != 30 else snapshot_path
snapshot_path = snapshot_path+'_bs'+str(args.batch_size)
snapshot_path = snapshot_path + '_lr' + str(args.lr) if args.lr != 0.01 else snapshot_path
snapshot_path = snapshot_path + '_'+str(args.img_size)
snapshot_path = snapshot_path + '_s'+str(args.seed) if args.seed!=1234 else snapshot_path
#current_time = time.strftime("%H%M%S")
#print("The current time is", current_time)
#snapshot_path = snapshot_path +'_t'+current_time
if not os.path.exists(snapshot_path):
os.makedirs(snapshot_path)
args.test_save_dir = os.path.join(snapshot_path, args.test_save_dir)
test_save_path = os.path.join(args.test_save_dir, args.exp)
if not os.path.exists(test_save_path):
os.makedirs(test_save_path, exist_ok=True)
if args.encoder=='PVT':
net = PVT_GCASCADE(n_class=args.num_classes, img_size=args.img_size, k=11, padding=5, conv='mr', gcb_act='gelu', skip_aggregation=args.skip_aggregation).cuda()
elif args.encoder=='MERIT':
net = MERIT_GCASCADE(n_class=args.num_classes, img_size_s1=(args.img_size,args.img_size), img_size_s2=(224,224), k=11, padding=5, conv='mr', gcb_act='gelu', skip_aggregation=args.skip_aggregation).cuda()
else:
print('Implementation not found for this encoder. Exiting!')
sys.exit()
if args.checkpoint:
net.load_state_dict(torch.load(args.checkpoint))
train_dataset = ACDCdataset(args.root_dir, args.list_dir, split="train", transform=
transforms.Compose(
[RandomGenerator(output_size=[args.img_size, args.img_size])]))
print("The length of train set is: {}".format(len(train_dataset)))
Train_loader = DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True)
db_val=ACDCdataset(base_dir=args.root_dir, list_dir=args.list_dir, split="valid")
valloader=DataLoader(db_val, batch_size=1, shuffle=False)
db_test =ACDCdataset(base_dir=args.volume_path,list_dir=args.list_dir, split="test")
testloader = DataLoader(db_test, batch_size=1, shuffle=False)
if args.n_gpu > 1:
net = nn.DataParallel(net)
net = net.cuda()
net.train()
ce_loss = CrossEntropyLoss()
dice_loss = DiceLoss(args.num_classes)
save_interval = args.n_skip
iterator = tqdm(range(0, args.max_epochs), ncols=70)
iter_num = 0
Loss = []
Test_Accuracy = []
Best_dcs = 0.80
Best_test_dcs = 0.80
logging.basicConfig(filename=snapshot_path + "/log.txt", level=logging.INFO,
format='[%(asctime)s.%(msecs)03d] %(message)s', datefmt='%H:%M:%S')
max_iterations = args.max_epochs * len(Train_loader)
base_lr = args.lr
optimizer = optim.AdamW(net.parameters(), lr=base_lr, weight_decay=0.0001)
def val():
logging.info("Validation ===>")
dc_sum=0
metric_list = 0.0
net.eval()
for i, val_sampled_batch in enumerate(valloader):
val_image_batch, val_label_batch = val_sampled_batch["image"], val_sampled_batch["label"]
val_image_batch, val_label_batch = val_image_batch.squeeze(0).cpu().detach().numpy(), val_label_batch.squeeze(0).cpu().detach().numpy()
x, y = val_image_batch.shape[0], val_image_batch.shape[1]
if x != args.img_size or y != args.img_size:
val_image_batch = zoom(val_image_batch, (args.img_size / x, args.img_size / y), order=3)
val_image_batch = torch.from_numpy(val_image_batch).unsqueeze(0).unsqueeze(0).float().cuda()
P = net(val_image_batch)
val_outputs = 0.0
for idx in range(len(P)):
val_outputs += P[idx]
val_outputs = torch.softmax(val_outputs, dim=1)
val_outputs = torch.argmax(val_outputs, dim=1).squeeze(0)
val_outputs = val_outputs.cpu().detach().numpy()
if x != args.img_size or y != args.img_size:
val_outputs = zoom(val_outputs, (x / args.img_size, y / args.img_size), order=0)
else:
val_outputs = val_outputs
dc_sum+=dc(val_outputs,val_label_batch[:])
performance = dc_sum / len(valloader)
logging.info('Testing performance in val model: mean_dice : %f, best_dice : %f' % (performance, Best_dcs))
print('Testing performance in val model: mean_dice : %f, best_dice : %f' % (performance, Best_dcs))
return performance
l = [0, 1, 2, 3]
ss = [x for x in powerset(l)]
#ss = [[0],[1],[2],[3]]
print(ss)
for epoch in iterator:
net.train()
train_loss = 0
for i_batch, sampled_batch in enumerate(Train_loader):
image_batch, label_batch = sampled_batch["image"], sampled_batch["label"]
image_batch, label_batch = image_batch.type(torch.FloatTensor), label_batch.type(torch.FloatTensor)
image_batch, label_batch = image_batch.cuda(), label_batch.cuda()
P = net(image_batch)
loss = 0.0
lc1, lc2 = 0.3, 0.7
for s in ss:
iout = 0.0
if(s==[]):
continue
for idx in range(len(s)):
iout += P[s[idx]]
loss_ce = ce_loss(iout, label_batch[:].long())
loss_dice = dice_loss(iout, label_batch, softmax=True)
loss += (lc1 * loss_ce + lc2 * loss_dice)
optimizer.zero_grad()
loss.backward()
optimizer.step()
#lr_ = base_lr * (1.0 - iter_num / max_iterations) ** 0.9 # We did not use this
lr_ = base_lr
for param_group in optimizer.param_groups:
param_group['lr'] = lr_
iter_num = iter_num + 1
if iter_num%50 == 0:
logging.info('iteration %d : loss : %f lr_: %f' % (iter_num, loss.item(), lr_))
print('iteration %d : loss : %f lr_: %f' % (iter_num, loss.item(), lr_))
train_loss += loss.item()
Loss.append(train_loss/len(train_dataset))
logging.info('iteration %d : loss : %f lr_: %f' % (iter_num, loss.item(), lr_))
print('iteration %d : loss : %f lr_: %f' % (iter_num, loss.item(), lr_))
save_model_path = os.path.join(snapshot_path, 'last.pth')
torch.save(net.state_dict(), save_model_path)
#logging.info("save model to {}".format(save_model_path))
avg_dcs = val()
if avg_dcs >= Best_dcs:
save_model_path = os.path.join(snapshot_path, 'best.pth')
torch.save(net.state_dict(), save_model_path)
logging.info("save model to {}".format(save_model_path))
print("save model to {}".format(save_model_path))
Best_dcs = avg_dcs
avg_test_dcs, avg_hd, avg_jacard, avg_asd = inference(args, net, testloader, args.test_save_dir)
print("test avg_dsc: %f" % (avg_test_dcs))
logging.info("test avg_dsc: %f" % (avg_test_dcs))
Test_Accuracy.append(avg_test_dcs)
if(Best_test_dcs <= avg_test_dcs):
Best_test_dcs = avg_test_dcs
save_model_path = os.path.join(snapshot_path, 'test_best.pth')
torch.save(net.state_dict(), save_model_path)
logging.info("save model to {}".format(save_model_path))
print("save model to {}".format(save_model_path))
if epoch >= args.max_epochs - 1:
save_model_path = os.path.join(snapshot_path, 'epoch={}_lr={}_avg_dcs={}.pth'.format(epoch, lr_, avg_dcs))
torch.save(net.state_dict(), save_model_path)
logging.info("save model to {}".format(save_model_path))
print("save model to {}".format(save_model_path))
iterator.close()
break