语义分割混淆矩阵、 mIoU、mPA计算

一、操作

#vx:桔子code / juzicode.com
import cv2 
img_gray = cv2.imread("nezha.jpg",cv2.IMREAD_GRAYSCALE)
for i in range(22):
    dst = cv2.applyColorMap(img_gray,i) 
    cv2.imshow('map',dst) 
    cv2.waitKey(500)
    cv2.imwrite("map-"+str(i)+".jpg",dst)

需要会调试代码的人自己改,小白直接运行会出错

这是我从自己的大文件里摘取的一部分代码,可以运行,只是要改的文件地址path比较多,遇到双引号""的地址注意一下,不然地址不对容易出错

把 calculate.py和 utiles_metrics.py放在同一文件夹下,然后运行 calculate.py

二、理解

test_mIou,test_mPA,test_miou,test_mpa=compute_mIoU(gt_dir, pred_dir, image_ids, num_classes, name_classes,weight_name)  # 执行计算mIoU的函数

gt_dir 真实标签文件夹

pred_dir 预测结果文件夹

主要是这两个变量设置,后面的可以选择性修改

image_ids 文件名称 dirList(pred_dir,path_list) saveList(path_list) 这两个函数得到

num_classes 类别数

name_classes 类别名称

weight_name 权重名称

hist为混淆矩阵,mIoU为交并比

三、代码

calculate.py

# -*- coding: utf-8 -*-
import torch
import os

from time import time
# from PIL import Image
from utils_metrics import compute_mIoU
def saveList(pathName):
    for file_name in pathName:
        #f=open("C:/Users/Administrator/Desktop/DeepGlobe-Road-Extraction-link34-py3/dataset/real/gt.txt", "x")
        with open("./dataset/gt.txt", "a") as f:
            f.write(file_name.split(".")[0] + "\n")
        f.close

def dirList(gt_dir,path_list):
    for i in range(0, len(path_list)):
        path = os.path.join(gt_dir, path_list[i])
    if os.path.isdir(path):
        saveList(os.listdir(path))

data_path  = './dataset/'


f=open("./dataset/gt.txt", 'w')
gt_dir      = os.path.join(data_path, "real/")
pred_dir    = "./submits/log01_Dink101_five_100/test_iou/iou_60u/"
path_list = os.listdir(pred_dir)
path_list.sort()
dirList(pred_dir,path_list)
saveList(path_list)
num_classes=2
name_classes    = ["nontarget","target"]
weight_name='log01_Dink101_five_100'
image_ids   = open(os.path.join(data_path, "gt.txt"),'r').read().splitlines() 

test_mIou,test_mPA,test_miou,test_mpa=compute_mIoU(gt_dir, pred_dir, image_ids, num_classes, name_classes,weight_name)  # 执行计算mIoU的函数
print('  test_mIoU:  '+str(test_miou))

utiles_metrics.py

from os.path import join

import numpy as np
import torch
import torch.nn.functional as F
from PIL import Image
import os
import cv2

# from matplotlib import pyplot as plt
import shutil
import numpy as np
# from matplotlib.pyplot import MultipleLocator

def f_score(inputs, target, beta=1, smooth = 1e-5, threhold = 0.5):
    n, c, h, w = inputs.size()
    nt, ht, wt, ct = target.size()
    if h != ht and w != wt:
        inputs = F.interpolate(inputs, size=(ht, wt), mode="bilinear", align_corners=True)
        
    temp_inputs = torch.softmax(inputs.transpose(1, 2).transpose(2, 3).contiguous().view(n, -1, c),-1)
    temp_target = target.view(n, -1, ct)

    #--------------------------------------------#
    #   计算dice系数
    #--------------------------------------------#
    temp_inputs = torch.gt(temp_inputs, threhold).float()
    tp = torch.sum(temp_target[...,:-1] * temp_inputs, axis=[0,1])
    fp = torch.sum(temp_inputs                       , axis=[0,1]) - tp
    fn = torch.sum(temp_target[...,:-1]              , axis=[0,1]) - tp

    score = ((1 + beta ** 2) * tp + smooth) / ((1 + beta ** 2) * tp + beta ** 2 * fn + fp + smooth)
    score = torch.mean(score)
    return score

