-
Notifications
You must be signed in to change notification settings - Fork 3
/
trainer.py
234 lines (195 loc) · 9.72 KB
/
trainer.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
import argparse
import logging
import os
import random
import sys
import time
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from tensorboardX import SummaryWriter
from torch.nn.modules.loss import CrossEntropyLoss
from torch.utils.data import DataLoader
from tqdm import tqdm
from utils import DiceLoss, BoundaryDoULoss, JaccardLoss
from torchvision import transforms
from utils import test_single_volume
from torch.nn import functional as F
from datasets.datasets_synapse import Synapse_dataset, RandomGenerator
import matplotlib.pyplot as plt
import pandas as pd
import datetime
def inference(model, testloader, args, test_save_path=None):
model.eval()
metric_list = 0.0
for i_batch, sampled_batch in tqdm(enumerate(testloader)):
h, w = sampled_batch["image"].size()[2:]
image, label, case_name = sampled_batch["image"], sampled_batch["label"], sampled_batch['case_name'][0]
metric_i = test_single_volume(image, label, model, classes=args.num_classes, patch_size=[args.img_size, args.img_size],
test_save_path=test_save_path, case=case_name, z_spacing=args.z_spacing)
metric_list += np.array(metric_i)
logging.info(' idx %d case %s mean_dice %f mean_hd95 %f' % (i_batch, case_name, np.mean(metric_i, axis=0)[0], np.mean(metric_i, axis=0)[1]))
metric_list = metric_list / len(testloader.dataset)
for i in range(1, args.num_classes):
logging.info('Mean class %d mean_dice %f mean_hd95 %f' % (i, metric_list[i-1][0], metric_list[i-1][1]))
performance = np.mean(metric_list, axis=0)[0]
mean_hd95 = np.mean(metric_list, axis=0)[1]
logging.info('Testing performance in best val model: mean_dice : %f mean_hd95 : %f' % (performance, mean_hd95))
return performance, mean_hd95
def plot_result(dice, h, snapshot_path,args):
dict = {'mean_dice': dice, 'mean_hd95': h}
df = pd.DataFrame(dict)
plt.figure(0)
df['mean_dice'].plot()
resolution_value = 1200
plt.title('Mean Dice')
date_and_time = datetime.datetime.now()
filename = f'{args.model_name}_' + str(date_and_time)+'dice'+'.png'
save_mode_path = os.path.join(snapshot_path, filename)
plt.savefig(save_mode_path, format="png", dpi=resolution_value)
plt.figure(1)
df['mean_hd95'].plot()
plt.title('Mean hd95')
filename = f'{args.model_name}_' + str(date_and_time)+'hd95'+'.png'
save_mode_path = os.path.join(snapshot_path, filename)
#save csv
filename = f'{args.model_name}_' + str(date_and_time)+'results'+'.csv'
save_mode_path = os.path.join(snapshot_path, filename)
df.to_csv(save_mode_path, sep='\t')
def trainer_synapse(args, model, snapshot_path):
os.makedirs(os.path.join(snapshot_path, 'test'), exist_ok=True)
test_save_path = os.path.join(snapshot_path, 'test')
log_filename = f'{snapshot_path}' + '/log_' + f'{args.model_name}' + '.txt'
logging.basicConfig(filename=log_filename, level=logging.INFO,
format='[%(asctime)s.%(msecs)03d] %(message)s', datefmt='%H:%M:%S')
logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
logging.info(str(args))
base_lr = args.base_lr
num_classes = args.num_classes
batch_size = args.batch_size * args.n_gpu
# max_iterations = args.max_iterations
x_transforms = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.5], [0.5])
])
y_transforms = transforms.ToTensor()
db_train = Synapse_dataset(base_dir=args.root_path, list_dir=args.list_dir, split="train",img_size=args.img_size,
norm_x_transform = x_transforms, norm_y_transform = y_transforms)
print("The length of train set is: {}".format(len(db_train)))
def worker_init_fn(worker_id):
random.seed(args.seed + worker_id)
trainloader = DataLoader(db_train, batch_size=batch_size, shuffle=True, num_workers=args.num_workers, pin_memory=True,
worker_init_fn=worker_init_fn)
db_test = Synapse_dataset(base_dir=args.test_path, split="test_vol", list_dir=args.list_dir, img_size=args.img_size)
testloader = DataLoader(db_test, batch_size=1, shuffle=False, num_workers=1)
if args.n_gpu > 1:
model = nn.DataParallel(model)
model.train()
jc_loss = JaccardLoss()
ce_loss = CrossEntropyLoss()
dice_loss = DiceLoss(num_classes)
boundary_loss = BoundaryDoULoss(num_classes)
optimizer = optim.SGD(model.parameters(), lr=base_lr, momentum=0.9, weight_decay=0.0001)
#optimizer = optim.AdamW(model.parameters(),lr=base_lr, weight_decay=0.0001)
writer = SummaryWriter(snapshot_path + '/log')
iter_num = 0
max_epoch = args.max_epochs
max_iterations = args.max_epochs * len(trainloader) # max_epoch = max_iterations // len(trainloader) + 1
logging.info("{} iterations per epoch. {} max iterations ".format(len(trainloader), max_iterations))
best_performance = 0.0
iterator = tqdm(range(max_epoch), ncols=70)
dice_=[]
hd95_= []
acc_loss = 0.0
acc_loss_ce = 0.0
acc_loss_dc = 0.0
acc_loss_bo = 0.0
acc_loss_jc = 0.0
for epoch_num in iterator:
for i_batch, sampled_batch in enumerate(trainloader):
image_batch, label_batch = sampled_batch['image'], sampled_batch['label']
# print("data shape---------", image_batch.shape, label_batch.shape)
image_batch, label_batch = image_batch.cuda(), label_batch.squeeze(1).cuda()
outputs = model(image_batch)
# outputs = F.interpolate(outputs, size=label_batch.shape[1:], mode='bilinear', align_corners=False)
#loss_ce = ce_loss(outputs, label_batch[:].long())
#loss_dice = dice_loss(outputs, label_batch, softmax=True)
#loss_jacard = jc_loss(outputs, label_batch)
loss_boundary = boundary_loss(outputs, label_batch[:])
#loss = 0.4 * loss_ce + 0.6 * loss_dice
loss2 = loss_boundary
#loss3 = 0.5 * loss_ce + 0.5 * loss_jacard
# print("loss-----------", loss)
optimizer.zero_grad()
#loss.backward()
loss2.backward()
#loss3.backward()
optimizer.step()
lr_ = base_lr * (1.0 - iter_num / max_iterations) ** 0.9
for param_group in optimizer.param_groups:
param_group['lr'] = lr_
iter_num = iter_num + 1
writer.add_scalar('info/lr', lr_, iter_num)
writer.add_scalar('info/boundary_loss', loss2, iter_num)
#writer.add_scalar('info/jaccard_loss', loss3, iter_num)
#writer.add_scalar('info/total_loss', loss, iter_num)
#writer.add_scalar('info/loss_ce', loss_ce, iter_num)
#writer.add_scalar('info/loss_dice', loss_dice, iter_num)
#acc_loss += loss.item()
#acc_loss_dc += loss_dice.item()
#acc_loss_ce += loss_ce.item()
acc_loss_bo += loss2.item()
#acc_loss_jc += loss3.item()
if iter_num % 100 == 0:
#acc_loss = acc_loss / 100
#acc_loss_ce = acc_loss_ce / 100
#acc_loss_dc = acc_loss_dc / 100
acc_loss_bo = acc_loss_bo / 100
#acc_loss_jc = acc_loss_jc / 100
logging.info('iteration %d : loss_boundary: %f' % (iter_num,acc_loss_bo))
#acc_loss = 0.0
#acc_loss_ce = 0.0
#acc_loss_dc = 0.0
acc_loss_bo = 0.0
#acc_loss_jc = 0.0
if iter_num % 100 == 0:
image = image_batch[1, 0:1, :, :]
image = (image - image.min()) / (image.max() - image.min())
writer.add_image('train/Image', image, iter_num)
outputs = torch.argmax(torch.softmax(outputs, dim=1), dim=1, keepdim=True)
writer.add_image('train/Prediction', outputs[1, ...] * 50, iter_num)
labs = label_batch[1, ...].unsqueeze(0) * 50
writer.add_image('train/GroundTruth', labs, iter_num)
# Test
eval_interval = args.eval_interval
if epoch_num >= int(max_epoch / 2) and (epoch_num + 1) % eval_interval == 0:
filename = f'{args.model_name}_seed_{args.seed}_epoch_{epoch_num}.pth'
save_mode_path = os.path.join(snapshot_path, filename)
torch.save(model.state_dict(), save_mode_path)
logging.info("save model to {}".format(save_mode_path))
logging.info("*" * 20)
logging.info(f"Running Inference after epoch {epoch_num}")
print(f"Epoch {epoch_num}")
mean_dice, mean_hd95 = inference(model, testloader, args, test_save_path=test_save_path)
dice_.append(mean_dice)
hd95_.append(mean_hd95)
model.train()
if epoch_num >= max_epoch - 1:
filename = f'{args.model_name}_seed_{args.seed}_epoch_{epoch_num}.pth'
save_mode_path = os.path.join(snapshot_path, filename)
torch.save(model.state_dict(), save_mode_path)
logging.info("save model to {}".format(save_mode_path))
if not (epoch_num + 1) % args.eval_interval == 0:
logging.info("*" * 20)
logging.info(f"Running Inference after epoch {epoch_num} (Last Epoch)")
print(f"Epoch {epoch_num}, Last Epcoh")
mean_dice, mean_hd95 = inference(model, testloader, args, test_save_path=test_save_path)
dice_.append(mean_dice)
hd95_.append(mean_hd95)
model.train()
iterator.close()
break
plot_result(dice_, hd95_, snapshot_path, args)
writer.close()
return "Training Finished!"