【问题标题】:How to save prediction result of the CNN model in the image format?如何以图像格式保存CNN模型的预测结果?
【发布时间】:2021-02-16 13:47:01
【问题描述】:

我一直在 Ke​​ras 中使用 CNN,我想将预测保存为 png 图像。这是我创建模型并运行预测的代码:

 history = model.fit(
        train_generator,
        steps_per_epoch=train_steps,
        epochs=epochs,
        validation_data=validation_generator,
        validation_steps=valid_steps)

    pred= model.predict(X_test)

我根据以下链接使用了以下代码,而 pred 文件夹仍然为空。 (How to save output (prediction) of the CNN model in the form of an image?)

images = []
for i in range(len(images)*2):
        pred.append(id_to_name[np.argmax(predictions[i // 2])])
        plt.imsave(('pred/ {}.png'.format(str(i // 2)+id_to_name[np.argmax(predictions[i // 2])])),images[i // 2])

您能告诉我如何以 png 图像的形式保存 model.predict 预测吗?

【问题讨论】:

    标签: python image tensorflow keras


    【解决方案1】:

    如果您的图像具有以下格式:

    (batch_size, height, width, channels)
    

    您可以使用tf.keras.preprocessing.image.save_img

    import tensorflow as tf
    
    images = tf.random.uniform((4, 224, 224, 3))
    
    for i, image in enumerate(images, 1):
        tf.keras.preprocessing.image.save_img(f'my_picture_{i}.png', image)
    

    【讨论】:

      猜你喜欢
      • 2021-06-27
      • 2021-07-01
      • 2020-05-14
      • 2018-11-03
      • 2018-12-13
      • 1970-01-01
      • 2020-05-31
      • 1970-01-01
      • 2017-12-24
      相关资源
      最近更新 更多