OneHex
OneHex · ML/CV and LLM engineering notes

← All posts

Class recall was 91%. One lighting condition was 0%.

I wanted a local “find me a similar photo” feature that did not call an embedding API and did not stand up a vector database.

The version I actually ran is small. Eight hundred synthetic images, a 260-dimensional descriptor, a cosine search that is just a dot product. On this machine a query embedding took 0.823 milliseconds at p50, and the gallery lookup was 0.012 milliseconds. The index was 832,000 bytes. Class recall at rank 1 was 91.12%.

Then I sliced the queries by lighting, the way I would slice a classification eval, and asked a stricter question: did we get the same object back, not just the same class? Overall instance recall was 27.75%. On the warm queries it was 0.00%.

The systems part was fine. The representation was searching the lighting.

I did not pull a public dataset. I generated the images with Pillow so the file is self-contained. Five classes — mug, bottle, box, ball, can — forty instances each, and four conditions: clear, warm, dim, and occluded. That is 800 images. Instances inside a class share a color palette on purpose. If every mug is a unique hue, mean RGB becomes a cheat code and you will not notice the representation is thin.

The conditions are boring on purpose. Dim is a brightness and contrast drop. Occlusion is a dark bar. Warm is not “a bit yellow.” It is a per-channel scale plus an offset, with clipping:

# from apply_condition()
arr[..., 0] = np.clip(arr[..., 0] * 1.25 + 12, 0, 255)
arr[..., 1] = np.clip(arr[..., 1] * 1.02, 0, 255)
arr[..., 2] = np.clip(arr[..., 2] * 0.72 - 8, 0, 255)

That last one is the trap I wanted in the set. A global dim is mostly a scale. A color-temperature shift is not.

The eval is leave-one-out. For each image I search the other 799, with self excluded, and count a hit if the same instance appears in the top k. I also count a looser hit if only the class matches. Random chance for instance recall at rank 1 is 3/799, a little under 0.4%. Random chance for class recall is 159/799, just under 20%. If a method cannot beat those, it is not searching.

# Cosine is a dot product because vectors are L2-normalized.
scores = vectors @ vectors.T
np.fill_diagonal(scores, -np.inf)
order = np.argsort(-scores, axis=1)

Mean color, then a bigger vector

The baseline is the mean RGB of a 64×64 resize, L2-normalized. Three dimensions. It took 0.158 milliseconds at p50 to embed, and the index was 9,600 bytes.

def embed_mean_color(img: Image.Image) -> np.ndarray:
    arr = _resize(img)
    vec = arr.reshape(-1, 3).mean(axis=0)
    n = np.linalg.norm(vec) + 1e-8
    return (vec / n).astype(np.float32)

On this run it printed:

recall@1    instance=9.12%  class=47.00%
  query=clear   n=200   instance=14.00% class=50.00%
  query=warm    n=200   instance=0.00%  class=35.50%
  query=dim n=200   instance=8.50%  class=49.00%
  query=occluded    n=200   instance=14.00% class=53.50%

Better than chance, barely, and already dead on the warm slice. I expected that. Mean color has nothing else to hold onto.

So I made a 260-d descriptor: an 8×8 grid of mean RGB (192 dims), a 12-bin histogram per channel (36), and a 2×2 × 8-bin gradient orientation histogram (32). Layout, color, a little shape. All L2-normalized, so a search is one matrix-vector product.

def embed_cheap(img: Image.Image) -> np.ndarray:
    arr = _resize(img, 64)
    cells = arr.reshape(8, 8, 8, 8, 3).mean(axis=(1, 3)).reshape(-1)
    # color histograms and 2x2 gradient orientations follow
    vec = np.concatenate([cells, hist, ori])
    n = np.linalg.norm(vec) + 1e-8
    return (vec / n).astype(np.float32)

It was worse at the thing I cared about.

recall@1    instance=0.25%  class=58.88%

Class recall went up a bit. Instance recall fell to chance. The extra dimensions did not forget the lighting. Most of the vector is a spatial RGB grid. Warm, dim, and a dark bar all rewrite those cells. After L2 normalization, a dim mug is closer to other dim images than to the same mug in clear light. I had built a lighting detector and called it a search index.

Chromaticity helps, until it does not

