【发布时间】:2018-07-15 06:22:36
【问题描述】:
你好!
我是第一次使用 Keras。 我训练并保存了一个模型。 (作为 json 文件及其权重) 该模型旨在将图像分类为 3 类。 我的编译方法:
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
之后,我加载模型及其权重,并尝试对随机图像进行预测
# Predicting images
img =image.load_img('/path/to/image/index.jpeg', target_size=(224, 224))
x = image.img_to_array(img)
#normilize the array output
x *= (255.0/x.max())
image = np.expand_dims(x, axis = 0)
image = preprocess(image)
preds = loaded_model.predict(image,)
pred_classes = np.argmax(preds)
print(preds)
print(pred_classes)
如何获得概率列表?
例如[75% 15% 10%]
目前我得到了输出
[[5.571262e-21 0.000000e+00 1.000000e+00]]
2
这是模型摘要print(loaded_model.summary())
模型已成功从磁盘加载!
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 222, 222, 64) 1792
_________________________________________________________________
activation_1 (Activation) (None, 222, 222, 64) 0
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 111, 111, 64) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 109, 109, 64) 36928
_________________________________________________________________
activation_2 (Activation) (None, 109, 109, 64) 0
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 54, 54, 64) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 52, 52, 128) 73856
_________________________________________________________________
activation_3 (Activation) (None, 52, 52, 128) 0
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 26, 26, 128) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 24, 24, 256) 295168
_________________________________________________________________
activation_4 (Activation) (None, 24, 24, 256) 0
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 12, 12, 256) 0
_________________________________________________________________
conv2d_5 (Conv2D) (None, 10, 10, 512) 1180160
_________________________________________________________________
activation_5 (Activation) (None, 10, 10, 512) 0
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 5, 5, 512) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 12800) 0
_________________________________________________________________
dense_1 (Dense) (None, 512) 6554112
_________________________________________________________________
activation_6 (Activation) (None, 512) 0
_________________________________________________________________
dropout_1 (Dropout) (None, 512) 0
_________________________________________________________________
dense_2 (Dense) (None, 3) 1539
_________________________________________________________________
activation_7 (Activation) (None, 3) 0
=================================================================
Total params: 8,143,555
Trainable params: 8,143,555
Non-trainable params: 0
【问题讨论】:
-
你试过
.predict_proba()而不是predict()吗? -
是的,我得到了完全相同的输出!
-
您的输出在我看来确实像概率。这些值看起来像您在最后一层使用 softmax。这些导致很小的数字。只需将这些数字乘以 100 并四舍五入,即可得到百分比
-
我知道这看起来像是概率,但实际上它们是 [0 0 1] 我像 predict_classes 一样工作。我正在寻找更好的概率。就像我说的 [0.7 0.15 0.1]。同样作为最后一层,我使用 sigmoid
-
您显示的输出是概率。值 0、0、1 仅表示您的模型 100% 确信该样本属于类 2。为了获得更好的概率,您需要确保您的模型没有过度拟合并且有足够的数据来训练它。
标签: python machine-learning keras