我正在制作一个简单的应用程序来实现数字识别。问题是它在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)
上述代码是否会导致模型过度拟合?如果是,我如何使其更有效率,以便它能够预测任何类型的数据?需要帮助!!
答案 0 :(得分:0)
您不应对这些结果感到惊讶,因为您在一个数据域上训练您的模型,但在另一个数据域上测试它。是的,如果你有一个设计得更好的网络,你可能会获得更好的性能,但这个差距仍然存在。
要缩小这一差距,您可以改进以下方面:
rotation
会降低您的模型性能,那么您可以考虑这个因素通过在预测数字旋转中添加辅助任务来进行模型训练;或者您认为一个好的模型应该只预测数字而不是该训练样本所属的数据集,那么您可以添加一个对抗性任务来强制网络忘记这些信息。