在Keras 2D卷积网络中遇到输入形状/层面的问题。

huangapple go评论67阅读模式
英文:

Facing input shape/layer problem in keras 2D convutional network

问题

我面临以下错误:

数值错误:调用层 'conv2d'(类型 Conv2D)时遇到异常。
由于从 1 减去 3 导致负维度大小,用于 'sequential/conv2d/Conv2D' 节点(输入形状为:[?,200,1,1], [3,3,1,9])。

层 'conv2d'(类型 Conv2D)接收到的调用参数:
• inputs=tf.Tensor(shape=(None, 200, 1, 1), dtype=float32)

我的模型:

model = Sequential()
model.add(Conv2D(9, kernel_size=3, activation='relu', input_shape=(200,200, 1)))
model.add(Conv2D(4, kernel_size=3, activation='relu'))
model.add(Flatten())
model.add(Dense(2, activation='sigmoid'))
model.predict(image)

这里的 "image" 是一个维度为 (200, 200, 1) 的数组,其中包含了 0 和 1 的值。

我该怎么办?

英文:

I face the following error:

ValueError: Exception encountered when calling layer 'conv2d' (type Conv2D).
Negative dimension size caused by subtracting 3 from 1 for '{{node sequential/conv2d/Conv2D}} = Conv2D[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1], explicit_paddings=[], padding="VALID", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true](sequential/Cast, sequential/conv2d/Conv2D/ReadVariableOp)' with input shapes: [?,200,1,1], [3,3,1,9].

Call arguments received by layer 'conv2d' (type Conv2D):
  • inputs=tf.Tensor(shape=(None, 200, 1, 1), dtype=float32)

my model:

model = Sequential()
model.add(Conv2D(9, kernel_size=3, activation='relu', input_shape=(200,200, 1)))
model.add(Conv2D(4, kernel_size=3, activation='relu'))
model.add(Flatten())
model.add(Dense(2, activation='sigmoid'))
model.predict(image)

Here "image" is a array of dimensions (200, 200, 1) with values in it as 0's&1's

what should I do??

答案1

得分: 1

你的模型的输入形状是 (200,200, 1),但不要忘记训练和推理是在批次上进行的。你的真正输入形状是 (None, 200,200, 1)。

如果你只想针对单个图像进行预测,你应该在开头添加另一个维度,像这样:

image = np.expand_dims(image, 0)
# 图像形状应该是 (1, 200,200, 1)
model.predict(image)
英文:

Your model's input is (200,200, 1) but don't forget that training and inference is done on batches. Your true input shape is (None, 200,200, 1)

If you want predictions only for a single image you should add another dimension at the beginning, like this:

image=np.expand_dims(image, 0)
# image shape should be (1, 200,200, 1)
model.predict(image)

huangapple
  • 本文由 发表于 2023年6月22日 03:35:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526584.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定