簡體   English   中英

如何以圖像格式保存CNN model的預測結果?

[英]How to save prediction result of the CNN model in the image format?

我一直在 Keras 中使用 CNN,我想將預測保存為 png 圖像。 這是我創建 model 並運行預測的代碼:

 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 文件夾保持為空。 如何以圖像的形式保存CNN model的output(預測)?

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 預測?

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

(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)

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM