Output Probability score with keras using model.predict()

别等时光非礼了梦想. 提交于 2021-02-10 04:59:46

问题


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

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?


回答1:


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.

I hope this helps somehow.



来源:https://stackoverflow.com/questions/50115762/output-probability-score-with-keras-using-model-predict

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!