model.predict()返回类而不是概率 [英] model.predict() returns classes and not probabilities

查看:1431
本文介绍了model.predict()返回类而不是概率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

我是第一次使用Keras. 我训练并保存了模型. (也是json文件及其权重) 该模型旨在将图像分类为3类. 我的编译方法:

I am using Keras for first time. I trained and saved a model. (as a json file and its weights too) The model it is intended to classify an image among 3 classes. My compile method :

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

此后,我加载模型及其权重,并尝试对随机图像进行预测

After that, I load the model and its weights and i try to make a prediction for a random image

# 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%] 目前,我得到的输出是

How to i get a list with the probabilities? For example [75% 15% 10%] Currently i get as an output

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

这是模型摘要print(loaded_model.summary()) 模型已成功从磁盘加载!

This is the model summary print(loaded_model.summary()) Model successfully loaded from disk!

_________________________________________________________________
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

推荐答案

您已经拥有了概率:)

查看您的列表:

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

和概率是:

0, 0, 1

顺便说一句,过度拟合.检查此页面: https://www.kdnuggets.com/2015/04/Prevention-overfitting-neural-networks.html

Btw, it looks like the overfitting. Check this page: https://www.kdnuggets.com/2015/04/preventing-overfitting-neural-networks.html

这篇关于model.predict()返回类而不是概率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