Datasets:
Tasks:
Image Classification
Languages:
English
Size Categories:
1K<n<10K
Language Creators:
crowdsourced
Annotations Creators:
crowdsourced
Source Datasets:
extended
Tags:
License:
frgfm
commited on
Commit
•
132eb05
1
Parent(s):
9cffdef
feat: Added builder script
Browse files- imagewoof.py +161 -0
imagewoof.py
ADDED
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (C) 2022, François-Guillaume Fernandez.
|
2 |
+
|
3 |
+
# This program is licensed under the Apache License 2.0.
|
4 |
+
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0> for full license details.
|
5 |
+
|
6 |
+
"""Imagewoof dataset."""
|
7 |
+
|
8 |
+
import os
|
9 |
+
import json
|
10 |
+
|
11 |
+
import datasets
|
12 |
+
|
13 |
+
|
14 |
+
_HOMEPAGE = "https://github.com/fastai/imagenette#imagewoof"
|
15 |
+
|
16 |
+
_LICENSE = "Apache License 2.0"
|
17 |
+
|
18 |
+
_CITATION = """\
|
19 |
+
@software{Howard_Imagewoof_2019,
|
20 |
+
title={Imagewoof: a subset of 10 classes from Imagenet that aren't so easy to classify},
|
21 |
+
author={Jeremy Howard},
|
22 |
+
year={2019},
|
23 |
+
month={March},
|
24 |
+
publisher = {GitHub},
|
25 |
+
url = {https://github.com/fastai/imagenette#imagewoof}
|
26 |
+
}
|
27 |
+
"""
|
28 |
+
|
29 |
+
_DESCRIPTION = """\
|
30 |
+
Imagewoof is a subset of 10 classes from Imagenet that aren't so
|
31 |
+
easy to classify, since they're all dog breeds. The breeds are:
|
32 |
+
Australian terrier, Border terrier, Samoyed, Beagle, Shih-Tzu,
|
33 |
+
English foxhound, Rhodesian ridgeback, Dingo, Golden retriever,
|
34 |
+
Old English sheepdog.
|
35 |
+
"""
|
36 |
+
|
37 |
+
_LABEL_MAP = [
|
38 |
+
'n02086240',
|
39 |
+
'n02087394',
|
40 |
+
'n02088364',
|
41 |
+
'n02089973',
|
42 |
+
'n02093754',
|
43 |
+
'n02096294',
|
44 |
+
'n02099601',
|
45 |
+
'n02105641',
|
46 |
+
'n02111889',
|
47 |
+
'n02115641',
|
48 |
+
]
|
49 |
+
|
50 |
+
_REPO = "https://huggingface.co/datasets/frgfm/imagewoof/resolve/main/metadata"
|
51 |
+
|
52 |
+
|
53 |
+
class ImagewoofConfig(datasets.BuilderConfig):
|
54 |
+
"""BuilderConfig for OpenFire."""
|
55 |
+
|
56 |
+
def __init__(self, data_url, metadata_urls, **kwargs):
|
57 |
+
"""BuilderConfig for OpenFire.
|
58 |
+
Args:
|
59 |
+
data_url: `string`, url to download the zip file from.
|
60 |
+
matadata_urls: dictionary with keys 'train' and 'validation' containing the archive metadata URLs
|
61 |
+
**kwargs: keyword arguments forwarded to super.
|
62 |
+
"""
|
63 |
+
super(ImagewoofConfig, self).__init__(version=datasets.Version("1.0.0"), **kwargs)
|
64 |
+
self.data_url = data_url
|
65 |
+
self.metadata_urls = metadata_urls
|
66 |
+
|
67 |
+
|
68 |
+
class Imagewoof(datasets.GeneratorBasedBuilder):
|
69 |
+
"""Imagewoof dataset."""
|
70 |
+
|
71 |
+
BUILDER_CONFIGS = [
|
72 |
+
ImagewoofConfig(
|
73 |
+
name="full_size",
|
74 |
+
description="All images are in their original size.",
|
75 |
+
data_url="https://s3.amazonaws.com/fast-ai-imageclas/imagewoof2.tgz",
|
76 |
+
metadata_urls={
|
77 |
+
"train": f"{_REPO}/imagewoof2/train.txt",
|
78 |
+
"validation": f"{_REPO}/imagewoof2/val.txt",
|
79 |
+
},
|
80 |
+
),
|
81 |
+
ImagewoofConfig(
|
82 |
+
name="320px",
|
83 |
+
description="All images were resized on their shortest side to 320 pixels.",
|
84 |
+
data_url="https://s3.amazonaws.com/fast-ai-imageclas/imagewoof2-320.tgz",
|
85 |
+
metadata_urls={
|
86 |
+
"train": f"{_REPO}/imagewoof2-320/train.txt",
|
87 |
+
"validation": f"{_REPO}/imagewoof2-320/val.txt",
|
88 |
+
},
|
89 |
+
),
|
90 |
+
ImagewoofConfig(
|
91 |
+
name="160px",
|
92 |
+
description="All images were resized on their shortest side to 160 pixels.",
|
93 |
+
data_url="https://s3.amazonaws.com/fast-ai-imageclas/imagewoof2-160.tgz",
|
94 |
+
metadata_urls={
|
95 |
+
"train": f"{_REPO}/imagewoof2-160/train.txt",
|
96 |
+
"validation": f"{_REPO}/imagewoof2-160/val.txt",
|
97 |
+
},
|
98 |
+
),
|
99 |
+
]
|
100 |
+
|
101 |
+
def _info(self):
|
102 |
+
return datasets.DatasetInfo(
|
103 |
+
description=_DESCRIPTION + self.config.description,
|
104 |
+
features=datasets.Features(
|
105 |
+
{
|
106 |
+
"image": datasets.Image(),
|
107 |
+
"label": datasets.ClassLabel(
|
108 |
+
names=[
|
109 |
+
"Australian terrier",
|
110 |
+
"Border terrier",
|
111 |
+
"Samoyed",
|
112 |
+
"Beagle",
|
113 |
+
"Shih-Tzu",
|
114 |
+
"English foxhound",
|
115 |
+
"Rhodesian ridgeback",
|
116 |
+
"Dingo",
|
117 |
+
"Golden retriever",
|
118 |
+
"Old English sheepdog",
|
119 |
+
]
|
120 |
+
),
|
121 |
+
}
|
122 |
+
),
|
123 |
+
supervised_keys=None,
|
124 |
+
homepage=_HOMEPAGE,
|
125 |
+
license=_LICENSE,
|
126 |
+
citation=_CITATION,
|
127 |
+
)
|
128 |
+
|
129 |
+
def _split_generators(self, dl_manager):
|
130 |
+
archive_path = dl_manager.download(self.config.data_url)
|
131 |
+
metadata_paths = dl_manager.download(self.config.metadata_urls)
|
132 |
+
archive_iter = dl_manager.iter_archive(archive_path)
|
133 |
+
return [
|
134 |
+
datasets.SplitGenerator(
|
135 |
+
name=datasets.Split.TRAIN,
|
136 |
+
gen_kwargs={
|
137 |
+
"images": archive_iter,
|
138 |
+
"metadata_path": metadata_paths["train"],
|
139 |
+
},
|
140 |
+
),
|
141 |
+
datasets.SplitGenerator(
|
142 |
+
name=datasets.Split.VALIDATION,
|
143 |
+
gen_kwargs={
|
144 |
+
"images": archive_iter,
|
145 |
+
"metadata_path": metadata_paths["validation"],
|
146 |
+
},
|
147 |
+
),
|
148 |
+
]
|
149 |
+
|
150 |
+
def _generate_examples(self, images, metadata_path):
|
151 |
+
with open(metadata_path, encoding="utf-8") as f:
|
152 |
+
files_to_keep = set(f.read().split("\n"))
|
153 |
+
idx = 0
|
154 |
+
for file_path, file_obj in images:
|
155 |
+
if file_path in files_to_keep:
|
156 |
+
label = _LABEL_MAP.index(file_path.split("/")[-2])
|
157 |
+
yield idx, {
|
158 |
+
"image": {"path": file_path, "bytes": file_obj.read()},
|
159 |
+
"label": label,
|
160 |
+
}
|
161 |
+
idx += 1
|