def eval_common(typestr, iou_threshold_list, withoccl=True, ptype='walk_ride', psize='near_mid_far'): all_ann_bboxes = datatools.parse_whole_dataset_ann_filtered() #所有GT all_detect_bboxes = datatools.parse_whole_dataset_detected_box( typestr) #所有检测框 fAnnBoxes = filterAnnBoxes(all_ann_bboxes, typestr, withoccl, ptype, psize) match_cnt = 0 for framestr, ann_bboxes in fAnnBoxes.items(): #遍历每一帧GoundTruth for ann_box in ann_bboxes: for detect_box in all_detect_bboxes[framestr]: # if detect_box[4]>0:continue#只评估segs # if detect_box[4]<0:continue#只评估SURF if evaltools.iou(ann_box[:4], detect_box[:4], iou_threshold_list): match_cnt = match_cnt + 1 break detect_cnt = datatools.countBoxes(all_detect_bboxes) GT_cnt = datatools.countBoxes(fAnnBoxes) fp = eval_common_fp(typestr, iou_threshold_list, withoccl, ptype, psize) print(('匹配成功:%d GT总数:%d 输出框总数:%d 检测率:%lf 虚警:%d iou = [%.2f,%.2f]') % (match_cnt, GT_cnt, detect_cnt, match_cnt / GT_cnt, fp, iou_threshold_list[0], iou_threshold_list[1]))
def split_15sat_features_train_valid(prepath, typestr, pos_iou, neg_iou): outdir = prepath + '%.1f_%.1f-%.1f_%.1f\\' % (neg_iou[0], neg_iou[1], pos_iou[0], pos_iou[1]) if not os.path.exists(outdir): os.mkdir(outdir) outtrainpath, outvalidpath = '', '' if typestr == 'far': #far outtrainpath = outdir + 'far_train.txt' outvalidpath = outdir + 'far_valid.txt' elif typestr == 'mid': #mid outtrainpath = outdir + 'mid_train.txt' outvalidpath = outdir + 'mid_valid.txt' elif typestr == 'near': #near outtrainpath = outdir + 'near_train.txt' outvalidpath = outdir + 'near_valid.txt' trainfile, validfile = open(outtrainpath, 'w'), open(outvalidpath, 'w') anns_dict = datatools.parse_all_ann_filtered() far = parse_15sat_features(typestr) for key in far.keys(): #仅处理有提取到特征的帧 for roiandfea in far[key]: #处理每一个RoI roi = roiandfea[0] features = roiandfea[1] setnum = int(key[:2]) # if setnum > 15:continue#set16-set20暂时不要 for GTs in anns_dict[key]: #一帧中的GTs if evaltools.iou(GTs[:4], roi) >= pos_iou[0] and evaltools.iou( GTs[:4], roi) <= pos_iou[1]: #大于正样本阈值,当成正样本 if (setnum <= 10): trainfile.write('+1 ' + features + '\n') elif (setnum > 10 and setnum <= 20): validfile.write('+1 ' + features + '\n') elif evaltools.iou(GTs[:4], roi) < neg_iou[1] and evaltools.iou( GTs[:4], roi) >= neg_iou[0]: if (setnum <= 10): trainfile.write('-1 ' + features + '\n') elif (setnum > 10 and setnum <= 20): validfile.write('-1 ' + features + '\n') trainfile.close() validfile.close()
def computeloss(all_ann_bboxes, all_surf_bboxes, iou_threshold_list): match_cnt = 0 for framestr, ann_bboxes in all_ann_bboxes.items(): #遍历每一帧GoundTruth for ann_box in ann_bboxes: for detect_box in all_surf_bboxes[framestr]: if evaltools.iou(ann_box[:4], detect_box[:4], iou_threshold_list): match_cnt = match_cnt + 1 break GT_cnt = datatools.countBoxes(all_ann_bboxes) loss = GT_cnt - match_cnt dr = match_cnt / GT_cnt print('检测率', dr) return loss
def eval_common_fp(typestr, iou_threshold_list, withoccl=True, ptype='walk_ride', psize='near_mid_far'): all_ann_bboxes = datatools.parse_whole_dataset_ann_filtered() #所有GT all_detect_bboxes = datatools.parse_whole_dataset_detected_box( typestr) #所有检测框 filteredBoxes = filterAnnBoxes(all_ann_bboxes, typestr, withoccl, ptype, psize) #根据行人类别,尺度过滤 match_cnt = 0 for framestr, ann_bboxes in filteredBoxes.items(): #遍历每一帧GoundTruth for detect_box in all_detect_bboxes[framestr]: for ann_box in ann_bboxes: if evaltools.iou(ann_box[:4], detect_box[:4], iou_threshold_list): match_cnt = match_cnt + 1 break detect_cnt = datatools.countBoxes(all_detect_bboxes) fp = detect_cnt - match_cnt return fp
def write_eval_sate(typestr, iou_threshold_list, withoccl=True): total_id = 0 # how many boxes(filtered) scut have matched_id = 0 #how many boxes iou greater than iou_threshold scut = [2, 3, 1, 2, 11, 10, 6, 1, 2, 1, 0, 3, 3, 1, 2, 11, 9, 7, 1, 2, 1] #scut[0] = 2 means set00/ have V000.txt,V001.txt,V002.txt #destiguish withoccl and not if withoccl == True: eval_path = './../../data/eval/sate/eval_' + 'withoccl_' + typestr + '_' + str( iou_threshold_list[0]) + '_' + str( iou_threshold_list[1]) + '_' + '.txt' else: eval_path = './../../data/eval/sate/eval_' + typestr + '_' + str( iou_threshold_list[0]) + '_' + str( iou_threshold_list[1]) + '_' + '.txt' fobj = open(eval_path, 'w') ann_id_dict = {} detect_id_dict = {} total_detect_num, temp_detect_num = 0, 0 total_matched_num, temp_matched_num = 0, 0 #process set by set for set_num in range(21): #process v by v for v_num in range(scut[set_num] + 1): ann_dict = datatools.parse_ann_filtered(set_num, v_num) for framestr, box_ann_list in ann_dict.items(): #process all box in a frame for ann_box in box_ann_list: if withoccl == False and ann_box[5] == 1: continue ann_key = '%02d%03d%05d' % (set_num, v_num, ann_box[4]) detect_key = '%02d%03d%05d' % (set_num, v_num, ann_box[4]) if ann_key not in ann_id_dict: ann_id_dict[ann_key] = 0 else: ann_id_dict[ann_key] = ann_id_dict[ann_key] + 1 if detect_key not in detect_id_dict: detect_id_dict[detect_key] = 0 print(len(ann_id_dict), len(detect_id_dict)) #process set by set for set_num in range(21): #process v by v for v_num in range(scut[set_num] + 1): ann_dict = datatools.parse_ann_filtered(set_num, v_num) detect_dict = datatools.parse_detected_box(set_num, v_num, typestr) temp_detect_num = 0 temp_matched_num = 0 #process all frames for framestr, box_ann_list in ann_dict.items(): #process all box in a frame for ann_box in box_ann_list: if withoccl == False and ann_box[5] == 1: continue first_match_flag = False for detect_box in detect_dict[framestr]: if evaltools.iou(ann_box[:4], detect_box[:4], iou_threshold_list): if first_match_flag == False: first_match_flag == True detect_key = '%02d%03d%05d' % (set_num, v_num, ann_box[4]) detect_id_dict[detect_key] = detect_id_dict[ detect_key] + 1 temp_matched_num = temp_matched_num + 1 # counter how many box in detect file temp_detect_num = temp_detect_num + len(detect_dict[framestr]) total_matched_num = total_matched_num + temp_matched_num temp_total_id = 0 temp_matched_id = 0 total_detect_num = total_detect_num + temp_detect_num for k, v in detect_id_dict.items(): if k[:5] == ('%02d%03d' % (set_num, v_num)): temp_total_id = temp_total_id + 1 if k[:5] == ('%02d%03d' % (set_num, v_num)) and v != 0: temp_matched_id = temp_matched_id + 1 if temp_total_id == 0: fobj.writelines('%02d%03d[%d %d %f] %d\n' % (set_num, v_num, temp_matched_id, temp_total_id, 0, temp_detect_num)) else: fobj.writelines( '%02d%03d[%d %d %f] %d\n' % (set_num, v_num, temp_matched_id, temp_total_id, temp_matched_id * 1.0 / temp_total_id, temp_detect_num)) total_id = total_id + temp_total_id matched_id = matched_id + temp_matched_id resstr = 'sate' if withoccl == True: resstr = resstr + '带遮挡' else: resstr = resstr + '忽略遮挡' print(resstr) print(('匹配行人数:%d 行人总数:%d 输出框总数:%d 匹配框总数:%d iou=[%.2f,%.2f]') % (matched_id, total_id, total_detect_num, total_matched_num, iou_threshold_list[0], iou_threshold_list[1])) if total_id == 0: fobj.writelines('total:[%d %d %f] %d' % (matched_id, total_id, 0, total_detect_num)) print(0) else: fobj.writelines('total:[%d %d %f] %d' % (matched_id, total_id, matched_id * 1.0 / total_id, total_detect_num)) print(matched_id * 1.0 / total_id) fobj.flush() fobj.close()
def write_eval_common(typestr, iou_threshold_list, withoccl=True): print('common') total_ann_box = 0 # how many boxes(filtered) scut have matched_box = 0 #how many boxes iou greater than iou_threshold scut = [2, 3, 1, 2, 11, 10, 6, 1, 2, 1, 0, 3, 3, 1, 2, 11, 9, 7, 1, 2, 1] #scut[0] = 2 means set00/ have V000.txt,V001.txt,V002.txt total_detect_num, temp_detect_num = 0, 0 #destiguish withoccl and not if withoccl == True: eval_path = './../../data/eval/common/eval_' + 'withoccl_' + typestr + '_' + str( iou_threshold_list[0]) + '_' + str( iou_threshold_list[1]) + '_' '.txt' else: eval_path = './../../data/eval/common/eval_' + typestr + '_' + str( iou_threshold_list[0]) + '_' + str( iou_threshold_list[1]) + '_' + '.txt' fobj = open(eval_path, 'w') cnt = 0 #process set by set for set_num in range(21): for v_num in range(scut[set_num] + 1): tempcnt = 0 ann_dict = datatools.parse_ann_filtered(set_num, v_num) detect_dict = datatools.parse_detected_box(set_num, v_num, typestr) temp_total_box = 0 temp_matched_box = 0 temp_detect_num = 0 #process all frames for framestr, box_ann_list in ann_dict.items(): #process all box in a frame for ann_box in box_ann_list: if withoccl == False and ann_box[5] == 1: continue total_ann_box = total_ann_box + 1 temp_total_box = temp_total_box + 1 for detect_box in detect_dict[framestr]: if evaltools.iou(ann_box[:4], detect_box[:4], iou_threshold_list): matched_box = matched_box + 1 temp_matched_box = temp_matched_box + 1 break tempcnt = tempcnt + len(box_ann_list) temp_detect_num = temp_detect_num + len(detect_dict[framestr]) cnt = cnt + tempcnt total_detect_num = total_detect_num + temp_detect_num if temp_total_box == 0: fobj.writelines('%02d%03d[%d %d %f] %d\n' % (set_num, v_num, temp_matched_box, temp_total_box, 0, temp_detect_num)) else: fobj.writelines( '%02d%03d[%d %d %f] %d\n' % (set_num, v_num, temp_matched_box, temp_total_box, temp_matched_box * 1.0 / temp_total_box, temp_detect_num)) if total_ann_box == 0: fobj.writelines('total:[%d %d %f] %d' % (matched_box, total_ann_box, 0, total_detect_num)) else: fobj.writelines('total:[%d %d %f] %d' % (matched_box, total_ann_box, matched_box * 1. / total_ann_box, total_detect_num)) print(cnt) resstr = 'common' if withoccl == True: resstr = resstr + '带遮挡' else: resstr = resstr + '忽略遮挡' print(resstr) print(('匹配成功:%d GT总数:%d 输出框总数:%d iou = [%.2f,%.2f]') % (matched_box, total_ann_box, total_detect_num, iou_threshold_list[0], iou_threshold_list[1])) if total_ann_box == 0: print(0) else: print(matched_box * 1. / total_ann_box) fobj.flush() fobj.close()
def write_eval_sate(typestr, iou_threshold_list, withoccl, surf_response_threshold): total_id = 0 # how many boxes(filtered) scut have matched_id = 0 #how many boxes iou greater than iou_threshold path_detect_prefix = 'H:/飒特项目/输出文件/行人检测ROI输出/recall21/' #detect bbox path scut = [2, 3, 1, 2, 11, 10, 6, 1, 2, 1, 0, 3, 3, 1, 2, 11, 9, 7, 1, 2, 1] #scut[0] = 2 means set00/ have V000.txt,V001.txt,V002.txt #destiguish withoccl and not if withoccl == True: eval_path = './../../data/surf_threshold_finetune_result/eval_' + 'withoccl_' + typestr + '_' + str( iou_threshold_list[0]) + '_' + str( iou_threshold_list[1]) + '_' + '.txt' else: eval_path = './../../data/surf_threshold_finetune_result/eval_' + typestr + '_' + str( iou_threshold_list[0]) + '_' + str( iou_threshold_list[1]) + '_' + '.txt' fobj = open(eval_path, 'a') ann_id_dict = {} detect_id_dict = {} total_detect_num, temp_detect_num = 0, 0 temp_matched_box, total_matched_box = 0, 0 total_matched_num, temp_matched_num = 0, 0 #process set by set for set_num in range(21): #process v by v for v_num in range(scut[set_num] + 1): ann_dict = parse_ann_filtered(path_ann_prefix, set_num, v_num) for framestr, box_ann_list in ann_dict.items(): #process all box in a frame for ann_box in box_ann_list: if withoccl == False and ann_box[5] == 1: continue ann_key = '%02d%03d%05d' % (set_num, v_num, ann_box[4]) detect_key = '%02d%03d%05d' % (set_num, v_num, ann_box[4]) if ann_key not in ann_id_dict: ann_id_dict[ann_key] = 0 else: ann_id_dict[ann_key] = ann_id_dict[ann_key] + 1 if detect_key not in detect_id_dict: detect_id_dict[detect_key] = 0 print(len(ann_id_dict), len(detect_id_dict)) #process set by set for set_num in range(21): #process v by v for v_num in range(scut[set_num] + 1): ann_dict = datatools.parse_ann_filtered(set_num, v_num) detect_dict = parse_detected_box(path_detect_prefix, set_num, v_num, typestr) temp_detect_num = 0 temp_matched_num = 0 #process all frames for framestr, box_ann_list in ann_dict.items(): #process all box in a frame for ann_box in box_ann_list: if withoccl == False and ann_box[5] == 1: continue first_match_flag = False for detect_box in detect_dict[framestr]: #双阈值分割贡献 # if detect_box[4] != -1:continue #头部贡献 # if detect_box[4]<surf_response_threshold:continue #总召回率 if detect_box[ 4] < surf_response_threshold and detect_box[ 4] != -1: continue if evaltools.iou(ann_box[:4], detect_box[:4], iou_threshold_list): if first_match_flag == False: first_match_flag == True detect_key = '%02d%03d%05d' % (set_num, v_num, ann_box[4]) detect_id_dict[detect_key] = detect_id_dict[ detect_key] + 1 temp_matched_num = temp_matched_num + 1 # counter how many box in detect file temp_detect_num = temp_detect_num + len(detect_dict[framestr]) total_matched_num = total_matched_num + temp_matched_num temp_total_id = 0 temp_matched_id = 0 total_detect_num = total_detect_num + temp_detect_num for k, v in detect_id_dict.items(): if k[:5] == ('%02d%03d' % (set_num, v_num)): temp_total_id = temp_total_id + 1 if k[:5] == ('%02d%03d' % (set_num, v_num)) and v != 0: temp_matched_id = temp_matched_id + 1 total_id = total_id + temp_total_id matched_id = matched_id + temp_matched_id print(( 'surf_response_threshold=%d matech:%d total_ann:%d total_surf__detect:%d total_matched_num:%d iou_threshold[0] = %.2f iou_threshold[1] = %.2f' ) % (surf_response_threshold, matched_id, total_id, total_detect_num, total_matched_num, iou_threshold_list[0], iou_threshold_list[1])) if total_id == 0: fobj.writelines('surf_response_threshold = %d total:[%d %d %f] %d\n' % (surf_response_threshold, matched_id, total_id, 0, total_detect_num)) print(0) else: fobj.writelines('surf_response_threshold = %d total:[%d %d %f] %d\n' % (surf_response_threshold, matched_id, total_id, matched_id * 1.0 / total_id, total_detect_num)) print(matched_id * 1.0 / total_id) fobj.flush() fobj.close()
def write_eval_common(typestr, iou_threshold_list, withoccl, surf_response_threshold): total_ann_box = 0 # how many boxes(filtered) scut have matched_box = 0 #how many boxes iou greater than iou_threshold path_detect_prefix = 'H:/飒特项目/输出文件/行人检测ROI输出/recall18/' #detect bbox path scut = [2, 3, 1, 2, 11, 10, 6, 1, 2, 1, 0, 3, 3, 1, 2, 11, 9, 7, 1, 2, 1] #scut[0] = 2 means set00/ have V000.txt,V001.txt,V002.txt total_detect_num, temp_detect_num = 0, 0 #destiguish withoccl and not if withoccl == True: eval_path = './../../data/surf_threshold_finetune_result/eval_' + 'withoccl_' + typestr + '_' + str( iou_threshold_list[0]) + '_' + str( iou_threshold_list[1]) + '_' '.txt' else: eval_path = './../../data/surf_threshold_finetune_result/eval_' + typestr + '_' + str( iou_threshold_list[0]) + '_' + str( iou_threshold_list[1]) + '_' + '.txt' fobj = open(eval_path, 'a') #process set by set for set_num in range(21): #process v by v for v_num in range(scut[set_num] + 1): # if v_num != 10:continue ann_dict = datatools.parse_ann_filtered(set_num, v_num) detect_dict = parse_detected_box(path_detect_prefix, set_num, v_num, typestr) temp_total_box, temp_matched_box, temp_detect_num = 0, 0, 0 #process all frames for framestr, box_ann_list in ann_dict.items(): count_detect = 0 #统计单帧总检测框数量 #process all box in a frame for ann_box in box_ann_list: # if ann_box[3] >= 90 or ann_box[3] <48:continue # if ann_box[3] < 90:continue if ann_box[3] > 48: continue if withoccl == False and ann_box[5] == 1: continue total_ann_box = total_ann_box + 1 temp_total_box = temp_total_box + 1 for detect_box in detect_dict[framestr]: #如果surf响应值阈值小于设定的阈值则将该surf提出的RoI过滤掉,-1代表阈值分割提出的RoI, #双阈值分割贡献 # if detect_box[4] == -1:continue #头部贡献 # if detect_box[4]<surf_response_threshold:continue #总召回率,忽略surf得分低的RoI if detect_box[ 4] < surf_response_threshold and detect_box[ 4] != -1: continue if evaltools.iou(ann_box[:4], detect_box[:4], iou_threshold_list): matched_box = matched_box + 1 temp_matched_box = temp_matched_box + 1 break # count_detect +=len(detect_dict[framestr] ) # count_detect += len([box for box in detect_dict[framestr] if (box[4]>surf_response_threshold or box[4]== -1)]) # count_detect += len([box for box in detect_dict[framestr] if box[4] == -1]) # temp_detect_num = temp_detect_num + count_detect temp_detect_num = temp_detect_num + len(detect_dict[framestr]) total_detect_num = total_detect_num + temp_detect_num if total_ann_box == 0: fobj.writelines('surf_response_threshold = %d-- total:[%d %d %f] %d' % (surf_response_threshold, matched_box, total_ann_box, 0, total_detect_num)) else: fobj.writelines('surf_response_threshold = %d total:[%d %d %f] %d\n' % (surf_response_threshold, matched_box, total_ann_box, matched_box * 1. / total_ann_box, total_detect_num)) print(( 'surf_threshold = %d,matech:%d total_ann:%d total_surf__detect:%d iou_threshold[ %.2f , %.2f]' ) % (surf_response_threshold, matched_box, total_ann_box, total_detect_num, iou_threshold_list[0], iou_threshold_list[1])) if total_ann_box == 0: print(0) else: print(matched_box * 1. / total_ann_box) fobj.flush() fobj.close()
def statistic_fast(typestr, iou_threshold_list, fastthresh, numthresh, withoccl=True): total_ann_box = 0 # how many boxes(filtered) scut have matched_box = 0 #how many boxes iou greater than iou_threshold path_ann_prefix = global_data.ann_filter_path #parsed scut ann path path_detect_prefix = 'H:/飒特项目/输出文件/行人检测ROI输出/recall27/' #detect bbox path scut = [2, 3, 1, 2, 11, 10, 6, 1, 2, 1, 0, 3, 3, 1, 2, 11, 9, 7, 1, 2, 1] #scut[0] = 2 means set00/ have V000.txt,V001.txt,V002.txt total_detect_num, temp_detect_num = 0, 0 temp_fast_match_num = 0 for set_num in range(21): for v_num in range(scut[set_num] + 1): ann_dict = datatools.parse_ann_filtered(set_num, v_num) addROI_dict = parse_detected_box( 'H:/飒特项目/输出文件/行人检测ROI输出/recall26/', set_num, v_num, 'addROI') detect_dict = parse_detected_box(path_detect_prefix, set_num, v_num, typestr) temp_total_box = 0 temp_matched_box = 0 temp_detect_num = 0 #process all frames for framestr, box_ann_list in ann_dict.items(): #process all box in a frame for ann_box in box_ann_list: # if ann_box[3] >= 90 or ann_box[3] <48:continue # if ann_box[3] < 90:continue # if ann_box[3] > 48:continue if withoccl == False and ann_box[5] == 1: continue total_ann_box = total_ann_box + 1 temp_total_box = temp_total_box + 1 for detect_box in addROI_dict[framestr]: #RoI提取的框 if not evaltools.iou(ann_box[:4], detect_box[:4], iou_threshold_list): continue # temp_fast_match_num+=1#统计有多少是与 if fastFilter(detect_box, detect_dict[framestr], fastthresh, numthresh): matched_box = matched_box + 1 temp_matched_box = temp_matched_box + 1 break # for detect_box in detect_dict[framestr]: # if fastKPinAnn(ann_box[:4],detect_box[:5],fastthresh) : # matched_box = matched_box + 1 # temp_matched_box = temp_matched_box + 1 # break temp_detect_num = temp_detect_num + len(detect_dict[framestr]) total_detect_num = total_detect_num + temp_detect_num print(( 'matech:%d total_ann:%d total_detect:%d iou_threshold[0] = %.2f iou_threshold[1] = %.2f' ) % (matched_box, total_ann_box, total_detect_num, iou_threshold_list[0], iou_threshold_list[1])) if total_ann_box == 0: print(0) return 0 else: print(matched_box * 1. / total_ann_box) return matched_box * 1. / total_ann_box