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