machine-learning - tensorflow 图像形状错误

标签 machine-learning neural-network tensorflow artificial-intelligence keras

我已经训练了一个分类器,现在我想要传递任何单个图像。

我使用 keras 库和 Tensorflow 作为后端。

我遇到了一个似乎无法克服的错误

img_path = '/path/to/my/image.jpg'

import numpy as np
from keras.preprocessing import image
x = image.load_img(img_path, target_size=(250, 250))

x = image.img_to_array(x)
x = np.expand_dims(x, axis=0)

preds = model.predict(x) 

我是否需要 reshape 数据以将 None 作为第一个维度?我很困惑为什么 Tensorflow 期望 None 作为第一个维度?

Error when checking : expected convolution2d_input_1 to have shape (None, 250, 250, 3) but got array with shape (1, 3, 250, 250)

我想知道我训练的模型的架构是否存在问题?

编辑:如果我调用 model.summary() 则将 convolution2d_input_1 指定为...

enter image description here

编辑:我确实尝试了下面的建议,但使用 numpy 来转置而不是 tf - 似乎仍然遇到同样的问题!

enter image description here

最佳答案

None 匹配任何数字。通常,当您将一些数据传递给模型时,预计您会传递维度张量:None x data_size,这意味着第一个维度是任何维度并表示批量大小。就您而言,问题是您传递了 250 x 250 x 3,而预期为 3 x 250 x 250。尝试:

x = image.load_img(img_path, target_size=(250, 250))
x_trans = tf.transpose(x, perm=[2, 0, 1])
x_expanded = np.expand_dims(x_trans, axis=0)
preds = model.predict(x_expanded) 

关于machine-learning - tensorflow 图像形状错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41192992/

相关文章:

neural-network - TensorFlow或Theano:他们如何基于神经网络图知道损失函数导数?

python - TensorFlow 2.0 : Cant run minimal TF Tutorial: TypeError: Can not convert a int64 into a Tensor or Operation

python - 如何根据keras中的条件替换张量的某些部分?

R 中用于超大型数据集处理和机器学习的推荐包

python - PyTorch模型验证: The size of tensor a (32) must match the size of tensor b (13)

python - 如何在 Numpy 中实现 ReLU 函数

python - 如何使用 tf.keras.Model.summary 查看父模型中子模型的层?

python - 如何在 Tensorflow 2.0 中应用 Guided BackProp?

python-3.x - 如何预处理数据并将其输入keras模型?

python - 使用许多多项式的梯度下降不收敛