File size: 1,858 Bytes
25dab88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python
"""Free local test of on-the-fly visual learning (ConceptMemory, synthetic embeddings)."""
import sys
from pathlib import Path

import numpy as np

HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE))


def ok(m): print("  [PASS]", m)
def fail(m): print("  [FAIL]", m); sys.exit(1)


def main():
    print("VISION checks (concept memory, free):")
    from core.vision import ConceptMemory

    m = ConceptMemory()
    # empty memory recognizes nothing
    if m.recognize(np.ones(8))[0] is not None:
        fail("empty memory should recognize nothing")
    ok("empty memory -> (None, 0)")

    # teach two distinct concepts on the fly
    a = np.array([1, 0, 0, 0, 0, 0, 0, 0], dtype=float)
    b = np.array([0, 0, 0, 0, 1, 0, 0, 0], dtype=float)
    m.learn(a, "my custom widget")
    m.learn(b, "the blue gizmo")
    ok("learned 2 concepts instantly (no training)")

    # a noisy version of 'a' is recognized as the widget with high similarity
    a_noisy = a + 0.05 * np.random.RandomState(0).randn(8)
    label, sim = m.recognize(a_noisy)
    if label != "my custom widget" or sim < 0.9:
        fail(f"recognize failed: {label} {sim}")
    ok(f"recognizes a just-taught concept: '{label}' (sim {sim:.2f})")

    # the other concept is not confused
    if m.recognize(b)[0] != "the blue gizmo":
        fail("second concept misrecognized")
    ok("distinct concepts stay distinct")

    # export-for-distill then clear (knowledge -> weights -> discard raw)
    pairs = m.export_for_distill()
    if len(pairs) != 2 or "label" not in pairs[0]:
        fail("export_for_distill wrong shape")
    m.clear()
    if m.embs:
        fail("clear() did not discard raw")
    ok("export-for-distill + discard-raw works (on-the-fly -> weighted)")

    print("VISION checks passed.\n")


if __name__ == "__main__":
    main()