【问题标题】:Keras + mnist + test own images. Bad predictionKeras + mnist + 测试自己的图像。糟糕的预测
【发布时间】:2019-06-28 09:07:33
【问题描述】:

通过测试 mnist 自己的测试图像可以正常工作,但是一旦我使用来自外部 mnist 的图像,它就会预测错误。我什至尝试从 mnist 数据集中复制其中一张图像,但它仍然无法预测正确的数字(即使在 mnist 数据集中,完全相同的图像也可以(预测))。

有人能看出我做错了什么吗?我猜图片的尺寸或形状有些问题。

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Dropout, Flatten, MaxPooling2D
import cv2 as cv

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
input_shape = (28, 28, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
# Normalizing the RGB codes by dividing it to the max RGB value.
x_train /= 255
x_test /= 255

# -------------------------- CREATE MODEL ------------------------------
'''
model = Sequential()
model.add(Conv2D(28, kernel_size=(3,3), input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten()) # Flattening the 2D arrays for fully connected layers
model.add(Dense(128, activation=tf.nn.relu))
model.add(Dropout(0.2))
model.add(Dense(10,activation=tf.nn.softmax))

# ----------------------------------------------------------------------

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
model.fit(x=x_train,y=y_train, epochs=1)

# ----------------------------------------------------------------------
'''
model = tf.keras.models.load_model("C:/Users/A551110/PycharmProjects/keras_mnist/venv/mnistv2.model")
file = "C:/Users/A551110/Documents/images/7.png"
model.evaluate(x_test, y_test)

image = cv.imread(file, cv.IMREAD_GRAYSCALE)
image = cv.resize(image, (28,28))
image = 255-image          #inverts image. Always gets read inverted.

plt.imshow(image.reshape(28, 28),cmap='Greys')
plt.show()
pred = model.predict(image.reshape(1, 28, 28, 1), batch_size=1)

print(pred.argmax())

我试过pred = model.predict(image.reshape(1, 28, 28, 1))

还有pred = model.predict_classes(image.reshape(1, 28, 28, 1))

The digits i was predicting. Upper one from mnist dataset (predicted correctly) and one lower one copied and put in (predicted wrongly)

【问题讨论】:

    标签: python tensorflow keras mnist


    【解决方案1】:
    import cv2
    import matplotlib.pyplot as plt
    %matplotlib inline
    import numpy as np
    
    y=cv2.imread("/content/download.png")   #image outside mnist data
    y1=cv2.resize(y,(28,28))                #you need to resize it on the bsis pf your  modeL's image shape
    plt.imshow(y1)
    
    temp = cv2.cvtColor(y1,cv2.COLOR_BGR2YCrCb)  #since its a three channel image i hav econverted into this so rbg are represented in the luminance one 
    temp=255-temp                                #negative image
    plt.imshow(temp)
    
    print(temp.shape)
    
    Y = np.zeros((temp.shape[0], temp.shape[1],1), dtype=float)    #array of (28,28,1)
    Y[:,:,0] = temp[:, :,0].astype(float) / 255           #fitting the data of temp image in that zeros and normalizing it
    yh= model.predict_classes(Y.reshape(1,28,28,1))       #finally the value of image
    yh
    

    【讨论】:

      【解决方案2】:

      我想通了。我没有通过这段代码得到正确的标准化值。

      image = cv.imread(file, cv.IMREAD_GRAYSCALE)
      image = cv.resize(image, (28,28))
      image = 255-image     
      

      相反,我不得不用底部的除法来纠正它(这里在底部),我在之前的尝试中错误地把它放在了 image = 255-image 之前。这是错误之一,以及缺少将类型转换为 float32 以使其能够正常化,以及中间的重塑。

      image = cv.imread(file, cv.IMREAD_GRAYSCALE)
      image = cv.resize(file, (28, 28))
      image = image.astype('float32')
      image = image.reshape(1, 28, 28, 1)
      image = 255-image
      image /= 255
      

      【讨论】:

        【解决方案3】:

        一些猜测

        1) 您对训练和测试数据进行了标准化。我假设您可能忘记在进行预测之前对输入数据进行标准化?

        x_train /= 255
        x_test /= 255
        

        2) 您是否验证模型已正确加载?保存和加载后,验证它在测试集上仍然执行相同的操作。如果结果不好,说明权重没有正确加载。

        3) tf.keras.datasets.mnist.load_data() 提供的数据集是否有任何预处理(在您自己的规范化之外)?如果是这样,您必须在推理之前使用相同的转换对自己的输入数据进行预处理

        【讨论】:

        • 2) 在 mnist 数据集上测试时(获得正确预测时),模型的加载方式完全相同,所以我不认为权重加载错误。 3) 不太确定。根据this thread,大小与训练样本不同。我不知道它是否说明以某种方式进行了预处理。
        • 如果您要在训练结束时打印一些权重,加载后它们是否相同?
        猜你喜欢
        • 2020-04-19
        • 2020-08-12
        • 2020-11-24
        • 1970-01-01
        • 2017-03-03
        • 2019-06-13
        • 2015-06-21
        • 2021-04-08
        • 2023-03-10
        相关资源
        最近更新 更多