无法获得 mnist 自己的数字图像数据集的准确性

Can't get accuracy in own digit image dataset for mnist

我是机器学习的新手,我尝试了 mnist 数据集,得到了大约 97% 的准确率,但后来我尝试处理我的图像数据集,得到的准确率为 0%。请帮帮我。

这是 97% 准确率的模型代码:

from keras.models import Sequential                  
from keras.layers import Dense, Dropout, Conv2D, Flatten  
from keras.callbacks import ModelCheckpoint               


x_train = tf.keras.utils.normalize(x_train, axis =1)
x_test = tf.keras.utils.normalize(x_test, axis = 1)

model = Sequential()
model.add(Flatten())
model.add(Dense(128, activation = 'relu')) 
model.add(Dense(128, activation = 'relu'))
model.add(Dense(10, activation = 'softmax')) 


model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])

checkpointer = ModelCheckpoint(filepath = 'mnist.model.weights.best.hdf5',verbose = 1,save_best_only = True, monitor = 'loss')

model.fit(x_train, y_train, epochs = 3, callbacks = [checkpointer], 
          batch_size = 32,verbose = 2,shuffle = True)

现在我尝试了 10 张图像,其中 none 被正确预测。 下面是代码:

from skimage import io
from skimage import color
import numpy as np
import tensorflow as tf
import keras

img_0 = color.rgb2gray(io.imread("0.jpg"))
img_2 = color.rgb2gray(io.imread("2.jpg"))
img_3 = color.rgb2gray(io.imread("3.jpg"))
img_4 = color.rgb2gray(io.imread("4.jpg"))
img_5 = color.rgb2gray(io.imread("5.jpg"))
img_6 = color.rgb2gray(io.imread("6.jpg"))
img_7 = color.rgb2gray(io.imread("7.jpg"))
img_8 = color.rgb2gray(io.imread("8.jpg"))
img_9 = color.rgb2gray(io.imread("9.jpg"))
array = [img_0, img_2, img_3, img_4, img_5, img_6, img_7, img_8, img_9]

#normalized the data between 0-1
array = tf.keras.utils.normalize(array, axis = 1)

#used the loop to increase the dimensions of the input layer as 1,28,28 which will be converted into 1*784
for i in array:
    i = np.expand_dims(i,axis = 0)
    print(i.shape)


new_model = tf.keras.models.load_model('mnist_save.model')
new_model.load_weights('mnist.model.weights.best.hdf5')
predictions = new_model.predict(array)

你能帮我解决我的问题吗?

如果我是你,我会检查以下三件事。

1.并排可视化训练和测试数据

这是查看低性能是否合理的最简单方法。基本上,如果测试数据与训练数据看起来非常不同,那么您的预训练模型就无法在这个新的测试域中实现高性能。即使情况并非如此,可视化应该有助于决定可以应用哪些简单的域自适应来实现更好的性能。

2。仔细检查你的 L2normalization

我看了一下keras.utils.normalize

的源码
@tf_export('keras.utils.normalize')
def normalize(x, axis=-1, order=2):
  """Normalizes a Numpy array.
  Arguments:
      x: Numpy array to normalize.
      axis: axis along which to normalize.
      order: Normalization order (e.g. 2 for L2 norm).
  Returns:
      A normalized copy of the array.
  """
  l2 = np.atleast_1d(np.linalg.norm(x, order, axis))
  l2[l2 == 0] = 1
  return x / np.expand_dims(l2, axis)

由于您使用的是 tensorflow 后端,因此 normalize 沿着第一轴意味着什么?规范化每一行?这很奇怪。进行归一化的正确方法是(1)将输入图像矢量化,即每个图像都变成一个矢量;和 (2) normalize 结果向量(在 axis=1)。

实际上,这有点不合适,尤其是当您想在不同领域应用预训练模型时。这是因为 L2 归一化对非零值更敏感。在 MNIST 样本中,几乎是二值化的,即 0 或 1。但是,在灰度图像中,您可能会遇到 [0,255] 中的值,这是完全不同的分布。

您可以尝试简单的 (0,1) 归一化,即

x_normalized = (x-min(x))/(max(x)-min(x))

但这需要您重新训练一个新模型。

3。应用领域适应技术

这意味着您需要在将测试图像输入模型之前(甚至在归一化之前)执行以下操作。

  • 二值化您的测试图像,即转换为 0/1 图像
  • 否定您的测试图像,即将 0 变为 1,将 1 变为 0
  • 集中您的测试图像,即移动您的图像,使其质心成为图像中心。

当然,应用何种技术取决于您在可视化结果中观察到的领域差异。