Advertisement
Guest User

Untitled

a guest
Jan 13th, 2022
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.87 KB | None | 0 0
  1. import pandas as pd
  2. import os
  3. import librosa
  4. import librosa.display
  5. import matplotlib.pyplot as plt
  6. from sklearn.preprocessing import normalize
  7. import warnings
  8. warnings.filterwarnings('ignore')
  9. import numpy as np
  10. import pickle
  11. import joblib
  12. from sklearn.model_selection import train_test_split
  13. from tensorflow.keras import models, layers
  14. import tensorflow as tf
  15. from google.colab import drive
  16.  
  17. #INTEGRATE GOOGLE DRIVE WITH GOOGLE COLAB
  18. drive.mount('/content/gdrive')
  19.  
  20. #ACCESSING THE DIRECTORY AND .WAV FILES
  21. os.chdir(r'/content/gdrive/MyDrive/Personal/G12/Research/Audio files') #access main directory
  22. df = pd.read_csv('metadata.csv')
  23. test_file = (r'/content/gdrive/MyDrive/Personal/G12/Research/Audio files/Train/Philippine Eagle/PE (170).wav') #our test file
  24. print("file path: " + os.path.abspath(test_file)) #show filepath
  25. df.head() #show metadata
  26. ipd.Audio(test_file) #audio player #audio player
  27.  
  28. #PADDING THE SIGNALS
  29. array = np.random.randint(0, 10, (7, 4)) #Scale and pad the audio features so that every “channel” is the same size
  30.  
  31. def padding(array, xx, yy):
  32.     """
  33.    :param array: numpy array
  34.    :param xx: desired height
  35.    :param yy: desirex width
  36.    :return: padded array
  37.    """
  38.     h = array.shape[0]
  39.     w = array.shape[1]
  40.  
  41.     a = max((xx - h) // 2,0)
  42.     aa = max(0,xx - a - h)
  43.  
  44.     b = max(0,(yy - w) // 2)
  45.     bb = max(yy - b - w,0)
  46.  
  47.     return np.pad(array, pad_width=((a, aa), (b, bb)), mode='constant')
  48.  
  49. #GENERATE FEATURES
  50. def generate_features(signal_cut):
  51.     max_size=1000 #max audio file feature width
  52.     stft = padding(np.abs(librosa.stft(signal_cut, n_fft=255, hop_length = 512)), 128, max_size)
  53.     MFCCs = padding(librosa.feature.mfcc(signal_cut, n_fft=n_fft, hop_length=hop_length,n_mfcc=128),128,max_size)
  54.     spec_centroid = librosa.feature.spectral_centroid(signal=signal_cut, sr=sr)
  55.     chroma_stft = librosa.feature.chroma_stft(signal=signal_cut, sr=sr)
  56.     spec_bw = librosa.feature.spectral_bandwidth(signal=signal_cut, sr=sr)
  57.     #Now the padding part
  58.     image = np.array([padding(normalize(spec_bw),1, max_size)]).reshape(1,max_size)
  59.     image = np.append(image,padding(normalize(spec_centroid),1, max_size), axis=0)
  60. #repeat the padded spec_bw,spec_centroid and chroma stft until they are stft and MFCC-sized
  61.     for i in range(0,9):
  62.         image = np.append(image,padding(normalize(spec_bw),1, max_size), axis=0)
  63.         image = np.append(image, padding(normalize(spec_centroid),1, max_size), axis=0)
  64.         image = np.append(image, padding(normalize(chroma_stft),12, max_size), axis=0)
  65.     image=np.dstack((image,np.abs(stft)))
  66.     image=np.dstack((image,MFCCs))
  67.     return image
  68.  
  69. X=df.drop('bird_id',axis=1)
  70. y=df.bird_id
  71.  
  72. #Split once to get the test and training set
  73. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=123, stratify=y)
  74. print(X_train.shape,X_test.shape)
  75.  
  76. #Split twice to get the validation set
  77. X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.25, random_state=123)
  78. print(X_train.shape, X_test.shape, X_val.shape, len(y_train), len(y_test), len(y_val))
  79.  
  80. #THIS IS WHERE THE ERROR APPEARS
  81. #file_name is different from filename
  82. #Calculate these features for every audio file and store as features and labels:
  83.  
  84. def get_features(df_in):  
  85.     features=[]    
  86.     labels = [] #empty array to store labels    
  87.     #For each species, determine how many augmentations are needed
  88.     df_in=df_in.reset_index()    
  89.     for i in df_in.bird_id.unique():
  90.            print('bird_id:',i)    
  91.            #all the file indices with the same bird_id    
  92.            filelist = df_in.loc[df_in.bird_id == i].index        
  93.     for j in range(0,len(filelist)):      
  94.             filename = df_in.iloc[filelist[j]].file_name
  95.             print("full path: " + os.path.abspath(filename))
  96.             #define the beginning time of the signal          
  97.             tstart = df_in.iloc[filelist[j]].t_min            
  98.             tend = df_in.iloc[filelist[j]].t_max #end of signal
  99.             file_name = df_in.iloc[filelist[j]].file_name
  100.             bird_id = i
  101.             songtype_id = df_in.iloc[filelist[j]].songtype_id  
  102.             #Load the file
  103.             signal, sr = librosa.load(filename,sr=28000)  
  104.             #cut the file to signal start and end  
  105.             y_cut=y[int(round(tstart*sr)):int(round(tend*sr))]  
  106.             #generate features & output numpy array          
  107.             data = generate_features(signal)
  108.             features.append(data[np.newaxis,...])    
  109.             labels.append(bird_id)    
  110.             output=np.concatenate(features,axis=0)    
  111.             return(np.array(output), labels)
  112. #use get_features to calculate and store the features
  113. test_features, test_labels = get_features(pd.concat([X_test,y_test],axis=1))
  114. train_features, train_labels = get_features_noOS(pd.concat([X_train,y_train],axis=1))
  115.  
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement