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/ResultMerge.py
Views: 475
"""1To use the code, users should to config detpath, annopath and imagesetfile2detpath is the path for 15 result files, for the format, you can refer to "http://captain.whu.edu.cn/DOTAweb/tasks.html"3search for PATH_TO_BE_CONFIGURED to config the paths4Note, the evaluation is on the large scale images5"""6import os7import numpy as np8import dota_utils as util9import re10import time11import polyiou1213# the thresh for nms when merge image14nms_thresh = 0.3151617def py_cpu_nms_poly(dets, thresh):18scores = dets[:, 8]19polys = []20areas = []21for i in range(len(dets)):22tm_polygon = polyiou.VectorDouble([dets[i][0], dets[i][1],23dets[i][2], dets[i][3],24dets[i][4], dets[i][5],25dets[i][6], dets[i][7]])26polys.append(tm_polygon)27order = scores.argsort()[::-1]2829keep = []30while order.size > 0:31ovr = []32i = order[0]33keep.append(i)34for j in range(order.size - 1):35iou = polyiou.iou_poly(polys[i], polys[order[j + 1]])36ovr.append(iou)37ovr = np.array(ovr)38inds = np.where(ovr <= thresh)[0]39order = order[inds + 1]40return keep414243def py_cpu_nms(dets, thresh):44"""Pure Python NMS baseline."""45#print('dets:', dets)46x1 = dets[:, 0]47y1 = dets[:, 1]48x2 = dets[:, 2]49y2 = dets[:, 3]50scores = dets[:, 4]5152areas = (x2 - x1 + 1) * (y2 - y1 + 1)53# index for dets54order = scores.argsort()[::-1]5556keep = []57while order.size > 0:58i = order[0]59keep.append(i)60xx1 = np.maximum(x1[i], x1[order[1:]])61yy1 = np.maximum(y1[i], y1[order[1:]])62xx2 = np.minimum(x2[i], x2[order[1:]])63yy2 = np.minimum(y2[i], y2[order[1:]])6465w = np.maximum(0.0, xx2 - xx1 + 1)66h = np.maximum(0.0, yy2 - yy1 + 1)67inter = w * h68ovr = inter / (areas[i] + areas[order[1:]] - inter)6970inds = np.where(ovr <= thresh)[0]71order = order[inds + 1]7273return keep747576def nmsbynamedict(nameboxdict, nms, thresh):77nameboxnmsdict = {x: [] for x in nameboxdict}78for imgname in nameboxdict:79#print('imgname:', imgname)80#keep = py_cpu_nms(np.array(nameboxdict[imgname]), thresh)81#print('type nameboxdict:', type(nameboxnmsdict))82#print('type imgname:', type(imgname))83#print('type nms:', type(nms))84keep = nms(np.array(nameboxdict[imgname]), thresh)85#print('keep:', keep)86outdets = []87#print('nameboxdict[imgname]: ', nameboxnmsdict[imgname])88for index in keep:89# print('index:', index)90outdets.append(nameboxdict[imgname][index])91nameboxnmsdict[imgname] = outdets92return nameboxnmsdict939495def poly2origpoly(poly, x, y, rate):96origpoly = []97for i in range(int(len(poly)/2)):98tmp_x = float(poly[i * 2] + x) / float(rate)99tmp_y = float(poly[i * 2 + 1] + y) / float(rate)100origpoly.append(tmp_x)101origpoly.append(tmp_y)102return origpoly103104105def mergebase(srcpath, dstpath, nms):106filelist = util.GetFileFromThisRootDir(srcpath)107for fullname in filelist:108name = util.custombasename(fullname)109#print('name:', name)110dstname = os.path.join(dstpath, name + '.txt')111with open(fullname, 'r') as f_in:112nameboxdict = {}113lines = f_in.readlines()114splitlines = [x.strip().split(' ') for x in lines]115for splitline in splitlines:116subname = splitline[0]117splitname = subname.split('__')118oriname = splitname[0]119pattern1 = re.compile(r'__\d+___\d+')120#print('subname:', subname)121x_y = re.findall(pattern1, subname)122x_y_2 = re.findall(r'\d+', x_y[0])123x, y = int(x_y_2[0]), int(x_y_2[1])124125pattern2 = re.compile(r'__([\d+\.]+)__\d+___')126127rate = re.findall(pattern2, subname)[0]128129confidence = splitline[1]130poly = list(map(float, splitline[2:]))131origpoly = poly2origpoly(poly, x, y, rate)132det = origpoly133det.append(confidence)134det = list(map(float, det))135if (oriname not in nameboxdict):136nameboxdict[oriname] = []137nameboxdict[oriname].append(det)138nameboxnmsdict = nmsbynamedict(nameboxdict, nms, nms_thresh)139with open(dstname, 'w') as f_out:140for imgname in nameboxnmsdict:141for det in nameboxnmsdict[imgname]:142#print('det:', det)143confidence = det[-1]144bbox = det[0:-1]145outline = imgname + ' ' + \146str(confidence) + ' ' + ' '.join(map(str, bbox))147#print('outline:', outline)148f_out.write(outline + '\n')149150151def mergebyrec(srcpath, dstpath):152"""153srcpath: result files before merge and nms154dstpath: result files after merge and nms155"""156# srcpath = r'E:\bod-dataset\results\bod-v3_rfcn_2000000'157# dstpath = r'E:\bod-dataset\results\bod-v3_rfcn_2000000_nms'158159mergebase(srcpath,160dstpath,161py_cpu_nms)162163164def mergebypoly(srcpath, dstpath):165"""166srcpath: result files before merge and nms167dstpath: result files after merge and nms168"""169mergebase(srcpath,170dstpath,171py_cpu_nms_poly)172173174if __name__ == '__main__':175# see demo for example176mergebypoly(r'work_dirs/temp/result_raw',\177r'work_dirs/temp/result_merge')178179180