IndexError:使用深度学习进行音频分类时,列表索引超出范围

时间:2020-07-24 18:31:03

标签: python machine-learning deep-learning

我正在研究音频分类,并收集了400个以上音频的数据集。我目前正在关注youtube上的视频系列。即使完整的代码是正确的,也发生了一个我不知道如何纠正的错误。谁能帮我这个忙,以便它可以正常工作?

我尝试用行和列更改数字,但没有编译起来:

import os
import sys
from tqdm import tqdm
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import wavfile
import python_speech_features
from python_speech_features import mfcc,logfbank
import librosa

def plot_signals(signals):
    fig, axes = plt.subplots(nrows=2, ncols=5, sharex=False,
                             sharey=True, figsize=(20,5))
    fig.suptitle('Time Series', size=16)
    i= 0
    for x in range(2):
    for y in range(5):
        axes[x,y].set_title(list(signals.keys())[i])
        axes[x,y].plot(list(signals.values())[i])
        axes[x,y].get_xaxis().set_visible(False)
        axes[x,y].get_yaxis().set_visible(False)
        i += 1

def plot_fft(fft):
    fig, axes = plt.subplots(nrows=2, ncols=5, sharex=False,
                             sharey=True, figsize=(20,5))
    fig.suptitle('Fourier Transform', size=16)
    i = 0
    for x in range(2):
    for y in range(5):
        data=list(fft.values())[i]
        Y, freq = data[0], data[1]
        axes[x,y].set_title(list(fft.keys())[i])
        axes[x,y].plot(freq, Y)
        axes[x,y].get_xaxis().set_visible(False)
        axes[x,y].get_yaxis().set_visible(False)
        i+=1

def plot_fbank(fbank):
     fig, axes = plt.subplots(nrows=2, ncols=5, sharex=False,
                         sharey=True, figsize=(20,5))
     fig.suptitle('Filter Bank Coefficients', size = 16)
     i = 0
     for x in range(2):
    for y in range(5):
        axes[x,y].set_title(list(fbank.keys())[i])
        axes[x,y].imshow(list(fbank.values())[i],
            cmap='hot', interpolation='nearest')
        axes[x,y].get_xaxis().set_visible(False)
        axes[x,y].get_yaxis().set_visible(False)
        i+=1

def plot_mfccs(mfccs):
     fig, axes = plt.subplots(nrows=2, ncols=5, sharex=False,
                             sharey=True, figsize=(20,5))
     fig.suptitle('Mel Frequency Cepstrum Coefficients', size=16)
     i = 0
     for x in range(2):
       for y in range(5):
        axes[x,y].set_title(list(mfcc.keys())[i])
        axes[x,y].imshow(list(mfccs.values())[i],
            cmap='hot', interpolation='nearest')
        axes[x,y].get_xaxis().set_visible(False)
        axes[x,y].get_yaxis().set_visible(False)
        i+=1

def calc_fft(y, rate):
    n = len(y)
    freq = np.fft.rfftfreq(n, d=1/rate)
    Y = abs(np.fft.rfft(y)/n)
    return(Y, freq)

df = pd.read_csv('cry.csv')
df.set_index('fname', inplace=True)

for f in df.index:
    rate, signal = wavfile.read('wavefiles/'+ f + '.wav')
    df.at[f, 'length'] = signal.shape[0]/rate

classes = list(np.unique(df.labels))
class_dist = df.groupby(['labels'])['length'].mean()

fig, ax = plt.subplots()
ax.set_title('Class Distribution', y=1.08)
ax.pie(class_dist, labels=class_dist.index, autopct='%1.1f%%',
       shadow=False, startangle=90)
ax.axis('equal')
plt.show()
df.reset_index(inplace=True)

signals = {}
fft = {}
fbank = {}
mfccs = {}

for c in classes:
    wav_file = df[df.labels == c].iloc[0,0]
    signal, rate = librosa.load('wavefiles/' +wav_file +'.wav', sr=44100)
    signals[c] = signal
    fft[c] = calc_fft(signal, rate)

     bank = logfbank(signal[:rate], rate, nfilt=26, nfft=1103).T
     fbank[c] = bank
     mel = mfcc(signal[:rate], rate, numcep=13, nfilt=26, nfft=1103).T
     mfccs[c] = mel

 plot_signals(signals)
 plt.show()

 plot_fft(fft)
 plt.show()

 plot_fbank(fbank)
 plt.show()

 plot_mfccs(mfccs)
 plt.show()

以下是发生的错误:-

Traceback (most recent call last):

  File "<ipython-input-44-0f3a8250d6ff>", line 1, in <module>
    runfile('C:/Users/atalp/Desktop/Audio-Classification/eda.py', wdir='C:/Users/atalp/Desktop/Audio- 
Classification')

  File "C:\Users\atalp\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 
827, in runfile
    execfile(filename, namespace)

  File "C:\Users\atalp\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 
110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

   File "C:/Users/atalp/Desktop/Audio-Classification/eda.py", line 108, in <module>
     plot_signals(signals)

  File "C:/Users/atalp/Desktop/Audio-Classification/eda.py", line 19, in plot_signals
    axes[x,y].set_title(list(signals.keys())[i])

 IndexError: list index out of range

0 个答案:

没有答案