|
from datasets import ( |
|
DatasetBuilder, |
|
DownloadManager, |
|
DatasetInfo, |
|
Features, |
|
Image, |
|
ClassLabel, |
|
Split, |
|
SplitGenerator, |
|
) |
|
import pandas as pd |
|
|
|
from pathlib import Path |
|
|
|
|
|
_DESCRIPTION = """\ |
|
This dataset includes spectrograms of ~950 afrobeats songs and 1k rock songs. |
|
The spectrograms were generated with `librosa`. |
|
""" |
|
|
|
_NAMES = [fdr.name for fdr in Path("./data").glob("*/")] |
|
|
|
_URLS = { |
|
"afrobeats": "https://huggingface.co/datasets/Kabilan108/spectrograms/resolve/main/data/afrobeats.zip", |
|
"rock": "https://huggingface.co/datasets/Kabilan108/spectrograms/resolve/main/data/rock.zip", |
|
} |
|
|
|
|
|
class SongSpectrograms(DatasetBuilder): |
|
""" "Song-Spectrograms Images Dataset""" |
|
|
|
def _info(self): |
|
return DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=Features({"image": Image(), "label": ClassLabel(names=_NAMES)}), |
|
supervised_keys=("image", "label"), |
|
) |
|
|
|
def _split_generators(self, dl_manager: DownloadManager): |
|
files = dl_manager.download_and_extract(_URLS) |
|
|
|
return [ |
|
SplitGenerator( |
|
name=Split.TRAIN, |
|
gen_kwargs={ |
|
"afrobeats_dir": Path(files["afrobeats"]) / "afrobeats", |
|
"rock_dir": Path(files["rock"]) / "rock", |
|
}, |
|
) |
|
] |
|
|
|
def _generate_examples(self, afrobeats_dir, rock_dir): |
|
for genre_dir, label in [(afrobeats_dir, "afrobeats"), (rock_dir, "rock")]: |
|
metadata = pd.read_csv(Path(genre_dir) / "metadata.tsv", sep="\t") |
|
|
|
for _, row in metadata.iterrows(): |
|
path = Path(genre_dir) / row["file_name"] |
|
|
|
yield ( |
|
f"{label}_{row['file_name'].replace('.png', '')}", |
|
{"image": str(path), "label": label}, |
|
) |
|
|