Audio Classification
Transformers
Safetensors
Chinese
genaid
feature-extraction
accent-recognition
speaker-disentanglement
wav2vec2
custom_code
Instructions to use walston/GenAID with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use walston/GenAID with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("audio-classification", model="walston/GenAID", trust_remote_code=True)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("walston/GenAID", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Add files using upload-large-folder tool
Browse files- README.md +39 -0
- config.json +157 -0
- configuration_genaid.py +23 -0
- model.safetensors +3 -0
- modeling_genaid.py +68 -0
- preprocessor_config.json +9 -0
README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- zh
|
| 4 |
+
license: apache-2.0
|
| 5 |
+
library_name: transformers
|
| 6 |
+
pipeline_tag: audio-classification
|
| 7 |
+
tags:
|
| 8 |
+
- accent-recognition
|
| 9 |
+
- speaker-disentanglement
|
| 10 |
+
- wav2vec2
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
# GenAID
|
| 14 |
+
|
| 15 |
+
GenAID is a Chinese accent encoder based on `facebook/wav2vec2-large-xlsr-53`. It produces a 64-dimensional accent embedding designed to reduce speaker information. The model recognizes nine labels: north, Sichuan, Guangdong, south, Henan, Shanghai, Wuhan, Tianjin, and Singapore.
|
| 16 |
+
|
| 17 |
+
## Usage
|
| 18 |
+
|
| 19 |
+
```python
|
| 20 |
+
import librosa
|
| 21 |
+
import torch
|
| 22 |
+
from transformers import AutoFeatureExtractor, AutoModel
|
| 23 |
+
|
| 24 |
+
repo = "walston/GenAID"
|
| 25 |
+
processor = AutoFeatureExtractor.from_pretrained(repo)
|
| 26 |
+
model = AutoModel.from_pretrained(repo, trust_remote_code=True).cuda().eval()
|
| 27 |
+
|
| 28 |
+
wav, _ = librosa.load("audio.wav", sr=16000, mono=True)
|
| 29 |
+
inputs = processor(wav, sampling_rate=16000, return_tensors="pt")
|
| 30 |
+
inputs = {key: value.cuda() for key, value in inputs.items()}
|
| 31 |
+
|
| 32 |
+
with torch.inference_mode():
|
| 33 |
+
output = model(**inputs)
|
| 34 |
+
|
| 35 |
+
accent_embedding = output.embedding # [batch, 64]
|
| 36 |
+
accent_logits = output.accent_logits # [batch, 9]
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
Loading this repository requires `trust_remote_code=True` because it includes the small GenAID bottleneck and classification heads around the standard XLS-R encoder.
|
config.json
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"accent_labels": [
|
| 3 |
+
"north",
|
| 4 |
+
"Sichuan",
|
| 5 |
+
"Guangdong",
|
| 6 |
+
"south",
|
| 7 |
+
"Henan",
|
| 8 |
+
"Shanghai",
|
| 9 |
+
"Wuhan",
|
| 10 |
+
"Tianjin",
|
| 11 |
+
"singapore"
|
| 12 |
+
],
|
| 13 |
+
"architectures": [
|
| 14 |
+
"GenAIDModel"
|
| 15 |
+
],
|
| 16 |
+
"auto_map": {
|
| 17 |
+
"AutoConfig": "configuration_genaid.GenAIDConfig",
|
| 18 |
+
"AutoModel": "modeling_genaid.GenAIDModel"
|
| 19 |
+
},
|
| 20 |
+
"bottleneck_dim": 64,
|
| 21 |
+
"encoder_config": {
|
| 22 |
+
"_name_or_path": "facebook/wav2vec2-large-xlsr-53",
|
| 23 |
+
"activation_dropout": 0.0,
|
| 24 |
+
"adapter_attn_dim": null,
|
| 25 |
+
"adapter_kernel_size": 3,
|
| 26 |
+
"adapter_stride": 2,
|
| 27 |
+
"add_adapter": false,
|
| 28 |
+
"apply_spec_augment": true,
|
| 29 |
+
"architectures": [
|
| 30 |
+
"Wav2Vec2ForPreTraining"
|
| 31 |
+
],
|
| 32 |
+
"attention_dropout": 0.1,
|
| 33 |
+
"bos_token_id": 1,
|
| 34 |
+
"chunk_size_feed_forward": 0,
|
| 35 |
+
"classifier_proj_size": 256,
|
| 36 |
+
"codevector_dim": 768,
|
| 37 |
+
"contrastive_logits_temperature": 0.1,
|
| 38 |
+
"conv_bias": true,
|
| 39 |
+
"conv_dim": [
|
| 40 |
+
512,
|
| 41 |
+
512,
|
| 42 |
+
512,
|
| 43 |
+
512,
|
| 44 |
+
512,
|
| 45 |
+
512,
|
| 46 |
+
512
|
| 47 |
+
],
|
| 48 |
+
"conv_kernel": [
|
| 49 |
+
10,
|
| 50 |
+
3,
|
| 51 |
+
3,
|
| 52 |
+
3,
|
| 53 |
+
3,
|
| 54 |
+
2,
|
| 55 |
+
2
|
| 56 |
+
],
|
| 57 |
+
"conv_stride": [
|
| 58 |
+
5,
|
| 59 |
+
2,
|
| 60 |
+
2,
|
| 61 |
+
2,
|
| 62 |
+
2,
|
| 63 |
+
2,
|
| 64 |
+
2
|
| 65 |
+
],
|
| 66 |
+
"ctc_loss_reduction": "sum",
|
| 67 |
+
"ctc_zero_infinity": false,
|
| 68 |
+
"diversity_loss_weight": 0.1,
|
| 69 |
+
"do_stable_layer_norm": true,
|
| 70 |
+
"dtype": null,
|
| 71 |
+
"eos_token_id": 2,
|
| 72 |
+
"feat_extract_activation": "gelu",
|
| 73 |
+
"feat_extract_dropout": 0.0,
|
| 74 |
+
"feat_extract_norm": "layer",
|
| 75 |
+
"feat_proj_dropout": 0.1,
|
| 76 |
+
"feat_quantizer_dropout": 0.0,
|
| 77 |
+
"final_dropout": 0.0,
|
| 78 |
+
"gradient_checkpointing": false,
|
| 79 |
+
"hidden_act": "gelu",
|
| 80 |
+
"hidden_dropout": 0.1,
|
| 81 |
+
"hidden_size": 1024,
|
| 82 |
+
"id2label": {
|
| 83 |
+
"0": "LABEL_0",
|
| 84 |
+
"1": "LABEL_1"
|
| 85 |
+
},
|
| 86 |
+
"initializer_range": 0.02,
|
| 87 |
+
"intermediate_size": 4096,
|
| 88 |
+
"is_encoder_decoder": false,
|
| 89 |
+
"label2id": {
|
| 90 |
+
"LABEL_0": 0,
|
| 91 |
+
"LABEL_1": 1
|
| 92 |
+
},
|
| 93 |
+
"layer_norm_eps": 1e-05,
|
| 94 |
+
"layerdrop": 0.1,
|
| 95 |
+
"mask_channel_length": 10,
|
| 96 |
+
"mask_channel_min_space": 1,
|
| 97 |
+
"mask_channel_other": 0.0,
|
| 98 |
+
"mask_channel_prob": 0.0,
|
| 99 |
+
"mask_channel_selection": "static",
|
| 100 |
+
"mask_feature_length": 10,
|
| 101 |
+
"mask_feature_min_masks": 0,
|
| 102 |
+
"mask_feature_prob": 0.0,
|
| 103 |
+
"mask_time_length": 10,
|
| 104 |
+
"mask_time_min_masks": 2,
|
| 105 |
+
"mask_time_min_space": 1,
|
| 106 |
+
"mask_time_other": 0.0,
|
| 107 |
+
"mask_time_prob": 0.075,
|
| 108 |
+
"mask_time_selection": "static",
|
| 109 |
+
"model_type": "wav2vec2",
|
| 110 |
+
"num_adapter_layers": 3,
|
| 111 |
+
"num_attention_heads": 16,
|
| 112 |
+
"num_codevector_groups": 2,
|
| 113 |
+
"num_codevectors_per_group": 320,
|
| 114 |
+
"num_conv_pos_embedding_groups": 16,
|
| 115 |
+
"num_conv_pos_embeddings": 128,
|
| 116 |
+
"num_feat_extract_layers": 7,
|
| 117 |
+
"num_hidden_layers": 24,
|
| 118 |
+
"num_negatives": 100,
|
| 119 |
+
"output_attentions": false,
|
| 120 |
+
"output_hidden_size": 1024,
|
| 121 |
+
"output_hidden_states": false,
|
| 122 |
+
"pad_token_id": 0,
|
| 123 |
+
"problem_type": null,
|
| 124 |
+
"proj_codevector_dim": 768,
|
| 125 |
+
"return_dict": true,
|
| 126 |
+
"tdnn_dilation": [
|
| 127 |
+
1,
|
| 128 |
+
2,
|
| 129 |
+
3,
|
| 130 |
+
1,
|
| 131 |
+
1
|
| 132 |
+
],
|
| 133 |
+
"tdnn_dim": [
|
| 134 |
+
512,
|
| 135 |
+
512,
|
| 136 |
+
512,
|
| 137 |
+
512,
|
| 138 |
+
1500
|
| 139 |
+
],
|
| 140 |
+
"tdnn_kernel": [
|
| 141 |
+
5,
|
| 142 |
+
3,
|
| 143 |
+
3,
|
| 144 |
+
1,
|
| 145 |
+
1
|
| 146 |
+
],
|
| 147 |
+
"transformers_version": "5.15.0",
|
| 148 |
+
"use_weighted_layer_sum": false,
|
| 149 |
+
"vocab_size": 32,
|
| 150 |
+
"xvector_output_dim": 512
|
| 151 |
+
},
|
| 152 |
+
"model_type": "genaid",
|
| 153 |
+
"num_accents": 9,
|
| 154 |
+
"num_speakers": 336,
|
| 155 |
+
"sampling_rate": 16000,
|
| 156 |
+
"transformers_version": "5.15.0"
|
| 157 |
+
}
|
configuration_genaid.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PreTrainedConfig
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class GenAIDConfig(PreTrainedConfig):
|
| 5 |
+
model_type = "genaid"
|
| 6 |
+
|
| 7 |
+
def __init__(
|
| 8 |
+
self,
|
| 9 |
+
encoder_config=None,
|
| 10 |
+
bottleneck_dim=64,
|
| 11 |
+
num_accents=9,
|
| 12 |
+
num_speakers=336,
|
| 13 |
+
accent_labels=None,
|
| 14 |
+
sampling_rate=16000,
|
| 15 |
+
**kwargs,
|
| 16 |
+
):
|
| 17 |
+
super().__init__(**kwargs)
|
| 18 |
+
self.encoder_config = encoder_config or {}
|
| 19 |
+
self.bottleneck_dim = bottleneck_dim
|
| 20 |
+
self.num_accents = num_accents
|
| 21 |
+
self.num_speakers = num_speakers
|
| 22 |
+
self.accent_labels = accent_labels or []
|
| 23 |
+
self.sampling_rate = sampling_rate
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6cfda4450be56bfc4c1c367cbb1f0c169bd2f31b8a0136b49e1cb21c747c1d3c
|
| 3 |
+
size 1262174728
|
modeling_genaid.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
from typing import Optional
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
from torch import nn
|
| 6 |
+
from transformers import AutoConfig, AutoModel, PreTrainedModel
|
| 7 |
+
from transformers.utils import ModelOutput
|
| 8 |
+
|
| 9 |
+
from .configuration_genaid import GenAIDConfig
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@dataclass
|
| 13 |
+
class GenAIDOutput(ModelOutput):
|
| 14 |
+
embedding: torch.FloatTensor = None
|
| 15 |
+
accent_logits: Optional[torch.FloatTensor] = None
|
| 16 |
+
speaker_logits: Optional[torch.FloatTensor] = None
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class GenAIDModel(PreTrainedModel):
|
| 20 |
+
config_class = GenAIDConfig
|
| 21 |
+
base_model_prefix = "genaid"
|
| 22 |
+
main_input_name = "input_values"
|
| 23 |
+
# GenAID has no tied parameters. Transformers 5.x expects custom models to
|
| 24 |
+
# expose this mapping while finalizing low-memory checkpoint loading.
|
| 25 |
+
all_tied_weights_keys = {}
|
| 26 |
+
|
| 27 |
+
def __init__(self, config):
|
| 28 |
+
super().__init__(config)
|
| 29 |
+
encoder_dict = dict(config.encoder_config)
|
| 30 |
+
model_type = encoder_dict.pop("model_type")
|
| 31 |
+
encoder_config = AutoConfig.for_model(model_type, **encoder_dict)
|
| 32 |
+
self.encoder = AutoModel.from_config(encoder_config)
|
| 33 |
+
hidden = encoder_config.hidden_size
|
| 34 |
+
dim = config.bottleneck_dim
|
| 35 |
+
self.bottleneck = nn.Sequential(
|
| 36 |
+
nn.Linear(hidden, dim), nn.GELU(), nn.Linear(dim, dim), nn.GELU()
|
| 37 |
+
)
|
| 38 |
+
self.accent_classifier = nn.Linear(dim, config.num_accents, bias=False)
|
| 39 |
+
self.speaker_classifier = nn.Linear(dim, config.num_speakers, bias=False)
|
| 40 |
+
|
| 41 |
+
@staticmethod
|
| 42 |
+
def masked_mean(hidden_states, attention_mask):
|
| 43 |
+
if attention_mask is None:
|
| 44 |
+
return hidden_states.mean(1)
|
| 45 |
+
lengths = attention_mask.sum(-1)
|
| 46 |
+
frame_lengths = (lengths * hidden_states.shape[1] / attention_mask.shape[1]).ceil().long()
|
| 47 |
+
frame_lengths = frame_lengths.clamp(1, hidden_states.shape[1])
|
| 48 |
+
frame_mask = torch.arange(hidden_states.shape[1], device=hidden_states.device)[None]
|
| 49 |
+
frame_mask = frame_mask < frame_lengths[:, None]
|
| 50 |
+
return (hidden_states * frame_mask.unsqueeze(-1)).sum(1) / frame_lengths.unsqueeze(-1)
|
| 51 |
+
|
| 52 |
+
def forward(self, input_values, attention_mask=None, return_dict=True, **kwargs):
|
| 53 |
+
encoded = self.encoder(
|
| 54 |
+
input_values=input_values,
|
| 55 |
+
attention_mask=attention_mask,
|
| 56 |
+
return_dict=True,
|
| 57 |
+
**kwargs,
|
| 58 |
+
).last_hidden_state
|
| 59 |
+
embedding = self.bottleneck(self.masked_mean(encoded, attention_mask))
|
| 60 |
+
accent_logits = self.accent_classifier(embedding)
|
| 61 |
+
speaker_logits = self.speaker_classifier(embedding)
|
| 62 |
+
if not return_dict:
|
| 63 |
+
return embedding, accent_logits, speaker_logits
|
| 64 |
+
return GenAIDOutput(
|
| 65 |
+
embedding=embedding,
|
| 66 |
+
accent_logits=accent_logits,
|
| 67 |
+
speaker_logits=speaker_logits,
|
| 68 |
+
)
|
preprocessor_config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"do_normalize": true,
|
| 3 |
+
"feature_extractor_type": "Wav2Vec2FeatureExtractor",
|
| 4 |
+
"feature_size": 1,
|
| 5 |
+
"padding_side": "right",
|
| 6 |
+
"padding_value": 0,
|
| 7 |
+
"return_attention_mask": true,
|
| 8 |
+
"sampling_rate": 16000
|
| 9 |
+
}
|