FMADataset / fma.py
Dragunflie-420's picture
Update fma.py
85e47e4 verified
raw
history blame contribute delete
8.08 kB
# coding=utf-8
# Copyright 2024 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import pandas as pd
import numpy as np
import datasets
_CITATION = """
@inproceedings{defferrard2016fma,
title={FMA: A Dataset for Music Analysis},
author={Defferrard, Micha{\"e}l and Benzi, Kirell and Vandergheynst, Pierre and Bresson, Xavier},
booktitle={18th International Society for Music Information Retrieval Conference},
year={2017},
}
"""
_DESCRIPTION = """
The Free Music Archive (FMA) is an open and easily accessible dataset of music collections.
"""
_HOMEPAGE = "https://github.com/mdeff/fma"
_LICENSE = "Creative Commons Attribution 4.0 International License"
_URLs = {
"small": "https://os.unil.cloud.switch.ch/fma/fma_small.zip",
"metadata": "https://os.unil.cloud.switch.ch/fma/fma_metadata.zip",
}
class FMADataset(datasets.GeneratorBasedBuilder):
"""FMA small dataset."""
VERSION = datasets.Version("1.0.0")
BUILDER_CONFIGS = [
datasets.BuilderConfig(name="small", version=VERSION, description="The small subset of FMA dataset"),
]
def _info(self):
features = datasets.Features(
{
"track_id": datasets.Value("int32"),
"title": datasets.Value("string"),
"artist": datasets.Value("string"),
"genre": datasets.Value("string"),
"audio": datasets.Audio(sampling_rate=44100),
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
data_dir = dl_manager.download_and_extract(_URLs)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"filepath": os.path.join(data_dir["small"], "fma_small"),
"metadata_path": os.path.join(data_dir["metadata"], "fma_metadata"),
},
),
]
def _generate_examples(self, filepath, metadata_path):
"""Yields examples."""
# Load metadata
tracks = pd.read_csv(os.path.join(metadata_path, "tracks.csv"), index_col=0, header=[0, 1])
# Iterate through audio files
for root, _, files in os.walk(filepath):
for file in files:
if file.endswith('.mp3'):
track_id = int(file.split('.')[0])
audio_path = os.path.join(root, file)
# Get metadata
title = tracks.loc[track_id, ('track', 'title')]
artist = tracks.loc[track_id, ('artist', 'name')]
genre = tracks.loc[track_id, ('track', 'genre_top')]
yield track_id, {
"track_id": track_id,
"title": title,
"artist": artist,
"genre": genre,
"audio": audio_path,
}
@property
def manual_download_instructions(self):
return """
To use the FMA dataset, you need to download it manually. Please follow these steps:
1. Go to https://github.com/mdeff/fma
2. Download the 'fma_small.zip' and 'fma_metadata.zip' files
3. Extract both zip files
4. Copy the 'fma_small' folder and the 'fma_metadata' folder to the root of this dataset repository
Once you have completed these steps, the dataset will be ready to use.
""" coding=utf-8
# Copyright 2024 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import pandas as pd
import numpy as np
import datasets
_CITATION = """
@inproceedings{defferrard2016fma,
title={FMA: A Dataset for Music Analysis},
author={Defferrard, Micha{\"e}l and Benzi, Kirell and Vandergheynst, Pierre and Bresson, Xavier},
booktitle={18th International Society for Music Information Retrieval Conference},
year={2017},
}
"""
_DESCRIPTION = """
The Free Music Archive (FMA) is an open and easily accessible dataset of music collections.
"""
_HOMEPAGE = "https://github.com/mdeff/fma"
_LICENSE = "Creative Commons Attribution 4.0 International License"
_URLs = {
"small": "https://os.unil.cloud.switch.ch/fma/fma_small.zip",
"metadata": "https://os.unil.cloud.switch.ch/fma/fma_metadata.zip",
}
class FMADataset(datasets.GeneratorBasedBuilder):
"""FMA small dataset."""
VERSION = datasets.Version("1.0.0")
BUILDER_CONFIGS = [
datasets.BuilderConfig(name="small", version=VERSION, description="The small subset of FMA dataset"),
]
def _info(self):
features = datasets.Features(
{
"track_id": datasets.Value("int32"),
"title": datasets.Value("string"),
"artist": datasets.Value("string"),
"genre": datasets.Value("string"),
"audio": datasets.Audio(sampling_rate=44100),
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
data_dir = dl_manager.download_and_extract(_URLs)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"filepath": os.path.join(data_dir["small"], "fma_small"),
"metadata_path": os.path.join(data_dir["metadata"], "fma_metadata"),
},
),
]
def _generate_examples(self, filepath, metadata_path):
"""Yields examples."""
# Load metadata
tracks = pd.read_csv(os.path.join(metadata_path, "tracks.csv"), index_col=0, header=[0, 1])
# Iterate through audio files
for root, _, files in os.walk(filepath):
for file in files:
if file.endswith('.mp3'):
track_id = int(file.split('.')[0])
audio_path = os.path.join(root, file)
# Get metadata
title = tracks.loc[track_id, ('track', 'title')]
artist = tracks.loc[track_id, ('artist', 'name')]
genre = tracks.loc[track_id, ('track', 'genre_top')]
yield track_id, {
"track_id": track_id,
"title": title,
"artist": artist,
"genre": genre,
"audio": audio_path,
}