当前位置: 首页 > news >正文

语音增强的损失函数选择

一、最优尺度不变信噪比(OSISNR)损失函数

  参考:论文解读 --Optimal scale-invariant signal-to-noise ratio and curriculum learning for monaural multi-spea
  最优尺度不变信噪比(OSI-SNR)是一种用于评估信号质量的指标,特别是在语音分离和增强任务中。OSI-SNR 通过优化估计信号与目标信号之间的相似性,提供了一种更稳定和可靠的信号质量度量。OSI-SNR 的计算步骤如下: s s s表示原始语音信号, s ^ \hat{s} s^表示重建的语音信号。
⟨ s , s ^ ⟩ = ∑ t = 1 T s [ t ] ⋅ s ^ [ t ] \langle s, \hat{s} \rangle = \sum_{t=1}^{T} s[t] \cdot \hat{s}[t] s,s^=t=1Ts[t]s^[t]
∣ s ^ ∣ 2 = ∑ t = 1 T ∣ s ^ [ t ] ∣ 2 |s^{\hat{}}|^2 = \sum_{t=1}^{T} |\hat{s}[t]|^2 s^2=t=1Ts^[t]2
λ = ∣ s ^ ∣ 2 ⟨ s , s ^ ⟩ \lambda = \frac{|s^{\hat{}}|^2}{\langle s, \hat{s} \rangle} λ=s,s^s^2
s target = λ ⋅ s ^ s_{\text{target}} = \lambda \cdot \hat{s} starget=λs^
e noise = s ^ − s target e_{\text{noise}} = \hat{s} - s_{\text{target}} enoise=s^starget
OSI-SNR = 10 log ⁡ 10 ( ∥ s target ∥ 2 ∥ e noise ∥ 2 ) \text{OSI-SNR} = 10 \log_{10} \left( \frac{\| s_{\text{target}} \|^2}{\| e_{\text{noise}} \|^2} \right) OSI-SNR=10log10(enoise2starget2)
  将 最优尺度不变信噪比(OSI-SNR) 的倒数作为损失函数是一个合理的想法,尤其是在某些情况下,可能会更好地反映模型的性能。OSI-SNR 是一个衡量信号质量的指标,值越高表示信号质量越好。在训练过程中,我们通常希望最小化损失函数,因此可以考虑将 OSI-SNR 的倒数作为损失函数。参考:基于深层声学特征的端到端语音分离

方法 1:每一帧的 OSI-SNR 取倒数计算损失,再取均值

  1. 计算每一帧的 OSI-SNR
    OSI-SNR i = 10 log ⁡ 10 ( ∥ s target , i ∥ 2 ∥ e noise , i ∥ 2 ) \text{OSI-SNR}_i = 10 \log_{10} \left( \frac{\| s_{\text{target}, i} \|^2}{\| e_{\text{noise}, i} \|^2} \right) OSI-SNRi=10log10(enoise,i2starget,i2)
  2. 计算损失
    Loss i = 1 OSI-SNR i + ϵ = 1 10 log ⁡ 10 ( ∥ s target , i ∥ 2 ∥ e noise , i ∥ 2 ) + ϵ \text{Loss}_i = \frac{1}{\text{OSI-SNR}_i + \epsilon} = \frac{1}{10 \log_{10} \left( \frac{\| s_{\text{target}, i} \|^2}{\| e_{\text{noise}, i} \|^2} \right) + \epsilon} Lossi=OSI-SNRi+ϵ1=10log10(enoise,i2starget,i2)+ϵ1
  3. 取均值
    Final Loss = 1 N ∑ i = 1 N Loss i = 1 N ∑ i = 1 N 1 10 log ⁡ 10 ( ∥ s target , i ∥ 2 ∥ e noise , i ∥ 2 ) + ϵ \text{Final Loss} = \frac{1}{N} \sum_{i=1}^{N} \text{Loss}_i = \frac{1}{N} \sum_{i=1}^{N} \frac{1}{10 \log_{10} \left( \frac{\| s_{\text{target}, i} \|^2}{\| e_{\text{noise}, i} \|^2} \right) + \epsilon} Final Loss=N1i=1NLossi=N1i=1N10log10(enoise,i2starget,i2)+ϵ1

