简体   繁体   中英

Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 2700 but received input with shape [None, 900]

I'm trying to do classification of traffic signs using webcam. I am facing an issue on the reshape line (last 2nd) in the block below. I tried to figure out a lot but can't find solution to it please help. Here I want to make the image array 1D.

while True:
ret, image_seen = cap.read()
image_ = np.asarray(image_seen)
image_ = cv2.resize(image_, (30, 30))
image_ = imgpreprocess(image_)
cv2.imshow("Processed Image", image_)
####
image_ = image_.reshape(1, 30, 30, 1)
####
cv2.putText(image_seen, "CLASS: ", (20,35), font, 0.75, (0,0,255), 2, 
cv2.LINE_AA)

Below is my model:

base_model = tf.keras.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu', input_shape=(30, 30, 1)),
tf.keras.layers.Dense(43)
])

And here's how I preprocessed the images:

def imgpreprocess(img):
# to grayscale
img = cv2.cvtColor(np.float32(img), cv2.COLOR_BGR2GRAY)
img = img/225.0
return img

Error is:

Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 2700 but received input with shape [None, 900]

Any help would be appreciated, thanks.

I can not reproduce your error. Your model works:

import tensorflow as tf
base_model = tf.keras.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu', input_shape=(30, 30, 1)),
tf.keras.layers.Dense(43)
])
inp = tf.random.uniform((1, 30, 30, 1))
a = base_model(inp)

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.

Related Question Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape (None, 1) ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 to have value 8 but received input with shape [None, 1] Input 0 of layer dense_18 is incompatible with the layer: expected axis -1 of input shape to have value 3500 but received input with shape [None, 7] Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 8192 but received input with shape (None, 61608) Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 8 but received input with shape (None, 71) ValueError: ...incompatible with the layer: expected axis -1 of input shape to have value 20 but received input with shape (None, 20, 637) “ValueError: …incompatible with the layer: expected axis -1 of input shape to have value 8 but received input with shape (None, 7, 169)” Input 0 of layer conv2d is incompatible with layer: expected axis -1 of input shape to have value 1 but received input with shape [None, 64, 64, 3] ValueError: Input 0 of layer dense_24 is incompatible: expected axis -1 of input shape to have value 1024 but received input with shape [16, 512] Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM