使用Matplotlib绘制和保存图像时如何获得更高的fps

时间:2019-06-15 09:25:06

标签: python matplotlib memory memory-leaks spectrogram

下面的代码遍历音频文件的目录(约50k),并将它们转换为声谱图图像,并将它们保存在同一顶级目录中。

def plot_and_save(denoised_data, f_name):
    fig, ax = plt.subplots()

    i = 0
    # Add this line to show plots else ignore warnings
    # plt.ion()

    ax.imshow(denoised_data)

    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    fig.set_size_inches(10, 10)
    fig.savefig(
        f"{f_name}" + "_{:04d}.png".format(i),
        dpi=80,
        bbox_inches="tight",
        quality=95,
        pad_inches=0.0)

    ax.draw_artist(ax.xaxis)
    ax.draw_artist(ax.yaxis)

    i += 1


def standardize_and_plot(sampling_rate, file_path_image):
    logger.info(f"All files will be resampled to {sampling_rate}Hz")

    output_image_folder = "PreProcessed_image/"

    for dirs, subdirs, files in os.walk(file_path_image):
        for i, file in enumerate(files):
            if file.endswith(('.wav', '.WAV')):
                logger.info(f"Pre-Processing file: {file}")
                data, sr = librosa.core.load(
                    os.path.join(dirs, file), sr=sampling_rate, res_type='kaiser_fast')
                target_path = os.path.join(output_image_folder, dirs)

                pcen_S = apply_per_channel_energy_norm(data, sr)

                denoised_data = wavelet_denoising(pcen_S)

                work_dir = os.getcwd()

                if not os.path.exists(target_path):
                    os.makedirs(target_path)

                os.chdir(target_path)

                f_name, _ = os.path.splitext(os.path.basename(file))

                plot_and_save(denoised_data, f_name)

                os.chdir(work_dir)

if __name__ == '__main__':
    chunkSize = 3
    sampling_rate = 44100
    file_path_audio = 'Recordings'
    file_path_audio = "data/"
    output_audio_folder = "PreProcessed_audio/"

    file_path_image = os.path.join(output_audio_folder, file_path_audio)

    standardize_and_plot(sampling_rate, file_path_image)

如何优化plot_and_save()方法?将大量图像保存在磁盘上需要花费大量时间(约2-3 fps)。我正在为此目的使用Google Colab。

保存这些图像大约半小时后,colab崩溃,表明已使用所有RAM。有没有办法知道内存泄漏在哪里?还是有办法加快打印和保存过程?

0 个答案:

没有答案