方法 2:每一帧的 OSI-SNR 取均值,再取倒数计算损失

  1. 计算每一帧的 OSI-SNR
    OSI-SNR i = 10 log ⁡ 10 ( ∥ s target , i ∥ 2 ∥ e noise , i ∥ 2 ) \text{OSI-SNR}_i = 10 \log_{10} \left( \frac{\| s_{\text{target}, i} \|^2}{\| e_{\text{noise}, i} \|^2} \right) OSI-SNRi=10log10(enoise,i2starget,i2)
  2. 取均值
    Mean OSI-SNR = 1 N ∑ i = 1 N OSI-SNR i = 1 N ∑ i = 1 N 10 log ⁡ 10 ( ∥ s target , i ∥ 2 ∥ e noise , i ∥ 2 ) \text{Mean OSI-SNR} = \frac{1}{N} \sum_{i=1}^{N} \text{OSI-SNR}_i = \frac{1}{N} \sum_{i=1}^{N} 10 \log_{10} \left( \frac{\| s_{\text{target}, i} \|^2}{\| e_{\text{noise}, i} \|^2} \right) Mean OSI-SNR=N1i=1NOSI-SNRi=N1i=1N10log10(enoise,i2starget,i2)
  3. 计算损失
    Final Loss = 1 Mean OSI-SNR + ϵ = 1 1 N ∑ i = 1 N 10 log ⁡ 10 ( ∥ s target , i ∥ 2 ∥ e noise , i ∥ 2 ) + ϵ \text{Final Loss} = \frac{1}{\text{Mean OSI-SNR} + \epsilon} = \frac{1}{\frac{1}{N} \sum_{i=1}^{N} 10 \log_{10} \left( \frac{\| s_{\text{target}, i} \|^2}{\| e_{\text{noise}, i} \|^2} \right) + \epsilon} Final Loss=Mean OSI-SNR+ϵ1=N1i=1N10log10(enoise,i2starget,i2)+ϵ1
  • 方法 1 的最终损失函数是对每一帧的 OSI-SNR 值取倒数后再取均值,强调了每一帧的信号质量。
  • 方法 2 的最终损失函数是先计算所有帧的 OSI-SNR 的均值,然后取倒数,提供了一个整体的信号质量评估。
import numpy as np
def calculate_osi_snr_frame(target, estimated):# 获取帧数和频点数num_bins, num_frames = target.shapeosi_snr_frames = np.zeros(num_frames)for frame in range(num_frames):dot_product = np.sum(target[:, frame] * estimated[:, frame])estimated_energy = np.sum(np.abs(estimated[:, frame]) ** 2)lambda_opt = estimated_energy / (dot_product + 1e-10)  # 防止除以零target_adjusted = lambda_opt * target[:, frame]noise = estimated[:, frame] - target_adjustedtarget_energy = np.sum(np.abs(target_adjusted) ** 2)noise_energy = np.sum(np.abs(noise) ** 2)osi_snr_frames[frame] = 10 * np.log10(target_energy / (noise_energy + 1e-8))return osi_snr_frames
def loss_method_1(osi_snr_frames, epsilon=1e-8):# 每一帧的 OSI-SNR 取倒数计算损失,再取均值losses = 1 / (osi_snr_frames + epsilon)final_loss = np.mean(losses)return final_loss
def loss_method_2(osi_snr_frames, epsilon=1e-8):# 每一帧的 OSI-SNR 取均值,再取倒数计算损失mean_osi_snr = np.mean(osi_snr_frames)final_loss = 1 / (mean_osi_snr + epsilon)return final_loss
# 示例掩蔽矩阵 M1 和 M2
M1 = np.array([[0.5, 0.6, 0.7],[0.8, 0.9, 1.0],[1.1, 1.2, 1.3]])M2 = np.array([[0.4, 0.5, 0.6],[0.7, 0.8, 0.9],[1.0, 1.1, 1.2]])
# 计算每一帧的 OSI-SNR
osi_snr_frames = calculate_osi_snr_frame(M1, M2)
# 计算损失
loss1 = loss_method_1(osi_snr_frames)
loss2 = loss_method_2(osi_snr_frames)
print(f"Loss Method 1 (Frame-wise OSI-SNR): {loss1:.4f}")
print(f"Loss Method 2 (Mean OSI-SNR): {loss2:.4f}")
Loss Method 1 (Frame-wise OSI-SNR): 0.03342
Loss Method 2 (Mean OSI-SNR): 0.03333

二、幅度幂律压缩均方误差(MC-MSE)损失函数

  在音频信号处理和深度学习领域,幅度幂律压缩均方误差(Magnitude Compression Mean Squared Error, MC-MSE)是一种重要的损失函数,特别适用于语音增强和音频分离任务。MC-MSE通过引入幅度压缩的概念,能够更有效地处理具有大动态范围的音频信号,从而提高模型的性能。

