Sematic Segmentation metric

Hi all
I just want to calculate the semantic segmentation metric values like : pixel accuracy, mIoU
and Kappa metric
and I found some code and then I adjust it as follows:
my question is:
are these functions ok to calculate the above metric? if not can you suggest me any thing else

from sklearn.metrics import jaccard_score,cohen_kappa_score
def pixel_accuracy(Pred, ground_truth):

  • with torch.no_grad():*
  •    Pred = torch.argmax(Pred, dim=1) #dim =1 => over rows *
    
  •    correct = torch.eq(Pred, ground_truth).int()  # the o/p here is 0 , 1 *
    
  •    accuracy = float(correct.sum()) / float(correct.numel())*
    
  • return accuracy*

def mIoU(pred, ground_truth, smooth=1e-10, n_classes=5):

  • with torch.no_grad():*

  •    # normalize the output becuse (we have identity activation functions) linear (same output)*
    
  •    pred         = torch.argmax(Pred, dim=1)*
    
  •    pred         = pred.cpu().contiguous().view(-1).numpy() # make it 1D *
    
  •    ground_truth = ground_truth.cpu().contiguous().view(-1).numpy() # make it 1D *
    
  •    MIoU=jaccard_score(ground_truth, pred,average='macro') #'none' per class *
    
  •    return MIoU*
    

def kapp_score_Value(pred, ground_truth):

  • with torch.no_grad():*
  •    pred              = torch.argmax(Pred, dim=1)*
    
  •    pred              = pred.cpu().contiguous().view(-1).numpy() # make it 1D *
    
  •    ground_truth      = ground_truth.cpu().contiguous().view(-1).numpy() # make it 1D *
    
  •    *
    
  •    kapp_score = cohen_kappa_score(ground_truth, pred_mask)       *
    
  •    *
    
  • return kapp_score*

*in mean code: *
*I just call them like this : *
pred = model(image)

acc = pixel_accuracy(Pred, ground_truth)
MiouVal = mIoU(Pred, ground_truth)
kappavalue = kapp_score_Value(Pred, ground_truth)

I think your functions will work fine for evaluation, but I do not think will not work if you want to use them for training (gradient optimization). I found Loss Function Library - Keras & PyTorch | Kaggle very helpful. You can also modify these functions, e.g. threshold for IoU and add a multiclass version for mIoU, and use them for faster evaluation on GPU.

1 Like

Thank you so much
i do not need the metric as loss, i just want to calculate it

1 Like