1

I have got a X_train np.array with shape of (1433, 1). The first dimension (1433) is the number of images for training. The second dimension (1) is an np.array which itself has a shape (224, 224, 3). I could confirm it by X_train[0][0].shape. I need to fit X_train to the model:

model.fit([X_train, y_train[:,1:]], y_train[:,0], epochs=50, batch_size=32,  verbose=1)

Error output is self-explanatory:

    Traceback (most recent call last):
  File "/home/combined/file_01.py", line 97, in <module>
    img_output = Flatten()(x_1)
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/base_layer.py", line 414, in __call__
    self.assert_input_compatibility(inputs)
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/base_layer.py", line 327, in assert_input_compatibility
    str(K.ndim(x)))
ValueError: Input 0 is incompatible with layer flatten_1: expected min_ndim=3, found ndim=2

y_train[:,1:] seems to be OK with a shape (1433, 9).

What do I need to do with X_train in model.fit to successfully be able to input as (1433, 224, 224, 3)?

2
  • Can you show how you create the model? Or model summary Commented Feb 24, 2020 at 3:01
  • 1
    @Sreeram TP I was able to resolve the issue, thanks Commented Feb 24, 2020 at 4:14

1 Answer 1

1

It seems that you have a case like this:

import numpy as np
x_train = np.zeros((1433, 1), dtype=object)
for i in range(x_train.shape[0]):
    x_train[i, 0] = np.random.random((224, 224, 3))

x_train.shape        # (1433, 1)
x_train[0, 0].shape  # (224, 224, 3)

Where x_train is an object array (like a nested list) not an numeric array.

You need to change x_train to a pure numeric array:

x_train = np.array([x for x in x_train.flatten()], dtype=float)
x_train.shape       # (1433, 224, 224, 3)
x_train[0].shape    # (224, 224, 3)
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the help, it did help to match the dimensions but the same error continues to happen.
hi again, I resolved the issue after some look ups, it was due to my mistake (flattening the already flattened output) and guess your answer played a significant role. I'll accept your answer.

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.