yangwang825 commited on
Commit
8d22b8d
·
verified ·
1 Parent(s): 9181b8c

Create ravdess-script.py

Browse files
Files changed (1) hide show
  1. ravdess-script.py +176 -0
ravdess-script.py ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+
3
+ """RAVDESS paralinguistics classification dataset."""
4
+
5
+
6
+ import os
7
+ import textwrap
8
+ import datasets
9
+ import itertools
10
+ import typing as tp
11
+ from pathlib import Path
12
+ from sklearn.model_selection import train_test_split
13
+
14
+ _COMPRESSED_FILENAME = 'ravdess.zip'
15
+
16
+ SAMPLE_RATE = 48_000
17
+
18
+ RAVDESS_EMOTIONS_MAPPING = {
19
+ '01': 'neutral',
20
+ '02': 'calm',
21
+ '03': 'happy',
22
+ '04': 'sad',
23
+ '05': 'angry',
24
+ '06': 'fearful',
25
+ '07': 'disgust',
26
+ '08': 'surprised',
27
+ }
28
+ RAVDESS_ACTOR_FOLD_MAPPING = {
29
+ '01': '4',
30
+ '02': '0',
31
+ '03': '1',
32
+ '04': '4',
33
+ '05': '0',
34
+ '06': '1',
35
+ '07': '1',
36
+ '08': '3',
37
+ '09': '4',
38
+ '10': '2',
39
+ '11': '2',
40
+ '12': '2',
41
+ '13': '1',
42
+ '14': '0',
43
+ '15': '0',
44
+ '16': '0',
45
+ '17': '3',
46
+ '18': '1',
47
+ '19': '2',
48
+ '20': '2',
49
+ '21': '3',
50
+ '22': '4',
51
+ '23': '3',
52
+ '24': '3',
53
+ }
54
+ CLASSES = list(RAVDESS_EMOTIONS_MAPPING.values())
55
+
56
+
57
+ class RavdessConfig(datasets.BuilderConfig):
58
+ """BuilderConfig for RAVDESS."""
59
+
60
+ def __init__(self, features, **kwargs):
61
+ super(RavdessConfig, self).__init__(version=datasets.Version("0.0.1", ""), **kwargs)
62
+ self.features = features
63
+
64
+
65
+ class RAVDESS(datasets.GeneratorBasedBuilder):
66
+
67
+ BUILDER_CONFIGS = [
68
+ RavdessConfig(
69
+ features=datasets.Features(
70
+ {
71
+ "file": datasets.Value("string"),
72
+ "audio": datasets.Audio(sampling_rate=SAMPLE_RATE),
73
+ "emotion": datasets.Value("string"),
74
+ "label": datasets.ClassLabel(names=CLASSES),
75
+ }
76
+ ),
77
+ name=f"fold{f}",
78
+ description='',
79
+ ) for f in range(1, 6)
80
+ ]
81
+
82
+ def _info(self):
83
+ return datasets.DatasetInfo(
84
+ description="",
85
+ features=self.config.features,
86
+ supervised_keys=None,
87
+ homepage="",
88
+ citation="",
89
+ task_templates=None,
90
+ )
91
+
92
+ def _split_generators(self, dl_manager):
93
+ """Returns SplitGenerators."""
94
+ archive_path = dl_manager.extract(_COMPRESSED_FILENAME)
95
+ return [
96
+ datasets.SplitGenerator(
97
+ name=datasets.Split.TRAIN, gen_kwargs={"archive_path": archive_path, "split": "train"}
98
+ ),
99
+ datasets.SplitGenerator(
100
+ name=datasets.Split.VALIDATION, gen_kwargs={"archive_path": archive_path, "split": "validation"}
101
+ ),
102
+ datasets.SplitGenerator(
103
+ name=datasets.Split.TEST, gen_kwargs={"archive_path": archive_path, "split": "test"}
104
+ ),
105
+ ]
106
+
107
+ def _generate_examples(self, archive_path, split=None):
108
+ extensions = ['.wav']
109
+ _, _walker = fast_scandir(archive_path, extensions, recursive=True)
110
+ _walker_with_fold = [(fileid, default_find_fold(fileid)) for fileid in _walker]
111
+
112
+ if self.config.name == 'fold1':
113
+ train_fold = ['2', '3', '4', '5']
114
+ test_fold = ['1']
115
+ elif self.config.name == 'fold2':
116
+ train_fold = ['1', '3', '4', '5']
117
+ test_fold = ['2']
118
+ elif self.config.name == 'fold3':
119
+ train_fold = ['1', '2', '4', '5']
120
+ test_fold = ['3']
121
+ elif self.config.name == 'fold4':
122
+ train_fold = ['1', '2', '3', '5']
123
+ test_fold = ['4']
124
+ elif self.config.name == 'fold5':
125
+ train_fold = ['1', '2', '3', '4']
126
+ test_fold = ['5']
127
+
128
+ if split == 'train':
129
+ audio_paths = [fileid for fileid, fold in _walker_with_fold if fold in train_fold]
130
+ elif split == 'test':
131
+ audio_paths = [fileid for fileid, fold in _walker_with_fold if fold in train_fold]
132
+
133
+ for guid, audio_path in enumerate(audio_paths):
134
+ yield guid, {
135
+ "id": str(guid),
136
+ "file": audio_path,
137
+ "audio": audio_path,
138
+ "emotion": default_find_classes(audio_path),
139
+ "label": default_find_classes(audio_path),
140
+ }
141
+
142
+
143
+ def default_find_classes(audio_path):
144
+ return RAVDESS_EMOTIONS_MAPPING.get(Path(audio_path).name.split('-')[2])
145
+
146
+
147
+ def default_find_fold(audio_path):
148
+ actor_id = Path(audio_path).parent.stem.split('_')[1]
149
+ return RAVDESS_ACTOR_FOLD_MAPPING.get(actor_id)
150
+
151
+
152
+ def fast_scandir(path: str, exts: tp.List[str], recursive: bool = False):
153
+ # Scan files recursively faster than glob
154
+ # From github.com/drscotthawley/aeiou/blob/main/aeiou/core.py
155
+ subfolders, files = [], []
156
+
157
+ try: # hope to avoid 'permission denied' by this try
158
+ for f in os.scandir(path):
159
+ try: # 'hope to avoid too many levels of symbolic links' error
160
+ if f.is_dir():
161
+ subfolders.append(f.path)
162
+ elif f.is_file():
163
+ if os.path.splitext(f.name)[1].lower() in exts:
164
+ files.append(f.path)
165
+ except Exception:
166
+ pass
167
+ except Exception:
168
+ pass
169
+
170
+ if recursive:
171
+ for path in list(subfolders):
172
+ sf, f = fast_scandir(path, exts, recursive=recursive)
173
+ subfolders.extend(sf)
174
+ files.extend(f) # type: ignore
175
+
176
+ return subfolders, files