当前位置: 代码网 > it编程>编程语言>Java > 【人工智能】Transformers之Pipeline(一):音频分类(audio-classification)

【人工智能】Transformers之Pipeline(一):音频分类(audio-classification)

2024年07月28日 Java 我要评论
本文对transformers之pipeline的音频分类(audio-classification)从概述、技术原理、pipeline参数、pipeline实战、模型排名等方面进行介绍,读者可以基于pipeline使用文中的代码极简的进行音频分类推理,应用于音频情感识别、音乐曲风判断等业务场景。

​​​​​​​

目录

一、引言 

二、音频分类(audio-classification)

2.1 概述

2.2 技术原理

2.2.1 wav2vec 2.0模型

 2.2.1 hubert模型

2.3 pipeline参数

2.3.1 pipeline对象实例化参数

2.3.2 pipeline对象使用参数 

2.4 pipeline实战

2.4.1 指令识别(默认模型)

 2.4.2 情感识别

2.5 模型排名

三、总结


一、引言 

 pipeline(管道)是huggingface transformers库中一种极简方式使用大模型推理的抽象,将所有大模型分为音频(audio)、计算机视觉(computer vision)、自然语言处理(nlp)、多模态(multimodal)等4大类,28小类任务(tasks)。共计覆盖32万个模型

今天介绍audio音频的第一篇,音频分类(audio-classification),在huggingface库内共有2500个音频分类模型。

二、音频分类(audio-classification)

2.1 概述

音频分类,顾名思义就是将音频打标签或分配类别的任务。主要应用场景有语音情绪分类语音命令分类说话人分类音乐风格判别语言判别等。

2.2 技术原理

音频分类,主要思想就是将音频的音谱切分成25ms-60ms的片段,通过cnn等卷积神经网络模型提取特征并进行embedding化,基于transformer与文本类别对齐训练。下面介绍2个代表模型:

2.2.1 wav2vec 2.0模型

wav2vec 2.0是 meta在2020年发表的无监督语音预训练模型。它的核心思想是通过向量量化(vector quantization,vq)构造自建监督训练目标,对输入做大量掩码后利用对比学习损失函数进行训练。模型结构如图,基于卷积网络(convoluational neural network,cnn)的特征提取器将原始音频编码为帧特征序列,通过 vq 模块把每帧特征转变为离散特征 q,并作为自监督目标。同时,帧特征序列做掩码操作后进入 transformer [5] 模型得到上下文表示 c。最后通过对比学习损失函数,拉近掩码位置的上下文表示与对应的离散特征 q 的距离,即正样本对。

 2.2.1 hubert模型

hubert是meta在2021年发表的模型,模型结构类似 wav2vec 2.0,不同的是训练方法。wav2vec 2.0 是在训练时将语音特征离散化作为自监督目标,而 hubert 则通过在 mfcc 特征或 hubert 特征上做 k-means 聚类,得到训练目标。hubert 模型采用迭代训练的方式,base 模型第一次迭代在 mfcc 特征上做聚类,第二次迭代在第一次迭代得到的 hubert 模型的中间层特征上做聚类,large 和 xlarge 模型则用 base 模型的第二次迭代模型提取特征做聚类。从原始论文实验结果来看,hubert 模型效果要优于 wav2vec 2.0,特别是下游任务有监督训练数据极少的情况,如 1 小时、10 分钟。

2.3 pipeline参数

2.3.1 pipeline对象实例化参数

2.3.2 pipeline对象使用参数 

2.4 pipeline实战

2.4.1 指令识别(默认模型)

pipeline对于audio-classification的默认模型时superb/wav2vec2-base-superb-ks,使用pipeline时,如果仅设置task=audio-classification,不设置模型,则下载并使用默认模型。

import os
os.environ["hf_endpoint"] = "https://hf-mirror.com"
os.environ["cuda_visible_devices"] = "2"

from transformers import pipeline

speech_file = "./output_video_enhanced.mp3"
pipe = pipeline(task="audio-classification")
result = pipe(speech_file)
print(result)

 这是一个上下左右yes及no的指令识别模型,感觉像是训练动物。

[{'score': 0.9988580942153931, 'label': '_unknown_'}, {'score': 0.000909291033167392, 'label': 'down'}, {'score': 9.889943612506613e-05, 'label': 'no'}, {'score': 7.015655864961445e-05, 'label': 'yes'}, {'score': 5.134344974067062e-05, 'label': 'stop'}]

 2.4.2 情感识别

我们指定模型为情感识别模型ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition,具体代码为:

import os
os.environ["hf_endpoint"] = "https://hf-mirror.com"
os.environ["cuda_visible_devices"] = "2"

from transformers import pipeline

speech_file = "./output_video_enhanced.mp3"
pipe = pipeline(task="audio-classification",model="ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition")
result = pipe(speech_file)
print(result)

输入为一段mp3格式的语音,输出为

[{'score': 0.13128453493118286, 'label': 'angry'}, {'score': 0.12990005314350128, 'label': 'calm'}, {'score': 0.1262471228837967, 'label': 'happy'}, {'score': 0.12568499147891998, 'label': 'surprised'}, {'score': 0.12327362596988678, 'label': 'disgust'}]

2.5 模型排名

在huggingface上,我们筛选音频分类模型,并按下载量从高到低排序:

三、总结

本文对transformers之pipeline的音频分类(audio-classification)从概述、技术原理、pipeline参数、pipeline实战、模型排名等方面进行介绍,读者可以基于pipeline使用文中的代码极简的进行音频分类推理,应用于音频情感识别、音乐曲风判断等业务场景。

期待您的3连+关注,如何还有时间,欢迎阅读我的其他文章:

《transformers-pipeline概述》

【人工智能】transformers之pipeline(概述):30w+大模型极简应用

《transformers-pipeline 第一章:音频(audio)篇》

【人工智能】transformers之pipeline(一):音频分类(audio-classification)​​​​​​​

【人工智能】transformers之pipeline(二):自动语音识别(automatic-speech-recognition)

【人工智能】transformers之pipeline(三):文本转音频(text-to-audio)

【人工智能】transformers之pipeline(四):零样本音频分类(zero-shot-audio-classification)

《transformers-pipeline 第二章:计算机视觉(cv)篇》

【人工智能】transformers之pipeline(五):深度估计(depth-estimation)

【人工智能】transformers之pipeline(六):图像分类(image-classification)

【人工智能】transformers之pipeline(七):图像分割(image-segmentation)

【人工智能】transformers之pipeline(八):图生图(image-to-image)

【人工智能】transformers之pipeline(九):物体检测(object-detection)

【人工智能】transformers之pipeline(十):视频分类(video-classification)

【人工智能】transformers之pipeline(十一):零样本图片分类(zero-shot-image-classification)

【人工智能】transformers之pipeline(十二):零样本物体检测(zero-shot-object-detection)

《transformers-pipeline 第三章:自然语言处理(nlp)篇》

【人工智能】transformers之pipeline(十三):填充蒙版(fill-mask)

【人工智能】transformers之pipeline(十四):问答(question-answering)

【人工智能】transformers之pipeline(十五):总结(summarization)

【人工智能】transformers之pipeline(十六):表格问答(table-question-answering)

【人工智能】transformers之pipeline(十七):文本分类(text-classification)

【人工智能】transformers之pipeline(十八):文本生成(text-generation)

【人工智能】transformers之pipeline(十九):文生文(text2text-generation)

【人工智能】transformers之pipeline(二十):令牌分类(token-classification)

【人工智能】transformers之pipeline(二十一):翻译(translation)

【人工智能】transformers之pipeline(二十二):零样本文本分类(zero-shot-classification)

《transformers-pipeline 第四章:多模态(multimodal)篇》

【人工智能】transformers之pipeline(二十三):文档问答(document-question-answering)

【人工智能】transformers之pipeline(二十四):特征抽取(feature-extraction)

【人工智能】transformers之pipeline(二十五):图片特征抽取(image-feature-extraction)

【人工智能】transformers之pipeline(二十六):图片转文本(image-to-text)

【人工智能】transformers之pipeline(二十七):掩码生成(mask-generation)

【人工智能】transformers之pipeline(二十八):视觉问答(visual-question-answering)

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com