-
Notifications
You must be signed in to change notification settings - Fork 33
/
train_SemanticKITTI.py
191 lines (164 loc) · 7.43 KB
/
train_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
# 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
from torch.utils.tensorboard import SummaryWriter
# my module
from dataset.semkitti_trainset import SemanticKITTI
from utils.config import ConfigSemanticKITTI as cfg
from utils.metric import compute_acc, IoUCalculator
from network.RandLANet import Network
from network.loss_func import compute_loss
torch.backends.cudnn.enabled = False
warnings.filterwarnings("ignore")
parser = argparse.ArgumentParser()
parser.add_argument('--checkpoint_path', default=None, help='Model checkpoint path [default: None]')
parser.add_argument('--log_dir', default='log', 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=5, help='Batch Size during training [default: 5]')
parser.add_argument('--val_batch_size', type=int, default=30, help='Batch Size during training [default: 30]')
parser.add_argument('--num_workers', type=int, default=5, help='Number of workers [default: 5]')
FLAGS = parser.parse_args()
def my_worker_init_fn(worker_id):
np.random.seed(np.random.get_state()[1][0] + worker_id)
class Trainer:
def __init__(self):
# Init Logging
if not os.path.exists(FLAGS.log_dir):
os.mkdir(FLAGS.log_dir)
self.log_dir = FLAGS.log_dir
log_fname = os.path.join(FLAGS.log_dir, 'log_train.txt')
LOGGING_FORMAT = '%(asctime)s %(levelname)s: %(message)s'
DATE_FORMAT = '%Y%m%d %H:%M:%S'
logging.basicConfig(level=logging.DEBUG, format=LOGGING_FORMAT, datefmt=DATE_FORMAT, filename=log_fname)
self.logger = logging.getLogger("Trainer")
# tensorboard writer
self.tf_writer = SummaryWriter(self.log_dir)
# get_dataset & dataloader
train_dataset = SemanticKITTI('training')
val_dataset = SemanticKITTI('validation')
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=train_dataset.collate_fn,
pin_memory=True
)
self.val_loader = DataLoader(
val_dataset,
batch_size=FLAGS.val_batch_size,
shuffle=True,
num_workers=FLAGS.num_workers,
worker_init_fn=my_worker_init_fn,
collate_fn=val_dataset.collate_fn,
pin_memory=True
)
# Network & Optimizer
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
self.net = Network(cfg)
self.net.to(device)
# Load the Adam optimizer
self.optimizer = optim.Adam(self.net.parameters(), lr=cfg.learning_rate)
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):
checkpoint = torch.load(CHECKPOINT_PATH)
self.net.load_state_dict(checkpoint['model_state_dict'])
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.from_numpy(train_dataset.get_class_weight()).float().cuda()
self.criterion = nn.CrossEntropyLoss(weight=class_weights, reduction='none')
# Multiple GPU Training
if torch.cuda.device_count() > 1:
self.logger.info("Let's use %d GPUs!" % (torch.cuda.device_count()))
# dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs
self.net = nn.DataParallel(self.net)
self.train_dataset = train_dataset
self.val_dataset = val_dataset
def train_one_epoch(self):
self.net.train() # set model to training mode
tqdm_loader = tqdm(self.train_loader, total=len(self.train_loader))
for batch_idx, batch_data in enumerate(tqdm_loader):
for key in batch_data:
if type(batch_data[key]) is list:
for i in range(cfg.num_layers):
batch_data[key][i] = batch_data[key][i].cuda(non_blocking=True)
else:
batch_data[key] = batch_data[key].cuda(non_blocking=True)
self.optimizer.zero_grad()
# Forward pass
torch.cuda.synchronize()
end_points = self.net(batch_data)
loss, end_points = compute_loss(end_points, self.train_dataset, self.criterion)
loss.backward()
self.optimizer.step()
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()
self.logger.info('**** EVAL EPOCH %03d ****' % (epoch))
mean_iou = self.validate()
# Save best checkpoint
if mean_iou > self.highest_val_iou:
self.hightest_val_iou = mean_iou
checkpoint_file = os.path.join(self.log_dir, 'checkpoint.tar')
self.save_checkpoint(checkpoint_file)
def validate(self):
self.net.eval() # set model to eval mode (for bn and dp)
iou_calc = IoUCalculator(cfg)
tqdm_loader = tqdm(self.val_loader, total=len(self.val_loader))
with torch.no_grad():
for batch_idx, batch_data in enumerate(tqdm_loader):
for key in batch_data:
if type(batch_data[key]) is list:
for i in range(cfg.num_layers):
batch_data[key][i] = batch_data[key][i].cuda(non_blocking=True)
else:
batch_data[key] = batch_data[key].cuda(non_blocking=True)
# Forward pass
torch.cuda.synchronize()
end_points = self.net(batch_data)
loss, end_points = compute_loss(end_points, self.train_dataset, self.criterion)
acc, end_points = compute_acc(end_points)
iou_calc.add_data(end_points)
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)
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()
}
# with nn.DataParallel() the net is added as a submodule of DataParallel
try:
save_dict['model_state_dict'] = self.net.module.state_dict()
except AttributeError:
save_dict['model_state_dict'] = self.net.state_dict()
torch.save(save_dict, fname)
def main():
trainer = Trainer()
trainer.train()
if __name__ == '__main__':
main()