Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/DOTA_devkit/mAOE_evaluation.py
Views: 475
# --------------------------------------------------------1# mAOEevaluation2# --------------------------------------------------------34"""5To use the code, users should to config detpath, annopath and imagesetfile6detpath is the path for class result files. The evaluation is performed on the merging results.7"""89import numpy as np10import polyiou11from dota_poly2rbox import poly2rbox_single_v2, poly2rbox_single_v31213def parse_gt(filename):14"""15:param filename: ground truth file to parse16:return: all instances in a picture17"""18objects = []19with open(filename, 'r') as f:20while True:21line = f.readline()22if line:23splitlines = line.strip().split(' ')24object_struct = {}25if (len(splitlines) < 9):26continue27object_struct['name'] = splitlines[8]2829if (len(splitlines) == 9):30object_struct['difficult'] = 031elif (len(splitlines) == 10):32object_struct['difficult'] = int(splitlines[9])33object_struct['bbox'] = [float(splitlines[0]),34float(splitlines[1]),35float(splitlines[2]),36float(splitlines[3]),37float(splitlines[4]),38float(splitlines[5]),39float(splitlines[6]),40float(splitlines[7])]41objects.append(object_struct)42else:43break44return objects454647def aoe_eval(detpath,48annopath,49imagesetfile,50classname,51# cachedir,52ovthresh=0.5):53"""rec, prec, ap = aoe_eval(detpath,54annopath,55imagesetfile,56classname,57[ovthresh])5859detpath: Path to detections60detpath.format(classname) should produce the detection results file.61annopath: Path to annotations62annopath.format(imagename) should be the xml annotations file.63imagesetfile: Text file containing the list of images, one image per line.64classname: Category name (duh)65cachedir: Directory for caching the annotations66[ovthresh]: Overlap threshold (default = 0.7)67"""6869with open(imagesetfile, 'r') as f:70lines = f.readlines()71imagenames = [x.strip() for x in lines]7273recs = {}74for i, imagename in enumerate(imagenames):75recs[imagename] = parse_gt(annopath.format(imagename))7677# extract gt objects for this class78class_recs = {}79npos = 080for imagename in imagenames:81R = [obj for obj in recs[imagename] if obj['name'] == classname]82bbox = np.array([x['bbox'] for x in R])83difficult = np.array([x['difficult'] for x in R]).astype(np.bool)84det = [False] * len(R)85npos = npos + sum(~difficult)86class_recs[imagename] = {'bbox': bbox,87'difficult': difficult,88'det': det}8990# read dets from Task1* files91detfile = detpath.format(classname)92with open(detfile, 'r') as f:93lines = f.readlines()9495splitlines = [x.strip().split(' ') for x in lines]96image_ids = [x[0] for x in splitlines]97confidence = np.array([float(x[1]) for x in splitlines])9899BB = np.array([[float(z) for z in x[2:]] for x in splitlines])100101# sort by confidence102sorted_ind = np.argsort(-confidence)103# sorted_scores = np.sort(-confidence)104105## note the usage only in numpy not for list106BB = BB[sorted_ind, :]107image_ids = [image_ids[x] for x in sorted_ind]108# go down dets and mark TPs and FPs109nd = len(image_ids)110111angle_dif_list = []112for d in range(nd):113R = class_recs[image_ids[d]]114bb = BB[d, :].astype(float)115ovmax = -np.inf116BBGT = R['bbox'].astype(float)117118## compute det bb with each BBGT119if BBGT.size > 0:120# 1. calculate the overlaps between hbbs, if the iou between hbbs are 0, the iou between obbs are 0, too.121# pdb.set_trace()122BBGT_xmin = np.min(BBGT[:, 0::2], axis=1)123BBGT_ymin = np.min(BBGT[:, 1::2], axis=1)124BBGT_xmax = np.max(BBGT[:, 0::2], axis=1)125BBGT_ymax = np.max(BBGT[:, 1::2], axis=1)126bb_xmin = np.min(bb[0::2])127bb_ymin = np.min(bb[1::2])128bb_xmax = np.max(bb[0::2])129bb_ymax = np.max(bb[1::2])130131ixmin = np.maximum(BBGT_xmin, bb_xmin)132iymin = np.maximum(BBGT_ymin, bb_ymin)133ixmax = np.minimum(BBGT_xmax, bb_xmax)134iymax = np.minimum(BBGT_ymax, bb_ymax)135iw = np.maximum(ixmax - ixmin + 1., 0.)136ih = np.maximum(iymax - iymin + 1., 0.)137inters = iw * ih138139# union140uni = ((bb_xmax - bb_xmin + 1.) * (bb_ymax - bb_ymin + 1.) +141(BBGT_xmax - BBGT_xmin + 1.) *142(BBGT_ymax - BBGT_ymin + 1.) - inters)143144overlaps = inters / uni145146BBGT_keep_mask = overlaps > 0147BBGT_keep = BBGT[BBGT_keep_mask, :]148# BBGT_keep_index = np.where(overlaps > 0)[0]149150def calcoverlaps(BBGT_keep, bb):151overlaps = []152for index, GT in enumerate(BBGT_keep):153overlap = polyiou.iou_poly(polyiou.VectorDouble(BBGT_keep[index]), polyiou.VectorDouble(bb))154overlaps.append(overlap)155return overlaps156if len(BBGT_keep) > 0:157overlaps = calcoverlaps(BBGT_keep, bb)158ovmax = np.max(overlaps)159if ovmax > ovthresh:160jmax = np.argmax(overlaps)161angle_box_GT = poly2rbox_single_v3(BBGT_keep[jmax])162angel_GT = angle_box_GT[-1]163164angle_box_bb = poly2rbox_single_v3(bb)165angel_bb = angle_box_bb[-1]166167angle_dif = abs(angel_bb - angel_GT) * 57.32168angle_dif_list.append(angle_dif)169170return angle_dif_list171172def main():173# detpath = r'/mnt/SSD/lwt_workdir/data/dota_angle/result_merge_fasterrcnn/{:s}.txt'174# annopath = r'/mnt/SSD/lwt_workdir/data/dota_new/val/labelTxt/{:s}.txt' # change the directory to the path of val/labelTxt, if you want to do evaluation on the valset175# imagesetfile = r'/mnt/SSD/lwt_workdir/data/dota_new/val/test.txt'176detpath = r'/data1/OrientedRepPoints/tools/parse_pkl/evaluation_results/40epoch_detection_results_merged/Task1_{:s}.txt'177annopath = r'/dataset/buyingjia/Dota/Dota_V1.0/val/labelTxt/{:s}.txt' # change the directory to the path of val/labelTxt, if you want to do evaluation on the valset178imagesetfile = r'/data1/OrientedRepPoints/tools/parse_pkl/evaluation_results/imgnamefile_val1.0.txt'179180# For DOTA-v1.0181classnames = ['plane', 'baseball-diamond', 'bridge', 'ground-track-field', 'small-vehicle', 'large-vehicle', 'ship', 'tennis-court',182'basketball-court', 'storage-tank', 'soccer-ball-field', 'roundabout', 'harbor', 'swimming-pool', 'helicopter']183184# for hrsc2016185# classnames = ['ship']186187# for ucas_aod188# classname = ['airplane', 'car']189190classaps = []191for classname in classnames:192print('classname:', classname)193angel_dif_list = aoe_eval(detpath,194annopath,195imagesetfile,196classname,197ovthresh=0.7) # set 0.7 as default198199angle_dif = 0.0200201for item in angel_dif_list:202angle_dif = angle_dif+item203204angle_dif_ave = angle_dif/len(angel_dif_list)205print('angle_dif_ave: ', angle_dif_ave)206classaps.append(angle_dif_ave)207208print('mAOE: ', sum(classaps)/len(classaps))209if __name__ == '__main__':210main()211212213