PSPNet 评价指标
1. pix_accuracy
比较预测值的每个像素和实际值的每个像素,若二者相等,则代表预测正确
总数量为实际值的像素总数,求二者比值,即为准确率
def batch_pix_accuracy(predict, target, labeled): pixel_labeled = labeled.sum() pixel_correct = ((predict == target) * labeled).sum() assert pixel_correct <= pixel_labeled, "Correct area should be smaller than Labeled" return pixel_correct.cpu().numpy(), pixel_labeled.cpu().numpy()
2. IoU_accuracy
计算相同类别的IoU
相交部分:predict == target
因为是多分类:根据类别形成直方图
area_inter = torch.histc(intersection.float(), bins=num_class, max=num_class, min=1)
相并部分:预测 加 实际 减 相交
因为是多分类:
area_pred = torch.histc(predict.float(), bins=num_class, max=num_class, min=1)
area_lab = torch.histc(target.float(), bins=num_class, max=num_class, min=1)
area_union = area_pred + area_lab - area_inter
def batch_intersection_union(predict, target, num_class, labeled): predict = predict * labeled.long() intersection = predict * (predict == target).long() area_inter = torch.histc(intersection.float(), bins=num_class, max=num_class, min=1) area_pred = torch.histc(predict.float(), bins=num_class, max=num_class, min=1) area_lab = torch.histc(target.float(), bins=num_class, max=num_class, min=1) area_union = area_pred + area_lab - area_inter assert (area_inter <= area_union).all(), "Intersection area should be smaller than Union area" return area_inter.cpu().numpy(), area_union.cpu().numpy()
3. 更新损失
def _get_seg_metrics(self): pixAcc = 1.0 * self.total_correct / (np.spacing(1) + self.total_label) IoU = 1.0 * self.total_inter / (np.spacing(1) + self.total_union) mIoU = IoU.mean() return { "Pixel_Accuracy": np.round(pixAcc, 3), "Mean_IoU": np.round(mIoU, 3), "Class_IoU": dict(zip(range(self.num_classes), np.round(IoU, 3)))}