-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_v2.py
371 lines (281 loc) · 13.1 KB
/
main_v2.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
# 改一个 mul-class的unet出来 先看看有没有用
# 尝试对 image, output 加入噪声 和 旋转 剪切什么的
# 先尝试用CE+DICE
# 尝试loss func 最好改一个mul-class的focal loss出来
import os
import numpy as np
import time
import datetime
import torch
import torchvision
from torch import optim
from torch.autograd import Variable
import torch.nn.functional as F
from metrics import *
from models.vnet import VNet #, R2U_Net, AttU_Net
#from models.u_net import U_Net
from losses.dice import DiceLoss, expand_as_one_hot
from losses.focal import FocalLoss
import csv
from tqdm import tqdm
import torch.nn as nn
from torch.utils.data import DataLoader
import argparse
from set.ds3d import HaN_OAR as Probset3d
from torch.backends import cudnn
import random
import warnings
warnings.filterwarnings("ignore")
class Solver(object):
def __init__(self, args, train_loader, val_loader, test_loader):
# data loader
self.train_loader = train_loader
self.val_loader = val_loader
self.test_loader = test_loader
# models
self.net = None
self.optimizer = None
self.criterion = FocalLoss(alpha=0.8,gamma=0.5) # torch.nn.BCELoss()
self.augmentation_prob = args.augmentation_prob
# hyper-param
self.lr = args.lr
self.decayed_lr = args.lr
self.beta1 = args.beta1
self.beta2 = args.beta2
# training settings
self.num_epochs = args.num_epochs
self.num_epochs_decay = args.num_epochs_decay
self.batch_size = args.batch_size
# step size for logging and val
self.log_step = args.log_step
self.val_step = args.val_step
# path
self.best_score = 0
self.best_epoch = 0
self.model_path = args.model_path
self.csv_path = args.result_path
self.model_type = args.model_type
self.comment = args.comment
self.net_path = os.path.join(
self.model_path, '%s-%d-%.7f-%d-%.4f-%s.pkl' %
(self.model_type,self.num_epochs,self.lr,self.num_epochs_decay,self.augmentation_prob,self.comment)
)
########### TO DO multi GPU setting ##########
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
self.build_model()
def build_model(self):
if self.model_type == 'VNet':
###### to do ########
self.net = VNet()
self.optimizer = optim.Adam(self.net.parameters(), self.lr, [self.beta1, self.beta2])
self.net.to(self.device)
#self.print_network(self.net, self.model_type)
def print_network(self, model, name):
num_params = 0
for p in model.parameters():
num_params += p.numel() # numel() return total num of elems in tensor
print(model)
print(name)
print('the number of parameters: {}'.format(num_params))
# =============================== train =========================#
# ===============================================================#
def train(self, epoch):
self.net.train(True)
# Decay learning rate
if (epoch + 1) > (self.num_epochs - self.num_epochs_decay):
self.decayed_lr -= (self.lr / float(self.num_epochs_decay))
for param_group in self.optimizer.param_groups:
param_group['lr'] = self.decayed_lr
print('epoch{}: Decay learning rate to lr: {}.'.format(epoch, self.decayed_lr))
epoch_loss = 0
acc = 0. # Accuracy
SE = 0. # Sensitivity (Recall)
SP = 0. # Specificity
PC = 0. # Precision
F1 = 0. # F1 Score
JS = 0. # Jaccard Similarity
DC = 0. # Dice Coefficient
length = 0
for i, (imgs, gts) in enumerate(tqdm(self.train_loader)):
imgs = imgs.to(self.device)
gts = gts.round().long().to(self.device)
self.optimizer.zero_grad()
outputs = self.net(imgs)
# make sure shapes are the same by flattening them
# weight = torch.tensor([1.,100.,100.,100.,50.,50.,80.,80.,50.,80.,80.,80.,50.,50.,70.,70.,70.,70.,
# 60.,60.,100.,100.,100.,]).to(self.device)
#ce_loss = nn.CrossEntropyLoss(weight=weight,reduction='mean')(outputs, gts.reshape(-1,128,128,128))
dice_loss = DiceLoss(sigmoid_normalization=False)(outputs, expand_as_one_hot(gts.reshape(-1,128,128,128),14))
# bce_loss = torch.nn.BCEWithLogitsLoss()(outputs, gts)
# focal_loss = FocalLoss(alpha=0.8,gamma=0.5)(outputs, gts)
loss = dice_loss
#loss = focal_loss + dice_loss
epoch_loss += loss.item() * imgs.size(0) # because reduction = 'mean'
loss.backward()
self.optimizer.step()
# DC += iou(outputs.detach().cpu().squeeze().argmax(dim=1),gts.detach().cpu(),n_classes=14)*imgs.size(0)
# length += imgs.size(0)
# DC = DC / length
# epoch_loss = epoch_loss/length
# # Print the log info
# print(
# 'Epoch [%d/%d], Loss: %.4f, \n[Training] DC: %.4f' % (
# epoch + 1, self.num_epochs,
# epoch_loss,
# DC))
print('EPOCH{}'.format(epoch))
# =============================== validation ====================#
# ===============================================================#
@torch.no_grad()
def validation(self, epoch):
self.net.eval()
acc = 0. # Accuracy
SE = 0. # Sensit ivity (Recall)
SP = 0. # Specificity
PC = 0. # Precision
F1 = 0. # F1 Score
JS = 0. # Jaccard Similarity
DC = 0. # Dice Coefficient
length = 0
for i, (imgs, gts) in enumerate(self.val_loader):
imgs = imgs.to(self.device)
gts = gts.round().long().to(self.device)
outputs = self.net(imgs)
weight = np.array(
[0., 100., 100., 100., 50., 50., 80., 80., 50., 80., 80., 80., 50., 50., 70., 70., 70., 70.,
60., 60., 100., 100., 100., ])
ious = IoU(gts.detach().cpu().squeeze().numpy().reshape(-1), outputs.detach().cpu().squeeze().argmax(dim=0).numpy().reshape(-1),num_classes=14)*imgs.size(0)
DC += np.array(ious[1:]).mean()
length += imgs.size(0)
DC = DC / length
score = DC
print('[Validation] DC: %.4f' % (
DC))
# save the best net model
if score > self.best_score:
self.best_score = score
self.best_epoch = epoch
print('Best %s model score: %.4f'%(self.model_type, self.best_score))
torch.save(self.net.state_dict(), self.net_path)
# if (1+epoch)%10 == 0 or epoch==0:
# torch.save(self.net.state_dict(), self.net_path+'epoch{}.pkl'.format(epoch))
if (epoch+1)%50 == 0 and epoch!=1:
torch.save(self.net.state_dict(),
'/mnt/HDD/datasets/competitions/vnet/models_for_cls/400-200-dice-epoch{}.pkl'.format(epoch+1))
def test(self):
del self.net
self.build_model()
self.net.load_state_dict(torch.load(self.net_path))
self.net.eval()
DC = 0. # Dice Coefficient
length = 0
for i, (imgs, gts) in enumerate(self.test_loader):
imgs = imgs.to(self.device)
gts = gts.round().long().to(self.device)
outputs = self.net(imgs)
weight = np.array(
[0., 100., 100., 100., 50., 50., 80., 80., 50., 80., 80., 80., 50., 50., 70., 70., 70., 70.,
60., 60., 100., 100., 100., ])
ious = IoU(gts.detach().cpu().squeeze().numpy().reshape(-1),
outputs.detach().cpu().squeeze().argmax(dim=0).numpy().reshape(-1), num_classes=14) * imgs.size(
0)
DC += np.array(ious[1:]).mean()
length += imgs.size(0)
DC = DC / length
score = DC
f = open(os.path.join(self.csv_path, 'result.csv'), 'a', encoding='utf8', newline='')
wr = csv.writer(f)
wr.writerow([self.model_type, DC,
self.lr, self.best_epoch, self.num_epochs,
self.num_epochs_decay, self.augmentation_prob, self.batch_size, self.comment])
f.close()
def train_val_test(self):
################# BUG
# if os.path.isfile(self.net_path):
# #self.net.load_state_dict(torch.load(self.net_path))
# print('saved {} is loaded form: {}'.format(self.model_type, self.net_path))
# else:
for epoch in range(self.num_epochs):
self.train(epoch)
self.validation(epoch)
self.test()
def main(config):
cudnn.benchmark = True
if config.model_type not in ['VNet',]:
print('ERROR!! model_type should be selected in VNet/')
print('Your input for model_type was %s' % config.model_type)
return
# Create directories if not exist
if not os.path.exists(config.model_path):
os.makedirs(config.model_path)
if not os.path.exists(config.result_path):
os.makedirs(config.result_path)
config.result_path = os.path.join(config.result_path, config.model_type)
if not os.path.exists(config.result_path):
os.makedirs(config.result_path)
if config.random_hyperparam_search:
lr = random.random() * 0.0005 + 0.0000005
augmentation_prob = random.random() * 0.7
epoch = random.choice([100, 150, 200, 250])
decay_ratio = random.random() * 0.8
decay_epoch = int(epoch * decay_ratio)
config.augmentation_prob = augmentation_prob
config.num_epochs = epoch
config.lr = lr
config.num_epochs_decay = decay_epoch
print(config)
train_set = Probset3d(config.train_path, foreground=False)
valid_set = Probset3d(config.valid_path,is_train=False, foreground=False)
test_set = Probset3d(config.test_path,is_train=False, foreground=False)
train_loader = DataLoader(train_set, batch_size=config.batch_size,num_workers=config.num_workers)
valid_loader = DataLoader(valid_set, batch_size=config.batch_size,num_workers=config.num_workers)
test_loader = DataLoader(test_set, batch_size=config.batch_size,num_workers=config.num_workers)
# train_loader = get_loader(image_path=config.train_path,
# batch_size=config.batch_size,
# num_workers=config.num_workers,
# is_train=True,
# augmentation_prob=0.)
#
# valid_loader = get_loader(image_path=config.valid_path,
# batch_size=config.batch_size,
# num_workers=config.num_workers,
# is_train=False,
# augmentation_prob=0.)
#
# test_loader = get_loader(image_path=config.test_path,
# batch_size=config.batch_size,
# num_workers=config.num_workers,
# is_train=False,
# augmentation_prob=0.)
solver = Solver(config, train_loader, valid_loader, test_loader)
solver.train_val_test()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
# model hyper-parameters
# training hyper-parameters
parser.add_argument('--num_epochs', type=int, default=400) # random hyperparam search
parser.add_argument('--num_epochs_decay', type=int, default=200) # random hyperparam search
parser.add_argument('--batch_size', type=int, default=1)
parser.add_argument('--num_workers', type=int, default=4)
parser.add_argument('--random-search', dest='random_hyperparam_search', action='store_true')
parser.add_argument('--no-random-search', dest='random_hyperparam_search', action='store_false')
parser.set_defaults(random_hyperparam_search=False)
parser.add_argument('--lr', type=float, default=0.0001) # random hyperparam search
parser.add_argument('--beta1', type=float, default=0.5) # momentum1 in Adam
parser.add_argument('--beta2', type=float, default=0.999) # momentum2 in Adam
parser.add_argument('--augmentation_prob', type=float, default=0.5) # random hyperparam search
parser.add_argument('--log_step', type=int, default=2)
parser.add_argument('--val_step', type=int, default=2)
# misc
data_root = '/mnt/HDD/datasets/competitions/HaN_OAR/'
save_root = '/mnt/HDD/datasets/competitions/vnet/'
parser.add_argument('--model_type', type=str, default='VNet', help='VNet/')
parser.add_argument('--model_path', type=str, default=save_root+'models_for_cls')
parser.add_argument('--train_path', type=str, default=data_root)
parser.add_argument('--valid_path', type=str, default=data_root)
parser.add_argument('--test_path', type=str, default=data_root)
parser.add_argument('--result_path', type=str, default=save_root+'result_for_cls/')
parser.add_argument('--cuda_idx', type=int, default=1)
parser.add_argument('--comment', type=str, default='ce-400-200-vnet-dice')
config = parser.parse_args()
main(config)