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

Input 0 of layer "dense_2" is incompatible with the layer: expected axis -1 of input shape to have value 16384, but received input with shape (None, 65536) #19367

Closed
coder-monishr opened this issue Mar 24, 2024 · 8 comments
Assignees
Labels

Comments

@coder-monishr
Copy link

Hey there!! This is the issue I get

ValueError: Exception encountered when calling Sequential.call().

Input 0 of layer "dense_2" is incompatible with the layer: expected axis -1 of input shape to have value 16384, but received input with shape (None, 65536)

Arguments received by Sequential.call():
• inputs=tf.Tensor(shape=(None, 256, 256, 3), dtype=float32)
• training=True
• mask=None

code is here!
history = model.fit(x=training_set,validation_data=validation_set, epochs=10)

@coder-monishr
Copy link
Author

@sachinprasadhs help me out

@sachinprasadhs
Copy link
Collaborator

Could you please provide reproducible code in a colab Gist to understand more on the issue. Thanks!

@sachinprasadhs
Copy link
Collaborator

You are restricting the input_shape for each convolution operation to be 128,128,3, which does not fit to the actual data size which you are passing which is 256,256,3, replacing your code with below code should work.

from keras.layers import Dense, Conv2D, MaxPool2D, Flatten
from keras.models import Sequential

model = Sequential()

model.add(Conv2D(filters=32, kernel_size=3, padding="same", activation="relu",input_shape=[256,256,3]))
model.add(Conv2D(filters=32, kernel_size=3, padding="same", activation="relu"))
model.add(MaxPool2D(pool_size=2,strides=2))

model.add(Conv2D(filters=64, kernel_size=3, padding="same", activation="relu"))
model.add(Conv2D(filters=64, kernel_size=3, padding="same", activation="relu"))
model.add(MaxPool2D(pool_size=2,strides=2))


model.add(Conv2D(filters=128, kernel_size=3, padding="same", activation="relu"))
model.add(Conv2D(filters=128, kernel_size=3, padding="same", activation="relu"))
model.add(MaxPool2D(pool_size=2,strides=2))

model.add(Conv2D(filters=256, kernel_size=3, padding="same", activation="relu"))
model.add(Conv2D(filters=256, kernel_size=3, padding="same", activation="relu"))
model.add(MaxPool2D(pool_size=2,strides=2))

model.add(Flatten())

model.add(Dense(units = 1024, activation="relu"))

model.add(Dense(units = 1024, activation="relu"))

model.add(Dense(units = 38, activation="softmax"))

model.compile(optimizer = "adam", loss ='categorical_crossentropy', metrics=['accuracy'])

@coder-monishr
Copy link
Author

history = model.fit(x=training_set, validation_data=validation_set, epochs=10)

ValueError Traceback (most recent call last)
Cell In[12], line 1
----> 1 history = model.fit(x=training_set, validation_data=validation_set, epochs=10)

File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/keras/src/utils/traceback_utils.py:123, in filter_traceback..error_handler(*args, **kwargs)
120 filtered_tb = _process_traceback_frames(e.traceback)
121 # To get the full stack trace, call:
122 # keras.config.disable_traceback_filtering()
--> 123 raise e.with_traceback(filtered_tb) from None
124 finally:
125 del filtered_tb

File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/keras/src/backend/tensorflow/nn.py:546, in categorical_crossentropy(target, output, from_logits, axis)
540 raise ValueError(
541 "Arguments target and output must be at least rank 1. "
542 "Received: "
543 f"target.shape={target.shape}, output.shape={output.shape}"
544 )
545 if len(target.shape) != len(output.shape):
--> 546 raise ValueError(
547 "Arguments target and output must have the same rank "
548 "(ndim). Received: "
549 f"target.shape={target.shape}, output.shape={output.shape}"
550 )
551 for e1, e2 in zip(target.shape, output.shape):
552 if e1 is not None and e2 is not None and e1 != e2:

ValueError: Arguments target and output must have the same rank (ndim). Received: target.shape=(None,), output.shape=(None, 38)

still i get the error dude! :(

@sachinprasadhs
Copy link
Collaborator

It is basically the number of classes you are using, if the data is a binary classification then you need to change the output layer to something like model.add(Dense(units = 1, activation="sigmoid"))

@coder-monishr
Copy link
Author

Thank you so much bro solved

Copy link

Are you satisfied with the resolution of your issue?
Yes
No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants