Jzuluaga commited on
Commit
7a68517
1 Parent(s): 3238848

Oh, ciao, ragazzi!!!

Browse files
hyperparams.yaml ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ############################################################################
2
+ # Model: WAV2VEC XLSR model for Accent Recognition (Italian)
3
+ # see paper: https://arxiv.org/abs/2305.18283
4
+ # ############################################################################
5
+
6
+ # Hparams NEEDED
7
+ HPARAMS_NEEDED: ["encoder_dim", "out_n_neurons", "label_encoder", "softmax"]
8
+ # Modules Needed
9
+ MODULES_NEEDED: ["wav2vec2", "avg_pool", "output_mlp"]
10
+
11
+ # Feature parameters
12
+ # wav2vec2_hub: facebook/wav2vec2-base
13
+ wav2vec2_hub: "facebook/wav2vec2-large-xlsr-53"
14
+
15
+ # Pretrain folder (HuggingFace)
16
+ pretrained_path: Jzuluaga/accent-id-commonaccent_xlsr-it-italian
17
+ # URL for the biggest Fairseq english wav2vec2 model.
18
+
19
+ # parameters
20
+ encoder_dim: 1024
21
+ out_n_neurons: 6
22
+
23
+ wav2vec2: !new:speechbrain.lobes.models.huggingface_wav2vec.HuggingFaceWav2Vec2
24
+ source: !ref <wav2vec2_hub>
25
+ output_norm: True
26
+ freeze: True
27
+ save_path: wav2vec2_checkpoints
28
+
29
+ # Mean and std normalization of the input features
30
+ mean_var_norm_emb: !new:speechbrain.processing.features.InputNormalization
31
+ norm_type: sentence
32
+ std_norm: False
33
+
34
+ avg_pool: !new:speechbrain.nnet.pooling.StatisticsPooling
35
+ return_std: False
36
+
37
+ output_mlp: !new:speechbrain.nnet.linear.Linear
38
+ input_size: !ref <encoder_dim>
39
+ n_neurons: !ref <out_n_neurons>
40
+ bias: False
41
+
42
+ model: !new:torch.nn.ModuleList
43
+ - [!ref <output_mlp>]
44
+
45
+ modules:
46
+ mean_var_norm_emb: !ref <mean_var_norm_emb>
47
+ wav2vec2: !ref <wav2vec2>
48
+ output_mlp: !ref <output_mlp>
49
+ avg_pool: !ref <avg_pool>
50
+
51
+ softmax: !new:speechbrain.nnet.activations.Softmax
52
+
53
+ label_encoder: !new:speechbrain.dataio.encoder.CategoricalEncoder
54
+
55
+
56
+ pretrainer: !new:speechbrain.utils.parameter_transfer.Pretrainer
57
+ loadables:
58
+ mean_var_norm_emb: !ref <mean_var_norm_emb>
59
+ wav2vec2: !ref <wav2vec2>
60
+ model: !ref <model>
61
+ label_encoder: !ref <label_encoder>
62
+ paths:
63
+ mean_var_norm_emb: !ref <pretrained_path>/normalizer_input.ckpt
64
+ wav2vec2: !ref <pretrained_path>/wav2vec2.ckpt
65
+ model: !ref <pretrained_path>/model.ckpt
66
+ label_encoder: !ref <pretrained_path>/label_encoder.txt
67
+
README.md CHANGED
@@ -1,3 +1,185 @@
1
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  license: mit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ language:
3
+ - it
4
+ thumbnail: null
5
+ tags:
6
+ - audio-classification
7
+ - speechbrain
8
+ - embeddings
9
+ - Accent Identification
10
+ - pytorch
11
+ - wav2vec2
12
+ - XLSR
13
+ - CommonAccent
14
+ - Italian
15
  license: mit
