yangwang825 commited on
Commit
7619d15
1 Parent(s): 39faa4d

Update gtzan.py

Browse files
Files changed (1) hide show
  1. gtzan.py +98 -96
gtzan.py CHANGED
@@ -9,6 +9,103 @@ GENRES = [
9
  "jazz", "metal", "pop", "reggae", "rock"
10
  ]
11
  EXTENSION = '.au'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  FILTERED_TEST = [
13
  "blues.00012",
14
  "blues.00013",
@@ -944,99 +1041,4 @@ FILTERED_VALID = [
944
  "rock.00083",
945
  "rock.00084",
946
  "rock.00085",
947
- ]
948
-
949
-
950
- class GTZAN(datasets.GeneratorBasedBuilder):
951
- """
952
- The partitioning of GTZAN is "fault-filtered",
953
- which is constructed by hand to include 443/197/290 excerpts [1].
954
-
955
- References
956
- ----------
957
- [1] https://arxiv.org/pdf/1507.04761.pdf
958
- """
959
- def _info(self):
960
- return datasets.DatasetInfo(
961
- description='Music genre classification dataset',
962
- features=datasets.Features(
963
- {
964
- "filepath": datasets.Value("string"),
965
- "audio": datasets.Audio(sampling_rate=SAMPLE_RATE),
966
- "genre": datasets.ClassLabel(names=GENRES),
967
- }
968
- ),
969
- )
970
-
971
- def _split_generators(self, dl_manager):
972
- data_dir = dl_manager.download_and_extract("genres.tar.gz")
973
- return [
974
- datasets.SplitGenerator(
975
- name=datasets.Split.TRAIN,
976
- gen_kwargs={
977
- "data_dir": data_dir,
978
- "split": 'train'
979
- },
980
- ),
981
- datasets.SplitGenerator(
982
- name=datasets.Split.VALIDATION,
983
- gen_kwargs={
984
- "data_dir": data_dir,
985
- "split": 'validation'
986
- },
987
- ),
988
- datasets.SplitGenerator(
989
- name=datasets.Split.TEST,
990
- gen_kwargs={
991
- "data_dir": data_dir,
992
- "split": 'test'
993
- },
994
- )
995
- ]
996
-
997
- def _generate_examples(self, data_dir, split):
998
- _, _walker = fast_scandir(data_dir, [EXTENSION], recursive=True)
999
- if split == 'train':
1000
- _walker = [fileid for fileid in _walker if Path(fileid).stem in FILTERED_TRAIN]
1001
- elif split == 'validation':
1002
- _walker = [fileid for fileid in _walker if Path(fileid).stem in FILTERED_VALID]
1003
- elif split == 'test':
1004
- _walker = [fileid for fileid in _walker if Path(fileid).stem in FILTERED_TEST]
1005
-
1006
- for idx, fileid in enumerate(_walker):
1007
- yield idx, {
1008
- 'filepath': fileid,
1009
- 'audio': fileid,
1010
- 'genre': default_find_classes(fileid)
1011
- }
1012
-
1013
-
1014
- def default_find_classes(audio_path):
1015
- return Path(audio_path).parent.stem
1016
-
1017
-
1018
- def fast_scandir(path: str, exts: tp.List[str], recursive: bool = False):
1019
- # Scan files recursively faster than glob
1020
- # From github.com/drscotthawley/aeiou/blob/main/aeiou/core.py
1021
- subfolders, files = [], []
1022
-
1023
- try: # hope to avoid 'permission denied' by this try
1024
- for f in os.scandir(path):
1025
- try: # 'hope to avoid too many levels of symbolic links' error
1026
- if f.is_dir():
1027
- subfolders.append(f.path)
1028
- elif f.is_file():
1029
- if os.path.splitext(f.name)[1].lower() in exts:
1030
- files.append(f.path)
1031
- except Exception:
1032
- pass
1033
- except Exception:
1034
- pass
1035
-
1036
- if recursive:
1037
- for path in list(subfolders):
1038
- sf, f = fast_scandir(path, exts, recursive=recursive)
1039
- subfolders.extend(sf)
1040
- files.extend(f) # type: ignore
1041
-
1042
- return subfolders, files
 
9
  "jazz", "metal", "pop", "reggae", "rock"
10
  ]
