Spaces:
Sleeping
Sleeping
Download src/scripts/test_embedding.py from build-small-hackathon/wardrobe-ai: direct link, hf CLI and curl.
- Browser
- Download file 1 kB
-
https://huggingface.co/spaces/build-small-hackathon/wardrobe-ai/resolve/main/src/scripts/test_embedding.py
- Command line
-
hf download hf://spaces/build-small-hackathon/wardrobe-ai/src/scripts/test_embedding.py
-
curl -L -o test_embedding.py https://huggingface.co/spaces/build-small-hackathon/wardrobe-ai/resolve/main/src/scripts/test_embedding.py
1 kB
| from pathlib import Path | |
| import pandas as pd | |
| from PIL import Image | |
| from sentence_transformers import SentenceTransformer | |
| from utils.utils import SRC_PATH | |
| image_paths = sorted((SRC_PATH / "data" / "images").glob("*.jpg")) | |
| if not image_paths: | |
| raise FileNotFoundError(f"No JPG images found in {SRC_PATH / 'data'}") | |
| labels = [ | |
| "top (tshirt, shirt, polo...)", | |
| ] | |
| model = SentenceTransformer( | |
| "sentence-transformers/clip-ViT-B-32", | |
| device="cpu", | |
| ) | |
| images = [Image.open(path).convert("RGB") for path in image_paths] | |
| image_embeddings = model.encode( | |
| images, | |
| batch_size=16, | |
| normalize_embeddings=True, | |
| convert_to_numpy=True, | |
| show_progress_bar=True, | |
| ) | |
| text_embeddings = model.encode( | |
| prompts, | |
| normalize_embeddings=True, | |
| convert_to_numpy=True, | |
| ) | |
| similarities = image_embeddings @ text_embeddings.T | |
| similarity_df = pd.DataFrame( | |
| similarities, | |
| index=[path.name for path in image_paths], | |
| columns=prompts, | |
| ) | |
| print(similarity_df.round(3).to_string()) |