MSINT - 图像分类 - 值错误不兼容的形状

MSINT - Image classification - value error incompatible shape

我开始使用 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])

错误:

WARNING:tensorflow:Model was constructed with shape (None, 28, 28) for input KerasTensor(type_spec=TensorSpec(shape=(None, 28, 28), dtype=tf.float32, name='flatten_input'), name='flatten_input', description="created by layer 'flatten_input'"), but it was called on an input with incompatible shape (None, 28).

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, 28)

您应该先调整图像大小,然后再将其传送到您的网络。该模型需要一个形状为 (28,28).

的图像
  • 像这样调整图像大小:img2 = cv2.resize(img2, (28, 28 ))

  • 由于模型需要批量维度,您应该像这样向图像添加另一个维度:z_predict = tf.expand_dims(z_predict,axis=0)

  • 请注意,预测将是每个 class 的概率。如果你想得到class个预测数,你可以使用np.argmax(prediction).

修改后的代码应该是这样的:

import cv2
img2 = cv2.imread("981_cropped.jpg",cv2.IMREAD_GRAYSCALE)
img2 = cv2.resize(img2, (28, 28 ))      #resize image
(thresh, blackAndWhiteImage) = cv2.threshold(img2, 128, 255, cv2.THRESH_BINARY)

z_predict = tf.keras.utils.normalize(blackAndWhiteImage,axis=1)

z_predict = tf.expand_dims(z_predict,axis=0)  # add batch dimension
predictions = model.predict(z_predict)
np.argmax(predictions)  # get predicted class