-
Notifications
You must be signed in to change notification settings - Fork 84
/
train.py
283 lines (194 loc) · 9.86 KB
/
train.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
# python3 -m torch.distributed.launch --nproc_per_node=4 --master_port 20003 train_spup3.py
import argparse
import os
import random
import logging
import numpy as np
import time
import setproctitle
import torch
import torch.backends.cudnn as cudnn
import torch.optim
from models.TransBTS.TransBTS_downsample8x_skipconnection import TransBTS
import torch.distributed as dist
from models import criterions
from data.BraTS import BraTS
from torch.utils.data import DataLoader
from utils.tools import all_reduce_tensor
from tensorboardX import SummaryWriter
from torch import nn
local_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
parser = argparse.ArgumentParser()
# Basic Information
parser.add_argument('--user', default='name of user', type=str)
parser.add_argument('--experiment', default='TransBTS', type=str)
parser.add_argument('--date', default=local_time.split(' ')[0], type=str)
parser.add_argument('--description',
default='TransBTS,'
'training on train.txt!',
type=str)
# DataSet Information
parser.add_argument('--root', default='path to training set', type=str)
parser.add_argument('--train_dir', default='Train', type=str)
parser.add_argument('--valid_dir', default='Valid', type=str)
parser.add_argument('--mode', default='train', type=str)
parser.add_argument('--train_file', default='train.txt', type=str)
parser.add_argument('--valid_file', default='valid.txt', type=str)
parser.add_argument('--dataset', default='brats', type=str)
parser.add_argument('--model_name', default='TransBTS', type=str)
parser.add_argument('--input_C', default=4, type=int)
parser.add_argument('--input_H', default=240, type=int)
parser.add_argument('--input_W', default=240, type=int)
parser.add_argument('--input_D', default=160, type=int)
parser.add_argument('--crop_H', default=128, type=int)
parser.add_argument('--crop_W', default=128, type=int)
parser.add_argument('--crop_D', default=128, type=int)
parser.add_argument('--output_D', default=155, type=int)
# Training Information
parser.add_argument('--lr', default=0.0002, type=float)
parser.add_argument('--weight_decay', default=1e-5, type=float)
parser.add_argument('--amsgrad', default=True, type=bool)
parser.add_argument('--criterion', default='softmax_dice', type=str)
parser.add_argument('--num_class', default=4, type=int)
parser.add_argument('--seed', default=1000, type=int)
parser.add_argument('--no_cuda', default=False, type=bool)
parser.add_argument('--gpu', default='0,1,2,3', type=str)
parser.add_argument('--num_workers', default=8, type=int)
parser.add_argument('--batch_size', default=8, type=int)
parser.add_argument('--start_epoch', default=0, type=int)
parser.add_argument('--end_epoch', default=1000, type=int)
parser.add_argument('--save_freq', default=1000, type=int)
parser.add_argument('--resume', default='', type=str)
parser.add_argument('--load', default=True, type=bool)
parser.add_argument('--local_rank', default=0, type=int, help='node rank for distributed training')
args = parser.parse_args()
def main_worker():
if args.local_rank == 0:
log_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'log', args.experiment+args.date)
log_file = log_dir + '.txt'
log_args(log_file)
logging.info('--------------------------------------This is all argsurations----------------------------------')
for arg in vars(args):
logging.info('{}={}'.format(arg, getattr(args, arg)))
logging.info('----------------------------------------This is a halving line----------------------------------')
logging.info('{}'.format(args.description))
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
random.seed(args.seed)
np.random.seed(args.seed)
torch.distributed.init_process_group('nccl')
torch.cuda.set_device(args.local_rank)
_, model = TransBTS(dataset='brats', _conv_repr=True, _pe_type="learned")
model.cuda(args.local_rank)
model = nn.parallel.DistributedDataParallel(model, device_ids=[args.local_rank], output_device=args.local_rank,
find_unused_parameters=True)
model.train()
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay, amsgrad=args.amsgrad)
criterion = getattr(criterions, args.criterion)
if args.local_rank == 0:
checkpoint_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'checkpoint', args.experiment+args.date)
if not os.path.exists(checkpoint_dir):
os.makedirs(checkpoint_dir)
resume = ''
writer = SummaryWriter()
if os.path.isfile(resume) and args.load:
logging.info('loading checkpoint {}'.format(resume))
checkpoint = torch.load(resume, map_location=lambda storage, loc: storage)
model.load_state_dict(checkpoint['state_dict'])
logging.info('Successfully loading checkpoint {} and training from epoch: {}'
.format(args.resume, args.start_epoch))
else:
logging.info('re-training!!!')
train_list = os.path.join(args.root, args.train_dir, args.train_file)
train_root = os.path.join(args.root, args.train_dir)
train_set = BraTS(train_list, train_root, args.mode)
train_sampler = torch.utils.data.distributed.DistributedSampler(train_set)
logging.info('Samples for train = {}'.format(len(train_set)))
num_gpu = (len(args.gpu)+1) // 2
train_loader = DataLoader(dataset=train_set, sampler=train_sampler, batch_size=args.batch_size // num_gpu,
drop_last=True, num_workers=args.num_workers, pin_memory=True)
start_time = time.time()
torch.set_grad_enabled(True)
for epoch in range(args.start_epoch, args.end_epoch):
train_sampler.set_epoch(epoch) # shuffle
setproctitle.setproctitle('{}: {}/{}'.format(args.user, epoch+1, args.end_epoch))
start_epoch = time.time()
for i, data in enumerate(train_loader):
adjust_learning_rate(optimizer, epoch, args.end_epoch, args.lr)
x, target = data
x = x.cuda(args.local_rank, non_blocking=True)
target = target.cuda(args.local_rank, non_blocking=True)
output = model(x)
loss, loss1, loss2, loss3 = criterion(output, target)
reduce_loss = all_reduce_tensor(loss, world_size=num_gpu).data.cpu().numpy()
reduce_loss1 = all_reduce_tensor(loss1, world_size=num_gpu).data.cpu().numpy()
reduce_loss2 = all_reduce_tensor(loss2, world_size=num_gpu).data.cpu().numpy()
reduce_loss3 = all_reduce_tensor(loss3, world_size=num_gpu).data.cpu().numpy()
if args.local_rank == 0:
logging.info('Epoch: {}_Iter:{} loss: {:.5f} || 1:{:.4f} | 2:{:.4f} | 3:{:.4f} ||'
.format(epoch, i, reduce_loss, reduce_loss1, reduce_loss2, reduce_loss3))
optimizer.zero_grad()
loss.backward()
optimizer.step()
end_epoch = time.time()
if args.local_rank == 0:
if (epoch + 1) % int(args.save_freq) == 0 \
or (epoch + 1) % int(args.end_epoch - 1) == 0 \
or (epoch + 1) % int(args.end_epoch - 2) == 0 \
or (epoch + 1) % int(args.end_epoch - 3) == 0:
file_name = os.path.join(checkpoint_dir, 'model_epoch_{}.pth'.format(epoch))
torch.save({
'epoch': epoch,
'state_dict': model.state_dict(),
'optim_dict': optimizer.state_dict(),
},
file_name)
writer.add_scalar('lr:', optimizer.param_groups[0]['lr'], epoch)
writer.add_scalar('loss:', reduce_loss, epoch)
writer.add_scalar('loss1:', reduce_loss1, epoch)
writer.add_scalar('loss2:', reduce_loss2, epoch)
writer.add_scalar('loss3:', reduce_loss3, epoch)
if args.local_rank == 0:
epoch_time_minute = (end_epoch-start_epoch)/60
remaining_time_hour = (args.end_epoch-epoch-1)*epoch_time_minute/60
logging.info('Current epoch time consumption: {:.2f} minutes!'.format(epoch_time_minute))
logging.info('Estimated remaining training time: {:.2f} hours!'.format(remaining_time_hour))
if args.local_rank == 0:
writer.close()
final_name = os.path.join(checkpoint_dir, 'model_epoch_last.pth')
torch.save({
'epoch': args.end_epoch,
'state_dict': model.state_dict(),
'optim_dict': optimizer.state_dict(),
},
final_name)
end_time = time.time()
total_time = (end_time-start_time)/3600
logging.info('The total training time is {:.2f} hours'.format(total_time))
logging.info('----------------------------------The training process finished!-----------------------------------')
def adjust_learning_rate(optimizer, epoch, max_epoch, init_lr, power=0.9):
for param_group in optimizer.param_groups:
param_group['lr'] = round(init_lr * np.power(1-(epoch) / max_epoch, power), 8)
def log_args(log_file):
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(
'%(asctime)s ===> %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
# args FileHandler to save log file
fh = logging.FileHandler(log_file)
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
# args StreamHandler to print log to console
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
# add the two Handler
logger.addHandler(ch)
logger.addHandler(fh)
if __name__ == '__main__':
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
assert torch.cuda.is_available(), "Currently, we only support CUDA version"
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = True
main_worker()