2

I have a dataset which consists of 5 classes, but the images in the dataset vary in the number of classes appearing in it. For example, one image has all 5 classes and another one only has 2 or 3 classes.

I am calculating the mean IOU for the segmentation of each image. The way I do is to calculate the IoU for each class and then average them to get the mean IOU of the image. Here's the reference of the IOU calculation: 1 2 (https://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=8569372&tag=1).

I'm pretty new to MATLAB and I am stuck at the part where I have to write the MATLAB code to average the IOU score of each class. How should I write the code to calculate the mean IOU, but excluding the classes that aren't in the ground-truth? (The predicted image might predict class labels that aren't in the ground-truth).

I have calculate the confusion matrix and the IOU for each class like this:

% Confusion matrix calculation 
confusion_matrix = zeros(5,5);
classes = 1:5;
for p = 1:length(classes)
    % Find the predicted labels of samples with class classes(p)
    indx = find(oned_gt == classes(p));
    for q = 1:length(classes)
        confusion_matrix(q, p) = sum(oned_pred(indx) == classes(q));
    end
end

% Calculate IoU score for each class in each image
iou_score = zeros(length(classes), no_images);
for j = 1:length(class)
    iou_score(j, i) = confusion_matrix(j,j)/(sum(confusion_matrix(:,j))+sum(confusion_matrix(j,:))-confusion_matrix(j,j));
end

Note:

  • oned_gt: the ground-truth of the image, reshaped into the number of pixels x 1
  • oned_pred: the predicted labels, also reshaped into the number of pixels x 1
  • no_images: the total number of images

Thank you very much in advance for your help. Feel free to comment if you need me to clarify anything.

lisabui
  • 21
  • 3

0 Answers0