# 设标签宽W,长H
def fast_hist(a, b, n):
    #--------------------------------------------------------------------------------#
    #   a是转化成一维数组的标签,形状(H×W,);b是转化成一维数组的预测结果,形状(H×W,)
    #--------------------------------------------------------------------------------#
    k = (a >= 0) & (a < n)
    #--------------------------------------------------------------------------------#
    #   np.bincount计算了从0到n**2-1这n**2个数中每个数出现的次数,返回值形状(n, n)
    #   返回中,写对角线上的为分类正确的像素点
    #--------------------------------------------------------------------------------#
    return np.bincount(n * a[k].astype(int) + b[k], minlength=n ** 2).reshape(n, n)  

def per_class_iu(hist):
    return np.diag(hist) / np.maximum((hist.sum(1) + hist.sum(0) - np.diag(hist)), 1) 

def per_class_PA(hist):
    return np.diag(hist) / np.maximum(hist.sum(1), 1) 

def compute_mIoU(gt_dir, pred_dir, png_name_list, num_classes, name_classes,weight_name):  
    # print('Num classes', num_classes)  
    #-----------------------------------------#
    #   创建一个全是0的矩阵,是一个混淆矩阵
    #-----------------------------------------#
    hist = np.zeros((num_classes, num_classes))
    
    #------------------------------------------------#
    #   获得验证集标签路径列表,方便直接读取
    #   获得验证集图像分割结果路径列表,方便直接读取
    #------------------------------------------------#
    gt_imgs     = [join(gt_dir, x + ".png") for x in png_name_list]  
    pred_imgs   = [join(pred_dir, x + ".png") for x in png_name_list]  
    # building_iou=[]
    # background_iou=[]
    m_iou=[]
    # building_pa=[]
    # background_pa=[]
    m_pa=[]

    #------------------------------------------------#
    #   读取每一个(图片-标签)对
    #------------------------------------------------#
    for ind in range(len(gt_imgs)): 
        #------------------------------------------------#
        #   读取一张图像分割结果,转化成numpy数组
        #------------------------------------------------#
        pred = np.array(Image.open(pred_imgs[ind]))
        
        #------------------------------------------------#
        #   读取一张对应的标签,转化成numpy数组
        #------------------------------------------------#
        label = np.array(Image.open(gt_imgs[ind]))  
        
        # 如果图像分割结果与标签的大小不一样,这张图片就不计算
        if len(label.flatten()) != len(pred.flatten()):  
            print(
                'Skipping: len(gt) = {:d}, len(pred) = {:d}, {:s}, {:s}'.format(
                    len(label.flatten()), len(pred.flatten()), gt_imgs[ind],
                    pred_imgs[ind]))
            continue

        #------------------------------------------------#
        #   对一张图片计算21×21的hist矩阵,并累加
        #------------------------------------------------#
        a=label.flatten()
        a//=254
       
        b=pred.flatten()
        b//=254
        hist += fast_hist(a, b,num_classes)  
        # # 每计算10张就输出一下目前已计算的图片中所有类别平均的mIoU值
        # mIoUs   = per_class_iu(hist)
        # mPA     = per_class_PA(hist)
        # m_iou.append(100 * np.nanmean(mIoUs[1]))
        # m_pa.append(100 * np.nanmean(mPA[1]))
        # # if ind > 0 and ind % 10 == 0:  
        # #     print('{:d} / {:d}: mIou-{:0.2f}; mPA-{:0.2f}'.format(ind, len(gt_imgs),
        # #                                             100 * np.nanmean(mIoUs[1]),
        # #                                             100 * np.nanmean(mPA[1])))
    mIoUs   = per_class_iu(hist)
    mPA     = per_class_PA(hist)
    print(mIoUs)

    # plt.figure()
    # x=np.arange(len(m_iou))
    # plt.plot(x,m_iou)
    # plt.plot(x,m_pa)
    # plt.grid(True)
    # y_major_locator=MultipleLocator(10)#把y轴的刻度间隔设置为10,并存在变量里
    # ax = plt.gca()
    # ax.yaxis.set_major_locator(y_major_locator)
    # ax.set_ylim(0,100)
    # plt.xlabel('Order')
    # plt.ylabel('mIOU & mPA')
    # plt.legend(['mIOU','mPA'],loc="upper right")

    # targ=os.path.join(pred_dir,os.path.pardir)
    

    # plt.savefig(os.path.join(targ, weight_name[:-3]+"_sin_miou.png"))

    return m_iou,m_pa,str(round(mIoUs[1] * 100, 2)),str(round(mPA[1] * 100, 2))

