-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_cylinder_asym_pl_eval.py
288 lines (229 loc) · 11.8 KB
/
train_cylinder_asym_pl_eval.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
# -*- coding:utf-8 -*-
# author: Xinge
# @file: train_cylinder_asym.py
import os
import argparse
import random
import numpy as np
import math
import os
import time
import argparse
import sys
import torch
# torch.set_float32_matmul_precision('high')
from utils.metric_util import per_class_iu, fast_hist_crop_torch
from dataloader.pc_dataset import get_SemKITTI_label_name
from dataloader.dataset_semantickitti import collate_fn_BEV
from builder import data_builder_pl, model_builder, loss_builder
from config.config_pl import load_config_data
from pytorch_lightning import loggers as pl_loggers
from pytorch_lightning.callbacks import *
import pytorch_lightning as pl
from multiprocessing import get_context
import warnings
warnings.filterwarnings("ignore")
class Cylinder3D(pl.LightningModule):
def __init__(self, configs):
super().__init__()
seed=123
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
self.save_hyperparameters(configs)
self.dataset_config = configs['dataset_params']
self.train_dataloader_config = configs['train_data_loader']
self.val_dataloader_config = configs['val_data_loader']
self.val_batch_size = self.val_dataloader_config['batch_size']
self.train_batch_size = self.train_dataloader_config['batch_size']
self.model_config = configs['model_params']
self.train_hypers = configs['train_params']
self.gpu_num = len(self.train_hypers['gpus'])
self.grid_size = self.model_config['output_shape']
SemKITTI_label_name = get_SemKITTI_label_name(self.dataset_config["label_mapping"])
self.unique_label = np.asarray(sorted(list(SemKITTI_label_name.keys())))[1:] - 1
self.unique_label_str = [SemKITTI_label_name[x] for x in self.unique_label + 1]
self.model = model_builder.build(self.model_config)
self.loss_func, self.lovasz_softmax = loss_builder.build(wce=True, lovasz=True,
num_class=self.model_config['num_class'], ignore_label=self.dataset_config['ignore_label'])
if self.gpu_num > 1:
self.effective_lr = math.sqrt((self.gpu_num)*(self.train_batch_size)) * self.train_hypers["base_lr"]
else:
self.effective_lr = math.sqrt(self.train_batch_size) * self.train_hypers["base_lr"]
print(self.effective_lr)
self.save_hyperparameters({'effective_lr': self.effective_lr})
self.train_dataset, self.val_dataset = data_builder_pl.build(self.dataset_config,
self.train_dataloader_config,
self.val_dataloader_config,
grid_size=self.grid_size)
# self.training_step_outputs = []
# self.eval_step_outputs = []
self.training_step_outputs = {'loss_list': [], 'first_step': None}
self.eval_step_outputs = {'loss_list': [], 'hist_sum': None}
def setup(self, stage=None):
"""
make datasets & seeding each worker separately
"""
##############################################
# NEED TO SET THE SEED SEPARATELY HERE
seed = self.global_rank
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
print('local seed:', seed)
##############################################
def train_dataloader(self):
return torch.utils.data.DataLoader(dataset=self.train_dataset,
batch_size=self.train_dataloader_config["batch_size"],
collate_fn=collate_fn_BEV,
shuffle=self.train_dataloader_config["shuffle"],
num_workers=self.train_dataloader_config["num_workers"],
pin_memory=True,
drop_last=True,
)
def val_dataloader(self):
return torch.utils.data.DataLoader(dataset=self.val_dataset,
batch_size=self.val_dataloader_config["batch_size"],
collate_fn=collate_fn_BEV,
shuffle=self.val_dataloader_config["shuffle"],
num_workers=self.val_dataloader_config["num_workers"],
pin_memory=True,
)
def forward(self, pt_fea_ten, vox_ten, batch_size):
outputs = self.model(pt_fea_ten, vox_ten, batch_size)
return outputs
def training_step(self, batch, batch_idx):
_, vox_label, grid, _, pt_fea = batch
pt_fea_ten = [torch.from_numpy(i).type(torch.FloatTensor).to(self.device) for i in
pt_fea]
vox_ten = [torch.from_numpy(i).to(self.device) for i in grid]
label_tensor = vox_label.type(torch.LongTensor).to(self.device)
batch_size = len(pt_fea)
predict_vox_labels = self(pt_fea_ten, vox_ten, batch_size)
loss = self.lovasz_softmax(torch.nn.functional.softmax(predict_vox_labels), label_tensor, ignore=0) + self.loss_func(
predict_vox_labels, label_tensor)
self.training_step_outputs['loss_list'].append(loss.cpu().detach())
if self.training_step_outputs['first_step'] == None:
self.training_step_outputs['first_step'] = self.global_step
if self.global_rank == 0:
self.log('train_loss_step', loss, prog_bar=True, rank_zero_only=True, logger=False)
return loss
def on_train_epoch_end(self):
loss_list = torch.stack(self.training_step_outputs['loss_list']).to(self.device)
loss_list = self.all_gather(loss_list)
if self.gpu_num > 1:
loss_step_mean = loss_list.mean(dim=0)
else:
loss_step_mean = loss_list
if self.global_rank == 0:
first_step = self.training_step_outputs['first_step']
for loss, step in zip(loss_step_mean, range(first_step, first_step+len(loss_step_mean))):
self.logger.experiment.add_scalar('train_loss_step', loss, step)
self.logger.experiment.add_scalar('train_loss_epoch', loss_step_mean.mean(), self.current_epoch)
del loss_list, loss_step_mean
self.training_step_outputs['loss_list'].clear()
self.training_step_outputs['first_step'] = None
def eval_share_step(self, batch, batch_idx):
_, vox_label, grid, pt_labs, pt_fea = batch
pt_fea_ten = [torch.from_numpy(i).type(torch.FloatTensor).to(self.device) for i in
pt_fea]
vox_ten = [torch.from_numpy(i).to(self.device) for i in grid]
label_tensor = vox_label.type(torch.LongTensor).to(self.device)
batch_size = len(pt_fea)
predict_vox_labels = self(pt_fea_ten, vox_ten, batch_size)
loss = self.lovasz_softmax(torch.nn.functional.softmax(predict_vox_labels), label_tensor, ignore=0) + self.loss_func(
predict_vox_labels, label_tensor)
predict_vox_labels = torch.argmax(predict_vox_labels, dim=1)
hist_list = []
for count, _ in enumerate(grid):
hist_list.append(fast_hist_crop_torch(predict_vox_labels[
count, grid[count][:, 0], grid[count][:, 1],
grid[count][:, 2]], torch.tensor(pt_labs[count]).type(torch.LongTensor).to(self.device),
self.unique_label))
self.eval_step_outputs['loss_list'].append(loss.cpu().detach())
hist_sum = sum(hist_list).cpu().detach()
if self.eval_step_outputs['hist_sum'] == None:
self.eval_step_outputs['hist_sum'] = hist_sum
else:
self.eval_step_outputs['hist_sum'] = sum([self.eval_step_outputs['hist_sum'], hist_sum])
def validation_step(self, batch, batch_idx):
self.eval_share_step(batch, batch_idx)
def on_validation_epoch_end(self):
loss_mean = torch.stack(self.eval_step_outputs['loss_list']).to(self.device).mean()
loss_mean = self.all_gather(loss_mean).mean()
hist_sum = self.eval_step_outputs['hist_sum'].to(self.device)
if self.gpu_num > 1:
hist_sum = sum(self.all_gather(hist_sum))
if self.global_rank == 0:
self.log('val_loss', loss_mean, prog_bar=True, logger=False)
self.logger.experiment.add_scalar('val_loss', loss_mean, self.global_step)
iou = per_class_iu(hist_sum.cpu().numpy())
val_miou = torch.tensor(np.nanmean(iou) * 100).to(self.device)
self.logger.experiment.add_scalar('val_miou', val_miou, self.global_step)
else:
val_miou = torch.tensor(0).to(self.device)
if self.gpu_num > 1:
val_miou = sum(self.all_gather(val_miou))
self.log('val_miou', val_miou, prog_bar=True, logger=False)
del loss_mean, hist_sum
self.eval_step_outputs['loss_list'].clear()
self.eval_step_outputs['hist_sum'] = None
def test_step(self, batch, batch_idx):
self.eval_share_step(batch, batch_idx)
def on_test_epoch_end(self):
hist_sum = self.eval_step_outputs['hist_sum'].to(self.device)
if self.gpu_num > 1:
hist_sum = sum(self.all_gather(hist_sum))
if self.global_rank == 0:
iou = per_class_iu(hist_sum.numpy())
print('Validation per class iou: ')
for class_name, class_iou in zip(self.unique_label_str, iou):
print('%s : %.2f%%' % (class_name, class_iou * 100))
print('%s : %.2f%%' % ('miou', np.nanmean(iou) * 100))
del hist_sum
self.eval_step_outputs['loss_list'].clear()
self.eval_step_outputs['hist_sum'] = None
def configure_optimizers(self):
optimizer = torch.optim.AdamW(self.model.parameters(), lr=self.effective_lr, eps=1e-4, weight_decay=self.train_hypers['weight_decay'])
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, self.trainer.max_epochs)
return {'optimizer':optimizer, 'lr_scheduler':scheduler}
if __name__ == '__main__':
# Training settings
parser = argparse.ArgumentParser(description='')
parser.add_argument('-y', '--config_path', default='config/semantickitti_pl.yaml')
args = parser.parse_args()
path = '/public/home/wudj/Cylinder3D_spconv_v2/logs_multiple_test_2/epoch=00-step=478-val_miou=44.159.ckpt'
checkpoint = torch.load(path)
configs = checkpoint['hyper_parameters']
# configs['train_params']['gpus'] = [0]
train_params = configs['train_params']
logdir = train_params['logdir']
model = Cylinder3D.load_from_checkpoint(path, configs=configs)
gpus = train_params['gpus']
gpu_num = len(gpus)
print(gpus)
if train_params['mixed_fp16']:
precision = '16'
else:
precision = '32'
if gpu_num > 1:
strategy='ddp'
use_distributed_sampler = True
else:
strategy = 'auto'
use_distributed_sampler = False
trainer = pl.Trainer(
max_epochs=train_params['max_num_epochs'],
devices=gpus,
num_nodes=1,
precision = precision,
accelerator='gpu',
strategy=strategy,
use_distributed_sampler=use_distributed_sampler,
)
trainer.test(model,
dataloaders=model.val_dataloader()
)