Spaces:
Paused
Paused
Use CLIP forward embeddings
Browse files
app.py
CHANGED
|
@@ -109,11 +109,11 @@ PRESETS = {
|
|
| 109 |
DEFAULT_PRESET = "Object names"
|
| 110 |
|
| 111 |
CLIP_CODE = """# CLIP makes text and images comparable by putting them in one space.
|
| 112 |
-
|
| 113 |
-
|
| 114 |
|
| 115 |
-
text_features =
|
| 116 |
-
image_features =
|
| 117 |
|
| 118 |
# Normalize first, then dot product equals cosine similarity.
|
| 119 |
text_features = text_features / text_features.norm(dim=-1, keepdim=True)
|
|
@@ -338,17 +338,18 @@ def to_device(batch, device):
|
|
| 338 |
def encode_clip(texts, images):
|
| 339 |
model, processor, device = load_clip()
|
| 340 |
|
| 341 |
-
|
| 342 |
text=texts,
|
|
|
|
| 343 |
padding=True,
|
| 344 |
truncation=True,
|
| 345 |
return_tensors="pt",
|
| 346 |
)
|
| 347 |
-
image_inputs = processor(images=images, return_tensors="pt")
|
| 348 |
|
| 349 |
with torch.inference_mode():
|
| 350 |
-
|
| 351 |
-
|
|
|
|
| 352 |
text_features = text_features / text_features.norm(dim=-1, keepdim=True)
|
| 353 |
image_features = image_features / image_features.norm(dim=-1, keepdim=True)
|
| 354 |
cosine = text_features @ image_features.T
|
|
|
|
| 109 |
DEFAULT_PRESET = "Object names"
|
| 110 |
|
| 111 |
CLIP_CODE = """# CLIP makes text and images comparable by putting them in one space.
|
| 112 |
+
inputs = processor(text=phrases, images=images, padding=True, truncation=True, return_tensors="pt")
|
| 113 |
+
outputs = model(**inputs)
|
| 114 |
|
| 115 |
+
text_features = outputs.text_embeds
|
| 116 |
+
image_features = outputs.image_embeds
|
| 117 |
|
| 118 |
# Normalize first, then dot product equals cosine similarity.
|
| 119 |
text_features = text_features / text_features.norm(dim=-1, keepdim=True)
|
|
|
|
| 338 |
def encode_clip(texts, images):
|
| 339 |
model, processor, device = load_clip()
|
| 340 |
|
| 341 |
+
inputs = processor(
|
| 342 |
text=texts,
|
| 343 |
+
images=images,
|
| 344 |
padding=True,
|
| 345 |
truncation=True,
|
| 346 |
return_tensors="pt",
|
| 347 |
)
|
|
|
|
| 348 |
|
| 349 |
with torch.inference_mode():
|
| 350 |
+
outputs = model(**to_device(inputs, device))
|
| 351 |
+
text_features = outputs.text_embeds
|
| 352 |
+
image_features = outputs.image_embeds
|
| 353 |
text_features = text_features / text_features.norm(dim=-1, keepdim=True)
|
| 354 |
image_features = image_features / image_features.norm(dim=-1, keepdim=True)
|
| 355 |
cosine = text_features @ image_features.T
|