🚀 基于Wav2vec 2.0(24层)的年龄和性别识别模型
本模型旨在解决音频数据中年龄和性别识别的问题,通过输入原始音频信号,输出年龄和性别的预测结果,为音频分析领域提供了有效的解决方案。
🚀 快速开始
此模型以原始音频信号作为输入,输出年龄预测值(范围约为 0...1,对应 0...100 岁)以及性别预测结果,该结果以概率形式表示为儿童、女性或男性。此外,模型还会提供最后一个Transformer层的池化状态。
该模型是通过在 aGender、Mozilla Common Voice、Timit 和 Voxceleb 2 数据集上对 Wav2Vec2-Large-Robust 进行微调而创建的。在这个版本的模型中,我们对所有 24 个Transformer层进行了训练。
模型的 ONNX 导出文件可从 doi:10.5281/zenodo.7761387 获取。更多详细信息请参考相关 论文 和 教程。
✨ 主要特性
- 多数据集训练:使用多个知名音频数据集进行训练,提高了模型的泛化能力。
- 输出丰富:不仅输出年龄和性别预测,还提供最后一个Transformer层的池化状态。
- 可导出ONNX:支持将模型导出为ONNX格式,方便在不同平台上部署。
📦 安装指南
文档未提供具体安装步骤,暂不展示。
💻 使用示例
基础用法
import numpy as np
import torch
import torch.nn as nn
from transformers import Wav2Vec2Processor
from transformers.models.wav2vec2.modeling_wav2vec2 import (
Wav2Vec2Model,
Wav2Vec2PreTrainedModel,
)
class ModelHead(nn.Module):
r"""Classification head."""
def __init__(self, config, num_labels):
super().__init__()
self.dense = nn.Linear(config.hidden_size, config.hidden_size)
self.dropout = nn.Dropout(config.final_dropout)
self.out_proj = nn.Linear(config.hidden_size, num_labels)
def forward(self, features, **kwargs):
x = features
x = self.dropout(x)
x = self.dense(x)
x = torch.tanh(x)
x = self.dropout(x)
x = self.out_proj(x)
return x
class AgeGenderModel(Wav2Vec2PreTrainedModel):
r"""Speech emotion classifier."""
def __init__(self, config):
super().__init__(config)
self.config = config
self.wav2vec2 = Wav2Vec2Model(config)
self.age = ModelHead(config, 1)
self.gender = ModelHead(config, 3)
self.init_weights()
def forward(
self,
input_values,
):
outputs = self.wav2vec2(input_values)
hidden_states = outputs[0]
hidden_states = torch.mean(hidden_states, dim=1)
logits_age = self.age(hidden_states)
logits_gender = torch.softmax(self.gender(hidden_states), dim=1)
return hidden_states, logits_age, logits_gender
device = 'cpu'
model_name = 'audeering/wav2vec2-large-robust-24-ft-age-gender'
processor = Wav2Vec2Processor.from_pretrained(model_name)
model = AgeGenderModel.from_pretrained(model_name)
sampling_rate = 16000
signal = np.zeros((1, sampling_rate), dtype=np.float32)
def process_func(
x: np.ndarray,
sampling_rate: int,
embeddings: bool = False,
) -> np.ndarray:
r"""Predict age and gender or extract embeddings from raw audio signal."""
y = processor(x, sampling_rate=sampling_rate)
y = y['input_values'][0]
y = y.reshape(1, -1)
y = torch.from_numpy(y).to(device)
with torch.no_grad():
y = model(y)
if embeddings:
y = y[0]
else:
y = torch.hstack([y[1], y[2]])
y = y.detach().cpu().numpy()
return y
print(process_func(signal, sampling_rate))
print(process_func(signal, sampling_rate, embeddings=True))
高级用法
文档未提供高级用法示例,暂不展示。
📚 详细文档
文档未提供更多详细说明,暂不展示。
🔧 技术细节
文档未提供具体技术实现细节,暂不展示。
📄 许可证
本模型使用的许可证为 cc-by-nc-sa-4.0
。