-
Notifications
You must be signed in to change notification settings - Fork 37
/
main.py
executable file
·309 lines (217 loc) · 9.44 KB
/
main.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env python
"""
Author: Anshul Paigwar
email: p.anshul6@gmail.com
"""
import argparse
import os
import shutil
import yaml
import time
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import numpy as np
import cv2
# from modules import gnd_est_Loss
from model import GroundEstimatorNet
from modules.loss_func import MaskedHuberLoss,SpatialSmoothLoss
from dataset_utils.dataset_provider import get_train_loader, get_valid_loader
from utils.point_cloud_ops import points_to_voxel
import ipdb as pdb
use_cuda = torch.cuda.is_available()
if use_cuda:
print('setting gpu on gpu_id: 0') #TODO: find the actual gpu id being used
#############################################xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx#######################################
parser = argparse.ArgumentParser()
parser.add_argument('--print-freq', '-p', default=100, type=int, metavar='N', help='print frequency (default: 50)')
parser.add_argument('--resume', default='', type=str, metavar='PATH', help='path to latest checkpoint (default: none)')
parser.add_argument('--config', default='config/config_kittiSem.yaml', type=str, metavar='PATH', help='path to config file (default: none)')
parser.add_argument('-e', '--evaluate', dest='evaluate', action='store_true', help='evaluate model on validation set')
parser.add_argument('-s', '--save_checkpoints', dest='save_checkpoints', action='store_true',help='evaluate model on validation set')
parser.add_argument('--start_epoch', default=0, type=int, help='epoch number to start from')
args = parser.parse_args()
if os.path.isfile(args.config):
print("using config file:", args.config)
with open(args.config) as f:
config_dict = yaml.load(f, Loader=yaml.FullLoader)
class ConfigClass:
def __init__(self, **entries):
self.__dict__.update(entries)
cfg = ConfigClass(**config_dict) # convert python dict to class for ease of use
else:
print("=> no config file found at '{}'".format(args.config))
#############################################xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx#######################################
train_loader = get_train_loader(cfg.data_dir, cfg.batch_size, skip = 4)
valid_loader = get_valid_loader(cfg.data_dir, cfg.batch_size, skip = 6)
model = GroundEstimatorNet(cfg).cuda()
optimizer = optim.SGD(model.parameters(), lr=cfg.lr, momentum=0.9, weight_decay=0.0005)
scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=15, gamma=0.8)
lossHuber = nn.SmoothL1Loss(reduction = "mean").cuda()
lossSpatial = SpatialSmoothLoss().cuda()
def train(epoch):
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
# top1 = AverageMeter()
# switch to train mode
model.train()
start = time.time()
for batch_idx, (data, labels) in enumerate(train_loader):
data_time.update(time.time() - start) # measure data loading time
B = data.shape[0] # Batch size
N = data.shape[1] # Num of points in PointCloud
voxels = []; coors = []; num_points = []; mask = []
# kernel = np.ones((3,3),np.uint8)
data = data.numpy()
for i in range(B):
v, c, n = points_to_voxel(data[i], cfg.voxel_size, cfg.pc_range, cfg.max_points_voxel, True, cfg.max_voxels)
# m = np.zeros((100,100),np.uint8)
# ind = c[:,1:]
# m[tuple(ind.T)] = 1
# m = cv2.dilate(m,kernel,iterations = 1)
c = torch.from_numpy(c)
c = F.pad(c, (1,0), 'constant', i)
voxels.append(torch.from_numpy(v))
coors.append(c)
num_points.append(torch.from_numpy(n))
# mask.append(torch.from_numpy(m))
#
voxels = torch.cat(voxels).float().cuda()
coors = torch.cat(coors).float().cuda()
num_points = torch.cat(num_points).float().cuda()
labels = labels.float().cuda()
# mask = torch.stack(mask).cuda()
optimizer.zero_grad()
output = model(voxels, coors, num_points)
# pdb.set_trace()
loss = cfg.alpha * lossHuber(output, labels) + cfg.beta * lossSpatial(output)
# loss = lossHuber(output, labels)
# loss = masked_huber_loss(output, labels, mask)
loss.backward()
# `clip_grad_norm` helps prevent the exploding gradient problem in RNNs / LSTMs.
# torch.nn.utils.clip_grad_norm_(model.parameters(), cfg.clip)
optimizer.step() # optimiser step must be after clipping bcoz optimiser step updates the gradients.
losses.update(loss.item(), B)
# measure elapsed time
batch_time.update(time.time() - start)
start = time.time()
if batch_idx % args.print_freq == 0:
print('Epoch: [{0}][{1}/{2}]\t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})'.format(
epoch, batch_idx, len(train_loader), batch_time=batch_time,
data_time=data_time, loss=losses))
return losses.avg
def validate():
batch_time = AverageMeter()
losses = AverageMeter()
data_time = AverageMeter()
# switch to evaluate mode
model.eval()
# if args.evaluate:
# model.train()
with torch.no_grad():
start = time.time()
for batch_idx, (data, labels) in enumerate(valid_loader):
data_time.update(time.time() - start) # measure data loading time
B = data.shape[0] # Batch size
N = data.shape[1] # Num of points in PointCloud
voxels = []; coors = []; num_points = []; mask = []
# kernel = np.ones((3,3),np.uint8)
data = data.numpy()
for i in range(B):
v, c, n = points_to_voxel(data[i], cfg.voxel_size, cfg.pc_range, cfg.max_points_voxel, True, cfg.max_voxels)
# m = np.zeros((100,100),np.uint8)
# ind = c[:,1:]
# m[tuple(ind.T)] = 1
# m = cv2.dilate(m,kernel,iterations = 1)
c = torch.from_numpy(c)
c = F.pad(c, (1,0), 'constant', i)
voxels.append(torch.from_numpy(v))
coors.append(c)
num_points.append(torch.from_numpy(n))
# mask.append(torch.from_numpy(m))
voxels = torch.cat(voxels).float().cuda()
coors = torch.cat(coors).float().cuda()
num_points = torch.cat(num_points).float().cuda()
labels = labels.float().cuda()
# mask = torch.stack(mask).cuda()
optimizer.zero_grad()
output = model(voxels, coors, num_points)
# pdb.set_trace()
loss = cfg.alpha * lossHuber(output, labels) + cfg.beta * lossSpatial(output)
# loss = lossHuber(output, labels)
# loss = masked_huber_loss(output, labels, mask)
losses.update(loss.item(), B)
# measure elapsed time
batch_time.update(time.time() - start)
start = time.time()
if batch_idx % args.print_freq == 0:
print('Test: [{0}/{1}]\t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'.format(
batch_idx, len(valid_loader), batch_time=batch_time, loss=losses))
return losses.avg
lowest_loss = 1
def main():
# rospy.init_node('pcl2_pub_example', anonymous=True)
global args, lowest_loss
# optionally resume from a checkpoint
if args.resume:
if os.path.isfile(args.resume):
print("=> loading checkpoint '{}'".format(args.resume))
checkpoint = torch.load(args.resume)
args.start_epoch = checkpoint['epoch']
lowest_loss = checkpoint['lowest_loss']
model.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
print("=> loaded checkpoint '{}' (epoch {})"
.format(args.resume, checkpoint['epoch']))
else:
print("=> no checkpoint found at '{}'".format(args.resume))
if args.evaluate:
validate()
return
for epoch in range(args.start_epoch, cfg.epochs):
# adjust_learning_rate(optimizer, epoch)
loss_t = train(epoch)
# evaluate on validation set
loss_v = validate()
scheduler.step()
if (args.save_checkpoints):
# remember best prec@1 and save checkpoint
is_best = loss_v < lowest_loss
lowest_loss = min(loss_v, lowest_loss)
save_checkpoint({
'epoch': epoch + 1,
'state_dict': model.state_dict(),
'lowest_loss': lowest_loss,
'optimizer' : optimizer.state_dict(),
}, is_best)
'''
Save the model for later
'''
def save_checkpoint(state, is_best, filename='checkpoint.pth.tar'):
torch.save(state, filename)
if is_best:
shutil.copyfile(filename, 'model_best.pth.tar')
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
if __name__ == '__main__':
main()