The next version keeps the same layout. Spatial color becomes chromaticity, r,g,b divided by their sum, so a global brightness scale moves the image less. Gradients run on a mean-normalized gray image. Near-black pixels, the occlusion bar, are downweighted in the histograms.

total = arr.sum(axis=2, keepdims=True) + 1e-6
chroma = arr / total
gray = arr.mean(axis=2)
gray = gray / (gray.mean() + 1e-6)

This one I would actually keep, with a warning stapled to it.

method  cheap_invariant
n=800   dim=260 index_bytes=832000
embed_ms_p50=0.823  embed_ms_p95=0.936
query_lookup_ms_p50=0.012   query_lookup_ms_p95=0.014
recall@1    instance=27.75% class=91.12%
  query=clear   n=200   instance=56.50% class=99.00%
  query=warm    n=200   instance=0.00%  class=95.00%
  query=dim n=200   instance=51.50% class=97.00%
  query=occluded    n=200   instance=3.00%  class=73.50%

Clear and dim can find each other. Query clear, look for the dim photo of the same instance: 56.50% at rank 1. The other direction was 51.50%. That is the case chromaticity is supposed to help, and it did. Class recall on those two slices is in the high nineties.

The overall instance number, 27.75%, is a mush of those two decent slices and two failed ones. Warm is still 0.00%. Occlusion is 3.00%. If I had only reported class recall, or only the average, I would have shipped a slide that said the search works.

Widening the list does not rescue the bad slices. At rank 5, instance recall only moves to 33.38%, and class recall reaches 98.00%. The clear and dim queries pick up a few more hits (62.00% and 59.00%). Occlusion goes from 3.00% to 12.50%. The warm queries stay at 0.00%. If the right photo is not even in the top five, looking farther is not a product strategy. The descriptor did not almost get there.

Why the warm slice is zero

Chromaticity cancels a scale, not a color-temperature shift. The warm transform multiplies the channels by different factors and then adds an offset, then clips. R/(R+G+B) is not invariant to that. I knew this and still wanted the number in the table, because it is the kind of “invariance” that looks right in a comment and is wrong in the pixels.

The pair table is blunt about it. Every warm → anything path is 0.00% instance recall at rank 1 and at rank 5. The method does not almost find the clear photo of the same mug. It never does, not in the top five, not once in 200 warm queries.

On the clear queries it missed, 87 of 200, the top-1 neighbor was still the same class 97.70% of the time. So when lighting is stable, the cheap descriptor is mostly confusing one mug with another mug. That is a different problem, and an honest one: the instances were drawn from a shared palette on purpose.

Occlusion is the other failure. A bar across the object changes the 8×8 layout enough that instance recall collapses, even with the dark pixels downweighted. Class recall holds up better, 73.50% at rank 1 and 93.00% at rank 5, because enough of the shape remains to say “this is still a bottle.” That is useful if you wanted a class filter. It is not useful if you wanted the same bottle back.

The systems numbers are not the bottleneck

I timed this on one machine, one run, no GPU. Building the 800 invariant embeddings took 0.675 seconds. A single embed was 0.823 milliseconds at p50 and 0.936 milliseconds at p95. A brute-force lookup against 800 L2-normalized vectors was 0.012 milliseconds at p50 and 0.014 milliseconds at p95. The index is 832 KB.

I would not introduce a vector database at this size. I would not call an API for the embedding either, not for this feature, not until the representation survives the slice I already know is going to show up in a camera roll: a yellowish indoor light, a thumb over the lens, a darker copy of the same object.

A 91.12% class recall and a sub-millisecond query are easy to like. They are also the numbers that hide the 0.00%.

What this is not

It is not CLIP, and it is not a claim about a production visual search. The gallery is 800 Pillow drawings with a fixed seed, 42. If your recall numbers come out different, the file changed. The timings above are from the same run; another machine will differ, the recall table should not.

I also did not try a learned descriptor, a proper color-constancy step, or a mask that actually segments the object. Those might fix the warm slice, or they might just hide it inside a better average. I would still print the table.

The file is embed_search.py. Run it with Python 3, NumPy, and Pillow. If the warm queries stop being zero, something in the condition or the descriptor moved. If they stay zero and you only had the 91%, you almost shipped the wrong story.