Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (2120, 1) #12318

Closed
masti96 opened this issue Feb 21, 2019 · 7 comments
Labels
type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.

Comments

@masti96
Copy link

masti96 commented Feb 21, 2019

Hey everyone,

I'm trying to use custom data on the resnet 50 model, but it keeps giving shape errors. After reading some other issues along the same lines.Any thoughts?

if name == 'main':

# Example to fine-tune on 3000 samples from Cifar10

img_rows, img_cols = 224, 224 # Resolution of inputs
channel = 3
num_classes = 1 
batch_size = 16 
nb_epoch = 1

Load our model

model = resnet50_model(img_rows, img_cols, channel, num_classes)

# Start Fine-tuning

model.fit(X_train, Y_train,
batch_size=batch_size,
nb_epoch=nb_epoch,
shuffle=True,
verbose=1,
validation_data=(X_test, Y_test),
)

Make predictions

predictions_valid = model.predict(X_test, batch_size=batch_size, verbose=1)

Cross-entropy loss score

score = log_loss(Y_test, predictions_valid)

output:

ValueError Traceback (most recent call last)
in
18 shuffle=True,
19 verbose=1,
---> 20 validation_data=(X_test, Y_test),
21 )
22

~/anaconda3/envs/py36/lib/python3.6/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
950 sample_weight=sample_weight,
951 class_weight=class_weight,
--> 952 batch_size=batch_size)
953 # Prepare validation data.
954 do_validation = False

~/anaconda3/envs/py36/lib/python3.6/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
749 feed_input_shapes,
750 check_batch_axis=False, # Don't enforce the batch size.
--> 751 exception_prefix='input')
752
753 if y is not None:

~/anaconda3/envs/py36/lib/python3.6/site-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
126 ': expected ' + names[i] + ' to have ' +
127 str(len(shape)) + ' dimensions, but got array '
--> 128 'with shape ' + str(data_shape))
129 if not check_batch_axis:
130 data_shape = data_shape[1:]

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

@gabrieldemarmiesse gabrieldemarmiesse added the type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited. label Feb 24, 2019
@pedro-r-marques
Copy link

The error seems to indicate that the X_train parameter that you passed to model.fit has a shape of (2120, 1). The resnet model expects a shape of (batch_size, 3, 224, 224) by default.

I would recommend that you print the shapes of the parameters to model.fit() and also use model.summary() to display the expected shapes of the network.

@Omer95
Copy link

Omer95 commented May 27, 2019

Also facing this issue. My data has a shape of (900, 300, 250, 3) but as soon as I increase the training set to 1000 images, I get the following shape (1000,). One thing to note is that I'm facing this problem only on Google Colab notebook and not on Jupyter where my training data shape is (1000, 300, 250, 3) with the same code

@deduble
Copy link

deduble commented Jun 13, 2019

The problem most probably that you have a none 300*250 image leaked into your dataset. Please make sure your data is matching. To have a N-dimensional array you have to have all dimension matching through your dataset. And on top of that your datatype should be the same. If you would have even one image that is not the same dimensions of others you would have a Structured array, thus your array shape would become N,1 instead of N,H,W,C.

@anitono
Copy link

anitono commented Sep 10, 2019

I'm facing the same issue VGG16 Model while taking the input from a different source and the eroor comes as: ValueError: Error when checking input: expected input_2 to have shape (224, 224, 3) but got array with shape (224, 224, 4)
Untitled Document 1.txt

@zhyiyu
Copy link

zhyiyu commented Feb 13, 2020

I am facing similar problem and converting input and output arrays to numpy.array solved the problem for me.

@Madwesh-india
Copy link

Madwesh-india commented Sep 3, 2020

adding extra bracket on the x_train and y_train and converting them to array solved it for me
(np.array([train_x, train_y]))
for me it was expected input_1 to have 3 dimensions

@rdruzian
Copy link

rdruzian commented Oct 7, 2020

I have the same problem, I try to convert to array as said, but didn't solved the problem...

My error: ValueError: Error when checking input: expected input_4 to have shape (None, 600) but got array with shape (600, 21661)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.
Projects
None yet
Development

No branches or pull requests

10 participants