Views
No views yet
jn12/2026LPCV-Track1-MobileCLIP2-B-Best is the exported ONNX version of the best current MobileCLIP2-B checkpoint used in this LPCV 2026 Track 1 image-to-text retrieval project.https://github.com/jn12-29/LPCV-Track1-EfficientAIMobileCLIP2-Bimage_encoder.onnximage_encoder.onnx.datatext_encoder.onnxtext_encoder.onnx.data1hf download jn12/2026LPCV-Track1-MobileCLIP2-B-Best \
2 --local-dir ./pretrained/2026LPCV-Track1-MobileCLIP2-B-Best1pretrained/2026LPCV-Track1-MobileCLIP2-B-Best/
2├── image_encoder.onnx
3├── image_encoder.onnx.data
4├── text_encoder.onnx
5└── text_encoder.onnx.data1pip install onnxruntime pillow numpy torch torchvision transformers
2hf download openai/clip-vit-base-patch321from pathlib import Path
2
3import numpy as np
4import onnxruntime as ort
5import torch
6import torch.nn.functional as F
7from PIL import Image
8from torchvision import transforms
9from transformers import CLIPTokenizer
10
11
12MODEL_DIR = Path("./pretrained/2026LPCV-Track1-MobileCLIP2-B-Best")
13IMAGE_PATHS = [
14 "examples/image1.jpg",
15 "examples/image2.jpg",
16]
17TEXTS = [
18 "a red bus on the street",
19 "a group of people near a building",
20 "a dog running on grass",
21]
22
23
24def preprocess_image(image_path: str) -> np.ndarray:
25 transform = transforms.Compose(
26 [
27 transforms.Resize((224, 224)),
28 transforms.ToTensor(),
29 ]
30 )
31 image = Image.open(image_path).convert("RGB")
32 image_tensor = transform(image).unsqueeze(0)
33 return image_tensor.numpy().astype(np.float32)
34
35
36def l2_normalize(x: np.ndarray) -> np.ndarray:
37 return x / np.linalg.norm(x, axis=-1, keepdims=True)
38
39
40def recall_at_k(image_features: np.ndarray, text_features: np.ndarray, positives, k: int) -> float:
41 similarities = image_features @ text_features.T
42 topk = np.argsort(-similarities, axis=1)[:, :k]
43 hits = 0
44 for i, gt in enumerate(positives):
45 if any(j in gt for j in topk[i]):
46 hits += 1
47 return hits / len(positives)
48
49
50image_session = ort.InferenceSession(
51 str(MODEL_DIR / "image_encoder.onnx"),
52 providers=["CPUExecutionProvider"],
53)
54text_session = ort.InferenceSession(
55 str(MODEL_DIR / "text_encoder.onnx"),
56 providers=["CPUExecutionProvider"],
57)
58
59tokenizer = CLIPTokenizer.from_pretrained(
60 "openai/clip-vit-base-patch32",
61 local_files_only=True,
62)
63tokenizer.add_special_tokens({"cls_token": tokenizer.eos_token})
64
65image_embeddings = []
66for image_path in IMAGE_PATHS:
67 image_input = preprocess_image(image_path)
68 image_output = image_session.run(None, {"image": image_input})[0]
69 image_embeddings.append(image_output[0])
70image_embeddings = l2_normalize(np.stack(image_embeddings, axis=0))
71
72text_embeddings = []
73for text in TEXTS:
74 token_ids = tokenizer(
75 [text],
76 padding="max_length",
77 truncation=True,
78 max_length=77,
79 return_tensors="pt",
80 )["input_ids"].numpy().astype(np.int32)
81 text_output = text_session.run(None, {"text": token_ids})[0]
82 text_embeddings.append(text_output[0])
83text_embeddings = l2_normalize(np.stack(text_embeddings, axis=0))
84
85# Example ground-truth mapping:
86# image 0 matches text 0, image 1 matches text 1.
87positive_text_indices = [{0}, {1}]
88
89r_at_1 = recall_at_k(image_embeddings, text_embeddings, positive_text_indices, k=1)
90r_at_2 = recall_at_k(image_embeddings, text_embeddings, positive_text_indices, k=2)
91
92print(f"Recall@1: {r_at_1:.4f}")
93print(f"Recall@2: {r_at_2:.4f}")224x224[0, 1] by dividing by 255CLIPTokenizer from openai/clip-vit-base-patch32max_length=77hf download openai/clip-vit-base-patch32MobileCLIP2-B as the base modelhttps://huggingface.co/datasets/jn12/VG100K4CLHui Xie, Jinyang Du, Jiacheng Wang, Xiaoze Ge, Fengjun Zhong, Yejun Zeng, Ruihao Gong#, Xiaoning Liu, Shenghao Jin, Jinyang Guo#, Xianglong Liu1@misc{mobileclip2b_lpcv2026,
2 title = {2026LPCV-Track1-MobileCLIP2-B-Best},
3 author = {Hui Xie and Jinyang Du and Jiacheng Wang and Xiaoze Ge and Fengjun Zhong and Yejun Zeng and Ruihao Gong and Xiaoning Liu and Shenghao Jin and Jinyang Guo and Xianglong Liu},
4 year = {2026},
5 howpublished = {\url{https://huggingface.co/jn12/2026LPCV-Track1-MobileCLIP2-B-Best}}
6}https://github.com/jn12-29/LPCV-Track1-EfficientAI