-
Notifications
You must be signed in to change notification settings - Fork 49
/
train_model.py
192 lines (172 loc) · 8.06 KB
/
train_model.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
# -*- coding: utf-8 -*-
# @Time : 2021/7/8 8:59 上午
# @Author : Haonan Wang
# @File : train.py
# @Software: PyCharm
import torch.optim
from tensorboardX import SummaryWriter
import os
import numpy as np
import random
from torch.backends import cudnn
from Load_Dataset import RandomGenerator,ValGenerator,ImageToImage2D
from nets.UCTransNet import UCTransNet
from torch.utils.data import DataLoader
import logging
from Train_one_epoch import train_one_epoch
import Config as config
from torchvision import transforms
from utils import CosineAnnealingWarmRestarts, WeightedDiceBCE
def logger_config(log_path):
loggerr = logging.getLogger()
loggerr.setLevel(level=logging.INFO)
handler = logging.FileHandler(log_path, encoding='UTF-8')
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(message)s')
handler.setFormatter(formatter)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
loggerr.addHandler(handler)
loggerr.addHandler(console)
return loggerr
def save_checkpoint(state, save_path):
'''
Save the current model.
If the model is the best model since beginning of the training
it will be copy
'''
logger.info('\t Saving to {}'.format(save_path))
if not os.path.isdir(save_path):
os.makedirs(save_path)
epoch = state['epoch'] # epoch no
best_model = state['best_model'] # bool
model = state['model'] # model type
if best_model:
filename = save_path + '/' + \
'best_model-{}.pth.tar'.format(model)
else:
filename = save_path + '/' + \
'model-{}-{:02d}.pth.tar'.format(model, epoch)
torch.save(state, filename)
def worker_init_fn(worker_id):
random.seed(config.seed + worker_id)
##################################################################################
#=================================================================================
# Main Loop: load model,
#=================================================================================
##################################################################################
def main_loop(batch_size=config.batch_size, model_type='', tensorboard=True):
# Load train and val data
train_tf= transforms.Compose([RandomGenerator(output_size=[config.img_size, config.img_size])])
val_tf = ValGenerator(output_size=[config.img_size, config.img_size])
train_dataset = ImageToImage2D(config.train_dataset, train_tf,image_size=config.img_size)
val_dataset = ImageToImage2D(config.val_dataset, val_tf,image_size=config.img_size)
train_loader = DataLoader(train_dataset,
batch_size=config.batch_size,
shuffle=True,
worker_init_fn=worker_init_fn,
num_workers=8,
pin_memory=True)
val_loader = DataLoader(val_dataset,
batch_size=config.batch_size,
shuffle=True,
worker_init_fn=worker_init_fn,
num_workers=8,
pin_memory=True)
lr = config.learning_rate
logger.info(model_type)
if model_type == 'UCTransNet':
config_vit = config.get_CTranS_config()
logger.info('transformer head num: {}'.format(config_vit.transformer.num_heads))
logger.info('transformer layers num: {}'.format(config_vit.transformer.num_layers))
logger.info('transformer expand ratio: {}'.format(config_vit.expand_ratio))
model = UCTransNet(config_vit,n_channels=config.n_channels,n_classes=config.n_labels)
elif model_type == 'UCTransNet_pretrain':
config_vit = config.get_CTranS_config()
logger.info('transformer head num: {}'.format(config_vit.transformer.num_heads))
logger.info('transformer layers num: {}'.format(config_vit.transformer.num_layers))
logger.info('transformer expand ratio: {}'.format(config_vit.expand_ratio))
model = UCTransNet(config_vit,n_channels=config.n_channels,n_classes=config.n_labels)
pretrained_UNet_model_path = "./nets/best_model-UNet.pth.tar"
pretrained_UNet = torch.load(pretrained_UNet_model_path, map_location='cuda')
pretrained_UNet = pretrained_UNet['state_dict']
model2_dict = model.state_dict()
state_dict = {k:v for k,v in pretrained_UNet.items() if k in model2_dict.keys()}
print(state_dict.keys())
model2_dict.update(state_dict)
model.load_state_dict(model2_dict)
logger.info('Load successful!')
else: raise TypeError('Please enter a valid name for the model type')
model = model.cuda()
# if torch.cuda.device_count() > 1:
# print ("Let's use {0} GPUs!".format(torch.cuda.device_count()))
# model = nn.DataParallel(model, device_ids=[0])
criterion = WeightedDiceBCE(dice_weight=0.5,BCE_weight=0.5)
optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=lr) # Choose optimize
if config.cosineLR is True:
lr_scheduler = CosineAnnealingWarmRestarts(optimizer, T_0=10, T_mult=1, eta_min=1e-4)
else:
lr_scheduler = None
if tensorboard:
log_dir = config.tensorboard_folder
logger.info('log dir: '.format(log_dir))
if not os.path.isdir(log_dir):
os.makedirs(log_dir)
writer = SummaryWriter(log_dir)
else:
writer = None
max_dice = 0.0
best_epoch = 1
for epoch in range(config.epochs): # loop over the dataset multiple times
logger.info('\n========= Epoch [{}/{}] ========='.format(epoch + 1, config.epochs + 1))
logger.info(config.session_name)
# train for one epoch
model.train(True)
logger.info('Training with batch size : {}'.format(batch_size))
train_one_epoch(train_loader, model, criterion, optimizer, writer, epoch, None, model_type, logger)
# evaluate on validation set
logger.info('Validation')
with torch.no_grad():
model.eval()
val_loss, val_dice = train_one_epoch(val_loader, model, criterion,
optimizer, writer, epoch, lr_scheduler,model_type,logger)
# =============================================================
# Save best model
# =============================================================
if val_dice > max_dice:
if epoch+1 > 5:
logger.info('\t Saving best model, mean dice increased from: {:.4f} to {:.4f}'.format(max_dice,val_dice))
max_dice = val_dice
best_epoch = epoch + 1
save_checkpoint({'epoch': epoch,
'best_model': True,
'model': model_type,
'state_dict': model.state_dict(),
'val_loss': val_loss,
'optimizer': optimizer.state_dict()}, config.model_path)
else:
logger.info('\t Mean dice:{:.4f} does not increase, '
'the best is still: {:.4f} in epoch {}'.format(val_dice,max_dice, best_epoch))
early_stopping_count = epoch - best_epoch + 1
logger.info('\t early_stopping_count: {}/{}'.format(early_stopping_count,config.early_stopping_patience))
if early_stopping_count > config.early_stopping_patience:
logger.info('\t early_stopping!')
break
return model
if __name__ == '__main__':
deterministic = True
if not deterministic:
cudnn.benchmark = True
cudnn.deterministic = False
else:
cudnn.benchmark = False
cudnn.deterministic = True
random.seed(config.seed)
np.random.seed(config.seed)
torch.manual_seed(config.seed)
torch.cuda.manual_seed(config.seed)
torch.cuda.manual_seed_all(config.seed)
if not os.path.isdir(config.save_path):
os.makedirs(config.save_path)
logger = logger_config(log_path=config.logger_path)
model = main_loop(model_type=config.model_name, tensorboard=True)