【问题标题】:How to reshape input data correctly for input in CNN model?如何正确重塑输入数据以在 CNN 模型中输入?
【发布时间】:2021-04-20 04:58:07
【问题描述】:

我有一个 CNN 模型,输入图像大小为(150, 150)。我想为predict 函数(tensorflow)提供类似数组的数据,如下所示:

fig = plt.figure(figsize=(1.5, 1.5))
plt.plot(time_values, signal_values, '-o', c='r')
plt.axis('off')
fig.canvas.draw()
data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))

当我尝试时:

CNN_model.predict(data)

我收到一个错误:

WARNING:tensorflow:Model was constructed with shape (None, 150, 150, 3) for input Tensor("input_1:0", shape=(None, 150, 150, 3), dtype=float32), but it was called on an input with incompatible shape (None, 150, 3).
Traceback (most recent call last):

为什么我的形状是(None, 150, 3),而不是(None, 150, 150, 3)

【问题讨论】:

  • 最后一行直接写data.reshape((1, 150, 150, 3))
  • @SreekantShenoy 非常感谢。据我了解,这里的第一个值 (None, 150, 150, 3) 等于 ((1, 150, 150, 3))

标签: python tensorflow neural-network conv-neural-network


【解决方案1】:

只需为您的数组添加一个新维度data

data = data[None, :]

然后它的形状将是 (1, 150, 150, 3),正如 tensorflow 所期望的那样。

【讨论】:

    猜你喜欢
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 2019-12-26
    • 2018-10-07
    • 2018-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多