yangwang825 commited on
Commit
18f336f
·
verified ·
1 Parent(s): 2507a1c

Rename mswc.py to emodb.py

Browse files
Files changed (2) hide show
  1. emodb.py +131 -0
  2. mswc.py +0 -30
emodb.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+
3
+ """EmoDB paralinguistics 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
+
13
+ from ._emodb import OFFICIAL_TRAIN, OFFICIAL_TEST
14
+
15
+
16
+ SAMPLE_RATE = 16_000
17
+
18
+ _COMPRESSED_FILENAME = 'emo-db.tar.gz'
19
+
20
+ EMOTIONS_MAPPING = {
21
+ 'A': 'anxiety',
22
+ 'E': 'disgust',
23
+ 'F': 'happiness',
24
+ 'L': 'boredom',
25
+ 'N': 'neutral',
26
+ 'T': 'sadness',
27
+ 'W': 'anger',
28
+ }
29
+
30
+ EMOTIONS = [
31
+ 'anxiety', 'disgust', 'happiness', 'boredom', 'neutral', 'sadness', 'anger'
32
+ ]
33
+
34
+
35
+ class EmodbConfig(datasets.BuilderConfig):
36
+ """BuilderConfig for EmoDB."""
37
+
38
+ def __init__(self, features, **kwargs):
39
+ super(EmodbConfig, self).__init__(version=datasets.Version("0.0.1", ""), **kwargs)
40
+ self.features = features
41
+
42
+
43
+ class EmoDB(datasets.GeneratorBasedBuilder):
44
+
45
+ BUILDER_CONFIGS = [
46
+ EmodbConfig(
47
+ features=datasets.Features(
48
+ {
49
+ "file": datasets.Value("string"),
50
+ "audio": datasets.Audio(sampling_rate=SAMPLE_RATE),
51
+ "emotion": datasets.Value("string"),
52
+ "label": datasets.ClassLabel(names=EMOTIONS),
53
+ }
54
+ ),
55
+ name="gtzan",
56
+ description=textwrap.dedent(
57
+ """\
58
+ Paralinguistics classifies each audio for its emotion as a multi-class
59
+ classification, where emotions are in the same pre-defined set for both training and testing.
60
+ The evaluation metric is accuracy (ACC).
61
+ """
62
+ ),
63
+ ),
64
+ ]
65
+
66
+ def _info(self):
67
+ return datasets.DatasetInfo(
68
+ description="",
69
+ features=self.config.features,
70
+ supervised_keys=None,
71
+ homepage="",
72
+ citation="",
73
+ task_templates=None,
74
+ )
75
+
76
+ def _split_generators(self, dl_manager):
77
+ """Returns SplitGenerators."""
78
+ archive_path = dl_manager.extract(_COMPRESSED_FILENAME)
79
+ return [
80
+ datasets.SplitGenerator(
81
+ name=datasets.Split.TRAIN, gen_kwargs={"archive_path": archive_path, "split": "train"}
82
+ ),
83
+ datasets.SplitGenerator(
84
+ name=datasets.Split.TEST, gen_kwargs={"archive_path": archive_path, "split": "test"}
85
+ ),
86
+ ]
87
+
88
+ def _generate_examples(self, archive_path, split=None):
89
+ extensions = ['.wav']
90
+ _, _walker = fast_scandir(archive_path, extensions, recursive=True)
91
+
92
+ if split == 'train':
93
+ _walker = [fileid for fileid in _walker if Path(fileid).stem in OFFICIAL_TRAIN]
94
+ elif split == 'test':
95
+ _walker = [fileid for fileid in _walker if Path(fileid).stem in OFFICIAL_TEST]
96
+
97
+ for guid, audio_path in enumerate(_walker):
98
+ yield guid, {
99
+ "id": str(guid),
100
+ "file": audio_path,
101
+ "audio": audio_path,
102
+ "emotion": EMOTIONS_MAPPING.get(Path(audio_path).stem[-2]),
103
+ "label": EMOTIONS_MAPPING.get(Path(audio_path).stem[-2]),
104
+ }
105
+
106
+
107
+ def fast_scandir(path: str, exts: tp.List[str], recursive: bool = False):
108
+ # Scan files recursively faster than glob
109
+ # From github.com/drscotthawley/aeiou/blob/main/aeiou/core.py
110
+ subfolders, files = [], []
111
+
112
+ try: # hope to avoid 'permission denied' by this try
113
+ for f in os.scandir(path):
114
+ try: # 'hope to avoid too many levels of symbolic links' error
115
+ if f.is_dir():
116
+ subfolders.append(f.path)
117
+ elif f.is_file():
118
+ if os.path.splitext(f.name)[1].lower() in exts:
119
+ files.append(f.path)
120
+ except Exception:
121
+ pass
122
+ except Exception:
123
+ pass
124
+
125
+ if recursive:
126
+ for path in list(subfolders):
127
+ sf, f = fast_scandir(path, exts, recursive=recursive)
128
+ subfolders.extend(sf)
129
+ files.extend(f) # type: ignore
130
+
131
+ return subfolders, files
mswc.py DELETED
@@ -1,30 +0,0 @@
1
- # coding=utf-8
2
-
3
- """EmoDB paralinguistics 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
-
13
- from ._mswc import (
14
- TRAIN_ENG, VALIDATION_ENG, TEST_ENG,
15
- TRAIN_SPA, VALIDATION_SPA, TEST_SPA,
16
- TRAIN_IND, VALIDATION_IND, TEST_IND,
17
- )
18
-
19
-
20
- SAMPLE_RATE = 16_000
21
-
22
- _COMPRESSED_FILENAME = 'gtzan.tar.gz'
23
-
24
-
25
- class EmodbConfig(datasets.BuilderConfig):
26
- """BuilderConfig for EmoDB."""
27
-
28
- def __init__(self, features, **kwargs):
29
- super(GtzanConfig, self).__init__(version=datasets.Version("0.0.1", ""), **kwargs)
30
- self.features = features