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.
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.