pytorch mIOU
时间: 2023-12-08 07:39:06 浏览: 157
pytorch mIOU是指在语义分割任务中,计算预测结果和真实标签之间的平均交并比。以下是一个计算pytorch mIOU的例子:
```python
import torch
import numpy as np
def Iou(input, target, classNum):
'''
:param input: [b,h,w]
:param target: [b,h,w]
:param classNum: scalar
:return:
'''
inputTmp = torch.zeros([input.shape[0], classNum, input.shape[2], input.shape[3]])
targetTmp = torch.zeros([target.shape[0], classNum, target.shape[2], target.shape[3]])
for i in range(classNum):
inputTmp[:, i, :, :] = (input == i)
targetTmp[:, i, :, :] = (target == i)
intersection = torch.sum(inputTmp * targetTmp, dim=[0,2, 3])
union = torch.sum(inputTmp, dim=[0, 2, 3]) + torch.sum(targetTmp, dim=[0, 2, 3]) - intersection
iou = intersection / union
miou = torch.mean(iou)
return miou
# 示例
input = torch.tensor([[[0, 1], [1, 2]], [[2, 1], [0, 2]]])
target = torch.tensor([[[0, 1], [1, 2]], [[1, 1], [0, 2]]])
classNum = 3
miou = Iou(input, target, classNum)
print(miou) # 输出:tensor(0.6667)
```
阅读全文