版本:python3
pred为模型预测的label,像素0表示背景,像素1表示类别1,像素2表示类别2,以此类推。
target为groundtruth,这里读入格式为PIL image,格式不一样的请自行修改
这里的n_classes是目标物类别数。比如,对于只有背景和一个检测物类别的二分类问题,n_classes=1
因为pythonfor循环的range(a,b),范围其实为[a,b),所以循环终点为n_classes+1
import numpy as np
import torch
def iou_mean(pred, target, n_classes = 1):
#n_classes :the number of classes in your dataset,not including background
# for mask and ground-truth label, not probability map
ious = []
iousSum = 0
pred = torch.from_numpy(pred