handicate-code / test_vision.py
AmongTheCouch23's picture
Upload folder using huggingface_hub
25dab88 verified
Raw
History Blame Contribute Delete
1.86 kB
#!/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()