Add script
Browse files- music_classification.py +101 -0
music_classification.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""The Kaggle Music Genre Prediction Challenge."""
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
import os
|
| 18 |
+
from pathlib import Path
|
| 19 |
+
|
| 20 |
+
import datasets
|
| 21 |
+
import pandas as pd
|
| 22 |
+
|
| 23 |
+
_CITATION = """
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
_DESCRIPTION = """\
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
_HOMEPAGE = ""
|
| 31 |
+
|
| 32 |
+
# TODO: Add the licence for the dataset here if you can find it
|
| 33 |
+
_LICENSE = ""
|
| 34 |
+
|
| 35 |
+
_URL = ""
|
| 36 |
+
|
| 37 |
+
genres_df = pd.read_csv("data/genres.csv")
|
| 38 |
+
GENRES = genres_df["genre"].tolist()
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
class MusicClassification(datasets.GeneratorBasedBuilder):
|
| 42 |
+
"""The Kaggle Music Genre Prediction Challenge"""
|
| 43 |
+
|
| 44 |
+
def _info(self):
|
| 45 |
+
return datasets.DatasetInfo(
|
| 46 |
+
description=_DESCRIPTION,
|
| 47 |
+
features=datasets.Features(
|
| 48 |
+
{
|
| 49 |
+
"song_id": datasets.Value("int32"),
|
| 50 |
+
"file": datasets.Value("string"),
|
| 51 |
+
"audio": datasets.Audio(sampling_rate=22_050),
|
| 52 |
+
"genre_id": datasets.ClassLabel(names=GENRES),
|
| 53 |
+
"genre": datasets.Value("string"),
|
| 54 |
+
}
|
| 55 |
+
),
|
| 56 |
+
homepage=_HOMEPAGE,
|
| 57 |
+
license=_LICENSE,
|
| 58 |
+
citation=_CITATION,
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
def _split_generators(self, dl_manager):
|
| 62 |
+
train_path = dl_manager.download_and_extract("data/train.zip")
|
| 63 |
+
test_path = dl_manager.download_and_extract("data/test.zip")
|
| 64 |
+
metadata_train = Path("data/train.csv")
|
| 65 |
+
metadata_test = Path("data/test.csv")
|
| 66 |
+
|
| 67 |
+
return [
|
| 68 |
+
datasets.SplitGenerator(
|
| 69 |
+
name=datasets.Split.TRAIN,
|
| 70 |
+
gen_kwargs={"audio_path": train_path, "metadata_path": metadata_train, "split": "train"},
|
| 71 |
+
),
|
| 72 |
+
datasets.SplitGenerator(
|
| 73 |
+
name=datasets.Split.TEST,
|
| 74 |
+
gen_kwargs={"audio_path": test_path, "metadata_path": metadata_test, "split": "test"},
|
| 75 |
+
),
|
| 76 |
+
]
|
| 77 |
+
|
| 78 |
+
def _generate_examples(self, audio_path, metadata_path, split):
|
| 79 |
+
print(audio_path)
|
| 80 |
+
print(metadata_path)
|
| 81 |
+
print(split)
|
| 82 |
+
metadata_df = pd.read_csv(metadata_path)
|
| 83 |
+
|
| 84 |
+
if split == "train":
|
| 85 |
+
for idx_, row in metadata_df.iterrows():
|
| 86 |
+
yield idx_, {
|
| 87 |
+
"song_id": row["song_id"],
|
| 88 |
+
"file": os.path.join(audio_path, row["filepath"]),
|
| 89 |
+
"audio": os.path.join(audio_path, row["filepath"]),
|
| 90 |
+
"genre_id": row["genre_id"],
|
| 91 |
+
"genre": row["genre"],
|
| 92 |
+
}
|
| 93 |
+
else:
|
| 94 |
+
for idx_, row in metadata_df.iterrows():
|
| 95 |
+
yield idx_, {
|
| 96 |
+
"song_id": row["song_id"],
|
| 97 |
+
"file": os.path.join(audio_path, row["filepath"]),
|
| 98 |
+
"audio": os.path.join(audio_path, row["filepath"]),
|
| 99 |
+
"genre_id": -1,
|
| 100 |
+
"genre": "NA",
|
| 101 |
+
}
|