-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_uie.py
136 lines (119 loc) · 5.83 KB
/
test_uie.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
from torch.utils.data import DataLoader
from Datasets.utils import ToTensor, Compose, CropCenter, dataset_intrinsics, DownscaleFlow, plot_traj, visflow
from Datasets.tartanTrajFlowDataset import TrajFolderDataset
from Datasets.transformation import ses2poses_quat
from evaluator.tartanair_evaluator import TartanAirEvaluator
from TartanVO_UIE import UIETartanVO
import argparse
import numpy as np
import cv2
from os import mkdir
from os.path import isdir
def get_args():
parser = argparse.ArgumentParser(description='HRL')
parser.add_argument('--batch-size', type=int, default=1,
help='batch size (default: 1)')
parser.add_argument('--worker-num', type=int, default=1,
help='data loader worker number (default: 1)')
parser.add_argument('--image-width', type=int, default=640,
help='image width (default: 640)')
parser.add_argument('--image-height', type=int, default=448,
help='image height (default: 448)')
parser.add_argument('--model-name', default='',
help='name of pretrained model (default: "")')
parser.add_argument('--uie-model-name', default='',
help='name of pretrained UIE model (default: "")')
parser.add_argument('--euroc', action='store_true', default=False,
help='euroc test (default: False)')
parser.add_argument('--kitti', action='store_true', default=False,
help='kitti test (default: False)')
parser.add_argument('--unity', action='store_true', default=False,
help='unity test (default: False)')
parser.add_argument('--subpipe', action='store_true', default=False,
help='subpipe test (default: False)')
parser.add_argument('--aqualoc', action='store_true', default=False,
help='aqualoc test (default: False)')
parser.add_argument('--kitti-intrinsics-file', default='',
help='kitti intrinsics file calib.txt (default: )')
parser.add_argument('--test-dir', default='',
help='test trajectory folder where the RGB images are (default: "")')
parser.add_argument('--pose-file', default='',
help='test trajectory gt pose file, used for scale calculation, and visualization (default: "")')
parser.add_argument('--save-flow', action='store_true', default=False,
help='save optical flow (default: False)')
args = parser.parse_args()
return args
if __name__ == '__main__':
args = get_args()
testvo = UIETartanVO(args.model_name, args.uie_model_name)
# print(testvo.vonet.parameters)
# load trajectory data from a folder
datastr = 'tartanair'
if args.kitti:
datastr = 'kitti'
elif args.euroc:
datastr = 'euroc'
elif args.unity:
datastr = 'unity'
# args.image_height = 640
# args.image_width = 640
elif args.subpipe:
datastr = 'subpipe'
elif args.aqualoc:
datastr = 'aqualoc'
#args.image_height = 608
#args.image_width = 968
else:
datastr = 'tartanair'
focalx, focaly, centerx, centery = dataset_intrinsics(datastr)
if args.kitti_intrinsics_file.endswith('.txt') and datastr=='kitti':
focalx, focaly, centerx, centery = load_kiiti_intrinsics(args.kitti_intrinsics_file)
transform = Compose([CropCenter((args.image_height, args.image_width)), DownscaleFlow(), ToTensor()])
testDataset = TrajFolderDataset(args.test_dir, posefile = args.pose_file, transform=transform,
focalx=focalx, focaly=focaly, centerx=centerx, centery=centery)
testDataloader = DataLoader(testDataset, batch_size=args.batch_size,
shuffle=False, num_workers=args.worker_num)
testDataiter = iter(testDataloader)
motionlist = []
testname = datastr + '_' + args.model_name.split('.')[0]
if args.save_flow:
flowdir = 'results/'+testname+'_flow'
if not isdir(flowdir):
mkdir(flowdir)
flowcount = 0
while True:
try:
sample = next(testDataiter)
except StopIteration:
break
motions, flow = testvo.test_batch(sample)
# print(motions.shape)
# print(sample['motion'])
motionlist.extend(motions)
if args.save_flow:
for k in range(flow.shape[0]):
flowk = flow[k].transpose(1,2,0)
np.save(flowdir+'/'+str(flowcount).zfill(6)+'.npy',flowk)
flow_vis = visflow(flowk)
cv2.imwrite(flowdir+'/'+str(flowcount).zfill(6)+'.png',flow_vis)
flowcount += 1
poselist = ses2poses_quat(np.array(motionlist))
# calculate ATE, RPE, KITTI-RPE
if args.pose_file.endswith('.txt'):
evaluator = TartanAirEvaluator()
results = evaluator.evaluate_one_trajectory(args.pose_file, poselist, scale=True, kittitype=(datastr=='kitti'))
if datastr=='euroc':
print("==> ATE: %.4f" %(results['ate_score']))
elif datastr=='subpipe':
print("==> ATE: %.4f" %(results['ate_score']))
elif datastr=='aqualoc':
print("==> ATE: %.4f" %(results['ate_score']))
else:
print("==> ATE: %.4f,\t KITTI-R/t: %.4f, %.4f" %(results['ate_score'], results['kitti_score'][0], results['kitti_score'][1]))
print('results/'+testname+'.png')
# save results and visualization
plot_traj(results['gt_aligned'], results['est_aligned'], vis=False, savefigname='results/'+testname+'_wflow.png', title='ATE %.4f' %(results['ate_score']))
np.savetxt('results/'+testname+'_wflow.txt',results['est_aligned'])
else:
np.savetxt('results/'+testname+'.txt',poselist)
np.save('results/'+testname+'.npy', poselist)