为什么我有图形执行错误?'当预测深度学习模型时

发布于 2025-02-10 08:48:47 字数 1286 浏览 0 评论 0原文

首先,我已经加载了模型以预测我已经准备好的推论集,但是当尝试预测和显示结果时,我会出错。

因此,这里的代码

def load_img(filename):
  img = read_file(filename) # Load Data
  img = decode_image(img, channels=3) # convert to RGB
  img = resize(img, size=[img_height, img_height])
  img = np.array(img)[:,:,1] # Resize image
  img = img/255. # Rescale Images
  return img

inf1 = load_img(r'ML2\COVID-19\inf_set\covid\covid - 1.jpeg')
inf2 = load_img(r'ML2\COVID-19\inf_set\covid\covid - 2.jpeg')
inf3 = load_img(r'ML2\COVID-19\inf_set\normal\Normal - 1.jpeg')
inf4 = load_img(r'ML2\COVID-19\inf_set\normal\Normal - 2.jpeg')
inf5 = load_img(r'ML2\COVID-19\inf_set\pneumonia\Pneumonia - 1.jpeg')
inf6 = load_img(r'ML2\COVID-19\inf_set\pneumonia\Pneumonia - 2.jpeg')


plt.figure(figsize=(35, 5))
plt.suptitle('Prediction Results', fontsize=15)
counter = 1
for i in [inf1,inf2, inf3, inf4, inf5,inf6]:
    plt.subplot(1,6,counter)
    res = int(tf.round(model.predict(x=expand_dims(i, axis=0))))
    plt.imshow(i)
    plt.title(f"Prediction: {label_data[res]}")
    plt.axis('off')
    counter += 1
plt.show()  

和错误通知

所以,我需要帮助来解决这个问题,谢谢

First of all i already load my model to predict inference set that i already prepared, but i got error when to try predict and show the result.

so here my code

def load_img(filename):
  img = read_file(filename) # Load Data
  img = decode_image(img, channels=3) # convert to RGB
  img = resize(img, size=[img_height, img_height])
  img = np.array(img)[:,:,1] # Resize image
  img = img/255. # Rescale Images
  return img

inf1 = load_img(r'ML2\COVID-19\inf_set\covid\covid - 1.jpeg')
inf2 = load_img(r'ML2\COVID-19\inf_set\covid\covid - 2.jpeg')
inf3 = load_img(r'ML2\COVID-19\inf_set\normal\Normal - 1.jpeg')
inf4 = load_img(r'ML2\COVID-19\inf_set\normal\Normal - 2.jpeg')
inf5 = load_img(r'ML2\COVID-19\inf_set\pneumonia\Pneumonia - 1.jpeg')
inf6 = load_img(r'ML2\COVID-19\inf_set\pneumonia\Pneumonia - 2.jpeg')


plt.figure(figsize=(35, 5))
plt.suptitle('Prediction Results', fontsize=15)
counter = 1
for i in [inf1,inf2, inf3, inf4, inf5,inf6]:
    plt.subplot(1,6,counter)
    res = int(tf.round(model.predict(x=expand_dims(i, axis=0))))
    plt.imshow(i)
    plt.title(f"Prediction: {label_data[res]}")
    plt.axis('off')
    counter += 1
plt.show()  

And here the error notification
enter image description here

So, i need help to solve this proble, thank you before

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失与倦" 2025-02-17 08:48:47

您将图像大小挤在此行中,

img = np.array(img)[:,:,1]

这就是为什么您的图像变为大小(220,220)而不是(220,220,1),当您进行Expand_dims < /code>将具有正确的输入形状(1,220,220,1)

您可以更改load_img函数,或者您可以通过执行

res = int(tf.round(model.predict(x=expand_dims(i, axis=[0,3]))))

You are squashing the image size in this line

img = np.array(img)[:,:,1]

This is why your image becomes size (220,220) instead of (220,220,1), which when you do expand_dims will be of proper input shape (1,220,220,1)

You could change the load_img function, or you could solve this by doing

res = int(tf.round(model.predict(x=expand_dims(i, axis=[0,3]))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文