File size: 2,490 Bytes
5b2e6c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python
"""Known-answer checks for directional node/edge conditions."""
from pathlib import Path
import sys

import torch

REPO_ROOT = Path(__file__).resolve().parents[1]
if str(REPO_ROOT) not in sys.path:
    sys.path.insert(0, str(REPO_ROOT))

from bbflow.models.latent import LatentEncoder


class _Cfg:
    dim = 16
    num_modes = 2
    motion_hidden_dim = 16
    motion_dist_bins = 4
    motion_dist_width = 4.0
    motion_message_passing_layers = 1
    template_residual_scale = 0.1
    template_remove_rotation = True


def main():
    torch.manual_seed(7)
    encoder = LatentEncoder(_Cfg()).eval()
    trans = torch.tensor([[[0.0, 0.0, 0.0], [1.2, 0.1, 0.0], [0.3, 1.1, 0.2], [1.0, 1.2, 0.5], [0.2, 0.4, 1.3]]])
    rotmats = torch.eye(3).reshape(1, 1, 3, 3).repeat(1, trans.shape[1], 1, 1)
    seq = torch.zeros(1, trans.shape[1], 21)
    seq[0, torch.arange(trans.shape[1]), torch.arange(trans.shape[1]) % 21] = 1.0
    mask = torch.ones(1, trans.shape[1])
    rotation = torch.tensor([[0.0, -1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]])

    with torch.no_grad():
        condition, _ = encoder.forward_prior(trans, rotmats, seq, mask)
        trans_rot = torch.einsum("ij,bnj->bni", rotation, trans)
        rotmats_rot = torch.einsum("ij,bnjk->bnik", rotation, rotmats)
        condition_rot, _ = encoder.forward_prior(trans_rot, rotmats_rot, seq, mask)

    disp_local_diff = (condition_rot["disp_node_cond"].float() - condition["disp_node_cond"].float()).abs().max()
    disp_edge_diff = (condition_rot["disp_edge_cond"].float() - condition["disp_edge_cond"].float()).abs().max()
    expected_u = torch.einsum("ij,bnkj->bnki", rotation, condition["u_global"].float())
    u_global_diff = (condition_rot["u_global"].float() - expected_u).abs().max()

    print("disp_node_cond_shape =", tuple(condition["disp_node_cond"].shape))
    print("disp_edge_cond_shape =", tuple(condition["disp_edge_cond"].shape))
    print("disp_local_equivariance_max_abs_diff =", float(disp_local_diff.item()))
    print("disp_edge_invariance_max_abs_diff =", float(disp_edge_diff.item()))
    print("u_global_equivariance_max_abs_diff =", float(u_global_diff.item()))

    assert tuple(condition["disp_node_cond"].shape) == (1, trans.shape[1], 3)
    assert tuple(condition["disp_edge_cond"].shape) == (1, trans.shape[1], trans.shape[1], 1)
    assert disp_local_diff < 1e-4
    assert disp_edge_diff < 1e-4
    assert u_global_diff < 1e-4


if __name__ == "__main__":
    main()