Search code examples
pythondeep-learningneural-networkconv-neural-networkclassification

Finding probability of a class in multiclass classification


I have made a Tf/Keras CNN Model which is a binary classifier. The model classifies if an image belongs to Class A or Class B. How do I make the model return if an image is a certain percent of Class A and a certain percent of Class B.

This is how I want my outputs.

output_image

Kindly help.


Solution

  • Since it's a binary classifier, we have the output layer's activation function as Sigmoid. In sigmoid, the outcome value ranges from 0 to 1. If the outcome is the probability for Class A is x, then the probability for Class B is 1-x.

    Therefore the code would be,

    pred = model.predict(img)
    pred = pred.flatten().flatten()
    classA_percent = float(pred)
    classB_percent = float(1.0-pred)
    pred_class = (pred > 0.5).astype(int)