2

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?

Wasswa Samuel
  • 2,139
  • 3
  • 30
  • 54

1 Answers1

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.

MBT
  • 21,733
  • 19
  • 84
  • 102
  • Am actually using Keras version 2.1.6 but I still get a 0 or 1. – Wasswa Samuel May 03 '18 at 09:29
  • Actually I have no concrete idea, why are you getting binary output, but you can consider trying the dummy code I posted above, if you get similar results, to see if it has to do something with your code, or keras itself. Otherwise maybe there are differences between python2 and python3, my code was produced with python3.5. – MBT May 03 '18 at 09:48