python - 使用 cv2 显示unet预测图像

标签 python tensorflow machine-learning deep-learning semantic-segmentation

我使用图像和掩模训练了一个模型,并预测了一张图像。 预测图像的形状为 (1024,1024,3)

代码。

nueva_imagen = cv2.imread("../input/dataset/Training_dataset/Images/all_imgs/zanzibar_4_35_04.jpg")
print(nueva_imagen.shape)
nueva_imagen = cv2.resize(nueva_imagen,(256,256))
nueva_imagen = np.expand_dims(nueva_imagen,axis=0)
print(nueva_imagen.shape)
pred_img = model.predict(nueva_imagen)
print(pred_img.shape)
#pred_img = np.squeeze(pred_img,axis=0)
#print(pred_img.shape)

我添加另一个维度的原因是因为我的模型只拍摄 3 维图像。 最后我预测的图像形状是 (1,256,256,1) 现在阅读 cv2 文档,我无法显示 4 维图像,所以这就是我所做的。

image_to_predict = _images[789]
mask_of_image = masks_arr[789]



pred_img = np.squeeze(pred_img,axis=0)
pred_img = np.squeeze(pred_img,axis=2)



fig = plt.figure()
fig.subplots_adjust(hspace=0.4, wspace=0.4)
ax = fig.add_subplot(1, 2, 1);plt.title("original image")
ax.imshow(image_to_predict)

#ax = fig.add_subplot(1, 2, 2);plt.title("mask")
#ax.imshow(mask_of_image)

ax = fig.add_subplot(1,2,2);plt.title("Predicted image")
ax.imshow(pred_img)

我删除了轴 0 和 3 上的尺寸以显示图像,但我得到的是紫色图像。 这是预测的图像还是我做错了什么?

最佳答案

您可能想要执行的一些检查(如果无法访问模型和图像,很难判断):

  • 当您使用 OpenCV 加载图像时,图像将存储为 BGR 而不是 RGB。如果您在使用 OpenCV 加载的图像上训练模型,那么这应该不是问题。但如果您在目录上使用 fit 方法,您可能需要使用 cv2.cvtColor(nueva_imagen, cv2.COLOR_BGR2RGB) 将 BGR 转换为 RGB。
  • 确保训练和预测之间的图像预处理相同。通常,检查它们的值是否为 [0, 1](作为 float)或 [0, 255](作为 int)
  • 使用 Matplotlib 计算 [0, 1] 值时也是如此

关于python - 使用 cv2 显示unet预测图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58182288/

相关文章:

python - 将 msgpack-python 与嵌套的命名元组一起使用

tensorflow - 在Tensorflow Estimator中,input_fn可以知道当前的训练步骤吗?

tensorflow - 如何在 Keras 中将多个数据集与一个模型一起使用?

artificial-intelligence - 人工智能 - 机器学习

python - 使用描述符协议(protocol)更改底层数据表示

python - 如何在python中使用RSA私钥加密数据?

python - Tensorflow 的 DNNLinearCombinedClassifier 打印回归损失而不是分类损失

machine-learning - 在图像上使用 Weka

python - 在某些条件下从列表中提取数据

python - 只要按下键,如何让游戏对象连续移动?