简体   繁体   中英

Keras Conv2D - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3

I konw, there are already some questions like this, but I couldn´t find any solution for this problem.

I created a model like this:

def CreateModel(optimizer=optimizer, loss=loss, learn_rate=learn_rate, activity_regularizer=activity_regularizer):
    model = keras.Sequential([
            keras.layers.Conv2D(32,3,input_shape=(9,21,1)),
            keras.layers.Flatten(),
            keras.layers.Dense(32, activation='relu', kernel_initializer=keras.initializers.RandomUniform(maxval=1, minval=0), bias_initializer=keras.initializers.Zeros(), activity_regularizer=activity_regularizer),
            keras.layers.Dense(2, activation='softmax', kernel_initializer=keras.initializers.RandomUniform(maxval=1, minval=0), bias_initializer=keras.initializers.Zeros(), activity_regularizer=activity_regularizer)
            ])
    model.compile(optimizer=optimizer,
              loss=loss,
              metrics=['accuracy', keras.metrics.FalseNegatives(), keras.metrics.FalsePositives(), keras.metrics.Precision(), keras.metrics.Recall()])
    return model

My input consists of 300 9x21 gray scale images.

Without the Conv2D layer it works perfectly fine. But with this Conv2D layer I got the error:

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

I also tried some other input_shapes like:

keras.layers.Conv2D(32,3,input_shape=(300,9,21,1))
keras.layers.Conv2D(32,3,input_shape=(300,9,21))

but without success.

Thanks Prickels

Prickels,

Just make sure that you feed the model with data in [n_items,9,21,1] shape. use data = tf.expand_dims(data, axis =-1)

Alternatively add Reshape layer first:

tf.keras.layers.Reshape((9,21,1), input_shape=(9,21))

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM