Pytorch 音频分类与对比学习
COLA是应用于音频的预训练框架。对于每个音频样本,作者选出该音频样本的两个片段,一个片段作为anchor A,一个作为positive P,对于该音频样本,选出training batch中的其他的音频样本作为distractor D。有很多distractor让训练的过程变得困难,使得模型学到更有意义的表达。使用同一个batch中的其他样本作为distractor,节约了distractor的
前言
本项目是基于Pytorch的声音分类项目,旨在实现对各种环境声音、动物叫声和语种的识别。项目提供了多种声音分类模型,如EcapaTdnn、PANNS、ResNetSE、CAMPPlus和ERes2Net,以支持不同的应用场景。此外,项目还提供了常用的Urbansound8K数据集测试报告和一些方言数据集的下载和使用例子。用户可以根据自己的需求选择适合的模型和数据集,以实现更准确的声音分类。项目的应用场景广泛,可以用于室外的环境监测、野生动物保护、语音识别等领域。同时,项目也鼓励用户探索更多的使用场景,以推动声音分类技术的发展和应用。
源码地址:AudioClassification-Pytorch
地址: AudioClassification-源码Pytorch
使用准备
- Anaconda 3
- Python 3.8
- Pytorch 1.13.1
- Windows 10 or Ubuntu 18.04
项目特性
- 支持模型:EcapaTdnn、PANNS、TDNN、Res2Net、ResNetSE、CAMPPlus、ERes2Net
- 支持池化层:AttentiveStatsPool(ASP)、SelfAttentivePooling(SAP)、TemporalStatisticsPooling(TSP)、TemporalAveragePooling(TAP)
支持池化山:AttentiveStatsPool(ASP)、SelfAttentivePooling(SAP)、TemporalStatisticsPooling(TSP)、TemporalAveragePooling(TAP) - 支持预处理方法:MelSpectrogram、Spectrogram、MFCC、Fbank
模型论文:
- EcapaTdnn:ECAPA-TDNN: Emphasized Channel Attention, Propagation and Aggregation in TDNN Based Speaker Verification
- PANNS:PANNs: Large-Scale Pretrained Audio Neural Networks for Audio Pattern Recognition
PANNS: PANN:用于音频模式识别的大规模预训练音频神经网络 - TDNN:Prediction of speech intelligibility with DNN-based performance measures
TDNN:使用基于 DNN 的性能测量来预测语音清晰度 - Res2Net:Res2Net: A New Multi-scale Backbone Architecture
- ResNetSE:Squeeze-and-Excitation Networks
- CAMPPlus:CAM++: A Fast and Efficient Network for Speaker Verification Using Context-Aware Masking
- ERes2Net:An Enhanced Res2Net with Local and Global Feature Fusion for Speaker Verification
模型测试表
模型 | Params(M) | 预处理方法 | 数据集 | 类别数量 | 准确率 |
---|---|---|---|---|---|
ResNetSE | 7.8 | Flank | UrbanSound8K | 10 | 0.98863 |
CAMPPlus | 7.1 | Flank | UrbanSound8K | 10 | 0.97727 |
ERes2Net | 6.6 | Flank | UrbanSound8K | 10 | 0.96590 |
PANNS(CNN10) | 5.2 | Flank | UrbanSound8K | 10 | 0.96590 |
Res2Net | 5.0 | Flank | UrbanSound8K | 10 | 0.94318 |
TDNN | 2.6 | Flank | UrbanSound8K | 10 | 0.92045 |
EcapaTdnn | 6.1 | Flank | UrbanSound8K | 10 | 0.91876 |
安装环境
- 首先安装的是Pytorch的GPU版本,如果已经安装过了,请跳过。
`conda install pytorch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1 pytorch-cuda=11.6 -c pytorch -c nvidia`
* 1
- 安装macls库。
使用pip安装,命令如下:
`python -m pip install macls -U -i https://pypi.tuna.tsinghua.edu.cn/simple`
* 1
建议源码安装,源码安装能保证使用最新代码。
`git clone https://github.com/yeyupiaoling/AudioClassification-Pytorch.git
cd AudioClassification-Pytorch/
python setup.py install`
* 1
* 2
* 3
准备数据
生成数据列表,用于下一步的读取需要,audio_path
为音频文件路径,用户需要提前把音频数据集存放在dataset/audio
目录下,每个文件夹存放一个类别的音频数据,每条音频数据长度在3秒以上,如 dataset/audio/鸟叫声/······
。audio
是数据列表存放的位置,生成的数据类别的格式为 音频路径\t音频对应的类别标签
,音频路径和标签用制表符 \t
分开。读者也可以根据自己存放数据的方式修改以下函数。
以Urbansound8K为例,Urbansound8K是目前应用较为广泛的用于自动城市环境声分类研究的公共数据集,包含10个分类:空调声、汽车鸣笛声、儿童玩耍声、狗叫声、钻孔声、引擎空转声、枪声、手提钻、警笛声和街道音乐声。数据集下载地址:UrbanSound8K.tar.gz。以下是针对Urbansound8K生成数据列表的函数。如果读者想使用该数据集,请下载并解压到 dataset
目录下,把生成数据列表代码改为以下代码。
执行create_data.py
即可生成数据列表,里面提供了生成多种数据集列表方式,具体看代码。
`python create_data.py`
* 1
生成的列表是长这样的,前面是音频的路径,后面是该音频对应的标签,从0开始,路径和标签之间用\t
隔开。
`dataset/UrbanSound8K/audio/fold2/104817-4-0-2.wav 4
dataset/UrbanSound8K/audio/fold9/105029-7-2-5.wav 7
dataset/UrbanSound8K/audio/fold3/107228-5-0-0.wav 5
dataset/UrbanSound8K/audio/fold4/109711-3-2-4.wav 3`
* 1
* 2
* 3
* 4
修改预处理方法
配置文件中默认使用的是MelSpectrogram预处理方法,如果要使用其他预处理方法,可以修改配置文件中的安装下面方式修改,具体的值可以根据自己情况修改。如果不清楚如何设置参数,可以直接删除该部分,直接使用默认值。
`preprocess_conf:
feature_method: 'MelSpectrogram'
method_args:
sample_rate: 16000
n_fft: 1024
hop_length: 320
win_length: 1024
f_min: 50.0
f_max: 14000.0
n_mels: 64`
训练
接着就可以开始训练模型了,创建 train.py
。配置文件里面的参数一般不需要修改,但是这几个是需要根据自己实际的数据集进行调整的,首先最重要的就是分类大小dataset_conf.num_class
,这个每个数据集的分类大小可能不一样,根据自己的实际情况设定。然后是dataset_conf.batch_size
,如果是显存不够的话,可以减小这个参数。
`CUDA_VISIBLE_DEVICES=0 python train.py
CUDA_VISIBLE_DEVICES=0,1 torchrun --standalone --nnodes=1 --nproc_per_node=2 train.py`
* 1
* 2
* 3
* 4
训练输出日志:
`[2023-08-07 22:54:22.148973 INFO ] utils:print_arguments:14 - ----------- 额外配置参数 -----------
[2023-08-07 22:54:22.148973 INFO ] utils:print_arguments:16 - configs: configs/ecapa_tdnn.yml
[2023-08-07 22:54:22.148973 INFO ] utils:print_arguments:16 - local_rank: 0
[2023-08-07 22:54:22.148973 INFO ] utils:print_arguments:16 - pretrained_model: None
[2023-08-07 22:54:22.148973 INFO ] utils:print_arguments:16 - resume_model: None
[2023-08-07 22:54:22.148973 INFO ] utils:print_arguments:16 - save_model_path: models/
[2023-08-07 22:54:22.148973 INFO ] utils:print_arguments:16 - use_gpu: True
[2023-08-07 22:54:22.148973 INFO ] utils:print_arguments:17 - ------------------------------------------------
[2023-08-07 22:54:22.202166 INFO ] utils:print_arguments:19 - ----------- 配置文件参数 -----------
[2023-08-07 22:54:22.202166 INFO ] utils:print_arguments:22 - dataset_conf:
[2023-08-07 22:54:22.202166 INFO ] utils:print_arguments:25 - aug_conf:
[2023-08-07 22:54:22.202166 INFO ] utils:print_arguments:27 - noise_aug_prob: 0.2
[2023-08-07 22:54:22.202166 INFO ] utils:print_arguments:27 - noise_dir: dataset/noise
[2023-08-07 22:54:22.202166 INFO ] utils:print_arguments:27 - speed_perturb: True
[2023-08-07 22:54:22.202166 INFO ] utils:print_arguments:27 - volume_aug_prob: 0.2
[2023-08-07 22:54:22.202166 INFO ] utils:print_arguments:27 - volume_perturb: False
[2023-08-07 22:54:22.202166 INFO ] utils:print_arguments:25 - dataLoader:
[2023-08-07 22:54:22.202166 INFO ] utils:print_arguments:27 - batch_size: 64
[2023-08-07 22:54:22.202166 INFO ] utils:print_arguments:27 - num_workers: 4
[2023-08-07 22:54:22.202166 INFO ] utils:print_arguments:29 - do_vad: False
[2023-08-07 22:54:22.202166 INFO ] utils:print_arguments:25 - eval_conf:
[2023-08-07 22:54:22.202166 INFO ] utils:print_arguments:27 - batch_size: 1
[2023-08-07 22:54:22.202166 INFO ] utils:print_arguments:27 - max_duration: 20
[2023-08-07 22:54:22.202166 INFO ] utils:print_arguments:29 - label_list_path: dataset/label_list.txt
[2023-08-07 22:54:22.202166 INFO ] utils:print_arguments:29 - max_duration: 3
[2023-08-07 22:54:22.202166 INFO ] utils:print_arguments:29 - min_duration: 0.5
[2023-08-07 22:54:22.202166 INFO ] utils:print_arguments:29 - sample_rate: 16000
[2023-08-07 22:54:22.202166 INFO ] utils:print_arguments:25 - spec_aug_args:
[2023-08-07 22:54:22.202166 INFO ] utils:print_arguments:27 - freq_mask_width: [0, 8]
[2023-08-07 22:54:22.202166 INFO ] utils:print_arguments:27 - time_mask_width: [0, 10]
[2023-08-07 22:54:22.203167 INFO ] utils:print_arguments:29 - target_dB: -20
[2023-08-07 22:54:22.203167 INFO ] utils:print_arguments:29 - test_list: dataset/test_list.txt
[2023-08-07 22:54:22.203167 INFO ] utils:print_arguments:29 - train_list: dataset/train_list.txt
[2023-08-07 22:54:22.203167 INFO ] utils:print_arguments:29 - use_dB_normalization: True
[2023-08-07 22:54:22.203167 INFO ] utils:print_arguments:29 - use_spec_aug: True
[2023-08-07 22:54:22.203167 INFO ] utils:print_arguments:22 - model_conf:
[2023-08-07 22:54:22.207167 INFO ] utils:print_arguments:29 - num_class: 10
[2023-08-07 22:54:22.207167 INFO ] utils:print_arguments:29 - pooling_type: ASP
[2023-08-07 22:54:22.207167 INFO ] utils:print_arguments:22 - optimizer_conf:
[2023-08-07 22:54:22.207167 INFO ] utils:print_arguments:29 - learning_rate: 0.001
[2023-08-07 22:54:22.207167 INFO ] utils:print_arguments:29 - optimizer: Adam
[2023-08-07 22:54:22.207167 INFO ] utils:print_arguments:29 - scheduler: WarmupCosineSchedulerLR
[2023-08-07 22:54:22.207167 INFO ] utils:print_arguments:25 - scheduler_args:
[2023-08-07 22:54:22.207167 INFO ] utils:print_arguments:27 - max_lr: 0.001
[2023-08-07 22:54:22.207167 INFO ] utils:print_arguments:27 - min_lr: 1e-05
[2023-08-07 22:54:22.207167 INFO ] utils:print_arguments:27 - warmup_epoch: 5
[2023-08-07 22:54:22.207167 INFO ] utils:print_arguments:29 - weight_decay: 1e-06
[2023-08-07 22:54:22.207167 INFO ] utils:print_arguments:22 - preprocess_conf:
[2023-08-07 22:54:22.207167 INFO ] utils:print_arguments:29 - feature_method: Fbank
[2023-08-07 22:54:22.208167 INFO ] utils:print_arguments:25 - method_args:
[2023-08-07 22:54:22.208167 INFO ] utils:print_arguments:27 - num_mel_bins: 80
[2023-08-07 22:54:22.208167 INFO ] utils:print_arguments:27 - sample_frequency: 16000
[2023-08-07 22:54:22.208167 INFO ] utils:print_arguments:22 - train_conf:
[2023-08-07 22:54:22.208167 INFO ] utils:print_arguments:29 - log_interval: 10
[2023-08-07 22:54:22.208167 INFO ] utils:print_arguments:29 - max_epoch: 30
[2023-08-07 22:54:22.208167 INFO ] utils:print_arguments:31 - use_model: EcapaTdnn
[2023-08-07 22:54:22.208167 INFO ] utils:print_arguments:32 - ------------------------------------------------
[2023-08-07 22:54:22.213166 WARNING] trainer:__init__:67 - Windows系统不支持多线程读取数据,已自动关闭!
==========================================================================================
Layer (type:depth-idx) Output Shape Param #
==========================================================================================
EcapaTdnn [1, 10] --
├─Conv1dReluBn: 1-1 [1, 512, 98] --
│ └─Conv1d: 2-1 [1, 512, 98] 204,800
│ └─BatchNorm1d: 2-2 [1, 512, 98] 1,024
├─Sequential: 1-2 [1, 512, 98] --
│ └─Conv1dReluBn: 2-3 [1, 512, 98] --
│ │ └─Conv1d: 3-1 [1, 512, 98] 262,144
│ │ └─BatchNorm1d: 3-2 [1, 512, 98] 1,024
│ └─Res2Conv1dReluBn: 2-4 [1, 512, 98] --
│ │ └─ModuleList: 3-15 -- (recursive)
│ │ └─ModuleList: 3-16 -- (recursive)
│ │ └─ModuleList: 3-15 -- (recursive)
│ │ └─ModuleList: 3-16 -- (recursive)
│ │ └─ModuleList: 3-15 -- (recursive)
│ │ └─ModuleList: 3-16 -- (recursive)
│ │ └─ModuleList: 3-15 -- (recursive)
│ │ └─ModuleList: 3-16 -- (recursive)
│ │ └─ModuleList: 3-15 -- (recursive)
│ │ └─ModuleList: 3-16 -- (recursive)
···································
│ │ └─ModuleList: 3-56 -- (recursive)
│ │ └─ModuleList: 3-55 -- (recursive)
│ │ └─ModuleList: 3-56 -- (recursive)
│ │ └─ModuleList: 3-55 -- (recursive)
│ │ └─ModuleList: 3-56 -- (recursive)
│ └─Conv1dReluBn: 2-13 [1, 512, 98] --
│ │ └─Conv1d: 3-57 [1, 512, 98] 262,144
│ │ └─BatchNorm1d: 3-58 [1, 512, 98] 1,024
│ └─SE_Connect: 2-14 [1, 512, 98] --
│ │ └─Linear: 3-59 [1, 256] 131,328
│ │ └─Linear: 3-60 [1, 512] 131,584
├─Conv1d: 1-5 [1, 1536, 98] 2,360,832
├─AttentiveStatsPool: 1-6 [1, 3072] --
│ └─Conv1d: 2-15 [1, 128, 98] 196,736
│ └─Conv1d: 2-16 [1, 1536, 98] 198,144
├─BatchNorm1d: 1-7 [1, 3072] 6,144
├─Linear: 1-8 [1, 192] 590,016
├─BatchNorm1d: 1-9 [1, 192] 384
├─Linear: 1-10 [1, 10] 1,930
==========================================================================================
Total params: 6,188,490
Trainable params: 6,188,490
Non-trainable params: 0
Total mult-adds (M): 470.96
==========================================================================================
Input size (MB): 0.03
Forward/backward pass size (MB): 10.28
Params size (MB): 24.75
Estimated Total Size (MB): 35.07
==========================================================================================
[2023-08-07 22:54:26.726095 INFO ] trainer:train:344 - 训练数据:8644
[2023-08-07 22:54:30.092504 INFO ] trainer:__train_epoch:296 - Train epoch: [1/30], batch: [0/4], loss: 2.57033, accuracy: 0.06250, learning rate: 0.00001000, speed: 19.02 data/sec, eta: 0:06:43`

