【问题标题】:MSINT - Image classification - value error incompatible shapeMNIST - 图像分类 - valueerror 不兼容的形状
【发布时间】: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)

【问题讨论】:

标签: python tensorflow image-classification


【解决方案1】:

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

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

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

  • 请注意,预测将是每个类别的概率。如果要获取预测的类号,可以使用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

【讨论】:

    猜你喜欢
    • 2021-05-12
    • 2021-05-29
    • 2021-10-19
    • 2016-10-23
    • 2021-02-23
    • 1970-01-01
    • 2022-10-24
    • 2023-02-01
    • 1970-01-01
    相关资源
    最近更新 更多