bird-species-dataset / bird-species-dataset.py
chriamue's picture
adds birds labels, bird species dataset script and test script
f74dbd6
raw
history blame
No virus
3.21 kB
import os
import datasets
from datasets import SplitGenerator, Split, ImageClassification
from kaggle.api.kaggle_api_extended import KaggleApi
_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/'
def _CLASSES() -> list[str]:
# reads from bird_labels.txt, line by line
with open("birds_labels.txt") as f:
return f.read().splitlines()
class BirdSpeciesDatasetDataset(datasets.GeneratorBasedBuilder):
VERSION = datasets.Version("0.1.0")
BUILDER_CONFIGS = [
datasets.BuilderConfig(
name="plain_text",
version=datasets.Version("0.1.0", ""),
description="Import of BIRDS 525 SPECIES Data Set",
)
]
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
# Downloading the dataset
if not os.path.exists(data_dir):
kaggle_api = KaggleApi()
kaggle_api.authenticate()
kaggle_api.dataset_download_files('gpiosenka/100-bird-species', path=data_dir, unzip=True)
# There is a bug in the dataset, where one of the folders is named "PARAKETT AUKLET" instead of "PARAKETT AUKLET"
# We fix it by adding a space to the valid folder, because train and test are wrong and also the names in the labels file
# Fixing the path
fault_path = os.path.join(data_dir, 'valid', 'PARAKETT AUKLET')
correct_path = os.path.join(data_dir, 'valid', 'PARAKETT AUKLET')
os.rename(fault_path, correct_path)
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
# To use the dataset:
# from datasets import load_dataset
# dataset = load_dataset('path/to/birds.py')