评估
每轮训练结束可以执行评估,评估会出来输出准确率,还保存了混合矩阵图片,保存路径output/images/
,如下。
预测
在训练结束之后,我们得到了一个模型参数文件,我们使用这个模型预测音频。
`python infer.py --audio_path=dataset/UrbanSound8K/audio/fold5/156634-5-2-5.wav`
* 1
其他功能
- 为了方便读取录制数据和制作数据集,这里提供了录音程序
record_audio.py
,这个用于录制音频,录制的音频采样率为16000,单通道,16bit。
`python record_audio.py`
* 1
infer_record.py
这个程序是用来不断进行录音识别,我们可以大致理解为这个程序在实时录音识别。通过这个应该我们可以做一些比较有趣的事情,比如把麦克风放在小鸟经常来的地方,通过实时录音识别,一旦识别到有鸟叫的声音,如果你的数据集足够强大,有每种鸟叫的声音数据集,这样你还能准确识别是那种鸟叫。如果识别到目标鸟类,就启动程序,例如拍照等等。
`python infer_record.py --record_seconds=3`
* 1
2. 音频中的对比学习
Contrastive Learning在CV领域风生水起,涌现了一批非常优秀的成果,例如:针对ImageNet有Google的SIMCLR, Facebook的MoCo, 和强化学习的CURL。在音频领域却少有研究。
最近Google发了一篇文章,讲述了通过对比学习到音频的通用表达。
Contrastive Learning of General-Purpose Audio Representations
通用音频表示的对比学习
1. 对比学习 Contrastive Learning
1.对比学习对比学习
首先介绍一下对比学习。
Contrastive self-supervised learning techniques are a promising class of methods that build representations by learning to encode what makes two things similar or different.
对比自监督学习技术是一类很有前途的方法,它通过学习编码使两个事物相似或不同的因素来构建表示。
对比学习的核心思想是学习这个事物与其他事物之间的差异,而非这个事物本身。 就像我们小时候在学习认识狗和猫,我们已经认识了的标准是:能够分辨他们的不同点,而不是它们“有眼睛,有鼻子,有嘴,有毛”这些相同点。
所以,表示学习的重点不是学习到所有的细节特征,而是学习到能够区别自身和其他样本的区别就好。
2. COLA 介绍
COLA是应用于音频的预训练框架。
对于每个音频样本,作者选出该音频样本的两个片段,一个片段作为anchor A,一个作为positive P,对于该音频样本,选出training batch中的其他的音频样本作为distractor D。
这样的方法有两个优点:
- 有很多distractor让训练的过程变得困难,使得模型学到更有意义的表达。
- 使用同一个batch中的其他样本作为distractor,节约了distractor的生成、计算和存储成本。
1) similarity measurement
COLA使用的是Bi-linear similarity,作者证明了它比cosine相似性度量提升了7%的准确率。
2) loss function: cross entropy
2)损失函数:交叉熵
更多实验细节见论文:Contrastive Learning of General-Purpose Audio Representations
更多实验细节见论文:Contrastive Learning of General-Purpose Audio Representations
3. 模型评估
1) 线性模型评估
COLA的encoder部分是用EfficientNet-B0在AudioSet(around 1M audio clips)训练得到的。然后把得到的特征向量输入一个线性分类器去做目标任务。
我们认为线性分类器得到的监督学习的结果越好,模型学到的表达越好。
文章中作者写道COLA比其他方法(如triplet loss)在正确率上能够提高20%。
2) 精细调整评估(fine-tuning evaluation)
另一个评估模型好坏的维度是调整模型以适应各种各样的任务(downstream task)。
作者介绍COLA预训练模型要比其他从头训练的模型正确率提高1.2%。
参考资料
https://blog.csdn.net/qq_33200967/article/details/119830756
https://blog.csdn.net/veritasalice/article/details/110043477
- https://github.com/PaddlePaddle/PaddleSpeech
- https://github.com/yeyupiaoling/PaddlePaddle-MobileFaceNets
- https://github.com/yeyupiaoling/PPASR
- https://github.com/alibaba-damo-academy/3D-Speaker

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)