[英]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.