2.1 、原理

  在传统的均方误差(MSE)损失函数中,模型对大幅度信号的敏感性可能导致对小幅度信号的学习不足。这种情况在音频信号处理中尤为明显,因为音频信号的幅度通常具有很大的动态范围。为了解决这个问题,MC-MSE引入了幅度压缩的机制,通过对信号幅度进行非线性变换,使得模型在训练过程中能够更好地关注小幅度信号的特征。

MC-MSE的核心思想是通过幂律压缩函数对信号幅度进行处理,从而使得损失函数在计算时能够更均衡地反映不同幅度信号的影响。具体来说,MC-MSE通过对目标信号和预测信号的幅度进行压缩,来计算它们之间的均方误差。

2.2、公式

MC-MSE Loss的计算公式可以表示为:
L MC-MSE = 1 N ∑ i = 1 N ( compress ( y i ) − compress ( y ^ i ) ) 2 L_{\text{MC-MSE}}= \frac{1}{N} \sum_{i=1}^{N} \left( \text{compress}(y_i) - \text{compress}(\hat{y}_i) \right)^2 LMC-MSE=N1i=1N(compress(yi)compress(y^i))2

其中:

  • y i y_i yi 是目标信号的幅度。
  • y ^ i \hat{y}_i y^i 是模型预测的幅度。
  • compress ( ⋅ ) \text{compress}(\cdot) compress() 是幅度压缩函数,通常采用幂律压缩形式。
    幅度压缩函数的形式为:
    compress ( x ) = sign ( x ) ⋅ ∣ x ∣ α \text{compress}(x) = \text{sign}(x) \cdot |x|^\alpha compress(x)=sign(x)xα

  在这里, α \alpha α是压缩因子,通常取值在 0 < α < 1 0 < \alpha < 1 0<α<1 之间。较小的 α \alpha α值会导致更强的压缩效果,从而使得模型在训练时能够更好地学习小幅度信号的特征。通过这种方式,MC-MSE损失函数能够有效地提高模型在音频处理任务中的性能,尤其是在复杂的噪声环境中。它不仅改善了模型对小幅度信号的学习能力,还增强了模型的鲁棒性,使其在实际应用中表现得更加出色。
  通过一个具体的例子来说明如何计算两个掩蔽之间的幅度幂律压缩均方误差(MC-MSE)。假设我们有两个掩蔽 M 1 M_1 M1 M 2 M_2 M2,它们的维度都是 3 × 3 3 \times 3 3×3(即3个时间帧和3个频率bin),并且我们选择压缩指数 p = 0.3 p = 0.3 p=0.3
  首先,我们对两个掩蔽应用幂律压缩。假设 M 1 M_1 M1 M 2 M_2 M2的值如下:
M 1 = [ 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 ] M_1 = \begin{bmatrix} 0.5 & 0.6 & 0.7 \\ 0.8 & 0.9 & 1.0 \\ 1.1 & 1.2 & 1.3 \end{bmatrix} M1= 0.50.81.10.60.91.20.71.01.3
M 2 = [ 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 ] M_2 = \begin{bmatrix} 0.4 & 0.5 & 0.6 \\ 0.7 & 0.8 & 0.9 \\ 1.0 & 1.1 & 1.2 \end{bmatrix} M2= 0.40.71.00.50.81.10.60.91.2

应用幂律压缩 p = 0.3 p = 0.3 p=0.3后,我们得到:
M 1 p = [ 0. 5 0.3 0. 6 0.3 0. 7 0.3 0. 8 0.3 0. 9 0.3 1. 0 0.3 1. 1 0.3 1. 2 0.3 1. 3 0.3 ] M_1^p = \begin{bmatrix} 0.5^{0.3} & 0.6^{0.3} & 0.7^{0.3} \\ 0.8^{0.3} & 0.9^{0.3} & 1.0^{0.3} \\ 1.1^{0.3} & 1.2^{0.3} & 1.3^{0.3} \end{bmatrix} M1p= 0.50.30.80.31.10.30.60.30.90.31.20.30.70.31.00.31.30.3
M 2 p = [ 0. 4 0.3 0. 5 0.3 0. 6 0.3 0. 7 0.3 0. 8 0.3 0. 9 0.3 1. 0 0.3 1. 1 0.3 1. 2 0.3 ] M_2^p = \begin{bmatrix} 0.4^{0.3} & 0.5^{0.3} & 0.6^{0.3} \\ 0.7^{0.3} & 0.8^{0.3} & 0.9^{0.3} \\ 1.0^{0.3} & 1.1^{0.3} & 1.2^{0.3} \end{bmatrix} M2p= 0.40.30.70.31.00.30.50.30.80.31.10.30.60.30.90.31.20.3
  接下来,我们计算两个压缩后的掩蔽之间的均方误差。具体地,对于每个元素,我们计算差的平方,然后对所有元素求平均。
