如何在训练了MNIST数据集后,使用Keras中的CNN预测我自己的图像

我已经使用MNIST数据集创建了一个卷积神经网络来预测手写数字,但现在我卡在了如何将我自己的图像作为输入进行预测。我在训练CNN后保存了权重,现在我想用这些权重来预测我自己的图像(注意:我已经确保我的输入图像是28×28的尺寸)。

代码如下:

new_mnist.py :

ap = argparse.ArgumentParser()
ap.add_argument("-s", "--save-model", type=int, default=-1,help="(optional) whether or not model should be saved to disk")
ap.add_argument("-l", "--load-model", type=int, default=-1,help="(optional) whether or not pre-trained model should be loaded")
ap.add_argument("-w", "--weights", type=str,help="(optional) path to weights file")
args  = vars(ap.parse_args())
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load data
print("[INFO] downloading data...")
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# reshape to be [samples][pixels][width][height]
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28).astype('float32')
print(X_test.shape[0])
# normalize inputs from 0-255 to 0-1
X_train = X_train / 255
X_test = X_test / 255
# one hot encode outputs
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
num_classes = y_test.shape[1]
# build the model
print("[INFO] compiling model...")
model = LeNet.build(num_classes = num_classes,weightsPath = args["weights"]
          if args["load_model"] > 0 else None)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
if args["load_model"] < 0:
# Fit the model
print("[INFO] training...")
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=1,
    batch_size=200, verbose=2)
# Final evaluation of the model
print("[INFO] evaluating...")
scores = model.evaluate(X_test, y_test, verbose=0)
print("Baseline Error: %.2f%%" % (100-scores[1]*100))
elif args["load_model"] > 0:
im = imread("C:\\Users\\Divyesh\\Desktop\\mnist.png")
im = im/255
pr = model.predict_classes(im)
print(pr)
# check to see if the model should be saved to file
if args["save_model"] > 0:
print("[INFO] dumping weights to file...")
model.save_weights(args["weights"], overwrite=True)

lenet.py :

class LeNet:
@staticmethod
def build(num_classes,weightsPath = None):
# create model
    model = Sequential()
    model.add(Convolution2D(30, 5, 5, border_mode='valid', input_shape=(1, 28, 28), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Convolution2D(15, 3, 3, activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.2))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dense(50, activation='relu'))
    model.add(Dense(num_classes, activation='softmax'))
    # Compile model
    #model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    if weightsPath is not None:
        model.load_weights(weightsPath)
    return model

在new_mnist.py中,我调用了predict(im),其中im是一个28×28的图像,但在运行这个程序后,我得到了如下错误:

ValueError: Error when checking : expected conv2d_1_input to have 4      dimensions, but got array with shape (28, 28)

求助!!!


回答:

尝试如下方法:

pr = model.predict_classes(im.reshape((1, 1, 28, 28)))

这里:第一个维度来自于示例(即使只有一个示例也需要指定),第二个维度来自于通道(看起来你使用的是Theano后端),其余的是空间维度。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注