首页 > 解决方案 > 在 python 循环中保存预测图像每次迭代都会变慢

问题描述

我在 keras 中的 predict_generator() 函数生成的 cnn 中循环预测图像,并在循环中使用 matplotlib> plt.savefig 保存它们。由于某种原因,这个循环的每次迭代都变得越来越慢;在 2000 次迭代之后,完成循环的时间从 <1s/it 变为 ~56s/it。什么可能导致放缓,以及可以采取什么措施来解决它?

循环结构如下:

results = model.predict_generator(test_img_gen,len(os.listdir(child)),verbose=1)

for i,img in tqdm(enumerate(results)):
        plt.imshow(np.reshape(img,(512,512)), interpolation='nearest')
        resultDir = '{}_{}_{}.png'.format(resdir,filenames[i],str(i))
        plt.savefig(resultDir)

标签: pythonloopstime

解决方案


推荐阅读