Python 3.9.7 (default, Sep 16 2021, 16:59:28) [MSC v.1916 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.
IPython 7.29.0 -- An enhanced Interactive Python.
In [1]:
...:
...: """
...: Purpose: to run through audio segments to export embeddings from Yamnet.
...: This code needs to be run on supercomputers, the log is produced on a subsample of our main dataset.
...: """
...:
...: '''
...: !pip install tensorflow-io==0.23.1
...: !pip install -q "tensorflow==2.11.*"
...: !pip install -q "tensorflow_io==0.28.*"
...:
...: !pip install tensorflow_hub
...: !pip install tensorflow_io
...: '''
...:
...: import librosa
...: import os
...: import pandas as pd
...: import numpy as np
...: import tensorflow as tf
...: import tensorflow_hub as hub
...: from scipy.io import wavfile
...: import tensorflow as tf
...: import tensorflow_hub as hub
...: import tensorflow_io as tfio
...:
...:
...: yamnet_model_handle = 'https://tfhub.dev/google/yamnet/1'
...: yamnet_model = hub.load(yamnet_model_handle)
...:
...:
...: import scipy
...:
...: def ensure_sample_rate(original_sample_rate, waveform,
...: desired_sample_rate=16000):
...: """Resample waveform if required."""
...: if original_sample_rate != desired_sample_rate:
...: desired_length = int(round(float(len(waveform)) /
...: original_sample_rate * desired_sample_rate))
...: waveform = scipy.signal.resample(waveform, desired_length)
...: return desired_sample_rate, waveform
...:
...:
...:
...: audio_folder_path=r'C:\Users\marks\Dropbox\JAR Registered Report\Final_version_submisison\data\daicwoz_select_segments'
...:
...: #Output folder to export Yamnet embeddings
...: output_root_path=r'C:\Users\marks\Dropbox\JAR Registered Report\Final_version_submisison\data\daicwoz_select_seg_embeddings'
...:
...:
...: flist=os.listdir(audio_folder_path)
...: os.chdir(audio_folder_path)
...:
...: error_li=[]
...:
...:
...: for audio in flist:
...:
...: try:
...: audio_path=os.path.join(audio_folder_path,audio)
...:
...: sample_rate, wav_data = wavfile.read(audio_path, 'rb')
...: sample_rate, wav_data = ensure_sample_rate(sample_rate, wav_data)
...:
...: scores, embeddings, spectrogram = yamnet_model(wav_data)
...: spectrogram_np=spectrogram.numpy()
...:
...:
...:
...: basename=audio[:-4]
...:
...: emb_fname=basename+'_yamnetemb.npy'
...:
...: emb_output_path=os.path.join(output_root_path,emb_fname)
...:
...: np.save(emb_output_path,embeddings)
...:
...: except Exception as e:
...: error_li.append(audio)
...: print('ERR:', audio, e)
...: continue
...:
...: print("Extracting embeddings completed")
2024-12-14 02:29:56.632472: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Extracting embeddings completed
In [2]: