mmsegmentation 版本从1.0更迭到2.0,绘制混淆矩阵的py文件还在,但是似乎已经无法使用,很多函数和库发生修改。因此,我结合自己的需求,将修改后的代码,发布如下:
这里需要在测试的时候生成.pkl文件,请教大神后,2.0版本生成测试时的.pkl命令如下:
python tools/test.py {configs} {ckpt} --cfg-options test_evaluator._delete_=True test_evaluator.type=DumpResults test_evaluator.out_file_path='work_dirs/results.pkl
import numpy as np
from mmengine.utils import ProgressBar
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
import os
from mmengine.fileio import load
class Plot_Cm(object):
def __init__(self, num_class,normalize=True):
self.num_class = num_class
self.normalize = normalize
def calculate_confusion_matrix(self,seg_results_path):
"""Calculate the confusion matrix.
Args:
dataset (Dataset): Test or val dataset.
results (list[ndarray]): A list of segmentation results in each image.
"""
n = self.num_class
confusion_matrix = np.zeros(shape=[n, n])
seg_results=load(seg_results_path)
prog_bar = ProgressBar(len(seg_results))
for per_img_res in seg_results:
gt_sem_seg =