-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_both_SemanticKITTI.py
291 lines (249 loc) · 13.3 KB
/
train_both_SemanticKITTI.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
# Common
import os
import logging
import warnings
import argparse
import numpy as np
from tqdm import tqdm
# torch
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
# my module
from dataset.two_semkitti_trainset import SemanticKITTI
from network.loss_func import compute_loss
from utils.metric import compute_acc, IoUCalculator, iouEval
import torch.nn.functional as F
from help_utils import seed_torch, my_worker_init_fn, get_logger, copyFiles, AverageMeter
# os.environ["CUDA_VISIBLE_DEVICES"] = '0'
# warnings.filterwarnings("ignore")
parser = argparse.ArgumentParser()
parser.add_argument('--backbone', type=str, default='randla', choices=['randla', 'baflac', 'baaf'])
parser.add_argument('--checkpoint_path', default=None, help='Model checkpoint path [default: None]')
parser.add_argument('--log_dir', type=str, default='polar-both', help='Dump dir to save model checkpoint [default: log]')
parser.add_argument('--max_epoch', type=int, default=100, help='Epoch to run [default: 100]')
parser.add_argument('--batch_size', type=int, default=8, help='Batch Size during training [default: 5]')
parser.add_argument('--val_batch_size', type=int, default=8, help='Batch Size during training [default: 30]')
parser.add_argument('--num_workers', type=int, default=16, help='Number of workers [default: 5]')
parser.add_argument('--seed', type=int, default=1024, help='Random Seed')
parser.add_argument('--step', type=int, default=0, help='sub dataset size')
parser.add_argument('--grid', nargs='+', type=int, default=[64, 64, 16], help='grid size of BEV representation')
FLAGS = parser.parse_args()
seed_torch(FLAGS.seed)
torch.backends.cudnn.enabled = False
if FLAGS.backbone == 'baflac':
from config import ConfigSemanticKITTI_BAF as cfg
else:
from config import ConfigSemanticKITTI as cfg
class Trainer:
def __init__(self):
# Init Logging
save_path = './save_semantic/' + FLAGS.log_dir + '/'
if not (os.path.exists(save_path)):
os.makedirs(save_path)
copyFiles(save_path)
self.log_dir = save_path
log_fname = os.path.join(self.log_dir, 'log_train.txt')
self.logger = get_logger(log_fname, name='Train')
argsDict = FLAGS.__dict__
for eachArg, value in argsDict.items():
self.logger.info(eachArg + ' : ' + str(value))
train_dataset = SemanticKITTI('training', step=FLAGS.step, grid=FLAGS.grid)
val_dataset = SemanticKITTI('validation', step=FLAGS.step, grid=FLAGS.grid)
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if FLAGS.backbone == 'baflac':
from network.BAF_LAC import BAF_LAC
self.logger.info("Use Baseline: BAF-LAC")
self.net = BAF_LAC(cfg, learn=True)
self.net.to(self.device)
collate_fn = train_dataset.collate_fn_baf_lac
elif FLAGS.backbone == 'randla':
from network.RandLANet import Network
self.logger.info("Use Baseline: Rand-LA")
self.net = Network(cfg, learn=True)
self.net.to(self.device)
collate_fn = train_dataset.collate_fn
elif FLAGS.backbone == 'baaf':
from network.BAAF import Network
self.logger.info("Use Baseline: BAAF")
self.net = Network(cfg, learn=True)
self.net.to(self.device)
collate_fn = train_dataset.collate_fn
else:
raise TypeError("1~5~!! can can need !!!")
self.train_loader = DataLoader(
train_dataset, batch_size=FLAGS.batch_size, shuffle=True, num_workers=FLAGS.num_workers,
worker_init_fn=my_worker_init_fn, collate_fn=collate_fn, pin_memory=True)
self.val_loader = DataLoader(
val_dataset, batch_size=FLAGS.val_batch_size, shuffle=False, num_workers=FLAGS.num_workers,
worker_init_fn=my_worker_init_fn, collate_fn=collate_fn, pin_memory=True)
self.logger.info((str(self.net)))
pytorch_total_params = sum(p.numel() for p in self.net.parameters() if p.requires_grad)
self.logger.info("Number of parameters: {} ".format(pytorch_total_params / 1000000) + "M")
# Load the Adam optimizer
self.optimizer = optim.Adam(self.net.parameters(), lr=0.01)
self.scheduler = optim.lr_scheduler.ExponentialLR(self.optimizer, 0.95)
# Load module
self.highest_val_iou = 0
self.start_epoch = 0
CHECKPOINT_PATH = FLAGS.checkpoint_path
if CHECKPOINT_PATH is not None and os.path.isfile(CHECKPOINT_PATH):
self.logger.info("Load Pretrain")
checkpoint = torch.load(CHECKPOINT_PATH)
self.net.load_state_dict(checkpoint['model_state_dict'], strict=True)
self.optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
self.scheduler.load_state_dict(checkpoint['scheduler_state_dict'])
self.start_epoch = checkpoint['epoch']
# Loss Function
class_weights = torch.tensor([[0, 17.1782, 49.4506, 49.0822, 45.9189, 44.9322, 49.0659, 49.6848, 49.8643,
5.3644, 31.3474, 7.2694, 41.0078, 5.5935, 11.1378, 2.8731, 37.3568,
9.1691, 43.3190, 48.0684]]).cuda()
self.logger.info(class_weights)
# class_weights = torch.from_numpy(class_weights).float().cuda()
# self.criterion = nn.CrossEntropyLoss(weight=class_weights, reduction='none')
self.criterion = nn.CrossEntropyLoss(weight=class_weights)
self.db_criterion = nn.L1Loss(reduction='mean')
self.evaluator = iouEval(20, self.device, 0)
self.train_dataset = train_dataset
self.val_dataset = val_dataset
def consistency_loss_l1(self, pred_cls, pred_cls_raw):
'''
Input:
pred_cls, pred_cls_raw (BS, C, N, 1)
'''
pred_cls_softmax = F.softmax(pred_cls, dim=1)
pred_cls_raw_softmax = F.softmax(pred_cls_raw, dim=1)
loss = (pred_cls_softmax - pred_cls_raw_softmax).abs().sum(dim=1).mean()
return loss
def train_one_epoch(self):
self.net.train() # set model to training mode
total_losses = AverageMeter()
losses = AverageMeter()
db_losses = AverageMeter()
tqdm_loader = tqdm(self.train_loader, total=len(self.train_loader))
scaler = torch.cuda.amp.GradScaler()
for batch_idx, (polar_data, random_data, idx) in enumerate(tqdm_loader):
for key in polar_data:
if type(polar_data[key]) is list:
for i in range(cfg.num_layers):
polar_data[key][i] = polar_data[key][i].cuda(non_blocking=True)
else:
polar_data[key] = polar_data[key].cuda(non_blocking=True)
for key in random_data:
if type(random_data[key]) is list:
for i in range(cfg.num_layers):
random_data[key][i] = random_data[key][i].cuda(non_blocking=True)
else:
random_data[key] = random_data[key].cuda(non_blocking=True)
self.optimizer.zero_grad()
with torch.cuda.amp.autocast():
random_out, _ = self.net(random_data)
polar_out, sigma = self.net(polar_data)
loss = self.criterion(polar_out, polar_data['labels']).mean()
# loss, end_points = compute_loss(polar_out, polar_data, self.train_dataset, self.criterion)
idx = idx[:, None, :].cuda(non_blocking=True)
random_out = torch.take_along_dim(random_out, indices=idx, dim=-1)
db_loss = self.db_criterion(F.softmax(polar_out, dim=1), F.softmax(random_out, dim=1))
# db_loss = self.consistency_loss_l1(polar_out, random_out)
factor_ce = 1.0 / (sigma[0] ** 2)
factor_l1 = 1.0 / (sigma[1] ** 2)
total_loss = factor_ce * loss + factor_l1 * db_loss + \
torch.log(1+sigma[0]) + \
torch.log(1+sigma[1])
# total_loss = loss + 15*db_loss
scaler.scale(total_loss).backward()
scaler.step(self.optimizer)
scaler.update()
# loss.backward()
# self.optimizer.step()
losses.update(loss.item())
db_losses.update(db_loss.item())
total_losses.update(total_loss.item())
if batch_idx % 100 == 0 or batch_idx == len(tqdm_loader)-1:
self.logger.info('{:.6f} || {:.6f} || {:.6f} || {:.6f}'.format(factor_ce.item() * loss.item(),
factor_l1.item() * db_loss.item(), factor_ce.item(), factor_l1.item()))
self.logger.info('{:.6f} || {:.6f} || {:.6f} || {:.6f}'.format(torch.log(1+sigma[0]).item(),
torch.log(1+sigma[1]).item(), sigma[0].item(), sigma[1].item()))
# self.logger.info('{:.6f} || {:.6f} || {:.6f}'.format(total_losses.val, losses.val, 15 * db_losses.val))
lr = self.optimizer.param_groups[0]['lr']
self.logger.info('Step {:08d} || Lr={:.6f} || '
'L_total={total.val:.4f}/({total.avg:.4f}) || '
'L_ce={loss.val:.4f}/({loss.avg:.4f}) '
'|| L_db={db.val:.4f}/({db.avg:.4f})'.format(batch_idx, lr, total=total_losses, loss=losses, db=db_losses))
# exit()
self.scheduler.step()
def train(self):
for epoch in range(self.start_epoch, FLAGS.max_epoch):
self.cur_epoch = epoch
self.logger.info('**** EPOCH %03d ****' % (epoch))
self.train_one_epoch()
checkpoint_file = os.path.join(self.log_dir, 'checkpoint.tar')
self.save_checkpoint(checkpoint_file)
self.logger.info('**** EVAL EPOCH %03d ****' % (epoch))
mean_iou = self.validate()
# Save best checkpoint
if mean_iou > self.highest_val_iou:
self.logger.info('**** Current: %03f Best: %03f ****' % (mean_iou, self.highest_val_iou))
self.highest_val_iou = mean_iou
checkpoint_file = os.path.join(self.log_dir, 'checkpoint-best.tar')
self.save_checkpoint(checkpoint_file)
else:
self.logger.info('**** Current: %03f Best: %03f ****' % (mean_iou, self.highest_val_iou))
def validate(self):
# torch.cuda.empty_cache()
self.net.eval() # set model to eval mode (for bn and dp)
self.evaluator.reset()
iou_calc = IoUCalculator(cfg)
tqdm_loader = tqdm(self.val_loader, total=len(self.val_loader))
with torch.no_grad():
for batch_idx, (polar_data, random_data, idx) in enumerate(tqdm_loader):
for key in polar_data:
if type(polar_data[key]) is list:
for i in range(cfg.num_layers):
polar_data[key][i] = polar_data[key][i].cuda(non_blocking=True)
else:
polar_data[key] = polar_data[key].cuda(non_blocking=True)
# for key in random_data:
# if type(random_data[key]) is list:
# for i in range(cfg.num_layers):
# random_data[key][i] = random_data[key][i].cuda(non_blocking=True)
# else:
# random_data[key] = random_data[key].cuda(non_blocking=True)
# Forward pass
# torch.cuda.synchronize()
semantic_out, _ = self.net(polar_data)
# loss, end_points = compute_loss(semantic_out, polar_data, self.train_dataset, self.criterion)
# acc, end_points = compute_acc(end_points)
# iou_calc.add_data(end_points)
argmax = F.softmax(semantic_out, dim=1).argmax(dim=1)
self.evaluator.addBatch(argmax, polar_data['labels'])
# mean_iou, iou_list = iou_calc.compute_iou()
# self.logger.info('mean IoU:{:.1f}'.format(mean_iou * 100))
# s = 'IoU:'
# for iou_tmp in iou_list:
# s += '{:5.2f} '.format(100 * iou_tmp)
# self.logger.info(s)
accuracy = self.evaluator.getacc()
mean_iou, class_jaccard = self.evaluator.getIoU()
class_func = ["unlabeled", "car", "bicycle", "motorcycle", "truck",
"other-vehicle", "person", "bicyclist", "motorcyclist", "road",
"parking", "sidewalk", "other-ground", "building", "fence",
"vegetation", "trunk", "terrain", "pole", "traffic-sign"]
self.logger.info('Validation set: ||' 'Acc avg {acc:.3f} ||' 'IoU avg {iou:.3f}'.format(acc=accuracy, iou=mean_iou))
for i, jacc in enumerate(class_jaccard):
self.logger.info('IoU class {i:} [{class_str:}] = {jacc:.3f}'.format(i=i, class_str=class_func[i], jacc=jacc))
return mean_iou
def save_checkpoint(self, fname):
save_dict = {
'epoch': self.cur_epoch+1, # after training one epoch, the start_epoch should be epoch+1
'optimizer_state_dict': self.optimizer.state_dict(),
'scheduler_state_dict': self.scheduler.state_dict()
}
save_dict['model_state_dict'] = self.net.state_dict()
torch.save(save_dict, fname)
def main():
trainer = Trainer()
trainer.train()
if __name__ == '__main__':
main()