Tramac / awesome-semantic-segmentation-pytorch

Semantic Segmentation on PyTorch (include FCN, PSPNet, Deeplabv3, Deeplabv3+, DANet, DenseASPP, BiSeNet, EncNet, DUNet, ICNet, ENet, OCNet, CCNet, PSANet, CGNet, ESPNet, LEDNet, DFANet)
Apache License 2.0
2.82k stars 581 forks source link

the calculation of mIoU #75

Closed cxzhou95 closed 4 years ago

cxzhou95 commented 4 years ago

I have some questions about the calculation of mIoU in the core/utils/score.py

I will be very grateful if you could give me a favor.

In my understanding, the codes below calcluate the area_inter and area_union for all images in a batch: area_inter = area_inter_image1+area_inter_image2+...+area_inter_imageN area_union = area_union_image1+area_union_image2+...+area_union_imageN Then the IoU = area_inter / area_union

But I think it should be: IoU = mean(area_inter_image1/area_union_image1+area_inter_image2/area_union_image2+...+area_inter_imageN/area_union_imageN)

If my understanding is wrong, please point it out, thank you!

def batch_intersection_union(output, target, nclass) """mIoU"""

inputs are numpy array, output 4D, target 3D

mini = 1
maxi = nclass
nbins = nclass
predict = torch.argmax(output, 1) + 1
target = target.float() + 1

predict = predict.float() * (target > 0).float()
intersection = predict * (predict == target).float()
# areas of intersection and union
# element 0 in intersection occur the main difference from np.bincount. set boundary to -1 is necessary.
area_inter = torch.histc(intersection.cpu(), bins=nbins, min=mini, max=maxi)
area_pred = torch.histc(predict.cpu(), bins=nbins, min=mini, max=maxi)
area_lab = torch.histc(target.cpu(), bins=nbins, min=mini, max=maxi)
area_union = area_pred + area_lab - area_inter
assert torch.sum(area_inter > area_union).item() == 0, "Intersection area should be smaller than Union area"
return area_inter.float(), area_union.float()