model.predict()返回类而不是概率

时间:2018-02-05 11:41:56

标签: python machine-learning keras

您好!

我第一次使用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

1 个答案:

答案 0 :(得分:-2)

你已经有了概率:)

看看你的清单:

[[5.571262e-21 0.000000e+00 1.000000e+00]]

,概率为:

0, 0, 1

顺便说一下,它看起来像过度拟合。查看此页面: https://www.kdnuggets.com/2015/04/preventing-overfitting-neural-networks.html