lewtun HF staff commited on
Commit
abc0bda
1 Parent(s): 116e5ec

Add loading script

Browse files
Files changed (1) hide show
  1. gtzan.py +97 -0
gtzan.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # TODO: Address all TODOs and remove all explanatory comments
15
+ """TODO: Add a description here."""
16
+
17
+
18
+ import csv
19
+ import json
20
+ import os
21
+ from pathlib import Path
22
+
23
+ import datasets
24
+ import pandas as pd
25
+
26
+ _CITATION = """\
27
+ @misc{tzanetakis_essl_cook_2001,
28
+ author = "Tzanetakis, George and Essl, Georg and Cook, Perry",
29
+ title = "Automatic Musical Genre Classification Of Audio Signals",
30
+ url = "http://ismir2001.ismir.net/pdf/tzanetakis.pdf",
31
+ publisher = "The International Society for Music Information Retrieval",
32
+ year = "2001"
33
+ }
34
+ """
35
+
36
+
37
+ _DESCRIPTION = """\
38
+ 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.
39
+ """
40
+
41
+ _HOMEPAGE = "http://marsyas.info/downloads/datasets.html"
42
+
43
+ # TODO: Add the licence for the dataset here if you can find it
44
+ _LICENSE = ""
45
+
46
+ _URL = "http://opihi.cs.uvic.ca/sound/genres.tar.gz"
47
+
48
+ GENRES = ["blues", "classical", "country", "disco", "hiphop", "jazz", "metal", "pop", "reggae", "rock"]
49
+ CORRUPTED_FILES = ["jazz.00054.wav"]
50
+
51
+
52
+ class Gtzan(datasets.GeneratorBasedBuilder):
53
+ """TODO: Short description of my dataset."""
54
+
55
+ def _info(self):
56
+ return datasets.DatasetInfo(
57
+ description=_DESCRIPTION,
58
+ features=datasets.Features(
59
+ {
60
+ "file": datasets.Value("string"),
61
+ "audio": datasets.Audio(sampling_rate=22_050),
62
+ "genre": datasets.ClassLabel(names=GENRES),
63
+ }
64
+ ),
65
+ homepage=_HOMEPAGE,
66
+ license=_LICENSE,
67
+ citation=_CITATION,
68
+ )
69
+
70
+ def _split_generators(self, dl_manager):
71
+ local_extracted_archive = dl_manager.download_and_extract("data/genres.tar.gz")
72
+ return [
73
+ datasets.SplitGenerator(
74
+ name=datasets.Split.TRAIN,
75
+ gen_kwargs={
76
+ "local_extracted_archive": local_extracted_archive,
77
+ },
78
+ )
79
+ ]
80
+
81
+ def _generate_examples(self, local_extracted_archive):
82
+ paths = list(Path(local_extracted_archive).glob("**/*.wav"))
83
+ paths = [p for p in paths if "._" not in p.name]
84
+ data = []
85
+
86
+ for path in paths:
87
+ label = str(path).split("/")[-2]
88
+ name = str(path).split("/")[-1]
89
+ if name in CORRUPTED_FILES:
90
+ continue
91
+
92
+ data.append({"file": str(path), "genre": label})
93
+ df = pd.DataFrame(data)
94
+ df.sort_values("file", inplace=True)
95
+
96
+ for idx_, row in df.iterrows():
97
+ yield idx_, {"file": row["file"], "audio": row["file"], "genre": row["genre"]}