'ValueError: Input 0 of layer dense is incompatible with the layer (Newbie Question)
I want to make something with the mnist set but it doesnt work, would be very pleased if someone could help me (https://www.youtube.com/watch?v=Zi4i7Q0zrBs&lc=UgxA2zG7_JXI7JJ3Pjl4AaABAg.9OgAlubwZvO9_ZtCIhrYGb) `
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
# load dataset of handwritten digits
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# normalise training data and cut down between 0 and 255 (greyscale)
x_train = tf.keras.utils.normalize(x_train, axis=1)
x_test = tf.keras.utils.normalize(x_test, axis=1)
# y_values already declared because they are from 0-9
model = tf.keras.models.Sequential()
# flatten layer
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
# all neurons are connected with the layers, units make neurons you wanna have in layer
# 2 dense hidden layers
model.add(tf.keras.layers.Dense(units=128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(units=128, activation=tf.nn.relu))
# output layer
model.add(tf.keras.layers.Dense(units=10, activation=tf.nn.softmax))
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
model.fit(x_train, y_train, epochs=3)
loss, accuracy = model.evaluate(x_test, y_test)
print(accuracy)
print(loss)
model.save("digits.model")
for x in range(1,4):
img = cv.imread(f"{x}.png")[:,:,0]
# invert to make it black and white digits
img = np.invert(np.array([img]))
prediction = model.predict(img)
print(f"The result ist probably: {np.argmax(prediction)}")
plt.imshow(img[0], cmap=plt.cm.binary)
plt.show()
I get this error:
ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 784 but received input with shape [None, 829440]
How do I fix this?
Thanks.
Solution 1:[1]
Probably the training data you are passing to the model are not in (28,28) shape. If they are in (28,28) shape, then it would have an input shape of (28*28) = 784. So try to resize the input images and resize them to (28,28). Though I tried to run your code in Google colab & the training part ran without any error for me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