16
+ datasets:
17
+ - CommonVoice
18
+ metrics:
19
+ - Accuracy
20
+ widget:
21
+ - example_title: Veneto
22
+ src: >-
23
+ https://huggingface.co/Jzuluaga/accent-id-commonaccent_xlsr-it-italian/resolve/main/data/veneto.wav
24
+ - example_title: Emilian
25
+ src: >-
26
+ https://huggingface.co/Jzuluaga/accent-id-commonaccent_xlsr-it-italian/resolve/main/data/emilian.wav
27
+ - example_title: Trentino
28
+ src: >-
29
+ https://huggingface.co/Jzuluaga/accent-id-commonaccent_xlsr-it-italian/resolve/main/data/trentino.wav
30
+ - example_title: Meridionale
31
+ src: >-
32
+ https://huggingface.co/Jzuluaga/accent-id-commonaccent_xlsr-it-italian/resolve/main/data/meridionale.wav
33
  ---
34
+
35
+
36
+ <iframe src="https://ghbtns.com/github-btn.html?user=speechbrain&repo=speechbrain&type=star&count=true&size=large&v=2" frameborder="0" scrolling="0" width="170" height="30" title="GitHub"></iframe>
37
+ <br/><br/>
38
+
39
+
40
+ # CommonAccent: Exploring Large Acoustic Pretrained Models for Accent Classification Based on Common Voice
41
+
42
+ **Italian Accent Classifier**
43
+
44
+
45
+ **Abstract**:
46
+ Despite the recent advancements in Automatic Speech Recognition (ASR), the recognition of accented speech still remains a dominant problem. In order to create more inclusive ASR systems, research has shown that the integration of accent information, as part of a larger ASR framework, can lead to the mitigation of accented speech errors. We address multilingual accent classification through the ECAPA-TDNN and Wav2Vec 2.0/XLSR architectures which have been proven to perform well on a variety of speech-related downstream tasks. We introduce a simple-to-follow recipe aligned to the SpeechBrain toolkit for accent classification based on Common Voice 7.0 (English) and Common Voice 11.0 (Italian, German, and Spanish). Furthermore, we establish new state-of-the-art for English accent classification with as high as 95% accuracy. We also study the internal categorization of the Wav2Vev 2.0 embeddings through t-SNE, noting that there is a level of clustering based on phonological similarity.
47
+
48
+
49
+ This repository provides all the necessary tools to perform accent identification from speech recordings with [SpeechBrain](https://github.com/speechbrain/speechbrain).
50
+ The system uses a model pretrained on the CommonAccent dataset in Italian (5 accents). This system is based on the CommonLanguage Recipe located here: https://github.com/speechbrain/speechbrain/tree/develop/recipes/CommonLanguage
51
+
52
+
53
+ The provided system can recognize the following 5 accents from short speech recordings in Italian (IT):
54
+
55
+ ```
56
+ - VENETO
57
+ - EMILIANO
58
+ - MERIDIONALE
59
+ - TENDENTE AL SICULO MA NON MARCATO
60
+ - BASILICATA TRENTINO
61
+ ```
62
+
63
+ <a href="https://github.com/JuanPZuluaga/accent-recog-slt2022"> <img alt="GitHub" src="https://img.shields.io/badge/GitHub-Open%20source-green"> </a> Github repository link: https://github.com/JuanPZuluaga/accent-recog-slt2022
64
+
65
+
66
+ **NOTE**: due to incompatibility with the model and the current SpeechBrain interfaces, we cannot offer the Inference API. Please, follow the steps in **"Perform Accent Identification from Speech Recordings"** to use this Italian Accent ID model.
67
+
68
+ For a better experience, we encourage you to learn more about
69
+ [SpeechBrain](https://speechbrain.github.io). The given model performance on the test set is:
70
+
71
+ | Release (dd/mm/yyyy) | Accuracy (%)
72
+ |:-------------:|:--------------:|
73
+ | 01-08-2023 (this model) | 68.5 |
74
+
75
+
76
+ ## Pipeline description
77
+ This system is composed of a fine-tuned XLSR model coupled with statistical pooling. A classifier, trained with NLL Loss, is applied on top of that.
78
+
79
+ The system is trained with recordings sampled at 16kHz (single channel).
80
+ The code will automatically normalize your audio (i.e., resampling + mono channel selection) when calling *classify_file* if needed. Make sure your input tensor is compliant with the expected sampling rate if you use *encode_batch* and *classify_batch*.
81
+
82
+ ## Install SpeechBrain
83
+
84
+ First of all, please install SpeechBrain with the following command:
85
+
86
+ ```
87
+ pip install speechbrain
88
+ ```
89
+
90
+ Please notice that we encourage you to read our tutorials and learn more about
91
+ [SpeechBrain](https://speechbrain.github.io).
92
+
93
+ ### Perform Accent Identification from Speech Recordings
94
+
95
+ ```python
96
+ import torchaudio
97
+ from speechbrain.pretrained.interfaces import foreign_class
98
+
99
+ classifier = foreign_class(source="Jzuluaga/accent-id-commonaccent_xlsr-it-italian", pymodule_file="custom_interface.py", classname="CustomEncoderWav2vec2Classifier")
100
+
101
+ # Cuban Accent Example
102
+ out_prob, score, index, text_lab = classifier.classify_file('Jzuluaga/accent-id-commonaccent_xlsr-it-italian/data/veneto.wav')
103
+ print(text_lab)
104
+
105
+ # Caribbean Example
106
+ out_prob, score, index, text_lab = classifier.classify_file('Jzuluaga/accent-id-commonaccent_xlsr-it-italian/data/trentino.wav')
107
+ print(text_lab)
108
+ ```
109
+
110
+ ### Inference on GPU
111
+ To perform inference on the GPU, add `run_opts={"device":"cuda"}` when calling the `from_hparams` method.
112
+
113
+ ### Training
114
+
115
+ The model was trained with SpeechBrain.
116
+
117
+ To train it from scratch follow these steps:
118
+
119
+ 1. Clone SpeechBrain:
120
+ ```bash
121
+ git clone https://github.com/speechbrain/speechbrain/
122
+ ```
123
+
124
+ 2. Install it:
125
+ ```bash
126
+ cd speechbrain
127
+ pip install -r requirements.txt
128
+ pip install -e .
129
+ ```
130
+
131
+ 3. Clone our repository in https://github.com/JuanPZuluaga/accent-recog-slt2022:
132
+
133
+ ```bash
134
+ git clone https://github.com/JuanPZuluaga/accent-recog-slt2022
135
+ cd CommonAccent/accent_id
136
+ python train_w2v2.py hparams/train_w2v2.yaml
137
+ ```
138
+
139
+ You can find our training results (models, logs, etc) in this repository's `Files and versions` page.
140
+
141
+ ### Limitations
142
+ The SpeechBrain team does not provide any warranty on the performance achieved by this model when used on other datasets.
143
+
144
+
145
+
146
+ #### Cite our work: CommonAccent
147
+
148
+ If you find useful this work, please cite our work as:
149
+
150
+ ```
151
+ @article{zuluaga2023commonaccent,
152
+ title={CommonAccent: Exploring Large Acoustic Pretrained Models for Accent Classification Based on Common Voice},
153
+ author={Zuluaga-Gomez, Juan and Ahmed, Sara and Visockas, Danielius and Subakan, Cem},
154
+ journal={Interspeech 2023},
155
+ url={https://arxiv.org/abs/2305.18283},
156
+ year={2023}
157
+ }
158
+ ```
159
+
160
+ #### Cite XLSR model
161
+
162
+ ```@article{conneau2020unsupervised,
163
+ title={Unsupervised cross-lingual representation learning for speech recognition},
164
+ author={Conneau, Alexis and Baevski, Alexei and Collobert, Ronan and Mohamed, Abdelrahman and Auli, Michael},
165
+ journal={arXiv preprint arXiv:2006.13979},
166
+ year={2020}
167
+ }
168
+ ```
169
+
170
+
171
+ # **Cite SpeechBrain**
172
+ Please, cite SpeechBrain if you use it for your research or business.
173
+
174
+
175
+ ```bibtex
176
+ @misc{speechbrain,
177
+ title={{SpeechBrain}: A General-Purpose Speech Toolkit},
178
+ author={Mirco Ravanelli and Titouan Parcollet and Peter Plantinga and Aku Rouhe and Samuele Cornell and Loren Lugosch and Cem Subakan and Nauman Dawalatabad and Abdelwahab Heba and Jianyuan Zhong and Ju-Chieh Chou and Sung-Lin Yeh and Szu-Wei Fu and Chien-Feng Liao and Elena Rastorgueva and François Grondin and William Aris and Hwidong Na and Yan Gao and Renato De Mori and Yoshua Bengio},
179
+ year={2021},
180
+ eprint={2106.04624},
181
+ archivePrefix={arXiv},
182
+ primaryClass={eess.AS},
183
+ note={arXiv:2106.04624}
184
+ }
185
+ ```
accent_encoder.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ 'VENETO' => 0
2
+ 'EMILIANO' => 1
3
+ 'MERIDIONALE' => 2
4
+ 'TENDENTE AL SICULO MA NON MARCATO' => 3
5
+ 'BASILICATA TRENTINO' => 4
6
+ ================
7
+ 'starting_index' => 0
config.json ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ {
2
+ "speechbrain_interface": "CustomEncoderWav2vec2Classifier",
3
+ "model_type": "wav2vec2"
4
+ }
custom_interface.py ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from speechbrain.pretrained import Pretrained
3
+
4
+
5
+ class CustomEncoderWav2vec2Classifier(Pretrained):
6
+ """A ready-to-use class for utterance-level classification (e.g, speaker-id,
7
+ language-id, emotion recognition, keyword spotting, etc).
8
+
9
+ The class assumes that an self-supervised encoder like wav2vec2/hubert and a classifier model
10
+ are defined in the yaml file. If you want to
11
+ convert the predicted index into a corresponding text label, please
12
+ provide the path of the label_encoder in a variable called 'lab_encoder_file'
13
+ within the yaml.
14
+
15
+ The class can be used either to run only the encoder (encode_batch()) to
16
+ extract embeddings or to run a classification step (classify_batch()).
17
+ ```
18
+
19
+ Example
20
+ -------
21
+ >>> import torchaudio
22
+ >>> from speechbrain.pretrained import EncoderClassifier
23
+ >>> # Model is downloaded from the speechbrain HuggingFace repo
24
+ >>> tmpdir = getfixture("tmpdir")
25
+ >>> classifier = EncoderClassifier.from_hparams(
26
+ ... source="speechbrain/spkrec-ecapa-voxceleb",
27
+ ... savedir=tmpdir,
28
+ ... )
29
+
30
+ >>> # Compute embeddings
31
+ >>> signal, fs = torchaudio.load("samples/audio_samples/example1.wav")
32
+ >>> embeddings = classifier.encode_batch(signal)
33
+
34
+ >>> # Classification
35
+ >>> prediction = classifier .classify_batch(signal)
36
+ """
37
+
38
+ def __init__(self, *args, **kwargs):
39
+ super().__init__(*args, **kwargs)
40
+
41
+ def encode_batch(self, wavs, wav_lens=None, normalize=False):
42
+ """Encodes the input audio into a single vector embedding.
43
+
44
+ The waveforms should already be in the model's desired format.
45
+ You can call:
46
+ ``normalized = <this>.normalizer(signal, sample_rate)``
47
+ to get a correctly converted signal in most cases.
48
+
49
+ Arguments
50
+ ---------
51
+ wavs : torch.tensor
52
+ Batch of waveforms [batch, time, channels] or [batch, time]
53
+ depending on the model. Make sure the sample rate is fs=16000 Hz.
54
+ wav_lens : torch.tensor
55
+ Lengths of the waveforms relative to the longest one in the
56
+ batch, tensor of shape [batch]. The longest one should have
57
+ relative length 1.0 and others len(waveform) / max_length.
58
+ Used for ignoring padding.
59
+ normalize : bool
60
+ If True, it normalizes the embeddings with the statistics
61
+ contained in mean_var_norm_emb.
62
+
63
+ Returns
64
+ -------
65
+ torch.tensor
66
+ The encoded batch
67
+ """
68
+ # Manage single waveforms in input
69
+ if len(wavs.shape) == 1:
70
+ wavs = wavs.unsqueeze(0)
71
+
72
+ # Assign full length if wav_lens is not assigned
73
+ if wav_lens is None:
74
+ wav_lens = torch.ones(wavs.shape[0], device=self.device)
75
+
76
+ # Storing waveform in the specified device
77
+ wavs, wav_lens = wavs.to(self.device), wav_lens.to(self.device)
78
+ wavs = wavs.float()
79
+
80
+ # Computing features and embeddings
81
+ outputs = self.mods.wav2vec2(wavs)
82
+
83
+ # last dim will be used for AdaptativeAVG pool
84
+ outputs = self.mods.avg_pool(outputs, wav_lens)
85
+ outputs = outputs.view(outputs.shape[0], -1)
86
+ return outputs
87
+
88
+ def classify_batch(self, wavs, wav_lens=None):
89
+ """Performs classification on the top of the encoded features.
90
+
91
+ It returns the posterior probabilities, the index and, if the label
92
+ encoder is specified it also the text label.
93
+
94
+ Arguments
95
+ ---------
96
+ wavs : torch.tensor
97
+ Batch of waveforms [batch, time, channels] or [batch, time]
98
+ depending on the model. Make sure the sample rate is fs=16000 Hz.
99
+ wav_lens : torch.tensor
100
+ Lengths of the waveforms relative to the longest one in the
101
+ batch, tensor of shape [batch]. The longest one should have
102
+ relative length 1.0 and others len(waveform) / max_length.
103
+ Used for ignoring padding.
104
+
105
+ Returns
106
+ -------
107
+ out_prob
108
+ The log posterior probabilities of each class ([batch, N_class])
109
+ score:
110
+ It is the value of the log-posterior for the best class ([batch,])
111
+ index
112
+ The indexes of the best class ([batch,])
113
+ text_lab:
114
+ List with the text labels corresponding to the indexes.
115
+ (label encoder should be provided).
116
+ """
117
+ outputs = self.encode_batch(wavs, wav_lens)
118
+ outputs = self.mods.output_mlp(outputs)
119
+ out_prob = self.hparams.softmax(outputs)
120
+ score, index = torch.max(out_prob, dim=-1)
121
+ text_lab = self.hparams.label_encoder.decode_torch(index)
122
+ return out_prob, score, index, text_lab
123
+
124
+ def classify_file(self, path):
125
+ """Classifies the given audiofile into the given set of labels.
126
+
127
+ Arguments
128
+ ---------
129
+ path : str
130
+ Path to audio file to classify.
131
+
132
+ Returns
133
+ -------
134
+ out_prob
135
+ The log posterior probabilities of each class ([batch, N_class])
136
+ score:
137
+ It is the value of the log-posterior for the best class ([batch,])
138
+ index
139
+ The indexes of the best class ([batch,])
140
+ text_lab:
141
+ List with the text labels corresponding to the indexes.
142
+ (label encoder should be provided).
143
+ """
144
+ waveform = self.load_audio(path)
145
+ # Fake a batch:
146
+ batch = waveform.unsqueeze(0)
147
+ rel_length = torch.tensor([1.0])
148
+ outputs = self.encode_batch(batch, rel_length)
149
+ outputs = self.mods.output_mlp(outputs).squeeze(1)
150
+ out_prob = self.hparams.softmax(outputs)
151
+ score, index = torch.max(out_prob, dim=-1)
152
+ text_lab = self.hparams.label_encoder.decode_torch(index)
153
+ return out_prob, score, index, text_lab
154
+
155
+ def forward(self, wavs, wav_lens=None, normalize=False):
156
+ return self.encode_batch(
157
+ wavs=wavs, wav_lens=wav_lens, normalize=normalize
158
+ )
data/emilian.wav ADDED
Binary file (495 kB). View file
 
data/meridionale.wav ADDED
Binary file (392 kB). View file
 
data/trentino.wav ADDED
Binary file (705 kB). View file
 
data/veneto.wav ADDED
Binary file (233 kB). View file
 
model.ckpt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:409f2e5889e685b590f74dcf70b3fa77e95f128e6f1954cbdf8d27f2396596d7
3
+ size 8419163
normalizer_input.ckpt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:01dd629a2a24b29ad133b15930494e168114b788c4d7579ac2862e3d406e00fb
3
+ size 1063