L MC-MSE = 1 9 ( ( 0. 5 0.3 − 0. 4 0.3 ) 2 + ( 0. 6 0.3 − 0. 5 0.3 ) 2 + … + ( 1. 3 0.3 − 1. 2 0.3 ) 2 ) = 0.00135 L_{\text{MC-MSE}} = \frac{1}{9} \left( (0.5^{0.3} - 0.4^{0.3})^2 \\+ (0.6^{0.3} - 0.5^{0.3})^2 \\+ \ldots + (1.3^{0.3} - 1.2^{0.3})^2 \right) = 0.00135 LMC-MSE=91((0.50.30.40.3)2+(0.60.30.50.3)2++(1.30.31.20.3)2)=0.00135

import numpy as npdef calculate_mc_mse(M1, M2, p=0.3):compressed_M1 = M1 ** pcompressed_M2 = M2 ** pmse = np.mean((compressed_M1 - compressed_M2) ** 2)return mseM1 = np.array([[0.5, 0.6, 0.7], [0.8, 0.9, 1.0], [1.1, 1.2, 1.3]])
M2 = np.array([[0.4, 0.5, 0.6], [0.7, 0.8, 0.9], [1.0, 1.1, 1.2]])mc_mse = calculate_mc_mse(M1, M2, p=0.3)
print("MC-MSE:", mc_mse)
MC-MSE: 0.00135

三、融合损失函数

参考:AEC论文解读 – A Deep Hierarchical Fusion Network for Fullband Acoustic Echo Cancellation
  结合 最优尺度不变信噪比(OSI-SNR)幅度幂律压缩均方误差(MC-MSE) 的损失函数,可以通过超参数 γ \gamma γ 加权来形成最终的损失函数。根据您提供的公式,最终的损失计算公式可以表示为:
L = L OSI-SNR + γ L MC-MSE L = L_{\text{OSI-SNR}} + \gamma L_{\text{MC-MSE}} L=LOSI-SNR+γLMC-MSE

  1. OSI-SNR 损失【方法 1】
    L OSI-SNR = 1 N ∑ i = 1 N Loss i = 1 N ∑ i = 1 N 1 10 log ⁡ 10 ( ∥ s target , i ∥ 2 ∥ e noise , i ∥ 2 ) + ϵ L_{\text{OSI-SNR}}= \frac{1}{N} \sum_{i=1}^{N} \text{Loss}_i = \frac{1}{N} \sum_{i=1}^{N} \frac{1}{10 \log_{10} \left( \frac{\| s_{\text{target}, i} \|^2}{\| e_{\text{noise}, i} \|^2} \right) + \epsilon} LOSI-SNR=N1i=1NLossi=N1i=1N10log10(enoise,i2starget,i2)+ϵ1

  2. MC-MSE 损失
    L MC-MSE = 1 N ∑ i = 1 N ( compress ( y i ) − compress ( y ^ i ) ) 2 L_{\text{MC-MSE}}= \frac{1}{N} \sum_{i=1}^{N} \left( \text{compress}(y_i) - \text{compress}(\hat{y}_i) \right)^2 LMC-MSE=N1i=1N(compress(yi)compress(y^i))2

  3. 最终损失公式
    L = L OSI-SNR + γ ⋅ L MC-MSE = 1 N ∑ i = 1 N Loss i = 1 N ∑ i = 1 N 1 10 log ⁡ 10 ( ∥ s target , i ∥ 2 ∥ e noise , i ∥ 2 ) + ϵ + γ ⋅ 1 N ∑ i = 1 N ( compress ( y i ) − compress ( y ^ i ) ) 2 L = L_{\text{OSI-SNR}} + \gamma \cdot L_{\text{MC-MSE}}= \frac{1}{N} \sum_{i=1}^{N} \text{Loss}_i \\= \frac{1}{N} \sum_{i=1}^{N} \frac{1}{10 \log_{10} \left( \frac{\| s_{\text{target}, i} \|^2}{\| e_{\text{noise}, i} \|^2} \right) + \epsilon} +\gamma \cdot\frac{1}{N} \sum_{i=1}^{N} \left( \text{compress}(y_i) - \text{compress}(\hat{y}_i) \right)^2 L=LOSI-SNR+γLMC-MSE=N1i=1NLossi=N1i=1N10log10(enoise,i2starget,i2)+ϵ1+γN1i=1N(compress(yi)compress(y^i))2

  • γ \gamma γ:需要调试的经验值。
  • ϵ \epsilon ϵ:一个小常数,用于防止除以零的情况。
  • 这个损失函数通过加权结合了 OSI-SNR 和 MC-MSE,能够同时考虑信号的重建质量和相似性。
  • 通过调整 γ \gamma γ 的值,可以控制两种损失在最终损失中的相对重要性,从而优化模型的性能。
