yangwang825 commited on
Commit
2b894fd
·
verified ·
1 Parent(s): 29c8586

Create crema-d-script.py

Browse files
Files changed (1) hide show
  1. crema-d-script.py +128 -0
crema-d-script.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+
3
+ """CREMA-D 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
+ SAMPLE_RATE = 16_000
15
+
16
+ _COMPRESSED_FILENAME = 'crema-d.zip'
17
+
18
+ CREMAD_EMOTIONS_MAPPING = {
19
+ 'ANG': 'anger',
20
+ 'DIS': 'disgust',
21
+ 'FEA': 'fear',
22
+ 'HAP': 'happy',
23
+ 'NEU': 'neutral',
24
+ 'SAD': 'sad',
25
+ }
26
+ CLASSES = list(sorted(CREMAD_EMOTIONS_MAPPING.values))
27
+
28
+
29
+ class CremaDConfig(datasets.BuilderConfig):
30
+ """BuilderConfig for CREMA-D."""
31
+
32
+ def __init__(self, features, **kwargs):
33
+ super(CremaDConfig, self).__init__(version=datasets.Version("0.0.1", ""), **kwargs)
34
+ self.features = features
35
+
36
+
37
+ class CREMAD(datasets.GeneratorBasedBuilder):
38
+
39
+ BUILDER_CONFIGS = [
40
+ GtzanConfig(
41
+ features=datasets.Features(
42
+ {
43
+ "file": datasets.Value("string"),
44
+ "audio": datasets.Audio(sampling_rate=SAMPLE_RATE),
45
+ "emotion": datasets.Value("string"),
46
+ "label": datasets.ClassLabel(names=CLASSES),
47
+ }
48
+ ),
49
+ name="crema-d",
50
+ description='',
51
+ ),
52
+ ]
53
+
54
+ def _info(self):
55
+ return datasets.DatasetInfo(
56
+ description="",
57
+ features=self.config.features,
58
+ supervised_keys=None,
59
+ homepage="",
60
+ citation="",
61
+ task_templates=None,
62
+ )
63
+
64
+ def _split_generators(self, dl_manager):
65
+ """Returns SplitGenerators."""
66
+ archive_path = dl_manager.extract(_COMPRESSED_FILENAME)
67
+ extensions = ['.au']
68
+ _, _walker = fast_scandir(archive_path, extensions, recursive=True)
69
+
70
+ train_walker, val_test_walker = train_test_split(
71
+ _walker, test_size=0.3, random_state=914, stratify=[default_find_classes(f) for f in _walker]
72
+ )
73
+ val_walker, test_walker = train_test_split(
74
+ val_test_walker, test_size=0.5, random_state=914, stratify=[default_find_classes(f) for f in val_test_walker]
75
+ )
76
+
77
+ return [
78
+ datasets.SplitGenerator(
79
+ name=datasets.Split.TRAIN, gen_kwargs={"audio_paths": train_walker, "split": "train"}
80
+ ),
81
+ datasets.SplitGenerator(
82
+ name=datasets.Split.VALIDATION, gen_kwargs={"audio_paths": val_walker, "split": "validation"}
83
+ ),
84
+ datasets.SplitGenerator(
85
+ name=datasets.Split.TEST, gen_kwargs={"audio_paths": test_walker, "split": "test"}
86
+ ),
87
+ ]
88
+
89
+ def _generate_examples(self, audio_paths, split=None):
90
+ for guid, audio_path in enumerate(audio_paths):
91
+ yield guid, {
92
+ "id": str(guid),
93
+ "file": audio_path,
94
+ "audio": audio_path,
95
+ "emotion": default_find_classes(audio_path),
96
+ "label": default_find_classes(audio_path),
97
+ }
98
+
99
+
100
+ def default_find_classes(audio_path):
101
+ return CREMAD_EMOTIONS_MAPPING.get(Path(audio_path).name.split('_')[2])
102
+
103
+
104
+ def fast_scandir(path: str, exts: tp.List[str], recursive: bool = False):
105
+ # Scan files recursively faster than glob
106
+ # From github.com/drscotthawley/aeiou/blob/main/aeiou/core.py
107
+ subfolders, files = [], []
108
+
109
+ try: # hope to avoid 'permission denied' by this try
110
+ for f in os.scandir(path):
111
+ try: # 'hope to avoid too many levels of symbolic links' error
112
+ if f.is_dir():
113
+ subfolders.append(f.path)
114
+ elif f.is_file():
115
+ if os.path.splitext(f.name)[1].lower() in exts:
116
+ files.append(f.path)
117
+ except Exception:
118
+ pass
119
+ except Exception:
120
+ pass
121
+
122
+ if recursive:
123
+ for path in list(subfolders):
124
+ sf, f = fast_scandir(path, exts, recursive=recursive)
125
+ subfolders.extend(sf)
126
+ files.extend(f) # type: ignore
127
+
128
+ return subfolders, files