# Copyright (C) 2022, François-Guillaume Fernandez. # This program is licensed under the Apache License 2.0. # See LICENSE or go to for full license details. """Imagenette dataset.""" import os import json import datasets _HOMEPAGE = "https://github.com/fastai/imagenette" _LICENSE = "Apache License 2.0" _CITATION = """\ @software{Howard_Imagenette_2019, title={Imagenette: A smaller subset of 10 easily classified classes from Imagenet}, author={Jeremy Howard}, year={2019}, month={March}, publisher = {GitHub}, url = {https://github.com/fastai/imagenette} } """ _DESCRIPTION = """\ 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). """ _LABEL_MAP = [ 'n01440764', 'n02102040', 'n02979186', 'n03000684', 'n03028079', 'n03394916', 'n03417042', 'n03425413', 'n03445777', 'n03888257', ] class OpenFireConfig(datasets.BuilderConfig): """BuilderConfig for OpenFire.""" def __init__(self, data_url, **kwargs): """BuilderConfig for OpenFire. Args: data_url: `string`, url to download the zip file from. **kwargs: keyword arguments forwarded to super. """ super(OpenFireConfig, self).__init__(version=datasets.Version("1.0.0"), **kwargs) self.data_url = data_url class OpenFire(datasets.GeneratorBasedBuilder): """OpenFire dataset.""" BUILDER_CONFIGS = [ OpenFireConfig( name="full_size", description="All images are in their original size.", data_url="https://s3.amazonaws.com/fast-ai-imageclas/imagenette2.tgz", ), OpenFireConfig( name="320px", description="All images were resized on their shortest side to 320 pixels.", data_url="https://s3.amazonaws.com/fast-ai-imageclas/imagenette2-320.tgz", ), OpenFireConfig( name="160px", description="All images were resized on their shortest side to 160 pixels.", data_url="https://s3.amazonaws.com/fast-ai-imageclas/imagenette2-160.tgz", ), ] def _info(self): return datasets.DatasetInfo( description=_DESCRIPTION + self.config.description, features=datasets.Features( { "image": datasets.Image(), "label": datasets.ClassLabel( names=[ "tench", "English springer", "cassette player", "chain saw", "church", "French horn", "garbage truck", "gas pump", "golf ball", "parachute", ] ), } ), supervised_keys=None, homepage=_HOMEPAGE, license=_LICENSE, citation=_CITATION, ) def _split_generators(self, dl_manager): _path = dl_manager.download_and_extract(self.config.data_url) local_extracted_archive = os.path.join(_path, self.config.data_url.rpartition("/")[-1].split('.')[0]) return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={ "split_folder": os.path.join(local_extracted_archive, "train"), }, ), datasets.SplitGenerator( name=datasets.Split.VALIDATION, gen_kwargs={ "split_folder": os.path.join(local_extracted_archive, "val"), }, ), ] def _generate_examples(self, split_folder): idx = 0 for class_idx, class_folder in enumerate(_LABEL_MAP): for path in os.listdir(os.path.join(split_folder, class_folder)): yield idx, {"image": os.path.join(split_folder, class_folder, path), "label": class_idx} idx += 1