Python 如何改进数字识别模型?

Python 如何改进数字识别模型?,python,tensorflow,machine-learning,deep-learning,keras,Python,Tensorflow,Machine Learning,Deep Learning,Keras,我正在做一个简单的应用程序来实现数字识别。问题是,它在mnist数据集上运行得非常好,但在从谷歌下载的随机图像上却预测得非常糟糕。我应该如何提高模型的准确性 另一个问题:有人建议在模型中添加更多层。如果是,如何向模型中添加更多层 在其中培训模型的文件: from __future__ import print_function import keras from keras.datasets import mnist from keras.models import Sequential fro

我正在做一个简单的应用程序来实现数字识别。问题是,它在mnist数据集上运行得非常好,但在从谷歌下载的随机图像上却预测得非常糟糕。我应该如何提高模型的准确性

另一个问题:有人建议在模型中添加更多层。如果是,如何向模型中添加更多层

在其中培训模型的文件:

from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
import cv2
import matplotlib.pyplot as plt
from keras.models import load_model
import pickle
import h5py
import numpy as np
from keras.callbacks import ModelCheckpoint
batch_size = 128
num_classes = 10
epochs = 12

# input image dimensions
img_rows, img_cols = 28, 28

model=load_model('my_model.h5')



the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print('initial shape')
print('x_test ',x_test.shape)
print('y_test ',y_test.shape)


# print(x_test);
# print(y_test.shape)

if K.image_data_format() == 'channels_first':
    print('reshape1')
    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
    x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
    input_shape = (1, img_rows, img_cols)
else:
    print('reshape2')
    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print('x_test shape: ' , x_test.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

print('x_test final : ')
print(x_test)

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.10))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.20))
model.add(Dense(256,activation='relu'))
model.add(Dropout(0.40))

model.add(Dense(num_classes, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))

model.save('my_model.h5')
score = model.evaluate(x_test, y_test, verbose=0)


# print(x_test.shape)
# print('\n')
# print(y_test.shape)

print('Test loss:', score[0])
print('Test accuracy:', score[1])
下面的代码用于使用我自己的图像测试我的模型

image = cv2.imread("2.jpg")
img_rows, img_cols = 28, 28

x_test1 = cv2.resize(image, (28, 28)) 

x_test1 = cv2.cvtColor(x_test1,cv2.COLOR_RGB2GRAY)


print(x_test1.shape)

if K.image_data_format() == 'channels_first':
    print('reshape1')
    x_test1 = x_test1.reshape(1, 1, img_rows, img_cols)
    input_shape = (1, img_rows, img_cols)

else:
    print('reshape2')
    x_test1 = x_test1.reshape(1, img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)

x_test1 = x_test1.astype('float32')
x_test1 /= 255
y_test1 = np.array([[ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.]])
score = model.evaluate(x_test1, y_test1, verbose=0)
print('done')
print('score of image = ')
print(score[1])
print(score[0])
score=model.predict_classes(x_test1)
print(score)
下面的代码用于加载以前训练过的模型,并从以前的检查点继续训练它。如果有任何错误,请提出建议

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])
filepath="my_model.h5"
checkpoint = ModelCheckpoint(filepath, monitor='acc', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]

score=model.fit(x_test1, y_test1,epochs=12, batch_size=128, callbacks=callbacks_list, verbose=0)


new_model = load_model("my_model.h5")
np.testing.assert_allclose(model.predict(x_test1),
                new_model.predict(x_test1),
                1e-5)


checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [checkpoint]
new_model.fit(x_train, y_train, epochs=12, batch_size=128, callbacks=callbacks_list)

上述代码是否会导致模型过度拟合?如果是,我如何使它更有效,以便它能够预测任何类型的数据?需要帮助

您不应该对这些结果感到惊讶,因为您在一个数据域上训练您的模型,但在另一个数据域上测试它。是的,如果您有一个设计更好的网络,您可能会获得稍好的性能,但这个差距仍然存在

要缩小这一差距,您可以改进以下方面:

  • 改进训练数据:(a)使用更真实的数字数据(例如街景房号()数据集或数据集中的数字)训练模型,以及(b)使用更好的数据增强技术训练模型,例如,将数字样本与随机背景图像混合
  • 调整您的测试数据:预处理测试样本,使其看起来与培训数据中的样本相似,例如,在将测试样本馈送到网络进行预测之前,将其装箱并使其看起来与MNIST相似
  • <强>通过显式地考虑真实数据的变化来改进你的模型旋转/代码>测试数据会降低你的模型性能,那么你可以通过在预测数字旋转中添加一个侧任务来考虑模型训练中的这个因素;或者您认为一个好的模型应该只预测数字,而不是该训练样本所属的数据集,那么您可以添加一个对抗性任务,迫使网络忘记这些信息
    您的代码几乎与相同。你自己尝试过什么?Maxim这是一个非常标准的初学者示例,我想了解如何添加层以提高准确性。我计划很快完成我自己的项目,我想清楚我的概念。非常感谢你的帮助!我会按照你说的做,我想知道使用SVM是否有助于提高准确性?