import numpy as np
def calculate_osi_snr_frame(target, estimated):num_bins, num_frames = target.shapeosi_snr_frames = np.zeros(num_frames)for frame in range(num_frames):dot_product = np.sum(target[:, frame] * estimated[:, frame])estimated_energy = np.sum(np.abs(estimated[:, frame]) ** 2)lambda_opt = estimated_energy / (dot_product + 1e-10)  # 防止除以零target_adjusted = lambda_opt * target[:, frame]noise = estimated[:, frame] - target_adjustedtarget_energy = np.sum(np.abs(target_adjusted) ** 2)noise_energy = np.sum(np.abs(noise) ** 2)osi_snr_frames[frame] = 10 * np.log10(target_energy / (noise_energy + 1e-8))return osi_snr_frames
def mc_mse_loss(target, estimated, gamma=0.3):compressed_target = np.power(np.abs(target), gamma)compressed_estimated = np.power(np.abs(estimated), gamma)loss = np.mean((compressed_estimated - compressed_target) ** 2)return loss
def combined_loss(target, estimated, gamma=15):# 计算每一帧的 OSI-SNRosi_snr_frames = calculate_osi_snr_frame(target, estimated)# 计算 OSI-SNR 损失osi_snr_loss = np.mean(1 / (osi_snr_frames + 1e-10))print('osi_snr_loss:', osi_snr_loss)# 计算 MC-MSE 损失mc_mse_value = mc_mse_loss(target, estimated)print('mc_mse_value:', mc_mse_value)# 计算最终损失final_loss = osi_snr_loss + gamma * mc_mse_valuereturn final_loss
# 示例掩蔽矩阵 M1 和 M2
M1 = np.array([[0.5, 0.6, 0.7],[0.8, 0.9, 1.0],[1.1, 1.2, 1.3]])M2 = np.array([[0.4, 0.5, 0.6],[0.7, 0.8, 0.9],[1.0, 1.1, 1.2]])
# 计算最终损失
loss_value = combined_loss(M1, M2, gamma=15)
print(f'Final Loss: {loss_value:.5f}')
osi_snr_loss: 0.033421230071106235
mc_mse_value: 0.0013543901266690674
Final Loss: 0.05374
http://www.lryc.cn/news/508383.html

相关文章:

  • 【python自动化六】UI自动化基础-selenium的使用
  • 【习题答案】让您的应用拥有领先的位置服务能力
  • java中list和map区别
  • java后端传时间戳给前端的三种方式
  • 【机器学习与数据挖掘实战】案例06:基于Apriori算法的餐饮企业菜品关联分析
  • oracle: create new database
  • 混合开发环境---使用编程AI辅助开发Qt
  • Sigrity SystemSI仿真分析教程文件路径
  • 【YashanDB知识库】Oracle pipelined函数在YashanDB中的改写
  • 【开发实战】QT5+ 工业相机 + OpenCV工作流集成演示
  • 各种电机原理介绍
  • 深入了解 React:从入门到高级应用
  • Cglib代理简单案例
  • FreeMarker语法
  • DP动态规划(装箱问题)
  • 内网IP段介绍与汇总
  • 三、ubuntu18.04安装docker
  • 数据库与表空间
  • 【CSS in Depth 2 精译_091】15.4:让 CSS 高度值过渡到自动高度 + 15.5:自定义属性的过渡设置(全新)+ 15.6:本章小结
  • Oracle中间件 SOA之 OSB 12C服务器环境搭建
  • Java设计模式 —— 【结构型模式】外观模式详解
  • 线性表实验
  • 003无重复字符的最长子串
  • 记录--uniapp 安卓端实现录音功能,保存为amr/mp3文件
  • 前端生成docx文档、excel表格、图片、pdf文件
  • c++---------流类
  • 3、基本复用原理和复用单元
  • Vue与React:前端框架的巅峰对决
  • Java 中的面向对象编程 (OOP) 概念
  • 十二月第20讲:Python中指数概率分布函数的绘图详解