我正在学习机器学习和在MNIST数据集上的神经网络,我在使用predict_proba函数时遇到了问题。我希望得到模型预测的概率,但当我调用predict_proba函数时,总是得到类似于[0, 0, 1., 0, 0, …]的数组,这意味着模型总是以100%的概率进行预测。
你能告诉我我的模型哪里出了问题,为什么会这样,以及如何修复吗?
我的模型如下:
# 加载MNIST数据集并分成训练集和测试集(train_images, train_labels), (test_images, test_labels) = mnist.load_data()# 重塑为CNN期望的格式(批次,高度,宽度,通道)train_images = train_images.reshape(train_images.shape[0], train_images.shape[1], train_images.shape[2], 1).astype( "float32")test_images = test_images.reshape(test_images.shape[0], test_images.shape[1], test_images.shape[2], 1).astype("float32")# 将图像从0-255归一化为0-1train_images /= 255test_images /= 255# 使用独热编码设置类别number_of_classes = 10train_labels = keras.utils.to_categorical(train_labels, number_of_classes)test_labels = keras.utils.to_categorical(test_labels, number_of_classes)# 创建模型,添加层model = Sequential()model.add(Conv2D(32, (5, 5), input_shape=(train_images.shape[1], train_images.shape[2], 1), activation="relu"))model.add(MaxPooling2D(pool_size=(2, 2)))model.add(Conv2D(32, (3, 3), activation="relu"))model.add(MaxPooling2D(pool_size=(2, 2)))model.add(Dropout(0.5))model.add(Flatten())model.add(Dense(128, activation="relu"))model.add(Dropout(0.5))model.add(Dense(number_of_classes, activation="softmax"))# 编译模型model.compile(loss="categorical_crossentropy", optimizer=Adam(), metrics=["accuracy"])# 训练模型model.fit(train_images, train_labels, validation_data=(test_images, test_labels), epochs=7, batch_size=200)# 测试获得的模型score = model.evaluate(test_images, test_labels, verbose=0)print("模型损失 = {}".format(score[0]))print("模型准确率 = {}".format(score[1]))# 保存模型model_filename = "cnn_model.h5"model.save(model_filename)print("CNN模型已保存至文件: {}".format(model_filename))
我使用PIL和NP来加载图像。我使用Keras的save函数保存模型,并在另一个脚本中使用keras.models的load_model函数加载模型,然后我只需调用
def load_image_for_cnn(filename): img = Image.open(filename).convert("L") img = np.resize(img, (28, 28, 1)) im2arr = np.array(img) return im2arr.reshape(1, 28, 28, 1) def load_cnn_model(self): return load_model("cnn_model.h5") def predict_probability(self, image): return self.model.predict_proba(image)[0]
使用它看起来像这样:
predictor.predict_probability(predictor.load_image_for_cnn(filename))
回答:
请看你代码的这部分:
# 将图像从0-255归一化为0-1train_images /= 255test_images /= 255
你在加载新图像时没有进行这种归一化处理:
def load_image_for_cnn(filename): img = Image.open(filename).convert("L") img = np.resize(img, (28, 28, 1)) im2arr = np.array(img) return im2arr.reshape(1, 28, 28, 1)
对新图像应用与训练集相同的归一化处理是测试任何新图像的要求,如果你不这样做,你会得到奇怪的结果。你可以像下面这样归一化图像像素:
def load_image_for_cnn(filename): img = Image.open(filename).convert("L") img = np.resize(img, (28, 28, 1)) im2arr = np.array(img) im2arr = im2arr / 255.0 return im2arr.reshape(1, 28, 28, 1)