|
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__( |
|
|
|
name=size + ("-v2" if base == "imagenette2" else ""), |
|
description="{} variant.".format(size), |
|
**kwargs) |
|
|
|
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( |
|
|
|
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 |
|
|