Datasets:

File size: 3,375 Bytes
abc0bda
 
 
 
 
 
 
 
 
 
 
 
 
50f9e6f
abc0bda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50f9e6f
abc0bda
 
 
 
 
 
 
 
 
 
 
 
 
 
50f9e6f
abc0bda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ea28ac
abc0bda
 
 
 
7ea28ac
abc0bda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Copyright 2020 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.
"""The GTZAN dataset."""


from pathlib import Path

import datasets
import pandas as pd

_CITATION = """\
@misc{tzanetakis_essl_cook_2001,
author    = "Tzanetakis, George and Essl, Georg and Cook, Perry",
title     = "Automatic Musical Genre Classification Of Audio Signals",
url       = "http://ismir2001.ismir.net/pdf/tzanetakis.pdf",
publisher = "The International Society for Music Information Retrieval",
year      = "2001"
}
"""


_DESCRIPTION = """\
GTZAN is a dataset for musical genre classification of audio signals. The dataset consists of 1,000 audio tracks, each of 30 seconds long. It contains 10 genres, each represented by 100 tracks. The tracks are all 22,050Hz Mono 16-bit audio files in WAV format. The genres are: blues, classical, country, disco, hiphop, jazz, metal, pop, reggae, and rock.
"""

_HOMEPAGE = "http://marsyas.info/downloads/datasets.html"

# TODO: Add the licence for the dataset here if you can find it
_LICENSE = ""

_URL = "http://opihi.cs.uvic.ca/sound/genres.tar.gz"

GENRES = ["blues", "classical", "country", "disco", "hiphop", "jazz", "metal", "pop", "reggae", "rock"]
CORRUPTED_FILES = ["jazz.00054.wav"]


class Gtzan(datasets.GeneratorBasedBuilder):
    """The GTZAn dataset"""

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "file": datasets.Value("string"),
                    "audio": datasets.Audio(sampling_rate=22_050),
                    "genre": datasets.ClassLabel(names=GENRES),
                }
            ),
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        local_extracted_archive = dl_manager.download_and_extract("data/genres.tar.gz")
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "local_extracted_archive": local_extracted_archive,
                },
            )
        ]

    def _generate_examples(self, local_extracted_archive):
        paths = list(Path(local_extracted_archive).glob("**/*.wav"))
        paths = [p for p in paths if "._" not in p.name]
        data = []

        for path in paths:
            label = str(path).split("/")[-2]
            name = str(path).split("/")[-1]
            if name in CORRUPTED_FILES:
                continue

            data.append({"file": str(path), "genre": label})
        df = pd.DataFrame(data)
        df.sort_values("file", inplace=True)

        for idx_, row in df.iterrows():
            yield idx_, {"file": row["file"], "audio": row["file"], "genre": row["genre"]}