File size: 3,467 Bytes
ef44f5f |
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 |
import os
from pathlib import Path
import datasets
_CITATION = """
@misc{imagenette,
author = "Jeremy Howard",
title = "imagenette",
url = "https://github.com/fastai/imagenette/"
}
"""
_DESCRIPTION = """\
Imagenette is a subset of 10 easily classified classes from the Imagenet
dataset. It was originally prepared by Jeremy Howard of FastAI. The objective
behind putting together a small version of the Imagenet dataset was mainly
because running new ideas/algorithms/experiments on the whole Imagenet take a
lot of time.
This version of the dataset allows researchers/practitioners to quickly try out
ideas and share with others. The dataset comes in three variants:
* Full size
* 320 px
* 160 px
Note: The v2 config correspond to the new 70/30 train/valid split (released
in Dec 6 2019).
"""
_LABELS_FNAME = "image_classification/imagenette_labels.txt"
_URL_PREFIX = "https://s3.amazonaws.com/fast-ai-imageclas/"
LABELS = [
"n01440764",
"n02102040",
"n02979186",
"n03000684",
"n03028079",
"n03394916",
"n03417042",
"n03425413",
"n03445777",
"n03888257"
]
class ImagenetteConfig(datasets.BuilderConfig):
"""BuilderConfig for Imagenette."""
def __init__(self, size, base, **kwargs):
super(ImagenetteConfig, self).__init__(
# `320px-v2`,...
name=size + ("-v2" if base == "imagenette2" else ""),
description="{} variant.".format(size),
**kwargs)
# e.g. `imagenette2-320.tgz`
self.dirname = base + {
"full-size": "",
"320px": "-320",
"160px": "-160",
}[size]
def _make_builder_configs():
configs = []
for base in ["imagenette2", "imagenette"]:
for size in ["full-size", "320px", "160px"]:
configs.append(ImagenetteConfig(base=base, size=size))
return configs
class Imagenette(datasets.GeneratorBasedBuilder):
"""A smaller subset of 10 easily classified classes from Imagenet."""
VERSION = datasets.Version("1.0.0")
BUILDER_CONFIGS = _make_builder_configs()
def _info(self):
return datasets.DatasetInfo(
# builder=self,
description=_DESCRIPTION,
features=datasets.Features({
"image_file_path": datasets.Value("string"),
"labels": datasets.ClassLabel(names=LABELS)
}),
supervised_keys=("image_file_path", "labels"),
homepage="https://github.com/fastai/imagenette",
citation=_CITATION,
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
print(self.__dict__.keys())
print(self.config)
dirname = self.config.dirname
url = _URL_PREFIX + "{}.tgz".format(dirname)
path = dl_manager.download_and_extract(url)
train_path = os.path.join(path, dirname, "train")
val_path = os.path.join(path, dirname, "val")
assert os.path.exists(train_path)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"datapath": train_path,
},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={
"datapath": val_path,
},
),
]
def _generate_examples(self, datapath):
"""Yields examples."""
for path in Path(datapath).glob("**/*.JPEG"):
record = {
"image_file_path": str(path),
"labels": path.parent.name
}
yield path.name, record
|