def label_process(detect_result, iou_thresh, iou_thresh11):
    assert detect_result != ''
    x_center, y_center = detect_result[1::6], detect_result[2::6]
    w_center, h_center = detect_result[3::6], detect_result[4::6]
    detect_result[1::6] = [i - 0.5 * j for i, j in zip(x_center, w_center)]
    detect_result[2::6] = [i - 0.5 * j for i, j in zip(y_center, h_center)]
    detect_result[3::6] = [i + 0.5 * j for i, j in zip(x_center, w_center)]
    detect_result[4::6] = [i + 0.5 * j for i, j in zip(y_center, h_center)]
    list_new = []

    for label_values in np.unique(detect_result[::6]):
        list_values = np.array([
            detect_result[6 * idx:6 * idx + 6]
            for idx, i in enumerate(detect_result[::6]) if i == label_values
        ])
        boxes = list_values[:, 1:5].tolist()
        scores = list_values[:, 5].tolist()
        labels = list_values[:, 0].tolist()
        if label_values in [2, 11]:
            boxes, scores, labels = nms([boxes], [scores], [labels],
                                        weights=None,
                                        iou_thr=iou_thresh11)
        else:
            boxes, scores, labels = nms([boxes], [scores], [labels],
                                        weights=None,
                                        iou_thr=iou_thresh)

        for box in list_values:
            if box[-1] in scores:
                list_new.extend(box)
    return list_new
def ensemble_models(ipt_json_paths, opt_json_path, img_ann_path, weights, method='weighted_boxes_fusion', iou_thr=0.3,
                    skip_box_thr=0.0001,
                    sigma=0.1):
    img_info_dicts = mmcv.load(img_ann_path)['images']
    img_info_dict = dict()
    for img_info in img_info_dicts:
        img_info_dict[img_info['id']] = img_info

    res_dicts = []
    res_dict = dict()
    for json_path in ipt_json_paths:
        res_dicts.append(json_to_lisdict(json_path, img_info_dict))
    for img_id in res_dicts[0]:
        boxes_list = []
        scores_list = []
        labels_list = []
        for i in range(len(res_dicts)):
            if img_id not in res_dicts[i]:
                boxes_list.append([])
                scores_list.append([])
                labels_list.append([])

            else:
                boxes_list.append(res_dicts[i][img_id]['boxes'])
                scores_list.append(res_dicts[i][img_id]['scores'])
                labels_list.append(res_dicts[i][img_id]['labels'])

        if method == 'nms':
            boxes, scores, labels = nms(boxes_list, scores_list, labels_list, weights=weights, iou_thr=iou_thr)
        elif method == 'soft_nms':
            boxes, scores, labels = soft_nms(boxes_list, scores_list, labels_list, weights=weights, iou_thr=iou_thr,
                                             sigma=sigma, thresh=skip_box_thr)
        elif method == 'non_maximum_weighted':
            boxes, scores, labels = non_maximum_weighted(boxes_list, scores_list, labels_list, weights=weights,
                                                         iou_thr=iou_thr,
                                                         skip_box_thr=skip_box_thr)
        else:
            boxes, scores, labels = weighted_boxes_fusion(boxes_list, scores_list, labels_list, weights=weights,
                                                          iou_thr=iou_thr,
                                                          skip_box_thr=skip_box_thr)
        res_dict[img_id] = dict(boxes=boxes, scores=scores, labels=labels)

    lisdict_to_json(res_dict, opt_json_path, img_info_dict)
Example #3
0
def ensemble_multibox(boxes, scores, labels, iou_thr, sigma,
                      skip_box_thr, weights=None, method='wbf'):
    if method=='nms':
        boxes, scores, labels = nms(boxes, scores, labels,
                                    weights=weights,
                                    iou_thr=iou_thr)
    elif method=='soft_nms':
        boxes, scores, labels = soft_nms(boxes, scores, labels,
                                         weights=weights,
                                         sigma=sigma,
                                         iou_thr=iou_thr,
                                         thresh=skip_box_thr)
    elif method=='nms_weight':
        boxes, scores, labels = non_maximum_weighted(boxes, scores, labels,
                                                     weights=weights,
                                                     iou_thr=iou_thr,
                                                     skip_box_thr=skip_box_thr)
    elif method=='wbf':
        boxes, scores, labels = weighted_boxes_fusion(boxes, scores, labels,
                                                      weights=weights,
                                                      iou_thr=iou_thr,
                                                      skip_box_thr=skip_box_thr)
    
    return boxes, scores, labels
