import librosa
import numpy as np
import utils
import torch
import torch.nn.functional as F
from matplotlib import pyplot as plt
from torchvision.models.feature_extraction import create_feature_extractor
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def extract_mbe(_y, _sr, _nfft, _nb_mel):
#梅尔频谱
spec = librosa.core.spectrum._spectrogram(y=_y, n_fft=_nfft, hop_length=_nfft // 2, power=1)[0]
mel_basis = librosa.filters.mel(sr=_sr, n_fft=_nfft, n_mels=_nb_mel)
mel_spec = np.log(np.dot(mel_basis, spec).T)
return mel_spec #最后必须是[frames, dimensions]
def preprocess_data(X, seq_len, nb_ch):
# split into sequences
X = utils.split_in_seqs(X, seq_len)
X = utils.split_multi_channels(X, nb_ch)
# Convert to PyTorch tensors
X = torch.Tensor(X)
X = X.permute(0,1,3,2) #x形状为[709,2,40,256],【总样本数,通道数,特征维度,像素宽度】
return X
# 提取梅尔频谱特征
audio_path = "b093.wav"
y, sr = librosa.load(audio_path, sr=44100)
mel = extract_mbe(y, sr, 2048, 64)
value = preprocess_data(mel, 256, 1).to(device) #value 为输入模型的样本特征
features = {"cnn1": '1', "cnn2": '2', "cnn3": '3', "cnn4": '4', "cnn5": '5', "cnn6": '6'}
model = torch.load(f'best_model_2.pth')
feature_extractor = create_feature_extractor(model, return_nodes=features)
out = feature_extractor(value)
layer = "3"
out = torch.cat((out[layer][0], out[layer][1]), dim=1)
out = out.unsqueeze(0)
out = F.interpolate(out, size=(470, 64), mode='bilinear', align_corners=False)
out = out.squeeze(0) .permute(2, 0, 1) #[128, 256, 64]->[64, 128, 256]->[纵, 值, 横]
plt.imshow(out.sum(1).detach().cpu().numpy(), origin='lower')
plt.show()
神经网络中间层特征图可视化(输入为音频)
孜孜不倦fly2023-11-15 12:41
相关推荐
OCR_wintone421几秒前
易泊车牌识别相机,助力智慧工地建设王哈哈^_^21 分钟前
【数据集】【YOLO】【VOC】目标检测数据集,查找数据集,yolo目标检测算法详细实战训练步骤!一者仁心28 分钟前
【AI技术】PaddleSpeech是瑶瑶子啦36 分钟前
【深度学习】论文笔记:空间变换网络(Spatial Transformer Networks)EasyCVR39 分钟前
萤石设备视频接入平台EasyCVR多品牌摄像机视频平台海康ehome平台(ISUP)接入EasyCVR不在线如何排查?runing_an_min40 分钟前
ffmpeg 视频滤镜:屏蔽边框杂色- fillborders柳鲲鹏1 小时前
OpenCV视频防抖源码及编译脚本西柚小萌新1 小时前
8.机器学习--决策树向阳12181 小时前
Bert快速入门jndingxin1 小时前
OpenCV视觉分析之目标跟踪(8)目标跟踪函数CamShift()使用