-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathpose_error_gpu.py
171 lines (147 loc) · 6.01 KB
/
pose_error_gpu.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
# Author: Van Nguyen Nguyen ([email protected])
# Imagine team, Ecole des Ponts ParisTech
import numpy as np
import torch
import gc
from bop_toolkit_lib import misc_torch as misc
MAX_BATCH_SIZE = 200 # (1.0 GB in average, less than 2 GB for all BOP objects)
class BatchedData:
# taken from https://github.com/nv-nguyen/gigapose/blob/f81a5413a912a0eae13c59b276ec4b41d4eca094/src/utils/batch.py
"""
A structure for storing data in batched format to handle very large batch size.
Implements basic filtering and concatenation.
"""
def __init__(self, batch_size, data=None, **kwargs) -> None:
self.batch_size = batch_size
if data is not None:
self.data = data
else:
self.data = []
def __len__(self):
assert self.batch_size is not None, "batch_size is not defined"
if isinstance(self.data, np.ndarray):
return np.ceil(len(self.data) / self.batch_size).astype(int)
elif isinstance(self.data, torch.Tensor):
length = self.data.shape[0]
return np.ceil(length / self.batch_size).astype(int)
else:
raise NotImplementedError
def __getitem__(self, idx):
assert self.batch_size is not None, "batch_size is not defined"
return self.data[idx * self.batch_size : (idx + 1) * self.batch_size]
def cat(self, data, dim=0):
if len(self.data) == 0:
self.data = data
else:
self.data = torch.cat([self.data, data], dim=dim)
def append(self, data):
self.data.append(data)
def stack(self, dim=0):
self.data = torch.stack(self.data, dim=dim)
@torch.no_grad()
def mssd_by_batch(R_est, t_est, R_gt, t_gt, pts, syms, max_batch_size=MAX_BATCH_SIZE):
"""
mssd with max_batch_size for R_est, t_est, R_gt, t_gt.
This allows to stabilize the memory usage (1GB for batch_size=200).
"""
batch_R_est = BatchedData(max_batch_size, R_est)
batch_t_est = BatchedData(max_batch_size, t_est)
batch_R_gt = BatchedData(max_batch_size, R_gt)
batch_t_gt = BatchedData(max_batch_size, t_gt)
output = BatchedData(batch_size=max_batch_size)
for i in range(len(batch_R_est)):
output_ = mssd(
batch_R_est[i],
batch_t_est[i],
batch_R_gt[i],
batch_t_gt[i],
pts,
syms,
)
output.cat(output_)
return output.data
@torch.no_grad()
def mssd(R_est, t_est, R_gt, t_gt, pts, syms):
"""Maximum Symmetry-Aware Surface Distance (MSSD).
See: http://bop.felk.cvut.cz/challenges/bop-challenge-2019/
:param R_est: Bx3x3 ndarray with the estimated rotation matrix.
:param t_est: Bx3x1 ndarray with the estimated translation vector.
:param R_gt: Bx3x3 ndarray with the ground-truth rotation matrix.
:param t_gt: Bx3x1 ndarray with the ground-truth translation vector.
:param pts: nx3 ndarray with 3D model points.
:param syms: Set of symmetry transformations, each given by a dictionary with:
- 'R': 3x3 ndarray with the rotation matrix.
- 't': 3x1 ndarray with the translation vector.
:return: The calculated error.
"""
pts_est = misc.transform_pts_Rt(pts, R_est, t_est)
es = []
for sym in syms:
batch_sym_R = sym["R"].unsqueeze(0).repeat(R_gt.shape[0], 1, 1)
batch_sym_t = sym["t"].unsqueeze(0).repeat(t_gt.shape[0], 1, 1)
R_gt_sym = torch.bmm(R_gt, batch_sym_R)
t_gt_sym = torch.bmm(R_gt, batch_sym_t) + t_gt
pts_gt_sym = misc.transform_pts_Rt(pts, R_gt_sym, t_gt_sym)
err = torch.norm(pts_est - pts_gt_sym, dim=2)
max_err = err.max(dim=1).values
es.append(max_err)
es = torch.stack(es, dim=1).min(dim=1).values
gc.collect()
torch.cuda.empty_cache()
return es
@torch.no_grad()
def mspd_by_batch(
R_est, t_est, R_gt, t_gt, K, pts, syms, max_batch_size=MAX_BATCH_SIZE
):
"""
mspd with max_batch_size for R_est, t_est, R_gt, t_gt.
This allows to stabilize the memory usage (1GB for batch_size=200).
"""
batch_R_est = BatchedData(max_batch_size, R_est)
batch_t_est = BatchedData(max_batch_size, t_est)
batch_R_gt = BatchedData(max_batch_size, R_gt)
batch_t_gt = BatchedData(max_batch_size, t_gt)
batch_K = BatchedData(max_batch_size, K)
output = BatchedData(batch_size=max_batch_size)
for i in range(len(batch_R_est)):
output_ = mspd(
batch_R_est[i],
batch_t_est[i],
batch_R_gt[i],
batch_t_gt[i],
batch_K[i],
pts,
syms,
)
output.cat(output_)
return output.data
@torch.no_grad()
def mspd(R_est, t_est, R_gt, t_gt, K, pts, syms):
"""Maximum Symmetry-Aware Projection Distance (MSPD).
See: http://bop.felk.cvut.cz/challenges/bop-challenge-2019/
:param R_est: Bx3x3 ndarray with the estimated rotation matrix.
:param t_est: Bx3x1 ndarray with the estimated translation vector.
:param R_gt: Bx3x3 ndarray with the ground-truth rotation matrix.
:param t_gt: Bx3x1 ndarray with the ground-truth translation vector.
:param K: Bx3x3 ndarray with the intrinsic camera matrix.
:param pts: nx3 ndarray with 3D model points.
:param syms: Set of symmetry transformations, each given by a dictionary with:
- 'R': 3x3 ndarray with the rotation matrix.
- 't': 3x1 ndarray with the translation vector.
:return: The calculated error.
"""
proj_est = misc.project_pts(pts, K, R_est, t_est)
es = []
for sym in syms:
batch_sym_R = sym["R"].unsqueeze(0).repeat(R_gt.shape[0], 1, 1)
batch_sym_t = sym["t"].unsqueeze(0).repeat(t_gt.shape[0], 1, 1)
R_gt_sym = torch.bmm(R_gt, batch_sym_R)
t_gt_sym = torch.bmm(R_gt, batch_sym_t) + t_gt
proj_gt_sym = misc.project_pts(pts, K, R_gt_sym, t_gt_sym)
err = torch.norm(proj_est - proj_gt_sym, dim=2)
max_err = err.max(dim=1).values
es.append(max_err)
es = torch.stack(es, dim=1).min(dim=1).values
gc.collect()
torch.cuda.empty_cache()
return es