我在使用MNIST尝试Keras时遇到了问题。我有一个保存的模型,其准确度超过99%但是当我使用它来预测一些图像时,它总是预测为1.我认为这是由于我在测试中以错误的方式重塑了图像数据输入。 py文件。
我收到了错误:
ValueError: Error when checking : expected conv2d_1_input to have 4 dimensions, but got array with shape (28, 28)
或者,如果我尝试随机重塑(1,1,28,28),我会收到此错误:
ValueError: Error when checking : expected conv2d_1_input to have shape (None, 28, 28, 1) but got array with shape (1, 1, 28, 28)
所以我尝试在image_to_data函数中添加以下内容:
image_data = image_data.reshape((1, 28, 28, 1))
现在代码运行但始终预测相同的值。如何将28 x 28像素的图像数据重新整形为适合模型中第一层的图像数据,以正确方式预测一个图像的类别?
train.py
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
batch_size = 128
num_classes = 10
epochs = 20
# input image dimensions
img_rows, img_cols = 28, 28
# the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
if K.image_data_format() == 'channels_first':
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:
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_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# 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.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'])
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
# serialize model to YAML
model_yaml = model.to_yaml()
with open("model-new.yaml", "w") as yaml_file:
yaml_file.write(model_yaml)
# serialize weights to HDF5
model.save_weights("model-new.h5")
print("Saved model to disk")
test.py
from PIL import Image
from keras.models import model_from_yaml
import numpy as np
def load_model():
# load YAML and create model
yaml_file = open('model.yaml', 'r')
model_yaml = yaml_file.read()
yaml_file.close()
model = model_from_yaml(model_yaml)
# load weights into new model
model.load_weights("model.h5")
print("Loaded model from disk")
model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
return model
def image_to_data(image):
image_data = np.array(image) / 255
image_data = image_data.reshape((1, 28, 28, 1))
return image_data
def predict(model, image):
data = image_to_data(image)
prediction = model.predict_classes(data)
return prediction
def predict_image(model, filename):
image = Image.open(filename)
data = image_to_data(image)
prediction = predict(model, data)
return prediction
model = load_model()
print(predict_image(model, '3.png'))
print(predict_image(model, '6.png'))
print(predict_image(model, '8.png'))
答案 0 :(得分:1)
可能出现的问题:
image_data.max()
和x_train.max()
)x_train
的图像并绘制image_data
。看看颜色是否倒置。或尝试使用image_data = 1 - image_data
进行预测。 image_data = numpy.swapaxes(image_data,1,2)