Alejandro98 commited on
Commit
abaa9a6
·
verified ·
1 Parent(s): bf20353

Add BioMedFlickr parquet retrieval benchmark

Browse files
README.md ADDED
@@ -0,0 +1,254 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: other
3
+ task_categories:
4
+ - image-to-text
5
+ - visual-question-answering
6
+ language:
7
+ - en
8
+ pretty_name: BioMedFlickr
9
+ size_categories:
10
+ - 1K<n<10K
11
+ tags:
12
+ - medical
13
+ - pathology
14
+ - retrieval
15
+ - clip
16
+ - biomedical
17
+ - flickr
18
+ dataset_info:
19
+ features:
20
+ - name: image
21
+ dtype: image
22
+ - name: caption
23
+ dtype: string
24
+ - name: caption_raw
25
+ dtype: string
26
+ - name: title
27
+ dtype: string
28
+ - name: key
29
+ dtype: string
30
+ - name: date_uploaded
31
+ dtype: string
32
+ - name: flickr_id
33
+ dtype: string
34
+ - name: url
35
+ dtype: string
36
+ - name: nsid
37
+ dtype: string
38
+ - name: category
39
+ dtype: string
40
+ - name: tags
41
+ sequence: string
42
+ - name: width
43
+ dtype: int32
44
+ - name: height
45
+ dtype: int32
46
+ splits:
47
+ - name: test
48
+ num_examples: 7185
49
+ download_size: 906819144
50
+ configs:
51
+ - config_name: default
52
+ data_files:
53
+ - split: test
54
+ path: data/test-*
55
+ ---
56
+
57
+ # BioMedFlickr
58
+
59
+ BioMedFlickr is a **biomedical image–caption retrieval benchmark** built from
60
+ public Flickr pathology / microscopy albums. Each example is a single image
61
+ paired with a cleaned English caption. The Hugging Face split matches the
62
+ evaluation set used in the EVVLM retrieval notebook
63
+ (`dev_retrival-Copy1.ipynb`): captions shorter than 10 characters after
64
+ cleaning are dropped, leaving **7185 test pairs**.
65
+
66
+ Images are stored as original JPEGs inside **parquet** files so the dataset
67
+ viewer is enabled (~865 MB download).
68
+
69
+ ## Load
70
+
71
+ ```python
72
+ from datasets import load_dataset
73
+
74
+ ds = load_dataset("Alejandro98/BioMedFlickr", split="test")
75
+ print(ds)
76
+ print(ds[0]["caption"])
77
+ ds[0]["image"]
78
+ ```
79
+
80
+ Columns:
81
+
82
+ | column | description |
83
+ | --- | --- |
84
+ | `image` | JPEG image (`datasets.Image`) |
85
+ | `caption` | cleaned caption used for retrieval |
86
+ | `caption_raw` | original Flickr text before cleaning |
87
+ | `title` | Flickr title |
88
+ | `key` | shard sample id |
89
+ | `date_uploaded` | Flickr upload timestamp |
90
+ | `flickr_id` | Flickr photo id (when available) |
91
+ | `url` | Flickr image URL (when available) |
92
+ | `nsid` | Flickr owner nsid |
93
+ | `category` | source album / caption file |
94
+ | `tags` | Flickr tags |
95
+ | `width`, `height` | original pixel size |
96
+
97
+ ## Retrieval protocol
98
+
99
+ This is a **1-to-1 paired retrieval** task. Encode every image and every
100
+ cleaned `caption`, L2-normalize the embeddings, then use inner-product search
101
+ (cosine). The relevant item for example `i` is the pair at the same index.
102
+
103
+ Reported metrics are **Recall@k** for `k in {1, 10, 100, 1000}`, in both
104
+ directions:
105
+
106
+ - **Image-to-text**: query with image embeddings against the caption index
107
+ - **Text-to-image**: query with caption embeddings against the image index
108
+
109
+ Do **not** apply extra caption cleaning at eval time; `caption` is already
110
+ the string used in the original notebook. Resize / normalize images with
111
+ your model's own `preprocess` (the notebook used 224×224 only as a loader
112
+ convenience; CLIP-style preprocessors already resize).
113
+
114
+ ### Minimal eval script (OpenCLIP + FAISS)
115
+
116
+ ```python
117
+ import numpy as np
118
+ import torch
119
+ import faiss
120
+ import open_clip
121
+ from datasets import load_dataset
122
+ from torch.utils.data import DataLoader
123
+
124
+ ds = load_dataset("Alejandro98/BioMedFlickr", split="test")
125
+
126
+ model_name, pretrained = "ViT-L-14", "openai"
127
+ model, _, preprocess = open_clip.create_model_and_transforms(
128
+ model_name, pretrained=pretrained
129
+ )
130
+ tokenizer = open_clip.get_tokenizer(model_name)
131
+ device = "cuda" if torch.cuda.is_available() else "cpu"
132
+ model = model.to(device).eval()
133
+
134
+
135
+ def collate(batch):
136
+ images = torch.stack([preprocess(ex["image"].convert("RGB")) for ex in batch])
137
+ captions = [ex["caption"] for ex in batch]
138
+ return {"image": images, "caption": captions}
139
+
140
+
141
+ loader = DataLoader(ds, batch_size=64, collate_fn=collate)
142
+
143
+ image_embeddings, text_embeddings = [], []
144
+ with torch.no_grad():
145
+ for batch in loader:
146
+ images = batch["image"].to(device)
147
+ texts = tokenizer(batch["caption"]).to(device)
148
+ ie = model.encode_image(images)
149
+ te = model.encode_text(texts)
150
+ ie = ie / ie.norm(dim=-1, keepdim=True)
151
+ te = te / te.norm(dim=-1, keepdim=True)
152
+ image_embeddings.append(ie.cpu().numpy())
153
+ text_embeddings.append(te.cpu().numpy())
154
+
155
+ image_embeddings = np.concatenate(image_embeddings).astype("float32")
156
+ text_embeddings = np.concatenate(text_embeddings).astype("float32")
157
+
158
+
159
+ def recall_at_k(gallery, queries, ks=(1, 10, 100, 1000)):
160
+ index = faiss.IndexFlatIP(gallery.shape[1])
161
+ index.add(gallery)
162
+ metrics = {}
163
+ for k in ks:
164
+ _, retrieved = index.search(queries, k)
165
+ hits = np.array([i in row for i, row in enumerate(retrieved)])
166
+ metrics[k] = float(hits.mean() * 100.0)
167
+ return metrics
168
+
169
+
170
+ # image queries -> caption gallery (image-to-text)
171
+ i2t = recall_at_k(text_embeddings, image_embeddings)
172
+ # caption queries -> image gallery (text-to-image)
173
+ t2i = recall_at_k(image_embeddings, text_embeddings)
174
+
175
+ print("image-to-text R@k", i2t)
176
+ print("text-to-image R@k", t2i)
177
+ ```
178
+
179
+ `Recall@k` is the fraction of queries whose **paired** index appears in the
180
+ top-`k` neighbors. With ~7185 pairs, chance R@1 is about
181
+ 0.014%.
182
+
183
+ The original notebook also reports a 95% t-interval around each recall. You
184
+ can recover that from the per-query hit vector (`hits` above).
185
+
186
+ ### Using EVVLM
187
+
188
+ If you already have the [evvlm](https://github.com/) package and a
189
+ CLIP-style `model_dict` (`model`, `tokenizer`, `preprocess`, `device`):
190
+
191
+ ```python
192
+ from datasets import load_dataset
193
+ from torch.utils.data import DataLoader
194
+ from evvlm.inference.embedding.utils import process_image, get_features
195
+
196
+ ds = load_dataset("Alejandro98/BioMedFlickr", split="test")
197
+
198
+ def collate(batch):
199
+ return {
200
+ "image": [ex["image"].convert("RGB") for ex in batch],
201
+ "caption": [ex["caption"] for ex in batch],
202
+ }
203
+
204
+ dataloader = DataLoader(ds, batch_size=64, collate_fn=collate)
205
+ # then reuse generate_embeddings / get_top_k_metrics from the notebook
206
+ ```
207
+
208
+ ## Construction
209
+
210
+ 1. Public biomedical Flickr albums were serialized as webdataset shards
211
+ (`jpg`, `txt`, `title`, `dateuploaded`).
212
+ 2. Captions are cleaned with the notebook `clean_caption` rules (strip
213
+ contribution / credit / HTML tails, collapse whitespace).
214
+ 3. Pairs whose **cleaned** caption is shorter than 10 characters are
215
+ removed. That is the only example filter; it yields 7185
216
+ pairs.
217
+ 4. Original JPEG bytes are written to parquet (no 224 resize) together
218
+ with Flickr metadata when a sidecar JSON exists.
219
+
220
+ Caption length on this split (from the notebook): median 78 characters
221
+ (min 10, max 3287). CLIP token counts with ViT-L-14: median 23
222
+ (min 4, max 77).
223
+
224
+ ## Reference results
225
+
226
+ Numbers below come from `dev_retrival-Copy1.ipynb` on this same filtered
227
+ set. The notebook labeled “image to text” as **text queries against the
228
+ image index** (standard **text-to-image**) and “text to image” as **image
229
+ queries against the text index** (standard **image-to-text**). We keep
230
+ the notebook column names so CSV dumps stay comparable.
231
+
232
+ Recall is percent (higher is better).
233
+
234
+ | model | I2T* R@1 | I2T R@10 | I2T R@100 | I2T R@1000 | T2I* R@1 | T2I R@10 | T2I R@100 | T2I R@1000 |
235
+ | --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: |
236
+ | PMC-CLIP | 0.026 | 0.142 | 1.382 | 13.017 | 0.000 | 0.181 | 1.175 | 12.965 |
237
+ | BioMedCLIP | 3.435 | 11.816 | 34.427 | 72.805 | 4.106 | 13.494 | 34.633 | 69.977 |
238
+ | ViT-H-14-378-quickgelu / dfn5b | 3.900 | 13.753 | 35.008 | 71.113 | 4.081 | 12.745 | 32.619 | 65.586 |
239
+ | ViT-L-14 / DataComp XL CLIP | 2.596 | 9.749 | 26.085 | 58.148 | 2.763 | 9.155 | 24.432 | 53.654 |
240
+ | CPT ViT-L-14 (biomed continued pretrain) | 4.148 | 15.143 | 38.344 | 76.785 | 4.134 | 13.751 | 36.089 | 72.053 |
241
+
242
+ \*Notebook names: **I2T** = `image to text` (text → image gallery), **T2I** =
243
+ `text to image` (image → text gallery).
244
+
245
+ ## License and source
246
+
247
+ Images and captions were collected from **public Flickr albums** (pathology,
248
+ microscopy, and related biomedical photography). Flickr items keep their
249
+ original photographer licenses; this repo does not re-license third-party
250
+ photos. If you are a rights holder and want an image removed, open an issue
251
+ on the dataset page.
252
+
253
+ Intended use is **research evaluation** of biomedical vision–language
254
+ models, not clinical deployment.
data/test-00000-of-00004.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:eae4108e03009908f042a43435c65b1f93bc1504efb7885ee2d82ee9e697139c
3
+ size 307899175
data/test-00001-of-00004.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d218abb2c98a7452459992b43fabd4f5bc2ef3c1ad734969a705111066dd3443
3
+ size 211606069
data/test-00002-of-00004.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a70466aa0eb89a54f43eb37056e9faad3ece68361b018b0fa5b38648c9b6ddcd
3
+ size 228462745
data/test-00003-of-00004.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8cf759186cca7864858a4bbf1a1940876db8c29930971e3cd242193280da2a2f
3
+ size 158851155