Example #4
0
    def mergeResults(self,outfile,merge_input_mode="xywh",is_nms=True,nms_thresh=0.9,nms_name="nms"):
        """
        :param is_nms: do non-maximum suppression on after merge
        :param nms_thresh: non-maximum suppression IoU threshold
        :return:
        """
        print('Loading source annotation json file: {}'.format(self.srcannopath))
        with open(self.srcannopath, 'r') as load_f:
            srcanno = json.load(load_f)
        mergedresults = defaultdict(list)
        # random.seed(0)
        # print("len",len(self.results))
        keep_input=[]
        for (filename, objlist) in self.results.items():
        # for (filename, objlist) in random.sample(self.results.items(),2):
            # srcfile, scale, left, up  = filename.split('___')
            # srcfile =srcfile.replace('_IMG', '/IMG')+".jpg"
            # up =up[:-4]
            # # print(filename, objlist)
            # # srcimageid =srcfile[-2:]
            # # print(srcfile)
            # srcimageid = srcanno[srcfile]['image id']
            # print("srcimageid",srcimageid)
            # print(srcfile, scale, left, up )

            srcfile, paras = filename.split('___')#srcfile, paras 15_Nanshani_Park_IMG_15_04 0.5__4224__6144.jpg
            # print("srcfile, paras",srcfile, paras )
            srcfile = srcfile.replace('_IMG', '/IMG') + self.imgext#02_Xili_Crossroad_IMG_02_01___0.5__0__0.jpg
            srcimageid = srcanno[srcfile]['image id']
            scale, left, up = paras.replace(self.imgext, '').split('__')#scale, left, up  0.5 4224 6144
            print(srcfile, scale, left, up )
            print(f"before objlist {len(objlist)}")
            if not iskeep_dets:
                for objdict in objlist:
                    mergedresults[srcimageid].append([*recttransfer(objdict['bbox'], float(scale), int(left), int(up),merge_input_mode),objdict['score'], objdict['category_id'],objdict['image_id']])
            if iskeep_dets:
                keep_input=keep_input+[[srcimageid,int(left), int(up),float(scale),i['bbox'][0],i['bbox'][1],i['bbox'][2],i['bbox'][3],i['score'],i['image_id'],i['category_id']] for i in objlist]
            # keep_input.append([[srcimageid,int(left), int(up),float(scale),i['bbox'][0],i['bbox'][1],i['bbox'][2],i['bbox'][3],i['score'],i['image_id'],i['category_id']] for i in objlist])
        if iskeep_dets:
            print(f"before keep {len(keep_input)}")
            if 391<=srcimageid<=420:#14otcUP_boundary
                keep_dets,_keep_values=self.keep_dets(np.array(keep_input),UP_boundary[0],PANDA_TEST_SIZE[0])
            if 421<=srcimageid<=450:#15 nanshangongyuan
                keep_dets,_keep_values=self.keep_dets(np.array(keep_input),UP_boundary[1],PANDA_TEST_SIZE[1])
            if 451<=srcimageid<=465:#16xiaoxue----------01
                keep_dets,_keep_values=self.keep_dets(np.array(keep_input),UP_boundary[2],PANDA_TEST_SIZE[2])
            if 466<=srcimageid<=480:#16xiaoxue--------02
                keep_dets,_keep_values=self.keep_dets(np.array(keep_input),UP_boundary[3],PANDA_TEST_SIZE[2])
            if 481<=srcimageid<=510:#17zhongguan
                keep_dets,_keep_values=self.keep_dets(np.array(keep_input),UP_boundary[4],PANDA_TEST_SIZE[3])
            if 511<=srcimageid<=540:#18xilin-------01
                keep_dets,_keep_values=self.keep_dets(np.array(keep_input),UP_boundary[5],PANDA_TEST_SIZE[4])
            if 541<=srcimageid<=555:#18xilin----------02
                keep_dets,_keep_values=self.keep_dets(np.array(keep_input),UP_boundary[6],PANDA_TEST_SIZE[5])
            print(f"after keep {len(keep_dets)}")
            for objdict,keep_value in zip(keep_dets,_keep_values):
                left, up,scale=keep_value[0],keep_value[1],keep_value[2]
                # print("objdict",objdict)#{'image_id': 7110, 'bbox': [47.7, 866.2, 198.8, 442.8], 'score': 0.83231, 'category_id': 1}
                mergedresults[srcimageid].append([*recttransfer(objdict['bbox'], float(scale), int(left), int(up),merge_input_mode),
                        objdict['score'], objdict['category_id'],objdict['image_id']])
                
        img_size = {}
        for anno in srcanno:
            # print(srcanno[anno]['image id'])
            img_size[srcanno[anno]['image id']] = srcanno[anno]['image size']
        if is_nms:
            if nms_name=="nms":
                for (imageid, objlist) in mergedresults.items():
                    masxlist=[i[2]*i[3] for i in objlist]
                    max_wh=np.max(masxlist)
                    objlist=[[i[0],i[1],i[2],i[3],i[4]*0.05+i[3]*i[2]*0.95/max_wh,i[5],i[6]] for i in objlist ]
                    keep = py_cpu_nms(np.array(objlist), nms_thresh)
                    outdets = []
                    for index in keep:
                        outdets.append(objlist[index])
                    mergedresults[imageid] = outdets
            if nms_name=="setnms":
                for (imageid, objlist) in mergedresults.items():
                    print("input nms element",objlist[0])#[829, 5939, 923, 6000, 0.24672751128673553, 1, 149]
                    print(f"before setnms {nms_thresh} ",len(objlist))
                    keep=np.array(objlist)[set_cpu_nms(np.array(objlist), nms_thresh)].tolist()
                    # print("keep",keep,"\n",len(keep),type(keep))
                    print(f"{imageid} after setnms_{nms_thresh} ",len(keep))
                    mergedresults[imageid] = keep
            if nms_name=="emnms":
                for (imageid, objlist) in mergedresults.items():
                    size_anno = img_size[imageid]
                    boxes = [[obj[0] / size_anno['width'], obj[1] / size_anno['height'],
                              obj[2] / size_anno['width'], obj[3] / size_anno['height']] for obj in objlist]
                    scores = [obj[4] for obj in objlist]
                    labels = [obj[5] for obj in objlist]
                    boxes, scores, labels = nms([boxes], [scores], [labels])
                    boxes[:, [0, 2]] *= size_anno['width']
                    boxes[:, [1, 3]] *= size_anno['height']
                    outdets = [x[0] + [x[1], x[2]] for x in zip(boxes.tolist(), scores.tolist(), labels.tolist())]
                    mergedresults[imageid] = outdets
            if nms_name=="softnms":
                for (imageid, objlist) in mergedresults.items():
                    print(f"{imageid} before softnms_{nms_thresh} ",len(objlist))
                    # print("ssss",len(objlist[0]))
                    # print("ssss",objlist[0])
                    masxlist=[i[2]*i[3] for i in objlist]
                    max_wh=np.max(masxlist)
                    objlist=[[i[0],i[1],i[2],i[3],i[4]*0.05+i[3]*i[2]*0.95/max_wh,i[5],i[6]] for i in objlist ]
                    
                    # tempmax=np.max(np.array(objlist)[:, 4])
                    # print("max",tempmax)#208909381.05317593
                    # objlist=[[i[0],i[1],i[2],i[3],i[4]/(tempmax+0.00001),i[5],i[6]] for i in objlist ]
                    # print(objlist)

                    newdets,keep=soft_nms(np.array(objlist),iou_thr=nms_thresh, method='linear',sigma=0.5, min_score=1e-3)#'gaussian''linear',
                    # keep =py_cpu_softnms(np.array(objlist),thresh=nms_thresh, Nt=0.02, sigma=0.5, method=1)
                    # print(keep)
                    outdets = []
                    for index in keep:
                        outdets.append(objlist[index])
                    print(f"{imageid} after softnms_{nms_thresh} ",len(keep))
                    mergedresults[imageid] = outdets
        savelist = []
        def say2(iss):
            imageid, objlist=iss[0],iss[1]
            # print(imageid, objlist)
            templist=[]
            for obj in objlist:#obj [22528, 1270, 24576, 1, 1.0, 4]
                # print(obj)
                templist.append({
                    "image_id": imageid,
                    "category_id": obj[5],
                    "bbox": tlbr2tlwh(obj[:4]),
                    "score": obj[4]
                })
            if test:
                print(f"fliter berfore len {len(templist)}")
            if isfliter:
                if 391<=imageid<=420:#14otc
                    templist=fliter(templist,fliterscore["14_OCT"],AnotPath="/root/data/gvision/dataset/xml/14_OCT_Habour.xml",
                    segma_woh=3,segma_area=3,up_bound=4000,down_bound=None,down_fs=0.95,yichang=0)
                if 421<=imageid<=450:#15 nanshangongyuan
                    templist=fliter(templist,fliterscore["15_nanshan"],AnotPath="/root/data/gvision/dataset/xml/15_Nanshani_Park.xml",
                    segma_woh=3,segma_area=2,up_bound=1500,down_bound=7000,down_fs=None,yichang=0)
                if 451<=imageid<=465:#16xiaoxue----------01
                    templist=fliter(templist,fliterscore["1601_shool"],AnotPath="/root/data/gvision/dataset/xml/IMG_16_01_head.xml",
                    segma_woh=3,segma_area=3,up_bound=0,down_bound=None,down_fs=None,yichang=0)
                if 466<=imageid<=480:#16xiaoxue--------02
                    templist=fliter(templist,fliterscore["1602_shool"],AnotPath="/root/data/gvision/dataset/xml/IMG_16_25_02_.xml",
                    segma_woh=3,segma_area=3,up_bound=0,down_bound=None,down_fs=None,yichang=0)
                if 481<=imageid<=510:#17zhongguan
                    templist=fliter(templist,fliterscore["17_newzhongguan"],AnotPath="/root/data/gvision/dataset/xml/17_New_Zhongguan.xml",
                    segma_woh=3,segma_area=3,up_bound=6000,down_bound=7000,down_fs=None,yichang=0)
                if 511<=imageid<=540:#18xilin-------01
                    templist=fliter(templist,fliterscore["1801_xilin"],AnotPath="/root/data/gvision/dataset/xml/IMG_18_01_01.xml",
                    segma_woh=3,segma_area=3,up_bound=4000,down_bound=None,down_fs=None,yichang=0)
                if 541<=imageid<=555:#18xilin----------02
                    templist=fliter(templist,fliterscore["1802_xilin"],AnotPath="/root/data/gvision/dataset/xml/IMG_18_02.xml",
                    segma_woh=3,segma_area=3,up_bound=4000,down_bound=None,down_fs=None,yichang=0)
            if isdel_inter:
                templist=del_inter(templist)
            if test:
                print(f"del_inter after len {len(templist)}")
            return templist
        executor = ThreadPoolExecutor(max_workers=80)
        func_var = [[file_name,dict_value] for file_name,dict_value in mergedresults.items()]

        print("merge bbox into self'image start ")
        pbar2= tqdm(total=len(mergedresults), ncols=50)
        for temp in executor.map(say2,func_var):
            savelist+=temp
            pbar2.update(1)
        pbar2.close()
        with open(os.path.join(self.outpath, outfile), 'w', encoding=self.code) as f:
            dict_str = json.dumps(savelist, indent=2)
            f.write(dict_str)
        print(f"save ***results*** json :{os.path.join(self.outpath, outfile)}")