AbstractPhil commited on
Commit
4b3c4dc
·
verified ·
1 Parent(s): 990151e

Create README.md

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