调试

个人使用

import os
import shutil
data_path='./submits/log01_Dink101_five_100/test_iou/'
data=open(os.path.join(data_path, "log01_Dink101_five_100_excel.txt"),'r').read().splitlines()
valid_path='./dataset/valid/'
real_path='./dataset/real/'
 
iou_100=os.path.join(data_path,'iou_60u/')
iou_80=os.path.join(data_path,'iou_60d/')

if not os.path.exists(iou_100):
    os.mkdir(iou_100)
    os.mkdir(iou_80)

for n in data:
    name=n.split()[1]
    iou=float(n.split()[2])
    if iou>=65:
        img_path=os.path.join(data_path,'87.650/'+name+'.png')

        
        shutil.copy(img_path,iou_100)

        
        print(name,iou)
        continue
    else :
        img_path=os.path.join(data_path,'87.650/'+name+'.png')
        
        shutil.copy(img_path,iou_80)
       
        print(name,iou)
        continue
 
print('Finish')

# -*- coding: utf-8 -*-
import torch
import os

from time import time
# from PIL import Image
from utils.utils_metrics import compute_mIoU
from utils.utils_metrics import compute_IoU
def saveList(pathName):
    for file_name in pathName:
        #f=open("C:/Users/Administrator/Desktop/DeepGlobe-Road-Extraction-link34-py3/dataset/real/gt.txt", "x")
        with open("./dataset/gt.txt", "a") as f:
            f.write(file_name.split(".")[0] + "\n")
        f.close

def dirList(gt_dir,path_list):
    for i in range(0, len(path_list)):
        path = os.path.join(gt_dir, path_list[i])
    if os.path.isdir(path):
        saveList(os.listdir(path))

data_path  = './dataset/'


f=open("./dataset/gt.txt", 'w')
gt_dir      = os.path.join(data_path, "real/")
pred_dir    = "./submits/log01_Dink101_five_100/test_iou/iou_60u/"
path_list = os.listdir(pred_dir)
path_list.sort()
dirList(pred_dir,path_list)
saveList(path_list)
num_classes=2
name_classes    = ["nontarget","target"]
weight_name='log01_Dink101_five_100'
image_ids   = open(os.path.join(data_path, "gt.txt"),'r').read().splitlines() 

# compute_IoU(gt_dir, pred_dir, image_ids, num_classes, weight_name)
test_mIou,test_mPA,test_miou,test_mpa=compute_mIoU(gt_dir, pred_dir, image_ids, num_classes, name_classes,weight_name)  # 执行计算mIoU的函数
print('  test_mIoU:  '+str(test_miou))

from os.path import join

import numpy as np
import torch
import torch.nn.functional as F
from PIL import Image
import os
import cv2

# from matplotlib import pyplot as plt
import shutil
import numpy as np
# from matplotlib.pyplot import MultipleLocator

