mnis在自己的数字图像数据集中无法获得准确度

2024-04-26 19:09:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我是机器学习的新手,我尝试了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)

你能帮我解决我的问题吗。你知道吗


Tags: fromioimportimgmodeltftrainarray
3条回答

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

<强>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样本中,几乎是二值化的,即0s或1s。但是,在灰度图像中,您可能会遇到[0255]中的值,这是一个完全不同的分布。你知道吗

您可以尝试简单的(0,1)规范化,即

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

但这需要你重新训练一种新的模式。你知道吗

3岁。应用域自适应技术

这意味着您需要在将测试图像馈送到模型之前(甚至在标准化之前)执行以下操作。你知道吗

  • 对测试图像进行二值化,即转换为0/1图像
  • 否定测试图像,即0到1和1到0
  • 集中测试图像,即移动图像,使其质量中心为图像中心。你知道吗

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

相关问题 更多 >