|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""The Kaggle Music Genre Prediction Challenge.""" |
|
|
|
|
|
|
|
|
import os |
|
|
from pathlib import Path |
|
|
|
|
|
import datasets |
|
|
import pandas as pd |
|
|
|
|
|
_CITATION = """ |
|
|
""" |
|
|
|
|
|
|
|
|
_DESCRIPTION = """\ |
|
|
""" |
|
|
|
|
|
_HOMEPAGE = "" |
|
|
|
|
|
|
|
|
_LICENSE = "" |
|
|
|
|
|
_URL = "" |
|
|
|
|
|
genres_df = pd.read_csv("data/genres.csv") |
|
|
GENRES = genres_df["genre"].tolist() |
|
|
|
|
|
|
|
|
class MusicClassification(datasets.GeneratorBasedBuilder): |
|
|
"""The Kaggle Music Genre Prediction Challenge""" |
|
|
|
|
|
def _info(self): |
|
|
return datasets.DatasetInfo( |
|
|
description=_DESCRIPTION, |
|
|
features=datasets.Features( |
|
|
{ |
|
|
"song_id": datasets.Value("int32"), |
|
|
"file": datasets.Value("string"), |
|
|
"audio": datasets.Audio(sampling_rate=22_050), |
|
|
"genre_id": datasets.ClassLabel(names=GENRES), |
|
|
"genre": datasets.Value("string"), |
|
|
} |
|
|
), |
|
|
homepage=_HOMEPAGE, |
|
|
license=_LICENSE, |
|
|
citation=_CITATION, |
|
|
) |
|
|
|
|
|
def _split_generators(self, dl_manager): |
|
|
train_path = dl_manager.download_and_extract("data/train.zip") |
|
|
test_path = dl_manager.download_and_extract("data/test.zip") |
|
|
metadata_train = Path("data/train.csv") |
|
|
metadata_test = Path("data/test.csv") |
|
|
|
|
|
return [ |
|
|
datasets.SplitGenerator( |
|
|
name=datasets.Split.TRAIN, |
|
|
gen_kwargs={"audio_path": train_path, "metadata_path": metadata_train, "split": "train"}, |
|
|
), |
|
|
datasets.SplitGenerator( |
|
|
name=datasets.Split.TEST, |
|
|
gen_kwargs={"audio_path": test_path, "metadata_path": metadata_test, "split": "test"}, |
|
|
), |
|
|
] |
|
|
|
|
|
def _generate_examples(self, audio_path, metadata_path, split): |
|
|
print(audio_path) |
|
|
print(metadata_path) |
|
|
print(split) |
|
|
metadata_df = pd.read_csv(metadata_path) |
|
|
|
|
|
if split == "train": |
|
|
for idx_, row in metadata_df.iterrows(): |
|
|
yield idx_, { |
|
|
"song_id": row["song_id"], |
|
|
"file": os.path.join(audio_path, row["filepath"]), |
|
|
"audio": os.path.join(audio_path, row["filepath"]), |
|
|
"genre_id": row["genre_id"], |
|
|
"genre": row["genre"], |
|
|
} |
|
|
else: |
|
|
for idx_, row in metadata_df.iterrows(): |
|
|
yield idx_, { |
|
|
"song_id": row["song_id"], |
|
|
"file": os.path.join(audio_path, row["filepath"]), |
|
|
"audio": os.path.join(audio_path, row["filepath"]), |
|
|
"genre_id": -1, |
|
|
"genre": "NA", |
|
|
} |
|
|
|