如何为 CNN 模型中的输入正确重塑输入数据?

How to reshape input data correctly for input in CNN model?

我有一个 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:

data = data[None, :]

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