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/dota_evaluation_task2.py
Views: 475
# --------------------------------------------------------1# dota_evaluation_task12# Licensed under The MIT License [see LICENSE for details]3# Written by Jian Ding, based on code from Bharath Hariharan4# --------------------------------------------------------56"""7To use the code, users should to config detpath, annopath and imagesetfile8detpath is the path for 15 result files, for the format, you can refer to "http://captain.whu.edu.cn/DOTAweb/tasks.html"9search for PATH_TO_BE_CONFIGURED to config the paths10Note, the evaluation is on the large scale images11"""12import xml.etree.ElementTree as ET13import os14#import cPickle15import numpy as np16import matplotlib.pyplot as plt1718def parse_gt(filename):19objects = []20with open(filename, 'r') as f:21lines = f.readlines()22splitlines = [x.strip().split(' ') for x in lines]23for splitline in splitlines:24object_struct = {}25if len(splitline) < 9:26continue27object_struct['name'] = splitline[8]28if (len(splitline) == 9):29object_struct['difficult'] = 030elif (len(splitline) == 10):31object_struct['difficult'] = int(splitline[9])32# object_struct['difficult'] = 033object_struct['bbox'] = [int(float(splitline[0])),34int(float(splitline[1])),35int(float(splitline[4])),36int(float(splitline[5]))]37w = int(float(splitline[4])) - int(float(splitline[0]))38h = int(float(splitline[5])) - int(float(splitline[1]))39object_struct['area'] = w * h40#print('area:', object_struct['area'])41# if object_struct['area'] < (15 * 15):42# #print('area:', object_struct['area'])43# object_struct['difficult'] = 144objects.append(object_struct)45return objects46def voc_ap(rec, prec, use_07_metric=False):47""" ap = voc_ap(rec, prec, [use_07_metric])48Compute VOC AP given precision and recall.49If use_07_metric is true, uses the50VOC 07 11 point method (default:False).51"""52if use_07_metric:53# 11 point metric54ap = 0.55for t in np.arange(0., 1.1, 0.1):56if np.sum(rec >= t) == 0:57p = 058else:59p = np.max(prec[rec >= t])60ap = ap + p / 11.61else:62# correct AP calculation63# first append sentinel values at the end64mrec = np.concatenate(([0.], rec, [1.]))65mpre = np.concatenate(([0.], prec, [0.]))6667# compute the precision envelope68for i in range(mpre.size - 1, 0, -1):69mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i])7071# to calculate area under PR curve, look for points72# where X axis (recall) changes value73i = np.where(mrec[1:] != mrec[:-1])[0]7475# and sum (\Delta recall) * prec76ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1])77return ap7879def voc_eval(detpath,80annopath,81imagesetfile,82classname,83# cachedir,84ovthresh=0.5,85use_07_metric=False):86"""rec, prec, ap = voc_eval(detpath,87annopath,88imagesetfile,89classname,90[ovthresh],91[use_07_metric])92Top level function that does the PASCAL VOC evaluation.93detpath: Path to detections94detpath.format(classname) should produce the detection results file.95annopath: Path to annotations96annopath.format(imagename) should be the xml annotations file.97imagesetfile: Text file containing the list of images, one image per line.98classname: Category name (duh)99cachedir: Directory for caching the annotations100[ovthresh]: Overlap threshold (default = 0.5)101[use_07_metric]: Whether to use VOC07's 11 point AP computation102(default False)103"""104# assumes detections are in detpath.format(classname)105# assumes annotations are in annopath.format(imagename)106# assumes imagesetfile is a text file with each line an image name107# cachedir caches the annotations in a pickle file108109# first load gt110#if not os.path.isdir(cachedir):111# os.mkdir(cachedir)112#cachefile = os.path.join(cachedir, 'annots.pkl')113# read list of images114with open(imagesetfile, 'r') as f:115lines = f.readlines()116imagenames = [x.strip() for x in lines]117#print('imagenames: ', imagenames)118#if not os.path.isfile(cachefile):119# load annots120recs = {}121for i, imagename in enumerate(imagenames):122#print('parse_files name: ', annopath.format(imagename))123recs[imagename] = parse_gt(annopath.format(imagename))124#if i % 100 == 0:125# print ('Reading annotation for {:d}/{:d}'.format(126# i + 1, len(imagenames)) )127# save128#print ('Saving cached annotations to {:s}'.format(cachefile))129#with open(cachefile, 'w') as f:130# cPickle.dump(recs, f)131#else:132# load133#with open(cachefile, 'r') as f:134# recs = cPickle.load(f)135136# extract gt objects for this class137class_recs = {}138npos = 0139for imagename in imagenames:140R = [obj for obj in recs[imagename] if obj['name'] == classname]141bbox = np.array([x['bbox'] for x in R])142difficult = np.array([x['difficult'] for x in R]).astype(np.bool)143det = [False] * len(R)144npos = npos + sum(~difficult)145class_recs[imagename] = {'bbox': bbox,146'difficult': difficult,147'det': det}148149# read dets150detfile = detpath.format(classname)151with open(detfile, 'r') as f:152lines = f.readlines()153154splitlines = [x.strip().split(' ') for x in lines]155image_ids = [x[0] for x in splitlines]156confidence = np.array([float(x[1]) for x in splitlines])157158#print('check confidence: ', confidence)159160BB = np.array([[float(z) for z in x[2:]] for x in splitlines])161162# sort by confidence163sorted_ind = np.argsort(-confidence)164sorted_scores = np.sort(-confidence)165166#print('check sorted_scores: ', sorted_scores)167#print('check sorted_ind: ', sorted_ind)168BB = BB[sorted_ind, :]169image_ids = [image_ids[x] for x in sorted_ind]170#print('check imge_ids: ', image_ids)171#print('imge_ids len:', len(image_ids))172# go down dets and mark TPs and FPs173nd = len(image_ids)174tp = np.zeros(nd)175fp = np.zeros(nd)176for d in range(nd):177R = class_recs[image_ids[d]]178bb = BB[d, :].astype(float)179ovmax = -np.inf180BBGT = R['bbox'].astype(float)181182if BBGT.size > 0:183# compute overlaps184# intersection185ixmin = np.maximum(BBGT[:, 0], bb[0])186iymin = np.maximum(BBGT[:, 1], bb[1])187ixmax = np.minimum(BBGT[:, 2], bb[2])188iymax = np.minimum(BBGT[:, 3], bb[3])189iw = np.maximum(ixmax - ixmin + 1., 0.)190ih = np.maximum(iymax - iymin + 1., 0.)191inters = iw * ih192193# union194uni = ((bb[2] - bb[0] + 1.) * (bb[3] - bb[1] + 1.) +195(BBGT[:, 2] - BBGT[:, 0] + 1.) *196(BBGT[:, 3] - BBGT[:, 1] + 1.) - inters)197198overlaps = inters / uni199ovmax = np.max(overlaps)200## if there exist 2201jmax = np.argmax(overlaps)202203if ovmax > ovthresh:204if not R['difficult'][jmax]:205if not R['det'][jmax]:206tp[d] = 1.207R['det'][jmax] = 1208else:209fp[d] = 1.210# print('filename:', image_ids[d])211else:212fp[d] = 1.213214# compute precision recall215216print('check fp:', fp)217print('check tp', tp)218219220print('npos num:', npos)221fp = np.cumsum(fp)222tp = np.cumsum(tp)223224rec = tp / float(npos)225# avoid divide by zero in case the first detection matches a difficult226# ground truth227prec = tp / np.maximum(tp + fp, np.finfo(np.float64).eps)228ap = voc_ap(rec, prec, use_07_metric)229230return rec, prec, ap231232def main():233# detpath = r'E:\documentation\OneDrive\documentation\DotaEvaluation\evluation_task2\evluation_task2\faster-rcnn-nms_0.3_task2\nms_0.3_task\Task2_{:s}.txt'234# annopath = r'I:\dota\testset\ReclabelTxt-utf-8\{:s}.txt'235# imagesetfile = r'I:\dota\testset\va.txt'236237detpath = r'tools/parse_pkl/evaluation_results/dotav2_merged_HBB/Task2_{:s}.txt'238annopath = r'/dataset/Dota/Dota_V2.0/val/labelTxt-v2.0/Val_Task2_gt/{:s}.txt'# change the directory to the path of val/labelTxt, if you want to do evaluation on the valset239imagesetfile = r'tools/parse_pkl/evaluation_results/imgnamefile_val2.0.txt'240241classnames = [ 'plane', 'baseball-diamond', 'bridge', 'ground-track-field', 'small-vehicle', 'large-vehicle', 'ship',242'tennis-court', 'basketball-court', 'storage-tank', 'soccer-ball-field', 'roundabout', 'harbor',243'swimming-pool', 'helicopter', 'container-crane', 'airport', 'helipad']244classaps = []245map = 0246for classname in classnames:247print('classname:', classname)248rec, prec, ap = voc_eval(detpath,249annopath,250imagesetfile,251classname,252ovthresh=0.5,253use_07_metric=True)254map = map + ap255#print('rec: ', rec, 'prec: ', prec, 'ap: ', ap)256print('ap: ', ap)257classaps.append(ap)258259## uncomment to plot p-r curve for each category260# plt.figure(figsize=(8,4))261# plt.xlabel('recall')262# plt.ylabel('precision')263# plt.plot(rec, prec)264# plt.show()265map = map/len(classnames)266print('map:', map)267classaps = 100*np.array(classaps)268print('classaps: ', classaps)269if __name__ == '__main__':270main()271272