【问题标题】:Can't get accuracy in own digit image dataset for mnist无法在自己的数字图像数据集中获得 mnist 的准确性
【发布时间】:2018-10-25 20:41:15
【问题描述】:

我是机器学习的新手,我尝试了 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 张图像,但没有一张被正确预测。 下面是代码:

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)

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

【问题讨论】:

  • 您的自定义数据看起来像 MNIST 吗?
  • 准确度是否为 0%?在这种情况下,您的图像格式可能完全不同。也许您传递的图像与训练过的图像具有不同的 RGB 值。

标签: python tensorflow neural-network keras mnist


【解决方案1】:

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

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

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

2。仔细检查您的 L2 标准化

我看了一下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 得到的向量(在轴=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
  • 集中您的测试图像,即移动您的图像,使其重心为图像中心。

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

【讨论】:

    猜你喜欢
    • 2018-08-06
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    • 2021-04-22
    • 2016-11-13
    • 1970-01-01
    相关资源
    最近更新 更多