代码之家  ›  专栏  ›  技术社区  ›  Aparajit Garg

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

  •  -1
  • Aparajit Garg  · 技术社区  · 6 年前

    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)
    

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

    1 回复  |  直到 6 年前
        1
  •  2
  •   pitfall    6 年前

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

    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)
    

    normalize 沿着第一轴意味着什么?规范化每一行?这很奇怪。正确的归一化方法是(1)对输入图像进行矢量化,即每个图像成为一个矢量;和(2) 正常化 结果向量(轴=1)。

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

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

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

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

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

    • 对测试图像进行二值化,即转换为0/1图像
    • 集中测试图像,即移动图像,使其质量中心为图像中心。

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