Datasets:
Tasks:
Image-to-Text
Sub-tasks:
image-captioning
Languages:
English
Size:
10M<n<100M
ArXiv:
License:
Commit
·
2be6e74
1
Parent(s):
71ad2a5
Improve RedCaps dataset card (#4100)
Browse files* Improve RedCaps dataset card
* Add newlines
* Add missing imports
* Fix Pillow import
Commit from https://github.com/huggingface/datasets/commit/0a4216c44a1bbe87587044cde651b848181fa9df
README.md
CHANGED
@@ -57,7 +57,7 @@ pretty_name: RedCaps
|
|
57 |
- **Repository:**
|
58 |
- **Paper:** https://arxiv.org/abs/2111.11431
|
59 |
- **Leaderboard:**
|
60 |
-
- **Point of Contact:** kdexd@umich.edu
|
61 |
|
62 |
### Dataset Summary
|
63 |
|
@@ -75,36 +75,120 @@ unrelated images through a common semantic meaning (r/perfectfit).
|
|
75 |
This dataset doesn't download the images locally by default. Instead, it exposes URLs to the images. To fetch the images, use the following code:
|
76 |
|
77 |
```python
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
from datasets import load_dataset
|
79 |
from datasets.utils.file_utils import get_datasets_user_agent
|
80 |
|
81 |
-
def fetch_images(batch, timeout):
|
82 |
-
import PIL.Image
|
83 |
-
import requests
|
84 |
|
85 |
-
|
86 |
-
for
|
87 |
try:
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
)
|
95 |
-
|
96 |
-
except
|
97 |
image = None
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
100 |
return batch
|
101 |
|
102 |
-
|
103 |
-
|
104 |
dset = load_dataset("red_caps", "rabbits_2017")
|
105 |
-
dset = dset.map(fetch_images, batched=True, batch_size=100, fn_kwargs={"
|
106 |
```
|
107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
### Supported Tasks and Leaderboards
|
109 |
|
110 |
From the paper:
|
|
|
57 |
- **Repository:**
|
58 |
- **Paper:** https://arxiv.org/abs/2111.11431
|
59 |
- **Leaderboard:**
|
60 |
+
- **Point of Contact:** [Karan Desai](mailto:kdexd@umich.edu)
|
61 |
|
62 |
### Dataset Summary
|
63 |
|
|
|
75 |
This dataset doesn't download the images locally by default. Instead, it exposes URLs to the images. To fetch the images, use the following code:
|
76 |
|
77 |
```python
|
78 |
+
from concurrent.futures import ThreadPoolExecutor
|
79 |
+
from functools import partial
|
80 |
+
import io
|
81 |
+
import urllib
|
82 |
+
|
83 |
+
import PIL.Image
|
84 |
+
|
85 |
from datasets import load_dataset
|
86 |
from datasets.utils.file_utils import get_datasets_user_agent
|
87 |
|
|
|
|
|
|
|
88 |
|
89 |
+
def fetch_single_image(image_url, timeout=None, retries=0):
|
90 |
+
for _ in range(retries + 1):
|
91 |
try:
|
92 |
+
request = urllib.request.Request(
|
93 |
+
image_url,
|
94 |
+
data=None,
|
95 |
+
headers={"user-agent": get_datasets_user_agent()},
|
96 |
+
)
|
97 |
+
with urllib.request.urlopen(request, timeout=timeout) as req:
|
98 |
+
image = PIL.Image.open(io.BytesIO(req.read()))
|
99 |
+
break
|
100 |
+
except Exception:
|
101 |
image = None
|
102 |
+
return image
|
103 |
+
|
104 |
+
|
105 |
+
def fetch_images(batch, num_threads, timeout=None, retries=0):
|
106 |
+
fetch_single_image_with_args = partial(fetch_single_image, timeout=timeout, retries=retries)
|
107 |
+
with ThreadPoolExecutor(max_workers=num_threads) as executor:
|
108 |
+
batch["image"] = list(executor.map(fetch_single_image_with_args, batch["image_url"]))
|
109 |
return batch
|
110 |
|
111 |
+
|
112 |
+
num_threads = 20
|
113 |
dset = load_dataset("red_caps", "rabbits_2017")
|
114 |
+
dset = dset.map(fetch_images, batched=True, batch_size=100, fn_kwargs={"num_threads": num_threads})
|
115 |
```
|
116 |
|
117 |
+
Some image links point to more than one image. You can process and downloaded those as follows:
|
118 |
+
|
119 |
+
```python
|
120 |
+
from concurrent.futures import ThreadPoolExecutor
|
121 |
+
from functools import partial
|
122 |
+
import io
|
123 |
+
import urllib
|
124 |
+
|
125 |
+
import PIL.Image
|
126 |
+
|
127 |
+
import datasets
|
128 |
+
from datasets import load_dataset
|
129 |
+
from datasets.utils.file_utils import get_datasets_user_agent
|
130 |
+
|
131 |
+
|
132 |
+
def fetch_single_image(image_url, timeout=None, retries=0):
|
133 |
+
for _ in range(retries + 1):
|
134 |
+
try:
|
135 |
+
request = urllib.request.Request(
|
136 |
+
image_url,
|
137 |
+
data=None,
|
138 |
+
headers={"user-agent": get_datasets_user_agent()},
|
139 |
+
)
|
140 |
+
with urllib.request.urlopen(request, timeout=timeout) as req:
|
141 |
+
image = PIL.Image.open(io.BytesIO(req.read()))
|
142 |
+
break
|
143 |
+
except Exception:
|
144 |
+
image = None
|
145 |
+
return image
|
146 |
+
|
147 |
+
|
148 |
+
def fetch_images(batch, num_threads, timeout=None, retries=0):
|
149 |
+
fetch_single_image_with_args = partial(fetch_single_image, timeout=timeout, retries=retries)
|
150 |
+
with ThreadPoolExecutor(max_workers=num_threads) as executor:
|
151 |
+
batch["image"] = list(executor.map(lambda image_urls: [fetch_single_image_with_args(image_url) for image_url in image_urls], batch["image_url"]))
|
152 |
+
return batch
|
153 |
+
|
154 |
+
|
155 |
+
def process_image_urls(batch):
|
156 |
+
processed_batch_image_urls = []
|
157 |
+
for image_url in batch["image_url"]:
|
158 |
+
processed_example_image_urls = []
|
159 |
+
image_url_splits = re.findall(r"http\S+", image_url)
|
160 |
+
for image_url_split in image_url_splits:
|
161 |
+
if "imgur" in image_url_split and "," in image_url_split:
|
162 |
+
for image_url_part in image_url_split.split(","):
|
163 |
+
if not image_url_part:
|
164 |
+
continue
|
165 |
+
image_url_part = image_url_part.strip()
|
166 |
+
root, ext = os.path.splitext(image_url_part)
|
167 |
+
if not root.startswith("http"):
|
168 |
+
root = "http://i.imgur.com/" + root
|
169 |
+
root = root.split("#")[0]
|
170 |
+
if not ext:
|
171 |
+
ext = ".jpg"
|
172 |
+
ext = re.split(r"[?%]", ext)[0]
|
173 |
+
image_url_part = root + ext
|
174 |
+
processed_example_image_urls.append(image_url_part)
|
175 |
+
else:
|
176 |
+
processed_example_image_urls.append(image_url_split)
|
177 |
+
processed_batch_image_urls.append(processed_example_image_urls)
|
178 |
+
batch["image_url"] = processed_batch_image_urls
|
179 |
+
return batch
|
180 |
+
|
181 |
+
|
182 |
+
dset = load_dataset("red_caps", "rabbits_2017")
|
183 |
+
dset = dset.map(process_image_urls, batched=True, num_proc=4)
|
184 |
+
features = dset["train"].features.copy()
|
185 |
+
features["image"] = datasets.Sequence(datasets.Image())
|
186 |
+
num_threads = 20
|
187 |
+
dset = dset.map(fetch_images, batched=True, batch_size=100, features=features, fn_kwargs={"num_threads": num_threads})
|
188 |
+
```
|
189 |
+
|
190 |
+
Note that in the above code, we use the `datasets.Sequence` feature to represent a list of images for the multi-image links.
|
191 |
+
|
192 |
### Supported Tasks and Leaderboards
|
193 |
|
194 |
From the paper:
|