使用model.predict()与keras一起输出的概率得分 [英] Output Probability score with keras using model.predict()

查看:724
本文介绍了使用model.predict()与keras一起输出的概率得分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于图像分类的cnn模型,该模型使用S型激活函数作为最后一层

I have a cnn model for image classification which uses a sigmoid activation function as its last layer

    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'))

图像属于两个类别.当我在图像上使用model.predict()时,我得到0或1.但是,例如,当我使用model.predict_generator()时,我想要获得像0.656这样的概率得分,则输出这些得分.但是,predict_generator要求将图像放置在标识其类的文件夹中,因此,它仅与验证和测试有关.我想为一个或多个新的未知图像输出此分数.我该怎么办?

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. However, predict_generator requires that the images are placed in folders that identify their classes, therefore, it is only relevant for validation and testing. 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))

产生以下输出:

[[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

真的很奇怪,您只能得到0和1的二进制输出.特别是当Sigmoid层实际上返回浮点值时.

Really weird that you get only a binary output of 0 and 1. Especially as the sigmoid layer actually returns float values.

我希望这会有所帮助.

这篇关于使用model.predict()与keras一起输出的概率得分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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