11
  EXTENSION = '.au'
12
+
13
+
14
+ class GTZAN(datasets.GeneratorBasedBuilder):
15
+ """
16
+ The partitioning of GTZAN is "fault-filtered",
17
+ which is constructed by hand to include 443/197/290 excerpts [1].
18
+
19
+ References
20
+ ----------
21
+ [1] https://arxiv.org/pdf/1507.04761.pdf
22
+ """
23
+ def _info(self):
24
+ return datasets.DatasetInfo(
25
+ description='Music genre classification dataset',
26
+ features=datasets.Features(
27
+ {
28
+ "filepath": datasets.Value("string"),
29
+ "audio": datasets.Audio(sampling_rate=SAMPLE_RATE),
30
+ "genre": datasets.ClassLabel(names=GENRES),
31
+ }
32
+ ),
33
+ )
34
+
35
+ def _split_generators(self, dl_manager):
36
+ data_dir = dl_manager.download_and_extract("gtzan.tar.gz")
37
+ return [
38
+ datasets.SplitGenerator(
39
+ name=datasets.Split.TRAIN,
40
+ gen_kwargs={
41
+ "data_dir": data_dir,
42
+ "split": 'train'
43
+ },
44
+ ),
45
+ datasets.SplitGenerator(
46
+ name=datasets.Split.VALIDATION,
47
+ gen_kwargs={
48
+ "data_dir": data_dir,
49
+ "split": 'validation'
50
+ },
51
+ ),
52
+ datasets.SplitGenerator(
53
+ name=datasets.Split.TEST,
54
+ gen_kwargs={
55
+ "data_dir": data_dir,
56
+ "split": 'test'
57
+ },
58
+ )
59
+ ]
60
+
61
+ def _generate_examples(self, data_dir, split):
62
+ _, _walker = fast_scandir(data_dir, [EXTENSION], recursive=True)
63
+ if split == 'train':
64
+ _walker = [fileid for fileid in _walker if Path(fileid).stem in FILTERED_TRAIN]
65
+ elif split == 'validation':
66
+ _walker = [fileid for fileid in _walker if Path(fileid).stem in FILTERED_VALID]
67
+ elif split == 'test':
68
+ _walker = [fileid for fileid in _walker if Path(fileid).stem in FILTERED_TEST]
69
+
70
+ for idx, fileid in enumerate(_walker):
71
+ yield idx, {
72
+ 'filepath': fileid,
73
+ 'audio': fileid,
74
+ 'genre': default_find_classes(fileid)
75
+ }
76
+
77
+
78
+ def default_find_classes(audio_path):
79
+ return Path(audio_path).parent.stem
80
+
81
+
82
+ def fast_scandir(path: str, exts: tp.List[str], recursive: bool = False):
83
+ # Scan files recursively faster than glob
84
+ # From github.com/drscotthawley/aeiou/blob/main/aeiou/core.py
85
+ subfolders, files = [], []
86
+
87
+ try: # hope to avoid 'permission denied' by this try
88
+ for f in os.scandir(path):
89
+ try: # 'hope to avoid too many levels of symbolic links' error
90
+ if f.is_dir():
91
+ subfolders.append(f.path)
92
+ elif f.is_file():
93
+ if os.path.splitext(f.name)[1].lower() in exts:
94
+ files.append(f.path)
95
+ except Exception:
96
+ pass
97
+ except Exception:
98
+ pass
99
+
100
+ if recursive:
101
+ for path in list(subfolders):
102
+ sf, f = fast_scandir(path, exts, recursive=recursive)
103
+ subfolders.extend(sf)
104
+ files.extend(f) # type: ignore
105
+
106
+ return subfolders, files
107
+
108
+
109
  FILTERED_TEST = [
110
  "blues.00012",
111
  "blues.00013",
 
1041
  "rock.00083",
1042
  "rock.00084",
1043
  "rock.00085",
1044
+ ]