File size: 3,423 Bytes
ca17769
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 道の評価をまとめて出す(これからの流しは全部これを使う)
#  位置のずれ(ATE): 時刻で対応(0.02 秒以内)→ 道全体を 1 回だけ大きさ・向き・位置を合わせる → コマごとの差の 2 乗平均の平方根(cm)
#  向きのずれ: 同じ合わせ方のあと、コマごとの「推定の向き」と「正解の向き」の角度の差の 2 乗平均の平方根(度)
#  1 秒あたりのずれ(RPE、TUM の公式の evaluate_rpe.py と同じ考え方): 1 秒離れた 2 コマのあいだの動きを正解と比べる。位置 cm/秒・向き 度/秒(2 乗平均の平方根)
import numpy as np
from scipy.spatial.transform import Rotation as Rot

def evaluate(c2w, gt_pos, gt_quat, gt_dt, stamps, delta=1.0):
    """c2w: {コマ: 4x4}、gt_pos [N,3]・gt_quat [N,4](x,y,z,w)・gt_dt [N](正解との時刻の差)・stamps [N](写真の時刻)"""
    idx = [i for i in sorted(c2w) if gt_dt[i] < 0.02]
    X = np.array([c2w[i][:3, 3] for i in idx]); Y = gt_pos[idx]
    mx, my = X.mean(0), Y.mean(0); Xc, Yc = X - mx, Y - my
    U, D, Vt = np.linalg.svd(Yc.T @ Xc / len(X)); Sg = np.eye(3); Sg[2, 2] = np.sign(np.linalg.det(U @ Vt))
    R = U @ Sg @ Vt; sc = np.trace(np.diag(D) @ Sg) / max(Xc.var(0).sum(), 1e-12); E = (sc * (R @ X.T)).T + (my - sc * R @ mx)
    e = np.linalg.norm(E - Y, axis=1) * 100
    Rg = Rot.from_quat(gt_quat[idx]).as_matrix(); Re = R[None] @ np.stack([c2w[i][:3, :3] for i in idx])
    Erot = Rot.from_matrix(np.einsum("nji,njk->nik", Rg, Re)); ang = np.degrees(Erot.magnitude())
    off = Erot.mean(); ang2 = np.degrees((Erot * off.inv()).magnitude())   # カメラの側の一定のずれ(取りつけの角度の差とみられる)を除いたもの
    # 1 秒あたりのずれ
    ts = stamps[idx]; rt, rr = [], []
    Pe = {i: c2w[i].copy() for i in idx}
    for i in Pe: Pe[i][:3, 3] = sc * Pe[i][:3, 3]                  # 大きさは道全体で合わせた値をかける
    Pg = {}
    for k, i in enumerate(idx):
        T = np.eye(4); T[:3, :3] = Rg[k]; T[:3, 3] = Y[k]; Pg[i] = T
    for a, i in enumerate(idx):
        b = np.searchsorted(ts, ts[a] + delta)
        if b >= len(idx) or abs(ts[b] - ts[a] - delta) > 0.05: continue
        j = idx[b]; dt = ts[b] - ts[a]
        Eg = np.linalg.inv(Pg[i]) @ Pg[j]; Ee = np.linalg.inv(Pe[i]) @ Pe[j]; Er = np.linalg.inv(Eg) @ Ee
        rt.append(np.linalg.norm(Er[:3, 3]) * 100 / dt); rr.append(np.degrees(Rot.from_matrix(Er[:3, :3]).magnitude()) / dt)
    rt, rr = np.array(rt), np.array(rr)
    return dict(n=len(idx), rmse=float(np.sqrt((e ** 2).mean())), mean=float(e.mean()), max=float(e.max()),
                rot_rmse=float(np.sqrt((ang ** 2).mean())), rot_mean=float(ang.mean()), rot_max=float(ang.max()),
                rot2_rmse=float(np.sqrt((ang2 ** 2).mean())), rot2_mean=float(ang2.mean()), rot_off=float(np.degrees(off.magnitude())),
                rpe_t=float(np.sqrt((rt ** 2).mean())) if len(rt) else float("nan"), rpe_r=float(np.sqrt((rr ** 2).mean())) if len(rr) else float("nan"))

def line(r):
    return (f"位置のずれ {r['rmse']:.2f}cm(平均 {r['mean']:.2f}・最大 {r['max']:.1f})  向きのずれ {r['rot_rmse']:.2f}°(一定のずれ {r['rot_off']:.2f}° を除くと {r['rot2_rmse']:.2f}°)  "
            f"1 秒あたり 位置 {r['rpe_t']:.2f}cm/秒・向き {r['rpe_r']:.2f}°/秒")