Upload crepe_vlms.py
Browse files- crepe_vlms.py +91 -0
crepe_vlms.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from datasets import DatasetInfo, GeneratorBasedBuilder, SplitGenerator, Version, Features, Value, Sequence, Image, Split
|
3 |
+
|
4 |
+
_CITATION = """\
|
5 |
+
@inproceedings{ma2023crepe,
|
6 |
+
title={Crepe: Can vision-language foundation models reason compositionally?},
|
7 |
+
author={Ma, Zixian and Hong, Jerry and Gul, Mustafa Omer and Gandhi, Mona and Gao, Irena and Krishna, Ranjay},
|
8 |
+
booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
|
9 |
+
pages={10910--10921},
|
10 |
+
year={2023}
|
11 |
+
}
|
12 |
+
"""
|
13 |
+
|
14 |
+
_DESCRIPTION = """\
|
15 |
+
Code and datasets for "CREPE: Can Vision-Language Foundation Models Reason Compositionally?".
|
16 |
+
"""
|
17 |
+
|
18 |
+
_HOMEPAGE = "https://huggingface.co/datasets/Mayfull/crepe_vlms"
|
19 |
+
_LICENSE = "MIT License"
|
20 |
+
|
21 |
+
class CREPEVLMsDataset(GeneratorBasedBuilder):
|
22 |
+
VERSION = Version("1.0.0")
|
23 |
+
|
24 |
+
def _info(self):
|
25 |
+
return DatasetInfo(
|
26 |
+
description=_DESCRIPTION,
|
27 |
+
homepage=_HOMEPAGE,
|
28 |
+
license=_LICENSE,
|
29 |
+
citation=_CITATION,
|
30 |
+
features=Features(
|
31 |
+
{
|
32 |
+
"images": Sequence(Image()), # Sequence of images
|
33 |
+
"positive_caption": Sequence(Value("string")),
|
34 |
+
"negative_caption": Sequence(Value("string")),
|
35 |
+
"x": Value("float"),
|
36 |
+
"y": Value("float"),
|
37 |
+
"width": Value("float"),
|
38 |
+
"height": Value("float"),
|
39 |
+
"original_file_name": Value("string"),
|
40 |
+
}
|
41 |
+
),
|
42 |
+
)
|
43 |
+
|
44 |
+
def _split_generators(self, dl_manager):
|
45 |
+
# URLs for images.zip and examples.jsonl
|
46 |
+
urls_to_download = {
|
47 |
+
"images": "https://huggingface.co/datasets/Mayfull/crepe_vlms/resolve/main/images.zip",
|
48 |
+
"examples": "https://huggingface.co/datasets/Mayfull/crepe_vlms/resolve/main/examples.jsonl",
|
49 |
+
}
|
50 |
+
downloaded_files = dl_manager.download_and_extract(urls_to_download)
|
51 |
+
|
52 |
+
return [
|
53 |
+
SplitGenerator(
|
54 |
+
name=Split.TEST,
|
55 |
+
gen_kwargs={
|
56 |
+
"examples_file": downloaded_files["examples"],
|
57 |
+
"images_dir": downloaded_files["images"],
|
58 |
+
},
|
59 |
+
),
|
60 |
+
]
|
61 |
+
|
62 |
+
def _generate_examples(self, examples_file, images_dir):
|
63 |
+
# Read the examples.jsonl file
|
64 |
+
with open(examples_file, "r", encoding="utf-8") as f:
|
65 |
+
for idx, line in enumerate(f):
|
66 |
+
data = eval(line.strip())
|
67 |
+
|
68 |
+
# Get image file path
|
69 |
+
image_file_name = data.get("image")
|
70 |
+
image_path = os.path.join(images_dir, image_file_name)
|
71 |
+
|
72 |
+
# Ensure the image file exists
|
73 |
+
images = [image_path] if os.path.exists(image_path) else []
|
74 |
+
|
75 |
+
# Convert bounding box values to float
|
76 |
+
x = float(data.get("x", 0.0))
|
77 |
+
y = float(data.get("y", 0.0))
|
78 |
+
width = float(data.get("width", 0.0))
|
79 |
+
height = float(data.get("height", 0.0))
|
80 |
+
|
81 |
+
# Prepare the example
|
82 |
+
yield idx, {
|
83 |
+
"images": images,
|
84 |
+
"positive_caption": data.get("positive_caption", []),
|
85 |
+
"negative_caption": data.get("negative_caption", []),
|
86 |
+
"x": x,
|
87 |
+
"y": y,
|
88 |
+
"width": width,
|
89 |
+
"height": height,
|
90 |
+
"original_file_name": data.get("original_file_name", ""),
|
91 |
+
}
|