AbstractPhil commited on
Commit
a403c2d
·
verified ·
1 Parent(s): ad058e2

cc12m champion release: loader.py

Browse files
Files changed (1) hide show
  1. loader.py +20 -3
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
- out.append(F.normalize(model((x - mean) / std), dim=-1).cpu())
 
 
 
 
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)