def f_score(inputs, target, beta=1, smooth = 1e-5, threhold = 0.5):
    n, c, h, w = inputs.size()
    nt, ht, wt, ct = target.size()
    if h != ht and w != wt:
        inputs = F.interpolate(inputs, size=(ht, wt), mode="bilinear", align_corners=True)
        
    temp_inputs = torch.softmax(inputs.transpose(1, 2).transpose(2, 3).contiguous().view(n, -1, c),-1)
    temp_target = target.view(n, -1, ct)

    #--------------------------------------------#
    #   计算dice系数
    #--------------------------------------------#
    temp_inputs = torch.gt(temp_inputs, threhold).float()
    tp = torch.sum(temp_target[...,:-1] * temp_inputs, axis=[0,1])
    fp = torch.sum(temp_inputs                       , axis=[0,1]) - tp
    fn = torch.sum(temp_target[...,:-1]              , axis=[0,1]) - tp

    score = ((1 + beta ** 2) * tp + smooth) / ((1 + beta ** 2) * tp + beta ** 2 * fn + fp + smooth)
    score = torch.mean(score)
    return score

# 设标签宽W,长H
def fast_hist(a, b, n):
    #--------------------------------------------------------------------------------#
    #   a是转化成一维数组的标签,形状(H×W,);b是转化成一维数组的预测结果,形状(H×W,)
    #--------------------------------------------------------------------------------#
    k = (a >= 0) & (a < n)
    #--------------------------------------------------------------------------------#
    #   np.bincount计算了从0到n**2-1这n**2个数中每个数出现的次数,返回值形状(n, n)
    #   返回中,写对角线上的为分类正确的像素点
    #--------------------------------------------------------------------------------#
    return np.bincount(n * a[k].astype(int) + b[k], minlength=n ** 2).reshape(n, n)  

def per_class_iu(hist):
    return np.diag(hist) / np.maximum((hist.sum(1) + hist.sum(0) - np.diag(hist)), 1) 

def per_class_PA(hist):
    return np.diag(hist) / np.maximum(hist.sum(1), 1) 

