Hi,
I wanted to calculate class level IoU for my Unet semantic segmentation model. But I think I am not calculating it right. I am using the below code:
def test(dataset, model, device, test_loader):
flaga = False
# set the model to evaluation
model.eval()
iou_score = 0
y_testiList = []
y_prediList = []
#predList = np.array([])
predList = []
#groundList = np.array([])
groundList = []
with torch.no_grad():

    for _data, _target in test_loader:
        # convert from 3-channel image to 1-channel image 
        _target = _target.sum(3, keepdim=True)
        # send data to evice
        _data, _target = _data.to(device), _target.to(device)
        # forward
        output = model.forward(_data)
        
        pred = output.argmax(3, keepdim=True)
        
        # convert to numpy
        _target = _target.cpu().numpy()
        pred = pred.cpu().numpy()
        iou_score += calculate_iou(pred, _target)

        groundList = np.append(groundList,_target)

        predList = np.append(predList,pred)


print(predList.shape,groundList.shape)

Yi = groundList
print(Yi[1])
y_predi = predList
print(y_predi[1])
IoUs = []
#Nclass = int(np.max(Yi)) + 1
Nclass = 5
for c in range(Nclass):
  TP = np.sum( (Yi == c)&(y_predi==c) )
  FP = np.sum( (Yi != c)&(y_predi==c) )
  FN = np.sum( (Yi == c)&(y_predi != c)) 
  IoU = TP/float(TP + FP + FN)
  print("class {:02.0f}: #TP={:6.0f}, #FP={:6.0f}, #FN={:5.0f}, IoU={:4.3f}".format(c,TP,FP,FN,IoU))
  IoUs.append(IoU)
mIoU = np.mean(IoUs)
#print("_________________")
print("Mean IoU: {:4.3f}".format(mIoU))

riou_score = mIoU
iou_score /= len(test_loader.dataset)
print("mIoU score : ",iou_score)

return iou_score

I am getting results where I find only the first class IoU. But for other classes I am not getting any IoU. Result is given below:
class 00: #TP=   698, #FP=    16, #FN=74459, IoU=0.009
class 01: #TP=     0, #FP=    81, #FN= 3941, IoU=0.000
class 02: #TP=     0, #FP=     0, #FN= 2590, IoU=0.000
class 03: #TP=     0, #FP=     0, #FN= 1699, IoU=0.000
class 04: #TP=     0, #FP=     0, #FN= 1293, IoU=0.000
I am not sure how to interpret it and how can I get class level IoU. Any help would be highly appreciated.
Thanks in advance
Razin Farhan Hussain