Anubhav
Anubhav

Reputation: 355

Input 0 of layer "sequential_23" is incompatible with the layer: expected shape=(None, 1797, 8, 8), found shape=(None, 8, 8)

When i fit my model a have a vallueError:"Input 0 of layer "sequential_41" is incompatible with the layer: expected shape=(None, 1347, 8, 8), found shape=(None, 8, 8) Here is my code.

from sklearn.datasets import load_digits
digits=load_digits()
digits.keys()
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(digits.images,digits.target)

model1=keras.Sequential([
    keras.layers.Conv2D(filters=32,kernel_size=(3,3),input_shape=(1347,8,8),activation='relu'),
    keras.layers.MaxPooling2D(2,2),

    keras.layers.Flatten(),
    keras.layers.Dense(50,activation='relu'),
    keras.layers.Dense(10,activation='sigmoid')

])
model1.compile(optimizer='SGD',
    loss='sparse_categorical_crossentropy',
             metrics=['accuracy'])

when i try to fit my model i am getting an error

model1.fit(x_train,y_train,epochs=10)

Upvotes: 1

Views: 1077

Answers (2)

USMAN KHALID
USMAN KHALID

Reputation: 1

Resize image to match model's expected sizing:

img = cv2.resize(img,(240,240))

Return the image with shaping that TF wants:

img = img.reshape(1,240,240,3)

Upvotes: 0

AloneTogether
AloneTogether

Reputation: 26708

Use an input shape of (8, 8, 1) and softmax as the activation function for your output layer. Here is a working example:

from sklearn.datasets import load_digits
import tensorflow as tf

digits=load_digits()
digits.keys()
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(digits.images,digits.target)

model1=tf.keras.Sequential([
    tf.keras.layers.Conv2D(filters=32,kernel_size=(3,3),input_shape=(8, 8, 1),activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),

    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(50,activation='relu'),
    tf.keras.layers.Dense(10,activation='softmax')

])
model1.compile(optimizer='SGD',
    loss='sparse_categorical_crossentropy',
             metrics=['accuracy'])
model1.fit(x_train,y_train,epochs=10)

Upvotes: 1

Related Questions