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

查看:80
本文介绍了ValueError: 层顺序的输入 0 与层不兼容::预期 min_ndim=4,发现 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]

请帮忙

推荐答案

从听起来像的错误来看,可能您还没有将 batch_axis 添加到您的训练数据集中.即[batch, w, h, channel]

From the error its sounds like, probably you have not added batch_axis to your training dataset. That is [batch, w, h, channel]

工作示例代码

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

这篇关于ValueError: 层顺序的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