def compute_IoU(gt_dir, pred_dir, png_name_list, num_classes,weight_name):  
    # print('Num classes')  
    #-----------------------------------------#
    #   创建一个全是0的矩阵,是一个混淆矩阵
    #-----------------------------------------#
    # hist = np.zeros((num_classes, num_classes))
    
    #------------------------------------------------#
    #   获得验证集标签路径列表,方便直接读取
    #   获得验证集图像分割结果路径列表,方便直接读取
    #------------------------------------------------#
    gt_imgs     = [join(gt_dir, x + "_mask.png") for x in png_name_list]  
    pred_imgs   = [join(pred_dir, x + ".png") for x in png_name_list]  

    m_iou=[]
    m_pa=[]
    hist_save=[] 
    
    #------------------------------------------------#
    #   读取每一个(图片-标签)对
    #------------------------------------------------#
    for ind in range(len(gt_imgs)): 

        #------------------------------------------------#
        #   读取一张图像分割结果,转化成numpy数组
        #------------------------------------------------#
        pred = np.array(Image.open(pred_imgs[ind]).convert('L'))
        # pred = pred/255
        #------------------------------------------------#
        #   读取一张对应的标签,转化成numpy数组
        #------------------------------------------------#
        label = np.array(Image.open(gt_imgs[ind]).convert('L'))  
        # label = label/255
        # 如果图像分割结果与标签的大小不一样,这张图片就不计算
        if len(label.flatten()) != len(pred.flatten()):  
            print(
                'Skipping: len(gt) = {:d}, len(pred) = {:d}, {:s}, {:s}'.format(
                    len(label.flatten()), len(pred.flatten()), gt_imgs[ind],
                    pred_imgs[ind]))
            continue

        #------------------------------------------------#
        #   对一张图片计算21×21的hist矩阵,并累加
        #------------------------------------------------#
        a=label.flatten()
        a//=254
        # for i in range(len(a)):
        #     a[i]=a[i]/255
        
        b=pred.flatten()
        b//=254
        # for i in range(len(b)):
        #     b[i]=b[i]/255
        hist = fast_hist(a, b,num_classes)  
        # 每计算10张就输出一下目前已计算的图片中所有类别平均的mIoU值
        mIoUs   = per_class_iu(hist)
        mPA     = per_class_PA(hist)
        mIoU_one = 100 * np.nanmean(mIoUs[1])
        mPA_one = 100 * np.nanmean(mPA[1])
        # if mIoU_one<80:
        #     shutil.copy(pred_imgs[ind],lower_iou)
        #     count=count+1
        # else:
        #     shutil.copy(pred_imgs[ind],higher_iou)
        img_name= png_name_list[ind]
        # if ind > 0 and ind % 10 == 0:  
        #print('{:d}  {}: Iou-{:0.2f}; PA-{:0.2f}'.format(ind,img_name,mIoU_one,mPA_one))
        #print(hist)
        m_iou.append(100 * np.nanmean(mIoUs[1]))
        m_pa.append(100 * np.nanmean(mPA[1]))
        hist_save.append(hist)
        targ=os.path.join(pred_dir,os.path.pardir)
        if ind==0:
            with open(os.path.join(targ, weight_name[:-3]+'.txt'),'w') as f:
                f.write(str(ind) +'  '+ str(img_name) +'   ')
                f.write('Iou:'+str(round(mIoU_one,2))+'  '+'PA:'+str(round(mPA_one,2))+'\n')
                f.write('                   '+'['+str(hist[0,0])+'   '+str(hist[0,1])+'\n')
                f.write('                   '+' '+str(hist[1,0])+'   '+str(hist[1,1])+']'+'\n')
            with open(os.path.join(targ, weight_name[:-3]+'_excel.txt'),'w') as f:
                f.write(str(ind) +'  '+ str(img_name) +'   ')
                f.write(str(round(mIoU_one,2))+'  '+str(round(mPA_one,2))+'\n')
        else:
            with open(os.path.join(targ, weight_name[:-3]+'.txt'),'a') as f:
                f.write(str(ind) +'  '+ str(img_name) +'   ')
                f.write('Iou:'+str(round(mIoU_one,2))+'  '+'PA:'+str(round(mPA_one,2))+'\n')
                f.write('                   '+'['+str(hist[0,0])+'   '+str(hist[0,1])+'\n')
                f.write('                   '+' '+str(hist[1,0])+'   '+str(hist[1,1])+']'+'\n')
            with open(os.path.join(targ, weight_name[:-3]+'_excel.txt'),'a') as f:
                f.write(str(ind) +'  '+ str(img_name) +'   ')
                f.write(str(round(mIoU_one,2))+'  '+str(round(mPA_one,2))+'\n')
    '''
    plt.figure()
    x=np.arange(len(m_iou))
    plt.plot(x,m_iou)
    plt.plot(x,m_pa)
    plt.grid(True)
    plt.xlabel('Order')
    plt.ylabel('test mIOU & mPA')
    plt.legend(['mIOU','mPA'],loc="upper right")

    plt.savefig(os.path.join(pred_dir[0:-13], weight_name[:-3]+"_test_iou.png"))
    '''

def compute_mIoU(gt_dir, pred_dir, png_name_list, num_classes, name_classes,weight_name):  
    # print('Num classes', num_classes)  
    #-----------------------------------------#
    #   创建一个全是0的矩阵,是一个混淆矩阵
    #-----------------------------------------#
    hist = np.zeros((num_classes, num_classes))
    
    #------------------------------------------------#
    #   获得验证集标签路径列表,方便直接读取
    #   获得验证集图像分割结果路径列表,方便直接读取
    #------------------------------------------------#
    gt_imgs     = [join(gt_dir, x + "_mask.png") for x in png_name_list]  
    pred_imgs   = [join(pred_dir, x + ".png") for x in png_name_list]  
    # building_iou=[]
    # background_iou=[]
    m_iou=[]
    # building_pa=[]
    # background_pa=[]
    m_pa=[]

    #------------------------------------------------#
    #   读取每一个(图片-标签)对
    #------------------------------------------------#
    for ind in range(len(gt_imgs)): 
        #------------------------------------------------#
        #   读取一张图像分割结果,转化成numpy数组
        #------------------------------------------------#
        pred = np.array(Image.open(pred_imgs[ind]).convert('L'))
        
        #------------------------------------------------#
        #   读取一张对应的标签,转化成numpy数组
        #------------------------------------------------#
        label = np.array(Image.open(gt_imgs[ind]).convert('L'))  
        
        # 如果图像分割结果与标签的大小不一样,这张图片就不计算
        if len(label.flatten()) != len(pred.flatten()):  
            print(
                'Skipping: len(gt) = {:d}, len(pred) = {:d}, {:s}, {:s}'.format(
                    len(label.flatten()), len(pred.flatten()), gt_imgs[ind],
                    pred_imgs[ind]))
            continue

        #------------------------------------------------#
        #   对一张图片计算21×21的hist矩阵,并累加
        #------------------------------------------------#
        a=label.flatten()
        a//=254
       
        b=pred.flatten()
        b//=254
        hist += fast_hist(a, b,num_classes)  
        # # 每计算10张就输出一下目前已计算的图片中所有类别平均的mIoU值
        # mIoUs   = per_class_iu(hist)
        # mPA     = per_class_PA(hist)
        # m_iou.append(100 * np.nanmean(mIoUs[1]))
        # m_pa.append(100 * np.nanmean(mPA[1]))
        # # if ind > 0 and ind % 10 == 0:  
        # #     print('{:d} / {:d}: mIou-{:0.2f}; mPA-{:0.2f}'.format(ind, len(gt_imgs),
        # #                                             100 * np.nanmean(mIoUs[1]),
        # #                                             100 * np.nanmean(mPA[1])))
    print(hist)
    mIoUs   = per_class_iu(hist)
    mPA     = per_class_PA(hist)
    print(mIoUs)

    # plt.figure()
    # x=np.arange(len(m_iou))
    # plt.plot(x,m_iou)
    # plt.plot(x,m_pa)
    # plt.grid(True)
    # y_major_locator=MultipleLocator(10)#把y轴的刻度间隔设置为10,并存在变量里
    # ax = plt.gca()
    # ax.yaxis.set_major_locator(y_major_locator)
    # ax.set_ylim(0,100)
    # plt.xlabel('Order')
    # plt.ylabel('mIOU & mPA')
    # plt.legend(['mIOU','mPA'],loc="upper right")

    # targ=os.path.join(pred_dir,os.path.pardir)
    

    # plt.savefig(os.path.join(targ, weight_name[:-3]+"_sin_miou.png"))

    return m_iou,m_pa,str(round(mIoUs[1] * 100, 2)),str(round(mPA[1] * 100, 2))
相关推荐
运维小文22 分钟前
服务器硬件介绍
运维·服务器·计算机网络·缓存·硬件架构
小周不摆烂28 分钟前
丹摩征文活动 | 丹摩智算平台:服务器虚拟化的璀璨明珠与实战秘籍
大数据·服务器
中云DDoS CC防护蔡蔡29 分钟前
为什么海外服务器IP会被封
服务器·经验分享
是安迪吖32 分钟前
nfs服务器
运维·服务器
鱼骨不是鱼翅34 分钟前
模拟回显服务器
运维·服务器
运维佬34 分钟前
CentOS 9 配置网卡
linux·centos
轩轩曲觞阁1 小时前
Linux网络——网络初识
linux·网络
2401_840192271 小时前
python基础大杂烩
linux·开发语言·python
EasyCVR1 小时前
ISUP协议视频平台EasyCVR视频设备轨迹回放平台智慧农业视频远程监控管理方案
服务器·网络·数据库·音视频
weixin_438197381 小时前
K8S创建云主机配置docker仓库
linux·云原生·容器·eureka·kubernetes