python - Keras Predict_proba 中的神经网络始终返回等于 1 的概率

标签 python tensorflow machine-learning keras neural-network

我正在学习 ML、MNIST 集上的神经网络,但我对 Predict_proba 函数有疑问。我想接收模型做出的预测的概率,但是当我调用函数 Predict_proba 时,我总是收到像 [0, 0, 1., 0, 0, ...] 这样的数组,这意味着模型总是以 100% 的概率进行预测。

您能告诉我我的模型出了什么问题吗?为什么会发生这种情况以及如何解决?

我的模型看起来像:

# Load MNIST data set and split to train and test sets
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

# Reshaping to format which CNN expects (batch, height, width, channels)
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")

# Normalize images from 0-255 to 0-1
train_images /= 255
test_images /= 255

# Use one hot encode to set classes
number_of_classes = 10

train_labels = keras.utils.to_categorical(train_labels, number_of_classes)
test_labels = keras.utils.to_categorical(test_labels, number_of_classes)

# Create model, add layers
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"))

# Compile model
model.compile(loss="categorical_crossentropy", optimizer=Adam(), metrics=["accuracy"])

# Learn model
model.fit(train_images, train_labels, validation_data=(test_images, test_labels), epochs=7, batch_size=200)

# Test obtained model
score = model.evaluate(test_images, test_labels, verbose=0)
print("Model loss = {}".format(score[0]))
print("Model accuracy = {}".format(score[1]))

# Save model
model_filename = "cnn_model.h5"
model.save(model_filename)
print("CNN model saved in file: {}".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))

最佳答案

看看你的代码的这一部分:

# Normalize images from 0-255 to 0-1
train_images /= 255
test_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)

关于python - Keras Predict_proba 中的神经网络始终返回等于 1 的概率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56550889/

相关文章:

python - 了解 Python OpenCV 中的 HoughCircles (cv2)

python - 语音命令android demo提供的tflite格式的模型如何转换?

python - 如何使用经过训练的分类器预测新数据集

python - 随机森林中的超参数调整

performance - 在序列的前零填充和后零填充之间进行选择如何影响结果

python - 获取 "No module named django_messages"

python - 如何在两个数据帧之间找到相同的索引并将其组合到一个新的数据帧,Python 3

machine-learning - TensorFlow:当批处理完成训练后,tf.train.batch 是否会自动加载下一批?

python - PIL Image.resize() 不调整图片大小

tensorflow - 如何在 TensorFlow 中使用可微傅立叶变换?