简体   繁体   中英

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. That is [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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Keras Conv1D ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=3, found ndim=2 Keras Conv2D - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3 binary signal data: keras ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=3, found ndim=2 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: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [8, 28, 28] ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: [None, 1] 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: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (10, 24) 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: 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