File size: 1,071 Bytes
ebeed67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""CLIP image embedding postprocessor.

Input: raw embedding tensor from a CLIP vision encoder (e.g. MobileCLIP S0/S1/S2)
Output: {"kind": "embedding", "embedding": [...], "embeddingNorm": [...]}

MobileCLIP uses resize 256×256 + rescale /255 only (no ImageNet mean/std).
The existing inference_pool resize_image path already produces this layout —
preprocessMode:'resize' + inputNormalization:'none' is the correct config.
"""
import numpy as np


def postprocess_clip(
    predictions: dict,
    config: dict,
    orig_w: int,
    orig_h: int,
    scale: float,
    pad: tuple[int, int],
) -> dict:
    """L2-normalize the CLIP embedding vector."""
    # Get the first output tensor (vision encoder emits a single embedding)
    raw = np.array(list(predictions.values())[0]).flatten().astype(np.float32)

    norm = np.linalg.norm(raw)
    if norm == 0:
        normalized = np.zeros_like(raw)
    else:
        normalized = raw / norm

    return {
        "kind": "embedding",
        "embedding": raw.tolist(),
        "embeddingNorm": normalized.tolist(),
    }