简体   繁体   English

ValueError:层顺序的输入0与层不兼容::预期的min_ndim = 4,发现ndim = 3

[英]ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3

I'm trying to run the following code.我正在尝试运行以下代码。

model = tf.keras.models.Sequential([tf.keras.layers.Conv2D(16,(3,3),activation='relu',input_shape = (200,200,3)),
                                tf.keras.layers.MaxPool2D(2,2),
                                #
                                tf.keras.layers.Conv2D(16,(3,3),activation='relu',input_shape = (200,200,3)),
                                tf.keras.layers.MaxPool2D(2,2),
                                #
                                tf.keras.layers.Conv2D(16,(3,3),activation='relu',input_shape = (200,200,3)),
                                tf.keras.layers.MaxPool2D(2,2),
                                ##
                                tf.keras.layers.Flatten(),
                                ##
                                tf.keras.layers.Dense(512,activation= 'relu'),
                                ##
                                tf.keras.layers.Dense(1,activation='sigmoid')
                                ])
image_dir = r"C:\Users\Shreya\Desktop\Project\basedata\testing\testing\tomato"
img_list = os.listdir(image_dir)

for i in img_list:
    path = os.path.join(image_dir, i)
    img = image.load_img(path, target_size = (150, 150))
    img = np.asarray(img)
    array = image.img_to_array(img)
    pred = model.predict_classes((img/255).reshape((150,150,3)))
    plt.figure('img')
    plt.imshow(img,cmap='gray')
    plt.title('pred:'+str(pred[0]), fontsize=22)
    plt.show()

After executing this, I'm getting the following error:执行此操作后,我收到以下错误:

ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 150, 3]

please help请帮忙

From the error its sounds like, probably you have not added batch_axis to your training dataset.从错误的声音来看,您可能尚未将 batch_axis 添加到您的训练数据集中。 That is [batch, w, h, channel][batch, w, h, channel]

Working sample code工作示例代码

x_train, x_test = x_train.reshape(-1,200,200,1), x_test.reshape(-1,200,200,1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Keras Conv1D ValueError:层顺序的输入 0 与层不兼容::预期 min_ndim=3,发现 ndim=2 - Keras Conv1D ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=3, found ndim=2 Keras Conv2D - ValueError:层序的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3 - Keras Conv2D - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3 二进制信号数据:keras ValueError:层顺序的输入 0 与层不兼容::预期 min_ndim=3,发现 ndim=2 - binary signal data: keras ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=3, found ndim=2 ValueError:layersequential_5 的输入 0 与 layer 不兼容::预期 min_ndim=4,发现 ndim=2。 收到的完整形状:[无,953] - ValueError: Input 0 of layer sequential_5 is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: [None, 953] ValueError:顺序层的输入 0 与层不兼容::预计 min_ndim=4,发现 ndim=3。 收到完整形状:[8, 28, 28] - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [8, 28, 28] ValueError: 层顺序的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=2。 收到完整形状:[无,1] - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: [None, 1] ValueError: 层序列 9 的输入 0 与层不兼容:预期 min_ndim=4,发现 ndim=3。 收到的完整形状:[无,无,无] - ValueError: Input 0 of layer sequential_9 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, None, None] ValueError:层顺序的输入 0 与层不兼容::预期 min_ndim=3,发现 ndim=2。 收到的完整形状:(10, 24) - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (10, 24) ValueError:layersequential_1 的输入 0 与 layer 不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:[无、256、256] - ValueError: Input 0 of layer sequential_1 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 256, 256] ValueError: 层序贯_1 的输入 0 与层不兼容: : 预期 min_ndim=4,发现 ndim=2。 收到的完整形状:(32768, 1) - ValueError: Input 0 of layer sequential_1 is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: (32768, 1)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM