多标签分类模型验证结果badcase查找与存储

背景:模型错误有标签True,判别负;标签负,判别正。因此需要将模型的True_negative与模型的False_positive的错误预测写为list,并且将相应的错误文件名找出。

目录

一、根据预测结果找出badcase

1.1 写出模型预测结果

1.2 根据结果算出指标

1.3 加入错误样本矩阵

二、每类的badcase

2.1 根据标签创建list

2.2 将相应的指引矩阵转为dict

三、文件名写入

3.1 coco的标注文件

3.2 coco读取

3.3 读取后的格式

四、文件名写入dict


一、根据预测结果找出badcase

1.1 写出模型预测结果

PyTorch实操(五)加载模型验证并将所有结果写入文件

1.2 根据结果算出指标

输出多标签分类模型每class指标OP,OR,OF1,CP,CR,CF1

1.3 加入错误样本矩阵

上面,有一个对应numpy矩阵,大小为n_imgs*n_class

我们也可以继续构建这样一个矩阵,是错误样本则对应位置为1,非错误样本对应位置为0

def evaluation_and_badcase(scores_, targets_):
    print('evaluation start...')
    n, n_class = scores_.shape
    print('img_numbers=',n,'n_class=',n_class)
    Nc, Np, Ng = np.zeros(n_class), np.zeros(n_class), np.zeros(n_class)
    ture_Positive,true_negative,false_positive=np.zeros((n,n_class)),np.zeros((n,n_class)),np.zeros((n,n_class))
    cls_P,cls_R,cls_F1=np.zeros(n_class),np.zeros(n_class),np.zeros(n_class)
    for k in range(n_class):
        scores = scores_[:, k]         # all img scores on class_k
        targets = targets_[:, k]       # all img labels on class_k
        targets[targets == -1] = 0     # set img labels from -1 to 0
        Ng[k] = np.sum(targets == 1)   # ture:     all ture labels sum number
        Np[k] = np.sum(scores >= 0)    # positive: all predict positive sum number
        Nc[k] = np.sum(targets * (scores >= 0)) # true_positive: true_positive sum number
        ture_Positive[:,k]=np.where(targets * (scores >= 0)==1 ,1,0)
        true_negative[:,k]=np.where(targets * (scores < 0)==1 ,1,0)
        false_positive[:,k]=np.where((targets==0)*(scores >= 0)==1, 1,0)
        cls_P[k]=Nc[k]/Np[k]
        cls_R[k]=Nc[k]/Ng[k]
        cls_F1[k]=(2 * cls_P[k] * cls_R[k]) / (cls_P[k] + cls_R[k])
    Np[Np == 0] = 1
    print('np.sum(Nc),true_positive=',np.sum(Nc))
    print('np.sum(Np),positive=',np.sum(Np))
    print('np.sum(Ng),ture=',np.sum(Ng))

    # for all labels num_imgs*n_classes
    OP = np.sum(Nc) / np.sum(Np)        # precision: true_positive/positive
    OR = np.sum(Nc) / np.sum(Ng)        # recall:    true_positive/true
    OF1 = (2 * OP * OR) / (OP + OR)     # F1_score: harmonic mean of precision and recall
    # average by class
    CP = np.sum(Nc / Np) / n_class      # precision: true_positive/positive
    CR = np.sum(Nc / Ng) / n_class      # recall:    true_positive/true
    CF1 = (2 * CP * CR) / (CP + CR)     # F1_score: harmonic mean of precision and recall

    return OP, OR, OF1, CP, CR, CF1,cls_P,cls_R,cls_F1,ture_Positive,true_negative,false_positive

有三个这样的矩阵,ture_Positive,true_negative,false_positive,其中,true,false表示标签的正负,positive,negative表示网络预测结果的正负。

二、每类的badcase

2.1 根据标签创建list

关于np.where  https://www.cnblogs.com/massquantity/p/8908859.html

遍历np.array

https://blog.csdn.net/github_39611196/article/details/79109843

[img_num,class_num]=ture_Positive.shape
for class_idx in range(class_num):
    ture_negative_dict[class_idx]=[]
    true_positive_dict[class_idx]=[]
    false_positive_dict[class_idx]=[]
for img_idx in range(img_num):
    for class_idx in range(class_num):
        if true_negative[img_idx,class_idx]==1:
            true_negative_num=true_negative_num+1
            ture_negative_dict[class_idx].append(img_idx)
        if ture_Positive[img_idx,class_idx]==1:
            true_positive_num=true_positive_num+1
            true_positive_dict[class_idx].append(img_idx)
        if false_positive[img_idx,class_idx]==1:
            false_positive_num=false_positive_num+1
            false_positive_dict[class_idx].append(img_idx)

2.2 将相应的指引矩阵转为dict

需要将每类的badcase存入一个dict,dict的key为类名,keyValue为图片编号的list

[img_num,class_num]=ture_Positive.shape
for class_idx in range(class_num):
    ture_negative_dict[class_idx]=[]
    true_positive_dict[class_idx]=[]
    false_positive_dict[class_idx]=[]
for img_idx in range(img_num):
    for class_idx in range(class_num):
        if true_negative[img_idx,class_idx]==1:
            true_negative_num=true_negative_num+1
            ture_negative_dict[class_idx].append(img_idx)
        if ture_Positive[img_idx,class_idx]==1:
            true_positive_num=true_positive_num+1
            true_positive_dict[class_idx].append(img_idx)
        if false_positive[img_idx,class_idx]==1:
            false_positive_num=false_positive_num+1
            false_positive_dict[class_idx].append(img_idx)
print('true_positive_num',true_positive_num)
print('true_negative_num=',true_negative_num)
print('false_positive_num',false_positive_num)

三、文件名写入

3.1 coco的标注文件

查找源码,看到coco的文件在下面这个文件中:

data/data/coco/data/val_anno.json

coco_img_list

3.2 coco读取

分别为根据文件位置读取文件,将每个文件作为item

    def get_anno(self):
        list_path = os.path.join(self.root, 'data', '{}_anno.json'.format(self.phase))
        self.img_list = json.load(open(list_path, 'r'))
        self.cat2idx = json.load(open(os.path.join(self.root, 'data', 'category.json'), 'r'))

    def __len__(self):
        return len(self.img_list)

    def __getitem__(self, index):
        item = self.img_list[index]
        return self.get(item)

item中的file_name即为我们需要的文件名

    def get(self, item):
        filename = item['file_name']
        labels = sorted(item['labels'])
        # fixme==========================
        if self.is_grouping:
            new_labels = [self.idxmap[l] for l in labels]
            labels = sorted(new_labels)
        # fixme===============================

        img = Image.open(os.path.join(self.root, 'data', '{}2014'.format(self.phase), filename)).convert('RGB')
        if self.transform is not None:
            img = self.transform(img)
        target = np.zeros(self.num_classes, np.float32) - 1
        target[labels] = 1
        return (img, filename, self.inp), target

 

3.3 读取后的格式

{'file_name': 'COCO_val2014_000000501919.jpg', 'labels': [8, 49, 68]}, 
{'file_name': 'COCO_val2014_000000436385.jpg', 'labels': [0, 49]}, {'file_name': 'COCO_val2014_000000043170.jpg', 'labels': [16, 49, 18, 68]}, {'file_name': 
'COCO_val2014_000000436387.jpg', 'labels': [49, 18, 31]}, {'file_name': 'COCO_val2014_000000305317.jpg', 'labels': [40, 49]}, {'file_name': 
'COCO_val2014_000000174247.jpg', 'labels': [42]}, {'file_name': 'COCO_val2014_000000436392.jpg', 'labels': [57, 75, 53]}, {'file_name': 
'COCO_val2014_000000239785.jpg', 'labels': [49, 59]}, {'file_name': 'COCO_val2014_000000575372.jpg', 'labels': [72]}, {'file_name': 
'COCO_val2014_000000055280.jpg', 'labels': [25]},

四、文件名写入dict

读出文件名,遍历相应的array,写入dict

coco_list_path = os.path.join('data/data/coco/data/val_anno.json')
coco_img_list = json.load(open(coco_list_path, 'r'))
# print(coco_img_list)
ture_negative_name_dict,true_positive_name_dict,false_positive_name_dict={},{},{}
for class_idx in range(class_num):
    ture_negative_name_dict[class_idx]=[]
    true_positive_name_dict[class_idx]=[]
    false_positive_name_dict[class_idx]=[]
for class_idx in range(len(false_positive_dict)):
    for object_idx in range(len(false_positive_dict[class_idx])):
        file_idx=false_positive_dict[class_idx][object_idx]
        item=coco_img_list[file_idx]
        false_positive_name_dict[class_idx].append(item['file_name'])
for class_idx in range(len(ture_negative_dict)):
    for object_idx in range(len(ture_negative_dict[class_idx])):
        file_idx=ture_negative_dict[class_idx][object_idx]
        item=coco_img_list[file_idx]
        ture_negative_name_dict[class_idx].append(item['file_name'])

with open('checkpoint/coco/resnet101_on_coco/ture_negative_name_dict.pkl','wb') as f:
    print('writing checkpoint/coco/resnet101_on_coco/ture_negative_name_dict.pkl')
    pickle.dump(ture_negative_name_dict, f)
with open('checkpoint/coco/resnet101_on_coco/false_positive_name_dict.pkl','wb') as f:
    print('writing checkpoint/coco/resnet101_on_coco/false_positive_name_dict.pkl')
    pickle.dump(false_positive_name_dict, f)

最终文件格式,dict格式,前面为class的编号,后面为每个file_name

class_num 4 file_names ['COCO_val2014_000000262274.jpg', 'COCO_val2014_000000461248.jpg', 'COCO_val2014_000000397483.jpg', 'COCO_val2014_000000140020.jpg', 'COCO_val2014_000000399297.jpg', 'COCO_val2014_000000202981.jpg', 'COCO_val2014_000000072873.jpg', 'COCO_val2014_000000074499.jpg', 'COCO_val2014_000000536110.jpg', 'COCO_val2014_000000339705.jpg', 'COCO_val2014_000000144058.jpg', 
'COCO_val2014_000000538005.jpg', 'COCO_val2014_000000538394.jpg', 'COCO_val2014_000000408757.jpg', 'COCO_val2014_000000505890.jpg', 'COCO_val2014_000000348475.jpg', 'COCO_val2014_000000480657.jpg', 'COCO_val2014_000000240804.jpg', 'COCO_val2014_000000436605.jpg', 
'COCO_val2014_000000226244.jpg', 'COCO_val2014_000000089884.jpg', 'COCO_val2014_000000484816.jpg', 'COCO_val2014_000000306415.jpg', 'COCO_val2014_000000421307.jpg', 'COCO_val2014_000000160239.jpg', 'COCO_val2014_000000234660.jpg', 'COCO_val2014_000000359677.jpg', 'COCO_val2014_000000230247.jpg', 'COCO_val2014_000000361551.jpg', 'COCO_val2014_000000001149.jpg', 'COCO_val2014_000000082462.jpg', 
'COCO_val2014_000000104327.jpg', 'COCO_val2014_000000170651.jpg', 'COCO_val2014_000000498687.jpg', 'COCO_val2014_000000500257.jpg', 'COCO_val2014_000000172935.jpg', 'COCO_val2014_000000107554.jpg', 
'COCO_val2014_000000050786.jpg', 'COCO_val2014_000000456584.jpg', 
'COCO_val2014_000000437859.jpg', 'COCO_val2014_000000214197.jpg', 'COCO_val2014_000000441072.jpg', 'COCO_val2014_000000444100.jpg', 'COCO_val2014_000000510122.jpg', 'COCO_val2014_000000183114.jpg', 'COCO_val2014_000000346621.jpg', 'COCO_val2014_000000382905.jpg', 'COCO_val2014_000000186472.jpg', 'COCO_val2014_000000448786.jpg', 
'COCO_val2014_000000449432.jpg', 'COCO_val2014_000000390975.jpg', 
'COCO_val2014_000000196415.jpg']
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

祥瑞Coding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值