AbstractPhil commited on
Commit
112bbeb
·
verified ·
1 Parent(s): ce2e190

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +126 -0
README.md ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ base_model:
4
+ - openai/clip-vit-base-patch32
5
+ new_version: AbstractPhil/geovit-32-32d
6
+ datasets:
7
+ - AbstractPhil/geometric-vocab-32d
8
+ tags:
9
+ - experiment
10
+ ---
11
+
12
+ A first experiment to test and convert clip-vit-base-patch32 into a geometric model by using only a classification head.
13
+
14
+ Below is GPT 5's auto-generated dictation based on the notebook. I'll include the full notebook in a moment here.
15
+
16
+ The question was simple; can linear layers learn geometric?
17
+
18
+ The answer is... maybe. More research required.
19
+
20
+ # Notebook-6 · Crystal-CLIP CIFAR-100
21
+
22
+ One-vector image embeddings (HF CLIP) + pentachora vocabulary anchors → cosine-similarity classifier for CIFAR-100.
23
+ This repo hosts the trained crystal classification head (+ run configs/metrics) built in Notebook 6.
24
+
25
+ ---
26
+
27
+ OVERVIEW
28
+ - Vision encoder: openai/clip-vit-base-patch32 (Hugging Face transformers), frozen by default.
29
+ Produces exactly one L2-normalized embedding per image (image_embeds, dim=512).
30
+ - Vocabulary: AbstractPhil/geometric-vocab-512d (pentachora crystals).
31
+ For CIFAR-100 class names, any missing tokens are deterministically synthesized via the unicode path to guarantee 100/100 coverage and preserve class ordering.
32
+ - Head: projects both image embeddings (De=512) and role-selected class anchors (Dv=512) into a shared symbol space (crystal_dims=128), L2-normalizes, and computes cosine logits divided by T (temperature).
33
+ - Training: Cross-Entropy on CIFAR-100, AdamW, optional AMP, cosine LR with warmup. Best checkpoint is saved and (optionally) pushed to Hugging Face.
34
+
35
+ ---
36
+
37
+ MODEL CARD
38
+ - Task: Image Classification (CIFAR-100)
39
+ - Backbone: openai/clip-vit-base-patch32 (vision-only)
40
+ - Head: Crystal projection head (image 512→128, anchor 512→128) + cosine logits (temperature)
41
+ - Vocabulary: AbstractPhil/geometric-vocab-512d (wordnet_eng split + deterministic unicode synth for gaps)
42
+ - Metrics: Top-1 = [80~], Top-3 = [90>]
43
+ - License: MIT
44
+
45
+ ---
46
+
47
+ FILES IN THIS REPO
48
+ - <run_name>_best.safetensors — weights for:
49
+ - head::* (crystal classifier head)
50
+ - encoder::* (optional, if you chose to unfreeze/fine-tune)
51
+ - <run_name>_best.config.json — full CONFIG used for the run
52
+ - <run_name>_best.metrics.json — summary metrics for the best epoch
53
+ - Optionally: *_latest.* variants if you pushed latest per-epoch artifacts.
54
+
55
+ Note: If you only want to ship the head, you can also include a stripped crystal_head.safetensors (head-only state_dict). The snippets below handle either format.
56
+
57
+ ---
58
+
59
+ QUICKSTART (Inference)
60
+ 1) Load CLIP vision (frozen) and processor
61
+ HF_CLIP_ID = "openai/clip-vit-base-patch32"
62
+ Processor = AutoImageProcessor.from_pretrained(HF_CLIP_ID)
63
+ Encoder = CLIPVisionModelWithProjection.from_pretrained(HF_CLIP_ID).eval().to("cuda")
64
+
65
+ 2) Build the crystal head (same shape as training)
66
+ image_dim = Encoder.config.projection_dim # 512
67
+ crystal_dim = 512 # vocab repo uses 512D anchors
68
+ sym_dim = 128 # crystal_dims from CONFIG
69
+ temperature = 0.07 # from CONFIG
70
+
71
+ class CrystalHead(torch.nn.Module):
72
+ def __init__(self, De, Dv, Dsym, T):
73
+ super().__init__()
74
+ self.proj_img = torch.nn.Linear(De, Dsym, bias=True)
75
+ self.proj_anc = torch.nn.Linear(Dv, Dsym, bias=False)
76
+ self.T = T
77
+ self.register_buffer("anchors_vocab", torch.empty(0, Dv), persistent=False)
78
+ def set_anchors(self, anchors): # [C, Dv]
79
+ self.anchors_vocab = anchors.contiguous()
80
+ def forward(self, image_embeds): # [B, De] (L2 ok)
81
+ z = torch.nn.functional.normalize(self.proj_img(image_embeds), dim=-1)
82
+ a = torch.nn.functional.normalize(self.proj_anc(self.anchors_vocab), dim=-1)
83
+ return (z @ a.T) / max(1e-8, self.T) # [B, C]
84
+
85
+ head = CrystalHead(De=image_dim, Dv=crystal_dim, Dsym=sym_dim, T=temperature).to("cuda")
86
+
87
+ 3) Load weights (handles prefixed multi-module .safetensors)
88
+ state = safetensors.torch.load_file("<run_name>_best.safetensors")
89
+ head_state = {k.split("head::",1)[1]: v for k,v in state.items() if k.startswith("head::")}
90
+ head.load_state_dict(head_state, strict=True)
91
+
92
+ 4) Prepare anchors from your vocabulary (same order as training)
93
+ You likely already exported anchors or can rebuild them exactly as in Notebook 6.
94
+ anchors: torch.Tensor of shape [100, 512]
95
+ head.set_anchors(anchors.to("cuda"))
96
+
97
+ 5) Inference on a batch of images (PIL or ndarray)
98
+ imgs = [PIL.Image.open("example_0.png").convert("RGB"), PIL.Image.open("example_1.png").convert("RGB")]
99
+ batch = Processor(images=imgs, return_tensors="pt").to("cuda")
100
+ with torch.no_grad():
101
+ out = Encoder(pixel_values=batch["pixel_values"], return_dict=True)
102
+ z = torch.nn.functional.normalize(out.image_embeds, dim=-1) # [B, 512]
103
+ logits = head(z) # [B, 100]
104
+ pred = logits.argmax(dim=-1).tolist()
105
+ print("pred:", pred)
106
+
107
+ Note: The head expects the same class order used at training time. Save and ship class_names.json (CIFAR-100 labels) and the exact anchors_vocab.pt you used (or rebuild deterministically with the vocab + synth step).
108
+
109
+ ---
110
+
111
+ REPRODUCE (Notebook 6)
112
+ 1. Config only (single source of truth): image size, CLIP stats, dataset, temperature, crystal dims, etc.
113
+ 2. Cell 5 – HF CLIP vision loader (one embedding per image).
114
+ 3. Cell 6 – Vocabulary interface; synth any missing CIFAR tokens, cache crystals, select role anchors.
115
+ 4. Cell 8 – Crystal head (image+anchor projections → cosine logits / T).
116
+ 5. Cell 9 – Trainer (AdamW + AMP + cosine LR). Saves latest/best, pushes to HF if enabled.
117
+
118
+
119
+ Replace with your final numbers after the run completes.
120
+
121
+ ---
122
+
123
+ ACKNOWLEDGEMENTS
124
+ - CLIP ViT-B/32: OpenAI (openai/clip-vit-base-patch32) via Hugging Face transformers.
125
+ - Pentachora Vocabulary: AbstractPhil/geometric-vocab-512d.
126
+ - Built in Notebook 6 (CONFIG-first, deterministic synth for gaps, head-only training).