import os import datasets from datasets import SplitGenerator, Split, ImageClassification _CITATION = """\ @TECHREPORT{gpiosenka/100-bird-species, author = {gpiosenka}, title = {BIRDS 525 SPECIES- IMAGE CLASSIFICATION}, institution = {}, year = {2023} } """ _DESCRIPTION = """\ A dataset of bird species downloaded from kaggle. """ _HOMEPAGE = "https://www.kaggle.com/datasets/gpiosenka/100-bird-species/" _DATA_DIR = 'data/' _VERSION = "0.1.0" def _CLASSES() -> list[str]: # reads from bird_labels.txt, line by line with open("birds_labels.txt") as f: return f.read().splitlines() class BirdSpeciesDataset(datasets.GeneratorBasedBuilder): """DatasetBuilder for bird_species_dataset dataset.""" DEFAULT_CONFIG_NAME = "bird_species_dataset" BUILDER_CONFIGS = [ datasets.BuilderConfig( name="bird_species_dataset", version=datasets.Version(_VERSION), description=_DESCRIPTION, ) ] def _info(self): _NAMES = _CLASSES() return datasets.DatasetInfo( description=_DESCRIPTION, features=datasets.Features( { "image": datasets.Image(), "label": datasets.features.ClassLabel(names=_NAMES), } ), supervised_keys=("image", "label"), homepage=_HOMEPAGE, citation=_CITATION, task_templates=ImageClassification(image_column="image", label_column="label"), ) def _split_generators(self, dl_manager): data_dir = _DATA_DIR return [ SplitGenerator(name=Split.TRAIN, gen_kwargs={"filepath": os.path.join(data_dir, "train")}), SplitGenerator(name=Split.VALIDATION, gen_kwargs={"filepath": os.path.join(data_dir, "valid")}), SplitGenerator(name=Split.TEST, gen_kwargs={"filepath": os.path.join(data_dir, "test")}), ] def _generate_examples(self, filepath): """Yields examples.""" idx = 0 for label in os.listdir(filepath): for f in os.listdir(os.path.join(filepath, label)): record = { "image": os.path.join(filepath, label, f), "label": label, } yield idx, record idx += 1