-
Notifications
You must be signed in to change notification settings - Fork 9
/
main_train_3D.py
178 lines (139 loc) · 6.73 KB
/
main_train_3D.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
#!/usr/bin/env python
import argparse
import time
import random
import numpy as np
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim
import torch.utils.data
import torch.utils.data.distributed
from torch.optim.lr_scheduler import MultiStepLR
from models.fewshot_anom_3D import FewShotSeg
from dataloading.datasets_3D import TrainDataset, TestDataset
from torch.utils.data import DataLoader
from utils import *
from dataloading.dataset_specifics import *
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument('--data_root', type=str, required=True)
parser.add_argument('--save_root', type=str, required=True)
parser.add_argument('--dataset', type=str, required=True)
parser.add_argument('--n_sv', type=int, required=True)
parser.add_argument('--fold', type=int, required=True)
# Training specs.
parser.add_argument('--max_slices', default=10, type=int)
parser.add_argument('--workers', default=4, type=int)
parser.add_argument('--steps', default=15000, type=int)
parser.add_argument('--n_shot', default=1, type=int)
parser.add_argument('--n_query', default=1, type=int)
parser.add_argument('--n_way', default=1, type=int)
parser.add_argument('--batch-size', default=1, type=int)
parser.add_argument('--max_iterations', default=1000, type=int)
parser.add_argument('--lr', default=1e-3, type=float)
parser.add_argument('--lr_gamma', default=0.95, type=float)
parser.add_argument('--momentum', default=0.9, type=float)
parser.add_argument('--weight-decay', default=0.0005, type=float)
parser.add_argument('--seed', default=None, type=int)
parser.add_argument('--bg_wt', default=0.1, type=float)
parser.add_argument('--t_loss_scaler', default=1.0, type=float)
parser.add_argument('--min_size', default=200, type=int)
# Inference specs.
parser.add_argument('--all_slices', default=True, type=bool)
parser.add_argument('--EP1', default=False, type=bool)
return parser.parse_args()
def main():
args = parse_arguments()
# Deterministic setting for reproducability.
if args.seed is not None:
random.seed(args.seed)
torch.manual_seed(args.seed)
cudnn.deterministic = True
# Set up logging.
logger = set_logger(args.save_root, 'train.log')
logger.info(args)
# Setup the path to save.
args.save_model_path = os.path.join(args.save_root, 'model.pth')
# Init model.
model = FewShotSeg(args)
model = nn.DataParallel(model.cuda())
# Init optimizer.
optimizer = torch.optim.SGD(model.parameters(), args.lr, momentum=args.momentum, weight_decay=args.weight_decay)
milestones = [(ii + 1) * 1000 for ii in range(args.steps // 1000 - 1)]
scheduler = MultiStepLR(optimizer, milestones=milestones, gamma=args.lr_gamma)
# Define loss function.
my_weight = torch.FloatTensor([args.bg_wt, 1.0]).cuda()
criterion = nn.NLLLoss(ignore_index=255, weight=my_weight)
# Enable cuDNN benchmark mode to select the fastest convolution algorithm.
cudnn.enabled = True
cudnn.benchmark = True
# Define data set and loader.
train_dataset = TrainDataset(args)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True,
num_workers=args.workers, pin_memory=True, drop_last=True)
logger.info(' Training on images not in test fold: ' +
str([elem[len(args.data_root):] for elem in train_dataset.image_dirs]))
# Start training.
sub_epochs = args.steps // args.max_iterations
logger.info(' Start training ...')
for epoch in range(sub_epochs):
# Train.
batch_time, data_time, losses, q_loss, align_loss, t_loss = train(train_loader, model, criterion, optimizer,
scheduler, epoch, args)
# Log
logger.info('============== Epoch [{}] =============='.format(epoch))
logger.info(' Batch time: {:6.3f}'.format(batch_time))
logger.info(' Loading time: {:6.3f}'.format(data_time))
logger.info(' Total Loss : {:.5f}'.format(losses))
logger.info(' Query Loss : {:.5f}'.format(q_loss))
logger.info(' Align Loss : {:.5f}'.format(align_loss))
logger.info(' Threshold Loss : {:.5f}'.format(t_loss))
if epoch == 29:
torch.save(model.state_dict(), args.save_model_path)
# Save trained model.
logger.info(' Saving model ...')
torch.save(model.state_dict(), args.save_model_path)
def train(train_loader, model, criterion, optimizer, scheduler, epoch, args):
batch_time = AverageMeter('Time', ':6.3f')
data_time = AverageMeter('Data', ':6.3f')
losses = AverageMeter('Loss', ':.4f')
q_loss = AverageMeter('Query loss', ':.4f')
a_loss = AverageMeter('Align loss', ':.4f')
t_loss = AverageMeter('Threshold loss', ':.4f')
# Train mode.
model.train()
end = time.time()
for i, sample in enumerate(train_loader):
# Extract episode data.
support_images = [[shot[None].float().cuda() for shot in way]
for way in sample['support_images']]
support_fg_mask = [[shot[None].float().cuda() for shot in way]
for way in sample['support_fg_labels']]
query_images = [query_image.float().cuda() for query_image in sample['query_images']]
query_labels = torch.cat([query_label.long().cuda() for query_label in sample['query_labels']], dim=0)
# Log loading time.
data_time.update(time.time() - end)
# Compute outputs and losses.
query_pred, align_loss, thresh_loss = model(support_images, support_fg_mask, query_images,
train=True, t_loss_scaler=args.t_loss_scaler)
query_loss = criterion(torch.log(torch.clamp(query_pred, torch.finfo(torch.float32).eps,
1 - torch.finfo(torch.float32).eps)), query_labels[None])
loss = query_loss + align_loss + thresh_loss
# compute gradient and do SGD step
for param in model.parameters():
param.grad = None
loss.backward()
optimizer.step()
scheduler.step()
# Log loss.
losses.update(loss.item(), query_pred.size(0))
q_loss.update(query_loss.item(), query_pred.size(0))
a_loss.update(align_loss.item(), query_pred.size(0))
t_loss.update(thresh_loss.item(), query_pred.size(0))
# Log elapsed time.
batch_time.update(time.time() - end)
end = time.time()
return batch_time.avg, data_time.avg, losses.avg, q_loss.avg, a_loss.avg, t_loss.avg
if __name__ == '__main__':
main()