In this work, we propose a metric-inspired loss function, based on the Kappa index. Unlike the Dice loss, a standard loss used in image segmentation CNN, the Kappa loss takes into account all the pixels in the image, including the true negative.
👉 The method has been accepted in ISBI2020, Slides.
👉 The paper can be viewed on ResearchGate.
👉 Formula:
👉 Code:
import tensorflow as tf
def Kappa_loss(y_true, y_pred, N=224*224):
Gi = tf.reshape(y_true, shape=(-1,))
Pi = tf.reshape(y_pred, shape=(-1,))
numerator = 2 * tf.reduce_sum(Pi * Gi) - tf.reduce_sum(Pi) * tf.reduce_sum(Gi) / N
denominator = tf.reduce_sum(Pi * Pi) + tf.reduce_sum(Gi * Gi) - 2 * tf.reduce_sum(Pi * Gi) / N
loss = 1 - numerator / denominator
return loss
import keras
from keras import backend as K
def Kappa_loss(y_true, y_pred, N=224*224):
Gi = K.flatten(y_true)
Pi = K.flatten(y_pred)
numerator = 2 * K.sum(Pi * Gi) - K.sum(Pi) * K.sum(Gi) / N
denominator = K.sum(Pi * Pi) + K.sum(Gi * Gi) - 2 * K.sum(Pi * Gi) / N
loss = 1 - numerator / denominator
return loss
import torch
def Kappa_loss(outputs, targets, N=224*224):
outputs = outputs.view(-1)
targets = targets.view(-1)
numerator = 2 * (outputs * targets).sum() - outputs.sum() * targets.sum / N
denominator = (outputs * outputs).sum() + (targets * targets).sum() - 2 * (outputs * targets).sum() / N
loss = 1 - numerator / denominator
return loss
- Python 3.*
- Tensorflow or Keras or Pytorch
@inproceedings{zhang2020kappa,
title={Kappa loss for skin lesion segmentation in fully convolutional network},
author={Zhang, Jing and Petitjean, Caroline and Ainouz, Samia},
booktitle={2020 IEEE 17th International Symposium on Biomedical Imaging (ISBI)},
pages={2001--2004},
year={2020},
organization={IEEE}
}