mIoU
(output, target, nclass)
| 86 | |
| 87 | |
| 88 | def batch_intersection_union(output, target, nclass): |
| 89 | """mIoU""" |
| 90 | # inputs are numpy array, output 4D, target 3D |
| 91 | mini = 1 |
| 92 | maxi = nclass |
| 93 | nbins = nclass |
| 94 | target = torch.max(target, 1).indices.squeeze() #change1 |
| 95 | target = target.unsqueeze(0) |
| 96 | output = torch.max(output, 1).indices.squeeze() |
| 97 | output = output.unsqueeze(0) |
| 98 | #predict = torch.argmax(output, 1) + 1 |
| 99 | target = target.float() + 1 |
| 100 | predict = output + 1 |
| 101 | |
| 102 | predict = predict.float() * (target > 0).float() |
| 103 | intersection = predict * (predict == target).float() |
| 104 | # areas of intersection and union |
| 105 | # element 0 in intersection occur the main difference from np.bincount. set boundary to -1 is necessary. |
| 106 | area_inter = torch.histc(intersection.cpu(), bins=nbins, min=mini, max=maxi) |
| 107 | area_pred = torch.histc(predict.cpu(), bins=nbins, min=mini, max=maxi) |
| 108 | area_lab = torch.histc(target.cpu(), bins=nbins, min=mini, max=maxi) |
| 109 | area_union = area_pred + area_lab - area_inter |
| 110 | assert torch.sum(area_inter > area_union).item() == 0, "Intersection area should be smaller than Union area" |
| 111 | return area_inter.float(), area_union.float() |
| 112 | |
| 113 | |
| 114 | def pixelAccuracy(imPred, imLab): |