简体   繁体   English

使用model.predict()与keras一起输出的概率得分

[英]Output Probability score with keras using model.predict()

I have a cnn model for image classification which uses a sigmoid activation function as its last layer 我有一个用于图像分类的cnn模型,该模型使用S型激活函数作为最后一层

    from keras import layers
    from keras import models
    model = models.Sequential()
    model.add(layers.Conv2D(32, (3, 3), activation='relu',
                    input_shape=(1500, 1500, 3)))
    ..........
    model.add(layers.Dense(1, activation='sigmoid'))

The images belong to two classes. 图像属于两类。 When I use the model.predict() on an image I get a 0 or a 1. However I want to get a probability score like 0.656 for example when I use model.predict_generator() , it outputs these scores. 当我在图像上使用model.predict()时,我得到0或1。但是,例如,当我使用model.predict_generator() ,我想要获得像0.656这样的概率得分,则输出这些得分。 However, predict_generator requires that the images are placed in folders that identify their classes, therefore, it is only relevant for validation and testing. 但是, predict_generator要求将图像放在标识其类的文件夹中,因此,它仅与验证和测试有关。 I want to output this score for a new unknown image or images. 我想输出一个新的未知图像的分数。 How can I do this? 我怎样才能做到这一点?

I'm not sure if this is a version issue, but I do get probability scores. 我不确定这是否是版本问题,但是我确实获得了概率分数。

I used a dummy network to test the output: 我使用了一个虚拟网络来测试输出:

from keras import layers
from keras import models
from keras import __version__ as used_keras_version
import numpy as np


model = models.Sequential()
model.add(layers.Dense(5, activation='sigmoid', input_shape=(1,)))
model.add(layers.Dense(1, activation='sigmoid'))
print((model.predict(np.random.rand(10))))
print('Keras version used: {}'.format(used_keras_version))

Yields to the following output: 产生以下输出:

[[0.252406  ]
 [0.25795603]
 [0.25083578]
 [0.24871194]
 [0.24901393]
 [0.2602583 ]
 [0.25237608]
 [0.25030616]
 [0.24940264]
 [0.25713784]]
Keras version used: 2.1.4

Really weird that you get only a binary output of 0 and 1. Especially as the sigmoid layer actually returns float values. 真的很奇怪,您只能得到0和1的二进制输出。特别是当Sigmoid层实际上返回浮点值时。

I hope this helps somehow. 我希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM