Skip to content Skip to sidebar Skip to footer

Keras - Get Probability Per Each Class

I'm trying to get the probability per each class out of the keras model. Please find sample keras model below: width = 80 height = 80 model = Sequential() model.add(Conv2D(32, (3,

Solution 1:

The problem is that you are using the 'sparse_categorical_crossentropy' loss with class_mode='binary' in your ImageDataGenerator.

You have two possibilities here:

  1. Change the loss to 'categorical_crossentropy' and set class_mode='categorical'.
  2. Leave the loss as is but set class_mode='sparse'.

Either will work.

Refer to this answer for the difference between the two losses (in Tensorflow, but it holds for Keras too). The short version is that the sparse loss expects labels to be integer classes (e.g. 1, 2, 3...), whereas the normal one wants one-hot encoded vectors (e.g. [0, 1, 0, 0]).

Cheers

EDIT: as @Simeon Kredatus pointed out, it was a normalization issue. This can be easily solved by setting the appropriate flags in the ImageDataGenerator constructors for both training and test sets, namely samplewise_center=True and samplewise_std_normalization=True. Updating the answer so people can see the solution. In general, remember the trash-in-trash-out principle.

Post a Comment for "Keras - Get Probability Per Each Class"