Image Feature Extraction
Transformers
Safetensors
clip_vitb_mini
feature-extraction
clip
knowledge-distillation
consensus-distillation
vit
custom_code
Instructions to use AbstractPhil/clip-vitb-mini-distilled with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use AbstractPhil/clip-vitb-mini-distilled with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-feature-extraction", model="AbstractPhil/clip-vitb-mini-distilled", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("AbstractPhil/clip-vitb-mini-distilled", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
cc12m champion release: loader.py
Browse files
loader.py
CHANGED
|
@@ -72,9 +72,22 @@ def load_model(path, device=None):
|
|
| 72 |
return model.to(device).eval()
|
| 73 |
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
@torch.no_grad()
|
| 76 |
-
def embed_images(model, x01, batch=256):
|
| 77 |
-
"""x01: (B,3,H,W) float in [0,1]. Returns L2-normalized (B,512).
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
dev = next(model.parameters()).device
|
| 79 |
mean = torch.tensor(CLIP_MEAN).view(1, 3, 1, 1).to(dev)
|
| 80 |
std = torch.tensor(CLIP_STD).view(1, 3, 1, 1).to(dev)
|
|
@@ -83,5 +96,9 @@ def embed_images(model, x01, batch=256):
|
|
| 83 |
x = x01[i:i + batch].to(dev)
|
| 84 |
x = F.interpolate(x, size=(160, 160), mode="bicubic",
|
| 85 |
align_corners=False).clamp(0, 1)
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
return torch.cat(out)
|
|
|
|
| 72 |
return model.to(device).eval()
|
| 73 |
|
| 74 |
|
| 75 |
+
def load_rotation(path):
|
| 76 |
+
"""Load a frame rotation shipped beside a consensus-distilled student.
|
| 77 |
+
The rotation maps the student's consensus frame into the CLIP-B/16
|
| 78 |
+
(LAION-2B) deployment frame; it was fitted once, offline, on 2,500
|
| 79 |
+
COCO-val pairs (fp64 orthogonal Procrustes) and is FROZEN."""
|
| 80 |
+
d = torch.load(path, map_location="cpu", weights_only=True)
|
| 81 |
+
return d["R"] if isinstance(d, dict) else d
|
| 82 |
+
|
| 83 |
+
|
| 84 |
@torch.no_grad()
|
| 85 |
+
def embed_images(model, x01, batch=256, rotation=None):
|
| 86 |
+
"""x01: (B,3,H,W) float in [0,1]. Returns L2-normalized (B,512).
|
| 87 |
+
Pass `rotation` (from load_rotation) with the consensus-distilled
|
| 88 |
+
champion to place outputs in the LAION text tower's frame — required
|
| 89 |
+
for zero-shot/retrieval against that tower, and how every headline
|
| 90 |
+
number on the card was measured."""
|
| 91 |
dev = next(model.parameters()).device
|
| 92 |
mean = torch.tensor(CLIP_MEAN).view(1, 3, 1, 1).to(dev)
|
| 93 |
std = torch.tensor(CLIP_STD).view(1, 3, 1, 1).to(dev)
|
|
|
|
| 96 |
x = x01[i:i + batch].to(dev)
|
| 97 |
x = F.interpolate(x, size=(160, 160), mode="bicubic",
|
| 98 |
align_corners=False).clamp(0, 1)
|
| 99 |
+
z = F.normalize(model((x - mean) / std), dim=-1)
|
| 100 |
+
if rotation is not None:
|
| 101 |
+
z = F.normalize(z.double() @ rotation.to(dev).double(),
|
| 102 |
+
dim=-1).float()
|
| 103 |
+
out.append(z.cpu())
|
| 104 |
return torch.cat(out)
|