anno_func.py

'''
import json
import pylab as pl
import random
import numpy as np
import cv2
import anno_func
%matplotlib inline
datadir = "../../data/"

filedir = datadir + "/annotations.json"
ids = open(datadir + "/test/ids.txt").read().splitlines()

annos = json.loads(open(filedir).read())
result_anno_file = "./../results/ours_result_annos.json"
results_annos = json.loads(open(result_anno_file).read())
test_annos = results_annos
minscore=40
'''
'''sm = anno_func.eval_annos(annos, test_annos, iou=0.5, check_type=True, types=anno_func.type45,
                         minboxsize=0,maxboxsize=32,minscore=minscore)
'''
def eval_annos(annos_gd, annos_rt, iou=0.75, imgids=None, check_type=True, types=None, minscore=40, minboxsize=0, maxboxsize=400, match_same=True):
    ac_n, ac_c = 0,0	#accuracy
    rc_n, rc_c = 0,0	#recall
    if imgids==None:
        imgids = annos_rt['imgs'].keys()	#字典 dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}
       
    if types!=None:							#key:value
        types = { t:0 for t in types }		#类似{1: 0, 2: 0, 3: 0, 4: 0, 5: 0}
    miss = {"imgs":{}}	#字典的嵌套?
    wrong = {"imgs":{}}
    right = {"imgs":{}}
    
    for imgid in imgids:	#对于某一张图片
        v = annos_rt['imgs'][imgid]		#预测的图片
        vg = annos_gd['imgs'][imgid]	#实际的
        convert = lambda objs: [ [ obj['bbox'][key] for key in ['xmin','ymin','xmax','ymax']] for obj in objs]
        												#不是常规的双层循环,按照括号来说,后面是外层循环
        												#lambda x, y: x*y
        												#函数输入是x和y,输出是它们的积x*y
        objs_g = vg["objects"]	#实际的目标
        objs_r = v["objects"]	#预测的目标
        bg = convert(objs_g)	#实际的边框
        br = convert(objs_r)	#预测的边框
        
        match_g = [-1]*len(bg)	#边框是否匹配?一开始都是-1?列表的乘法即为重复
        match_r = [-1]*len(br)	#列表的加法为拼接
        if types!=None:
            for i in range(len(match_g)):
                if not types.has_key(objs_g[i][  'category']):#如果键在字典里返回true,否则返回false。
                    match_g[i] = -2	#真实框的类别没有检测出来?真实框的类别不在设定的检测范围内?所以是没用的?
            for i in range(len(match_r)):
                if not types.has_key(objs_r[i]['category']):
                    match_r[i] = -2 #预测框的类别没有检测出来?所以也是没用的?
        for i in range(len(match_r)):
            if objs_r[i].has_key('score') and objs_r[i]['score']<minscore:
                match_r[i] = -2	#分数太低就淘汰?就没用了?
        matches = []			#空的列表
        for i,boxg in enumerate(bg):	#枚举 enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)
        								#组合为一个索引序列,同时列出下标和数据
            for j,boxr in enumerate(br):
                if match_g[i] == -2 or match_r[j] == -2:
                    continue	#if continue的用法(跳过本次循环,执行下一个循环)
                if match_same and objs_g[i]['category'] != objs_r[j]['category']: continue	#分类不一样就淘汰?
                #match_same是传进来的参数
                tiou = calc_iou(boxg, boxr)		#如果分类一致就计算iou(不一定是同一个目标)
                if tiou>iou:					#大于输入的阈值,有可能是同一目标?
                    matches.append((tiou, i, j))#列表中添加元组?
        matches = sorted(matches, key=lambda x:-x[0])	#按第一个元素(tiou)反向(由大到小)排列
		
														#lambda函数示例:
        												#lambda x, y: x*y
        												#函数输入是x和y,输出是它们的积x*y
        for tiou, i, j in matches:
            if match_g[i] == -1 and match_r[j] == -1:	#不是-2就证明起码不是没用的?
                match_g[i] = j
                match_r[j] = i	#记录对应关系?
                
        for i in range(len(match_g)):
            boxsize = box_long_size(objs_g[i]['bbox'])
            erase = False
            if not (boxsize>=minboxsize and boxsize<maxboxsize):
                erase = True	#大小不符合就擦除?
            #if types!=None and not types.has_key(objs_g[i]['category']):
            #    erase = True
            if erase:
                if match_g[i] >= 0:
                    match_r[match_g[i]] = -2	#match_g[i] = j
                match_g[i] = -2
        
        for i in range(len(match_r)):
            boxsize = box_long_size(objs_r[i]['bbox'])
            if match_r[i] != -1: continue
            if not (boxsize>=minboxsize and boxsize<maxboxsize):
                match_r[i] = -2	#??????预测框大小不符合也删掉?
                    
        miss["imgs"][imgid] = {"objects":[]}
        wrong["imgs"][imgid] = {"objects":[]}
        right["imgs"][imgid] = {"objects":[]}
        miss_objs = miss["imgs"][imgid]["objects"]
        wrong_objs = wrong["imgs"][imgid]["objects"]
        right_objs = right["imgs"][imgid]["objects"]
        
        tt = 0
        for i in range(len(match_g)):
            if match_g[i] == -1:	#类别存在但没找到匹配对象
                miss_objs.append(objs_g[i])
        for i in range(len(match_r)):
            if match_r[i] == -1:	#类别存在但没找到匹配对象
                obj = copy.deepcopy(objs_r[i])
                obj['correct_catelog'] = 'none'
                wrong_objs.append(obj)
            elif match_r[i] != -2:	#类别存在且找到匹配对象
                j = match_r[i]      
                obj = copy.deepcopy(objs_r[i])
                if not check_type or objs_g[j]['category'] == objs_r[i]['category']:	#优先级&&如果分类一致
                    right_objs.append(objs_r[i])
                    tt+=1	#分类正确的计数
                else:		#分类不正确?
                      obj['correct_catelog'] = objs_g[j]['category']
                    wrong_objs.append(obj)
                    
        
        rc_n += len(objs_g) - match_g.count(-2)	#计数 全部的真实框-没用的
        ac_n += len(objs_r) - match_r.count(-2)	#计数 全部的预测框-没用的
        
        ac_c += tt	#分类正确的计数
        rc_c += tt	#分类正确的计数
    if types==None:	#types是传进来的参数
        styps = "all"
    elif len(types)==1:
        styps = types.keys()[0]
    elif not check_type or len(types)==0:
        styps = "none"
    else:
        styps = "[%s, ...total %s...]"%(types.keys()[0], len(types))
    report = "iou:%s, size:[%s,%s), types:%s, accuracy:%s, recall:%s"% (
        iou, minboxsize, maxboxsize, styps, 1 if ac_n==0 else ac_c*1.0/ac_n, 1 if rc_n==0 else rc_c*1.0/rc_n)
    summury = {
        "iou":iou,
        "accuracy":1 if ac_n==0 else ac_c*1.0/ac_n,
        "recall":1 if rc_n==0 else rc_c*1.0/rc_n,
        "miss":miss,
        "wrong":wrong,
        "right":right,
        "report":report
    }
    return summury

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值