1

I'm trying to fit a ConvNet model using Keras' fit generator, but it fails when trying to feed the data to the input layer. It's telling me it's expecting a three-dimensional input, but my input is only two. If I add a channel to my input shape, it asks for four dimensions.

Here's the exact error when I don't add a channel parameter:

ValueError: Error when checking input: expected input_1 to have 3 dimensions, but got array with shape (1000, 597)

And again when I change the input shape to (1000, 597, 1):

ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (1000, 597)

Here's the code for my model:

def initialise_model():
    input_layer = Input((1, 1000, 597))
    conv_layer_1 = Conv2D(filters=30, kernel_size=(10, 1), strides=(1, 1), padding="same", activation="relu")(input_layer)
    conv_layer_2 = Conv2D(filters=30, kernel_size=(8, 1), strides=(8, 1), padding="same", activation="relu")(conv_layer_1)
    conv_layer_3 = Conv2D(filters=40, kernel_size=(6, 1), strides=(6, 1), padding="same", activation="relu")(conv_layer_2)
    conv_layer_4 = Conv2D(filters=50, kernel_size=(5, 1), strides=(1, 1), padding="same", activation="relu")(conv_layer_3)
    conv_layer_5 = Conv2D(filters=50, kernel_size=(5, 1), strides=(1, 1), padding="same", activation="relu")(conv_layer_4)
    flatten_layer = Flatten()(conv_layer_5)
    dense_layer = Dense(1024, activation="relu")(flatten_layer)
    label_layer = Dense(1024, activation="relu")(dense_layer)
    output_layer = Dense(1, activation="linear")(label_layer)

    model = Model(inputs=input_layer, outputs=output_layer)

    adam_optimiser = keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
    model.compile(optimizer=adam_optimiser, loss="mean_squared_error", metrics=["accuracy", "mean_squared_error"])
    return model

And my fit generator:

model = initialise_model()
early_stopping = EarlyStopping(monitor="val_loss", min_delta=0, patience=0, verbose=1, restore_best_weights=True)
model.fit_generator(generator, epochs=1, steps_per_epoch=1, verbose=2, callbacks=[early_stopping])

It's worth noting that the output of my generator is as expected, with the correct shape.

Many thanks

7
  • what is you target shape? Commented Oct 30, 2019 at 10:44
  • please provide full error Commented Oct 30, 2019 at 10:56
  • please provide generator code too, why you need a reshape layer?? Commented Oct 30, 2019 at 11:01
  • @IoannisNasios the target shape of the convnet is (1000, 1) Commented Oct 30, 2019 at 11:46
  • @Mukul As someone else pointed out, the reshape was a mistake on my part and it's been removed from the sample code Commented Oct 30, 2019 at 11:48

3 Answers 3

1

set our input layer as

input_layer = Input((1, 1000, 597))

or if channels are set to last

input_layer = Input((1000, 597, 1))

and make sure that your generator yields x_train data of shape

(batch_size, 1, 1000, 597)

or

(batch_size, 1000, 597, 1)
Sign up to request clarification or add additional context in comments.

Comments

0

Have a look at the Keras documentation for Convolutional layers. The input shape should be (batch, channels, rows, cols) for 2D convolutions. That means you need to pass (channels, rows, cols) to your Input-layer. There is actually no need for an reshape-layer as far as I can tell.

What are the dimensions of your images? I assume you want to pass images since you use 2D convolutions, but it looks a little bit more like you're trying to pass vectors.

Your loss is MSE which is fine, but your metric is accuracy. Why?

1 Comment

Having changed my code to reflect your suggested changes, I'm still getting the same error. I'll update the code in my question though! I'm using the generator to produce sliding windows, which is why I'm using 2D convolutions.
0

conv2D layer has by default data_format = "channels_last" if you want to use image having a shape (1, 1000, 597) then you have to set data_format = "channels_first" or change input dim to (1000, 597, 1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.