I am training a CNN for multiclass image classification into 4 images , what accuracy metric should i use from Keras. My labels are not one hot encoded as I am trying to predict probability of different images.
-
$\begingroup$ What do you mean that you have not one-hot encoded the labels because you want to predict the probabilities? Do you mean the outputs are probabilities instead of categories? $\endgroup$– DaveMay 13 at 18:41
-
$\begingroup$ Yeah , the I want the outputs to be probabilities not a single class , I am working on an emotion classifier whose output I later want to feed to a recommendation algo , as a project , so based on the probabilities of the output emotions , I want to be able to suggest music $\endgroup$– PriyanshuMay 13 at 18:45
2 Answers
For multiclass classification you can simply use a categorical cross entropy loss function. Depending on whether or not the values are one-hot encoded you would have to use either the sparse categorical cross entropy loss or the normal categorical cross entropy loss.
Another option is f1_score which is a combination of precision_score and recall_score. Below is an implementation in keras which you can use:
from tensorflow.keras import backend as K
def f1(y_true, y_pred):
def recall_m(y_true, y_pred):
TP = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
Positives = K.sum(K.round(K.clip(y_true, 0, 1)))
recall = TP / (Positives+K.epsilon())
return recall
def precision_m(y_true, y_pred):
TP = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
Pred_Positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
precision = TP / (Pred_Positives+K.epsilon())
return precision
precision, recall = precision_m(y_true, y_pred), recall_m(y_true, y_pred)
return 2*((precision*recall)/(precision+recall+K.epsilon()))
If you are predicting probabilities for each class, you can use the "categorical_crossentropy" loss function in Keras and set the "softmax" activation function on the output layer of your CNN.
Since your labels are not one-hot encoded, you can use the "sparse_categorical_accuracy" metric in Keras to evaluate the accuracy of your model. This metric will compare the predicted class probabilities to the actual class labels.
Here is an example code snippet:
from tensorflow import keras
model = keras.models.Sequential([
# add your CNN layers here
keras.layers.Dense(4, activation='softmax') # output layer with 4 classes and softmax activation
])
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['sparse_categorical_accuracy'])
Note that even though your labels are not one-hot encoded, Keras will automatically convert them to one-hot encoded format internally when using the "categorical_crossentropy" loss function.