Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Norrrrrrr lyn WAInjectBench extract embeddings

From Leeroopedia
Knowledge Sources
Domains Computer_Vision, Feature_Engineering
Last Updated 2026-02-14 16:00 GMT

Overview

Concrete tool for extracting L2-normalized CLIP embeddings from a list of image file paths, provided by the WAInjectBench image embedding trainer.

Description

The extract_embeddings function in train/embedding-i.py iterates over image paths, opens each with PIL, applies CLIP preprocessing, encodes with model.encode_image(), L2-normalizes the result, and converts to a numpy array. On failure, it substitutes a zero vector of the model's output dimension. Uses torch.no_grad() for inference efficiency and includes a tqdm progress bar.

Usage

Called once per JSONL training file to convert all image samples into embedding vectors for classifier training.

Code Reference

Source Location

Signature

def extract_embeddings(image_paths, model, preprocess, device):
    embeddings = []
    for path in tqdm(image_paths, desc="Embedding images"):
        try:
            image = Image.open(path).convert("RGB")
            image = preprocess(image).unsqueeze(0).to(device)
            with torch.no_grad():
                emb = model.encode_image(image)
                emb = emb / emb.norm(dim=-1, keepdim=True)  # normalize
            embeddings.append(emb.cpu().numpy().flatten())
        except Exception as e:
            print(f"Failed to process {path}: {e}")
            embeddings.append(np.zeros(model.visual.output_dim))
    return np.array(embeddings)

Import

import torch
import numpy as np
from PIL import Image
from tqdm import tqdm

I/O Contract

Inputs

Name Type Required Description
image_paths List[str] Yes List of image file paths
model open_clip.CLIP Yes Loaded CLIP model
preprocess callable Yes Image preprocessing transform from open_clip
device torch.device Yes Target device (cuda or cpu)

Outputs

Name Type Description
embeddings np.ndarray Shape (N, 512) array of L2-normalized CLIP embeddings

Usage Examples

Extracting Image Embeddings

import open_clip
import torch

model, _, preprocess = open_clip.create_model_and_transforms("ViT-B-32", pretrained="laion2b_s34b_b79k")
device = torch.device("cuda")
model = model.to(device)

image_paths = ["img/1.png", "img/2.png", "img/3.png"]
embeddings = extract_embeddings(image_paths, model, preprocess, device)
print(f"Shape: {embeddings.shape}")  # (3, 512)

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment