import os import json from pathlib import Path import datasets # Adapted from https://huggingface.co/datasets/nateraw/imagenette/blob/main/imagenette.py _CITATION = """ @misc{imagenette, author = "Jeremy Howard", title = "imagenette", url = "https://github.com/fastai/imagenette/" } """ _DESCRIPTION = """\ # ImageNette Imagenette is a subset of 10 easily classified classes from Imagenet (tench, English springer, cassette player, chain saw, church, French horn, garbage truck, gas pump, golf ball, parachute). 'Imagenette' is pronounced just like 'Imagenet', except with a corny inauthentic French accent. If you've seen Peter Sellars in The Pink Panther, then think something like that. It's important to ham up the accent as much as possible, otherwise people might not be sure whether you're refering to "Imagenette" or "Imagenet". (Note to native French speakers: to avoid confusion, be sure to use a corny inauthentic American accent when saying "Imagenet". Think something like the philosophy restaurant skit from Monty Python's The Meaning of Life.) 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 The '320 px' and '160 px' versions have their shortest side resized to that size, with their aspect ratio maintained. Too easy for you? In that case, you might want to try Imagewoof. # Imagewoof 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. (No we will not enter in to any discussion in to whether a dingo is in fact a dog. Any suggestions to the contrary are un-Australian. Thank you for your cooperation.) Full size download; 320 px download; 160 px download. """ _URL_PREFIX = "https://s3.amazonaws.com/fast-ai-imageclas/" _URL_IMAGENET_REFS = 'https://huggingface.co/datasets/jerpint/imagenette/raw/main/imagenet_refs.json' _LABELS = { "imagenette": [ "cassette_player", "chain_saw", "church", "English_springer", "French_horn", "garbage_truck", "gas_pump", "golf_ball", "parachute", "tench", ], "imagewoof": [ "Australian_terrier", "beagle", "Border_terrier", "dingo", "English_foxhound", "golden_retriever", "Old_English_sheepdog", "Rhodesian_ridgeback", "Samoyed", "Shih-Tzu", ], } _NAME_TO_DIR = { "imagenette-full-res": "imagenette2", "imagenette-320px": "imagenette2-320", "imagenette-160px": "imagenette2-160", "imagewoof-full-res": "imagewoof2", "imagewoof-320px": "imagewoof2-320", "imagewoof-160px": "imagewoof2-160", } class ImagenetteConfig(datasets.BuilderConfig): """BuilderConfig for Imagenette.""" def __init__(self, name, **kwargs): super(ImagenetteConfig, self).__init__( name=name, description="{} version.".format(name), **kwargs ) self.dataset = name.split("-")[0] self.labels = _LABELS[self.dataset] self.name = name def _make_builder_configs(): return [ImagenetteConfig(name) for name in _NAME_TO_DIR] 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": datasets.Image(), "labels": datasets.ClassLabel(names=self.config.labels), } ), supervised_keys=("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) name = self.config.name dirname = _NAME_TO_DIR[name] # Download the ref:label map for imagenet refs_path = dl_manager.download(_URL_IMAGENET_REFS) with open(refs_path) as f: self.ref_to_label = json.load(f) 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 = { # In Imagenette, the parent folder of the file is # the imagenet reference to the label name. "image": str(path), "labels": self.ref_to_label[path.parent.name], } yield path.name, record