| import os |
| import json |
| import datasets |
| from PIL import Image |
| import io |
| import kagglehub |
|
|
| class InriaHolidays(datasets.GeneratorBasedBuilder): |
| def _info(self): |
| return datasets.DatasetInfo( |
| description=( |
| "The INRIA Holidays dataset is a benchmark for image retrieval. " |
| "It contains 500 distinct scenes, each with one query image and several similar images, " |
| "totaling 1491 high-resolution photos of diverse holiday scenes and objects. " |
| "This dataset is widely used to evaluate image retrieval algorithms." |
| ), |
| features=datasets.Features({ |
| "scene_id": datasets.Value("string"), |
| "query": datasets.Image(), |
| "similar": datasets.Sequence(datasets.Image()), |
| }), |
| supervised_keys=None, |
| homepage="https://lear.inrialpes.fr/~jegou/data.php", |
| citation=""" |
| @inproceedings{jegou2008hamming, |
| title={Hamming embedding and weak geometric consistency for large scale image search}, |
| author={Jégou, Hervé and Douze, Matthijs and Schmid, Cordelia}, |
| booktitle={European Conference on Computer Vision (ECCV)}, |
| pages={304--317}, |
| year={2008}, |
| organization={Springer} |
| } |
| """, |
| license="Open Database License (ODbL)", |
| ) |
|
|
|
|
| def _split_generators(self, dl_manager): |
| path = kagglehub.dataset_download("vadimshabashov/inria-holidays") |
| return [datasets.SplitGenerator(name="train", gen_kwargs={"data_dir": path})] |
|
|
| def _generate_examples(self, data_dir): |
| images_dir = os.path.join(data_dir, "images") |
| with open(os.path.join(data_dir, "groundtruth.json"), "r") as f: |
| gt = json.load(f) |
|
|
| for scene_id, entry in gt.items(): |
| query_path = os.path.join(images_dir, entry["query"]) |
| query_img = self._load_image(query_path) |
|
|
| similar_imgs = [] |
| for sim in entry["similar"]: |
| sim_path = os.path.join(images_dir, sim) |
| similar_imgs.append(self._load_image(sim_path)) |
|
|
| yield scene_id, { |
| "scene_id": scene_id, |
| "query": query_img, |
| "similar": similar_imgs, |
| } |
|
|
| def _load_image(self, path): |
| with open(path, "rb") as f: |
| img = Image.open(io.BytesIO(f.read())) |
| if img.mode != "RGB": |
| img = img.convert("RGB") |
| return img |
|
|