File size: 5,094 Bytes
7207c65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""RigNet v2: one PointNet-style network for six body plans.
Reads a cloud of surface points, decides which body plan it is, then votes joint positions
and predicts skin weights with that plan's head."""
import numpy as np, time, sys, json
from tinygrad import T, Linear, Adam, cross_entropy, index

FAMS = ["biped", "quadruped", "bird", "fish", "snake", "insect"]
NJ = {"biped": 17, "quadruped": 19, "bird": 17, "fish": 8, "snake": 10, "insect": 21}
OFF = {}; _o = 0
for f in FAMS: OFF[f] = (_o, _o + NJ[f]); _o += NJ[f]
TOT = _o; NP = 512


class RigNet2:
    def __init__(s, seed=0):
        np.random.seed(seed)
        s.l1, s.l2, s.l3 = Linear(3, 64), Linear(64, 128), Linear(128, 256)
        s.ga, s.gb, s.l4 = Linear(128, 256), Linear(256, 256), Linear(256, 128)
        s.cls = Linear(256, len(FAMS))
        s.hs, s.ha, s.ho = Linear(128, TOT), Linear(128, TOT), Linear(128, TOT * 3)
        for p in s.ho.params: p.data *= 0.1
        s.mods = [s.l1, s.l2, s.l3, s.ga, s.gb, s.l4, s.cls, s.hs, s.ha, s.ho]
        s.params = [p for m in s.mods for p in m.params]

    def trunk(s, P):
        B, N, _ = P.shape
        x = T(P)
        f2 = s.l2(s.l1(x).relu()).relu()
        g = s.l3(f2).relu().max(1)
        h = s.l4((s.ga(f2) + s.gb(g).reshape(B, 1, 256)).relu()).relu()
        return g, h

    def save(s, f): np.savez(f, *[p.data for p in s.params])
    def load(s, f):
        d = np.load(f)
        for i, p in enumerate(s.params): p.data = d[f"arr_{i}"]
        return s

    def predict(s, P, fam=None):
        g, h = s.trunk(P[None].astype(np.float32))
        probs = np.exp(s.cls(g).data[0]); probs /= probs.sum()
        fam = fam or FAMS[int(probs.argmax())]
        a, b = OFF[fam]; N = P.shape[0]
        sk = s.hs(h).data[0][:, a:b]; at = s.ha(h).data[0][:, a:b]; of = s.ho(h).data[0].reshape(N, TOT, 3)[:, a:b]
        at = np.exp(at - at.max(0)); at /= at.sum(0)
        J = ((P[:, None, :] + of) * at[..., None]).sum(0)
        w = np.exp(sk - sk.max(1, keepdims=True)); w /= w.sum(1, keepdims=True)
        return fam, probs, J, w


def load_data():
    D = {}
    for f in FAMS:
        P = np.fromfile(f"data/{f}.P.bin", np.float32).reshape(-1, NP, 3)
        J = np.fromfile(f"data/{f}.J.bin", np.float32).reshape(len(P), NJ[f], 3)
        W = np.fromfile(f"data/{f}.W.bin", np.float32).reshape(len(P), NP, NJ[f])
        D[f] = (P, J, W)
    return D


def step_loss(m, batch):
    P = np.concatenate([b[0] for b in batch]); fam_ids = np.concatenate([[FAMS.index(b[3])] * len(b[0]) for b in batch])
    B, N = P.shape[:2]
    g, h = m.trunk(P)
    lc = cross_entropy(m.cls(g), fam_ids)
    sk, at, of = m.hs(h), m.ha(h), m.ho(h).reshape(B, N, TOT, 3)
    total = lc; r0 = 0; stats = {}
    for (Pf, Jf, Wf, f) in batch:
        n = len(Pf); rows = np.arange(r0, r0 + n); r0 += n
        a, b = OFF[f]
        skf = index(sk, (rows[:, None, None], np.arange(N)[None, :, None], np.arange(a, b)[None, None, :]))
        atf = index(at, (rows[:, None, None], np.arange(N)[None, :, None], np.arange(a, b)[None, None, :])).softmax(1)
        off = index(of, (rows[:, None, None, None], np.arange(N)[None, :, None, None], np.arange(a, b)[None, None, :, None], np.arange(3)[None, None, None, :]))
        votes = off + T(Pf[:, :, None, :])
        J = (votes * atf.reshape(n, N, b - a, 1)).sum(1)
        dj = J - T(Jf); dv = votes - T(Jf[:, None])
        lf = (dj * dj).mean() * 100 + (dv * dv).mean() * 10 + cross_entropy(skf, Wf, soft=True)
        total = total + lf * (n / B)
        stats[f] = (J.data, skf.data)
    return total, stats, g


def evaluate(m, D):
    res = {}
    for f in FAMS:
        P, J, W = D[f]; vi = np.arange(len(P) - 50, len(P))
        _, st, g = step_loss(m, [(P[vi], J[vi], W[vi], f)])
        Jp, sk = st[f]
        err = np.linalg.norm(Jp - J[vi], axis=-1).mean()
        acc = (sk.argmax(-1) == W[vi].argmax(-1)).mean()
        cl = (m.cls(g).data.argmax(-1) == FAMS.index(f)).mean()
        res[f] = (float(err), float(acc), float(cl))
    return res


def train(steps=4000):
    D = load_data(); m = RigNet2(); opt = Adam(m.params, lr=2e-3)
    rng = np.random.default_rng(0); log = []; t0 = time.time()
    for it in range(steps):
        batch = []
        for f in FAMS:
            P, J, W = D[f]; idx = rng.integers(0, len(P) - 50, 3)
            p = P[idx] + rng.normal(0, 0.003, P[idx].shape).astype(np.float32)
            batch.append((p, J[idx], W[idx], f))
        loss, _, _ = step_loss(m, batch); loss.backward()
        opt.step(2e-3 * 0.5 * (1 + np.cos(np.pi * it / steps)) + 1e-5)
        if it % 200 == 0 or it == steps - 1:
            ev = evaluate(m, D); log.append((it, float(loss.data), ev))
            print(it, f"loss {loss.data:.3f}", " ".join(f"{f[:4]}:{e*100:.1f}%/{a*100:.0f}%/{c*100:.0f}%" for f, (e, a, c) in ev.items()), f"{time.time()-t0:.0f}s", flush=True)
            m.save("rig2.npz"); json.dump(log, open("rig2_log.json", "w"))
    m.save("rig2.npz"); json.dump(log, open("rig2_log.json", "w"))


if __name__ == "__main__":
    train(int(sys.argv[1]) if len(sys.argv) > 1 else 4000)