Search an image corpus with a text query: a CLIP-family checkpoint’s text tower encodes the query, and its vision tower built the image index, both projecting into one shared space.
When to use this pattern. You have images (figures, drawings, photos) and want to find them by describing them in words, with no captions to index.
7.1 Model
The default is the hermetic tiny_open_clip fixture (random weights), so the recipe runs offline; it checks the mechanics and prints the ranking without judging it. For the semantics, point it at a trained checkpoint:
An OpenCLIP-format checkpoint carries a vision tower and a text tower that project into one shared space, so a text query encoded by the text tower searches an image index directly: no separate text encoder, no projection bridge. JAMMI_CROSS_MODAL_MODEL names the checkpoint; the default is the random-weight tiny_open_clip fixture.
The corpus is 20 synthetic drawings in five shape families, held as PNG bytes in a Parquet source. generate_embeddings with modality="image" runs the vision tower over them.
images = [ (p.stem, p.read_bytes()) for p insorted(fixtures.path("tiny_image_corpus").glob("img_*.png"))]assert images, "no corpus images"pq.write_table( pa.table( {"figure_id": pa.array([i for i, _ in images], type=pa.utf8()),"image": pa.array([b for _, b in images], type=pa.binary()), } ), home /"figures.parquet",)db.add_source("figures", url=str(home /"figures.parquet"), format="parquet")db.generate_embeddings( source="figures", model=MODEL, columns=["image"], key="figure_id", modality="image")
encode_query runs the same model’s text tower (text is the default modality), and search ranks the image index against that vector.
prompt ="a drawing of a circle"query_vec = db.encode_query(model=MODEL, query=prompt)results = db.search("figures", query=query_vec, k=K)ranked =list(zip(results.column("figure_id").to_pylist(), results.column("similarity").to_pylist()))print(f"text query {prompt!r} -> top-{K} images:")for image_id, similarity in ranked:print(f" {image_id:<18} similarity {similarity:.4f}")
text query 'a drawing of a circle' -> top-5 images:
img_grating_1 similarity 0.2854
img_grating_0 similarity 0.2742
img_hexagon_3 similarity 0.2730
img_square_1 similarity 0.2728
img_circle_1 similarity 0.2711
7.4 The ranking is the shared space’s
Encode every image on its own through the vision tower, score each against the text vector by cosine in plain Python, and the top five — ids and scores — are the search’s. That holds for any checkpoint.
def cosine(a: list[float], b: list[float]) ->float: dot =sum(x * y for x, y inzip(a, b))return dot / (math.sqrt(sum(x * x for x in a)) * math.sqrt(sum(y * y for y in b)))image_vectors = { image_id: db.encode_query(model=MODEL, query=png, modality="image") for image_id, png in images}assertall(len(v) ==len(query_vec) for v in image_vectors.values()), ("the text and vision towers project into one space of one width")scored =sorted( ((cosine(query_vec, v), i) for i, v in image_vectors.items()), key=lambda s: (-s[0], s[1]))oracle = [(i, s) for s, i in scored[:K]]assert [i for i, _ in ranked] == [i for i, _ in oracle], (ranked, oracle)for (_, got), (_, want) inzip(ranked, oracle):assertabs(got - want) <1e-4, (ranked, oracle)print(f"top-{K} equals the cosine ranking in the shared space (dim {len(query_vec)})")
top-5 equals the cosine ranking in the shared space (dim 16)
7.5 With a trained checkpoint
A trained model ranks first an image of the family its prompt names. With the random-weight fixture this step has nothing to show and prints nothing.
if MODEL != DEFAULT_MODEL:for name in FAMILIES: vec = db.encode_query(model=MODEL, query=f"a drawing of a {name}") best = db.search("figures", query=vec, k=1).column("figure_id").to_pylist()[0]print(f"'a drawing of a {name}' -> {best}")