-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_loader.py
More file actions
224 lines (185 loc) · 7.85 KB
/
data_loader.py
File metadata and controls
224 lines (185 loc) · 7.85 KB
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
# -------------------------------------------------------------------
# Copyright (C) 2020 Università degli studi di Milano-Bicocca, iralab
# Author: Daniele Cattaneo (d.cattaneo10@campus.unimib.it)
# Released under Creative Commons
# Attribution-NonCommercial-ShareAlike 4.0 International License.
# http://creativecommons.org/licenses/by-nc-sa/4.0/
# -------------------------------------------------------------------
# Modified Author: Xudong Lv
# based on github.com/cattaneod/CMRNet/blob/master/main_visibility_CALIB.py
import cv2
cv2.setNumThreads(0)
cv2.ocl.setUseOpenCL(False)
import torch
torch.set_num_threads(1)
import os
os.environ['OMP_NUM_THREADS'] = '0'
from logger import *
import math
import os
import random
import time
# import apex
import mathutils
import numpy as np
import torch.nn.functional as F
import torch.nn.parallel
import torch.optim as optim
import torch.utils.data
import torch.nn as nn
from sacred import Experiment
from sacred.utils import apply_backspaces_and_linefeeds
from DatasetLidarCamera import DatasetLidarCameraKittiOdometry
from losses import DistancePoints3D, GeometricLoss, L1Loss, ProposedLoss, CombinedLoss
from models.LCCNet import LCCNet
from quaternion_distances import quaternion_distance
from tensorboardX import SummaryWriter
from utils import (mat2xyzrpy, merge_inputs, overlay_imgs, quat2mat,
quaternion_from_matrix, rotate_back, rotate_forward,
tvector2mat)
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = False
ex = Experiment("LCCNet")
ex.captured_out_filter = apply_backspaces_and_linefeeds
# noinspection PyUnusedLocal
@ex.config
def config():
checkpoints = './checkpoints/'
dataset = 'kitti/odom' # 'kitti/raw'
data_folder = '/data/kitti_odometry/dataset'
use_reflectance = False
val_sequence = 0
epochs = 120
BASE_LEARNING_RATE = 3e-4 # 1e-4
loss = 'simple'
max_t = 1.5 # 1.5, 1.0, 0.5, 0.2, 0.1
max_r = 20 # 20.0, 10.0, 5.0, 2.0, 1.0
batch_size = 240 # 120
num_worker = 4
network = 'Res_f1'
optimizer = 'adam'
resume = True
weights = None
rescale_rot = 1.0
rescale_transl = 2.0
precision = "O0"
norm = 'bn'
dropout = 0.0
max_depth = 80.
weight_point_cloud = 0.5
log_frequency = 50
print_frequency = 50
starting_epoch = -1
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
os.environ['CUDA_VISIBLE_DEVICES'] = '4'
os.environ['OMP_NUM_THREADS'] = '1'
EPOCH = 1
def _init_fn(worker_id, seed):
seed = seed + worker_id + EPOCH*100
INFO(f"Init worker {worker_id} with seed {seed}")
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
def get_2D_lidar_projection(pcl, cam_intrinsic):
pcl_xyz = cam_intrinsic @ pcl.T
pcl_xyz = pcl_xyz.T
pcl_z = pcl_xyz[:, 2]
pcl_xyz = pcl_xyz / (pcl_xyz[:, 2, None] + 1e-10)
pcl_uv = pcl_xyz[:, :2]
return pcl_uv, pcl_z
def lidar_project_depth(pc_rotated, cam_calib, img_shape):
pc_rotated = pc_rotated[:3, :].detach().cpu().numpy()
cam_intrinsic = cam_calib.numpy()
pcl_uv, pcl_z = get_2D_lidar_projection(pc_rotated.T, cam_intrinsic)
mask = (pcl_uv[:, 0] > 0) & (pcl_uv[:, 0] < img_shape[1]) & (pcl_uv[:, 1] > 0) & (
pcl_uv[:, 1] < img_shape[0]) & (pcl_z > 0)
pcl_uv = pcl_uv[mask]
pcl_z = pcl_z[mask]
pcl_uv = pcl_uv.astype(np.uint32)
pcl_z = pcl_z.reshape(-1, 1)
depth_img = np.zeros((img_shape[0], img_shape[1], 1))
depth_img[pcl_uv[:, 1], pcl_uv[:, 0]] = pcl_z
depth_img = torch.from_numpy(depth_img.astype(np.float32))
depth_img = depth_img.cuda()
depth_img = depth_img.permute(2, 0, 1)
return depth_img, pcl_uv
@ex.automain
def main(_config, _run, seed):
global EPOCH
INFO('Loss Function Choice: {}'.format(_config['loss']))
if _config['val_sequence'] is None:
raise TypeError('val_sequences cannot be None')
else:
_config['val_sequence'] = f"{_config['val_sequence']:02d}"
INFO("Val Sequence: {}".format(_config['val_sequence']))
dataset_class = DatasetLidarCameraKittiOdometry
img_shape = (384, 1280) # 网络的输入尺度
input_size = (256, 512)
_config["checkpoints"] = os.path.join(_config["checkpoints"], _config['dataset'])
dataset_train = dataset_class(_config['data_folder'], max_r=_config['max_r'], max_t=_config['max_t'],
split='train', use_reflectance=_config['use_reflectance'],
val_sequence=_config['val_sequence'],config=_config, img_shape = img_shape)
dataset_val = dataset_class(_config['data_folder'], max_r=_config['max_r'], max_t=_config['max_t'],
split='val', use_reflectance=_config['use_reflectance'],
val_sequence=_config['val_sequence'],config=_config, img_shape = img_shape)
model_savepath = os.path.join(_config['checkpoints'], 'val_seq_' + _config['val_sequence'], 'models')
if not os.path.exists(model_savepath):
os.makedirs(model_savepath)
log_savepath = os.path.join(_config['checkpoints'], 'val_seq_' + _config['val_sequence'], 'log')
if not os.path.exists(log_savepath):
os.makedirs(log_savepath)
np.random.seed(seed)
torch.random.manual_seed(seed)
def init_fn(x): return _init_fn(x, seed)
train_dataset_size = len(dataset_train)
val_dataset_size = len(dataset_val)
INFO('Number of the train dataset: {}'.format(train_dataset_size))
INFO('Number of the val dataset: {}'.format(val_dataset_size))
# Training and validation set creation
num_worker = _config['num_worker']
batch_size = _config['batch_size']
print(_config)
TrainImgLoader = torch.utils.data.DataLoader(dataset=dataset_train,
shuffle=True,
batch_size=batch_size,
num_workers=16,
worker_init_fn=init_fn,
# collate_fn=merge_inputs,
drop_last=False,
pin_memory=True)
INFO(len(TrainImgLoader))
starting_epoch = _config['starting_epoch']
if _config['weights'] is not None and _config['resume']:
checkpoint = torch.load(_config['weights'], map_location='cpu')
opt_state_dict = checkpoint['optimizer']
optimizer.load_state_dict(opt_state_dict)
if starting_epoch != 0:
starting_epoch = checkpoint['epoch']
start_full_time = time.time()
BEST_VAL_LOSS = 10000.
old_save_filename = None
train_iter = 0
val_iter = 0
for epoch in range(starting_epoch, _config['epochs'] + 1):
EPOCH = epoch
INFO('This is %d-th epoch' % epoch)
epoch_start_time = time.time()
total_train_loss = 0
local_loss = 0.
## Training ##
time_for_50ep = time.time()
total_iter_start = time.time()
# for batch_idx, sample in enumerate(dataset_train):
# for batch_idx, sample in enumerate(TrainImgLoader):
for i in range(10000000):
sample = dataset_train.__getitem__(10)
#print(f'batch {batch_idx+1}/{len(TrainImgLoader)}', end='\r')
start_time = time.time()
INFO(f'end iter time cost{time.time() - total_iter_start}')
total_iter_start = time.time()
INFO("------------------------------------")
INFO('epoch %d total training loss = %.3f' % (epoch, total_train_loss / len(dataset_train)))
INFO('Total epoch time = %.2f' % (time.time() - epoch_start_time))
INFO("------------------------------------")
_run.log_scalar("Total training loss", total_train_loss / len(dataset_train), epoch)
return _run.result