I am trying to process about 700 .wav audio files by converting each one into a spectrogram and saving it in .jpg format.
During each iteration of processing an audio file, I attempt to delete the data at the end. However, the RAM usage continues to increase after each iteration, reaching about 30 GB after processing around 300 files, which causes the system to fail.
Could someone please help me identify where the memory leak might be occurring and suggest ways to fix it?
FRAME_SIZE=2048
HOP_SIZE=512
DURATION=30
def save_and_plot_spectrogram(Y, out_dir, sr, hop_length=HOP_SIZE, y_axis="log", cmap='viridis'):
plt.figure(figsize=(25, 10))
librosa.display.specshow(Y,
sr=sr,
hop_length=hop_length,
x_axis="time",
y_axis=y_axis,
cmap=cmap) # Set the colormap here
plt.axis('off')
plt.savefig(out_dir, bbox_inches='tight', pad_inches=0)
plt.close() # Close the plot to free memory
plt.clf() # Clear the current figure
plt.cla() # Clear the current axes
def process_signal(path,out_dir):
x,sr = librosa.load(path,duration=DURATION)
x_stft=librosa.stft(x,n_fft=FRAME_SIZE,hop_length=HOP_SIZE)
x_mag=np.abs(x_stft)**2
x_log=librosa.power_to_db(x_mag)
save_and_plot_spectrogram(x_log,out_dir,sr)
del x
del x_stft
del x_mag
del x_log
def extract_sprectrogram_from_audio(source_folder,spectogram_folder):
os.makedirs(spectogram_folder, exist_ok=True)
for filename in os.listdir(source_folder):
audio_path=os.path.join(source_folder,filename)
audio_name=os.path.splitext(filename)[0]
#print(audio_name)
audio_spectogram_path=audio_name+'.jpg'
#print(audio_spectogram_path)
output_dir =os.path.join(spectogram_folder,audio_spectogram_path)
#print(output_dir)
process_signal(audio_path,output_dir)
source_folder='/kaggle/input/audio-dataset/wav'
spectogram_folder='/kaggle/working/spectogram'
extract_sprectrogram_from_audio(source_folder,spectogram_folder)