【发布时间】:2021-06-20 08:19:49
【问题描述】:
我开始使用 keras 进行图像分类。尝试了一个简单的 minst 数据集来检测图像中的数字。运行模型。但是我想在我自己的数据集上测试模型并面临一些问题。
import tensorflow as tf
import matplotlib.pyplot as plt
msint = tf.keras.datasets.mnist #28x28 images of hand written digits 0-9
(x_train, y_train), (x_test,y_test) = msint.load_data()
x_train = tf.keras.utils.normalize(x_train,axis=1)
x_test = tf.keras.utils.normalize(x_test,axis=1)
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
model.fit(x_train,y_train, epochs=3)
#Testing on my own image data
img2 = cv2.imread("981_cropped.jpg",cv2.IMREAD_GRAYSCALE)
(thresh, blackAndWhiteImage) = cv2.threshold(img2, 128, 255, cv2.THRESH_BINARY)
z_predict = tf.keras.utils.normalize(blackAndWhiteImage,axis=1)
predictions = new_model.predict([z_predict])
错误:
警告:tensorflow:模型是用形状 (None, 28, 28) 构造的 输入 KerasTensor(type_spec=TensorSpec(shape=(None, 28, 28), dtype=tf.float32, name='flatten_input'), name='flatten_input', description="created by layer 'flatten_input'"),但它被调用了 形状不兼容的输入(无,28)。
ValueError: Input 0 of layer dense is in compatible with the layer: 输入形状的预期轴 -1 具有值 784 但接收到输入 带形状(无,28)
【问题讨论】:
-
您的图像只有一个值为 28 的轴,而模型需要尺寸为 (None, 28,28) 的图像。
标签: python tensorflow image-classification