competition_trick/WBF_emsemble.py
def WBF(args, iou_thr=0.55, draw_image=True):
    """
    This example shows how to ensemble boxes from 2 models using WBF method
    :return:
    """
    weights = args.weights
    results = defaultdict(lambda: defaultdict(defaultdict))
    print('loading results:')
    # 提取预测结果
    for model_num, model_result in enumerate(args.models_result):  #对每个模型
        result_files = os.listdir(model_result)
        for result_file in result_files:                   #每个图片
            result_path = os.path.join(model_result, result_file)
            tree = ET.parse(result_path)  # 打开xml文档
            root = tree.getroot()  # 获得root节点
            img_name = result_file.strip().split('.')[0]
            for object in root.find('objects').findall('object'):  # 每个结果
                label_name = object.find('possibleresult').find('name').text # 子节点下节点name的值
                conf = float(object.find('possibleresult').find('probability').text)
                bbox = []
                for i, point in enumerate(object.find('points').findall('point')):  # object下每个点
                    if i >= 4:
                        break
                    x, y = point.text.strip().split(',')
                    bbox.append(float(x))
                    bbox.append(float(y))
                #bbox = anchor_finetune(bbox, img_size=img_size[img_name])
                obj_det = [conf, bbox]

                try:
                    results[img_name][label_name][model_num].append(obj_det)
                except:
                    results[img_name][label_name][model_num] = []
                    results[img_name][label_name][model_num].append(obj_det)
            # else:
            #     print('error!')

    # init output xml

    # if draw_image:
    #     show_boxes(boxes_list, scores_list, labels_list)  #画图
    #  输出成DOTA格式再
    print('init emsemble results:')
    filltxt(args.save_path)
    print('emsembleling:')
    i=0
    for img_name, value1 in results.items():
        i=i+1
        print('img_name=',img_name,'  num:',i)
        root = minidom.parse(os.path.join(args.save_path, img_name + '.xml'))
        xmlBuilder = Document()
        objects = root.getElementsByTagName("objects").item(0)
        for label_name, value2 in value1.items():
            boxes = weighted_boxes_fusion(value2, weights=weights, iou_thr=args.IOU_threshold,
                                          skip_box_thr=args.conf_threshold)
            for box in boxes:
                conf = str(box[0])
                bbox = [str(point) for point in box[1]]
                # update_xml(os.path.join(args.save_path, img_name+'.xml'), label_name, conf, bbox)

                object = xmlBuilder.createElement("object")
                coordinate = xmlBuilder.createElement("coordinate")
                coordinateContent = xmlBuilder.createTextNode('pixel')
                coordinate.appendChild(coordinateContent)
                object.appendChild(coordinate)
                type = xmlBuilder.createElement("type")
                typeContent = xmlBuilder.createTextNode('rectangle')
                type.appendChild(typeContent)
                object.appendChild(type)
                description = xmlBuilder.createElement("description")
                descriptionContent = xmlBuilder.createTextNode('None')
                description.appendChild(descriptionContent)
                object.appendChild(description)
                possibleresult = xmlBuilder.createElement("possibleresult")
                objname = xmlBuilder.createElement("name")
                objnameContent = xmlBuilder.createTextNode(label_name)  # label_name
                objname.appendChild(objnameContent)
                possibleresult.appendChild(objname)
                probability = xmlBuilder.createElement("probability")
                #  probabilityContent = xmlBuilder.createTextNode(oneline[1])
                probabilityContent = xmlBuilder.createTextNode(conf)  # score
                probability.appendChild(probabilityContent)
                possibleresult.appendChild(probability)
                object.appendChild(possibleresult)
                points = xmlBuilder.createElement("points")
                point = xmlBuilder.createElement("point")
                #  pointContent = xmlBuilder.createTextNode(oneline[2]+','+oneline[3])
                pointContent = xmlBuilder.createTextNode(bbox[0] + ',' + bbox[1])  # point1
                point.appendChild(pointContent)
                points.appendChild(point)
                point = xmlBuilder.createElement("point")
                #  pointContent = xmlBuilder.createTextNode(oneline[4]+','+oneline[5])
                pointContent = xmlBuilder.createTextNode(bbox[2] + ',' + bbox[3])  # point2
                point.appendChild(pointContent)
                points.appendChild(point)
                point = xmlBuilder.createElement("point")
                #  pointContent = xmlBuilder.createTextNode(oneline[6]+','+oneline[7])
                pointContent = xmlBuilder.createTextNode(bbox[4] + ',' + bbox[5])  # point3
                point.appendChild(pointContent)
                points.appendChild(point)
                point = xmlBuilder.createElement("point")
                #  pointContent = xmlBuilder.createTextNode(oneline[8]+','+oneline[9])
                pointContent = xmlBuilder.createTextNode(bbox[6] + ',' + bbox[7])  # points
                point.appendChild(pointContent)
                points.appendChild(point)
                point = xmlBuilder.createElement("point")
                #  pointContent = xmlBuilder.createTextNode(oneline[2]+','+oneline[3])
                pointContent = xmlBuilder.createTextNode(bbox[0] + ',' + bbox[1])  #
                point.appendChild(pointContent)
                points.appendChild(point)
                object.appendChild(points)
                objects.appendChild(object)
        #objects.appendChild(object)
        root.writexml(open(os.path.join(args.save_path, img_name + '.xml'), "w"), encoding='utf-8')

    This example shows how to ensemble boxes from 2 models using WBF method
    :return:
    
This example shows how to ensemble boxes from 2 models using WBF method
This example shows how to ensemble boxes from 2 models using WBF method
def WBF(args, iou_thr=0.55, draw_image=True):
    weights = args.weights
    results = defaultdict(lambda: defaultdict(defaultdict))
    print('loading results:')
    for model_num, model_result in enumerate(args.models_result):  
        result_files = os.listdir(model_result)
        for result_file in result_files:                   
            result_path = os.path.join(model_result, result_file)
            tree = ET.parse(result_path)  
            root = tree.getroot()  
            img_name = result_file.strip().split('.')[0]
            for object in root.find('objects').findall('object'):  
                label_name = object.find('possibleresult').find('name').text 
                conf = float(object.find('possibleresult').find('probability').text)
                bbox = []
                for i, point in enumerate(object.find('points').findall('point')):  
                    if i >= 4:
                        break
                    x, y = point.text.strip().split(',')
                    bbox.append(float(x))
                    bbox.append(float(y))
                obj_det = [conf, bbox]
                try:
                    results[img_name][label_name][model_num].append(obj_det)
                except:
                    results[img_name][label_name][model_num] = []
                    results[img_name][label_name][model_num].append(obj_det)
    print('init emsemble results:')
    filltxt(args.save_path)
    print('emsembleling:')
    i=0
    for img_name, value1 in results.items():
        i=i+1
        print('img_name=',img_name,'  num:',i)
        root = minidom.parse(os.path.join(args.save_path, img_name + '.xml'))
        xmlBuilder = Document()
        objects = root.getElementsByTagName("objects").item(0)
        for label_name, value2 in value1.items():
            boxes = weighted_boxes_fusion(value2, weights=weights, iou_thr=args.IOU_threshold,
                                          skip_box_thr=args.conf_threshold)
            for box in boxes:
                conf = str(box[0])
                bbox = [str(point) for point in box[1]]
                object = xmlBuilder.createElement("object")
                coordinate = xmlBuilder.createElement("coordinate")
                coordinateContent = xmlBuilder.createTextNode('pixel')
                coordinate.appendChild(coordinateContent)
                object.appendChild(coordinate)
                type = xmlBuilder.createElement("type")
                typeContent = xmlBuilder.createTextNode('rectangle')
                type.appendChild(typeContent)
                object.appendChild(type)
                description = xmlBuilder.createElement("description")
                descriptionContent = xmlBuilder.createTextNode('None')
                description.appendChild(descriptionContent)
                object.appendChild(description)
                possibleresult = xmlBuilder.createElement("possibleresult")
                objname = xmlBuilder.createElement("name")
                objnameContent = xmlBuilder.createTextNode(label_name)  
                objname.appendChild(objnameContent)
                possibleresult.appendChild(objname)
                probability = xmlBuilder.createElement("probability")
                probabilityContent = xmlBuilder.createTextNode(conf)  
                probability.appendChild(probabilityContent)
                possibleresult.appendChild(probability)
                object.appendChild(possibleresult)
                points = xmlBuilder.createElement("points")
                point = xmlBuilder.createElement("point")
                pointContent = xmlBuilder.createTextNode(bbox[0] + ',' + bbox[1])  
                point.appendChild(pointContent)
                points.appendChild(point)
                point = xmlBuilder.createElement("point")
                pointContent = xmlBuilder.createTextNode(bbox[2] + ',' + bbox[3])  
                point.appendChild(pointContent)
                points.appendChild(point)
                point = xmlBuilder.createElement("point")
                pointContent = xmlBuilder.createTextNode(bbox[4] + ',' + bbox[5])  
                point.appendChild(pointContent)
                points.appendChild(point)
                point = xmlBuilder.createElement("point")
                pointContent = xmlBuilder.createTextNode(bbox[6] + ',' + bbox[7])  
                point.appendChild(pointContent)
                points.appendChild(point)
                point = xmlBuilder.createElement("point")
                pointContent = xmlBuilder.createTextNode(bbox[0] + ',' + bbox[1])  
                point.appendChild(pointContent)
                points.appendChild(point)
                object.appendChild(points)
                objects.appendChild(object)
        objects.appendChild(object)
        root.writexml(open(os.path.join(args.save_path, img_name + '.xml'), "w"), encoding='utf-8')
def WBF ( args , iou_thr = 0.55 , draw_image = True ) : weights = args . weights results = defaultdict ( lambda : defaultdict ( defaultdict ) ) print ( 'loading results:' ) for model_num , model_result in enumerate ( args . models_result ) : result_files = os . listdir ( model_result ) for result_file in result_files : result_path = os . path . join ( model_result , result_file ) tree = ET . parse ( result_path ) root = tree . getroot ( ) img_name = result_file . strip ( ) . split ( '.' ) [ 0 ] for object in root . find ( 'objects' ) . findall ( 'object' ) : label_name = object . find ( 'possibleresult' ) . find ( 'name' ) . text conf = float ( object . find ( 'possibleresult' ) . find ( 'probability' ) . text ) bbox = [ ] for i , point in enumerate ( object . find ( 'points' ) . findall ( 'point' ) ) : if i >= 4 : break x , y = point . text . strip ( ) . split ( ',' ) bbox . append ( float ( x ) ) bbox . append ( float ( y ) ) obj_det = [ conf , bbox ] try : results [ img_name ] [ label_name ] [ model_num ] . append ( obj_det ) except : results [ img_name ] [ label_name ] [ model_num ] = [ ] results [ img_name ] [ label_name ] [ model_num ] . append ( obj_det ) print ( 'init emsemble results:' ) filltxt ( args . save_path ) print ( 'emsembleling:' ) i = 0 for img_name , value1 in results . items ( ) : i = i + 1 print ( 'img_name=' , img_name , '  num:' , i ) root = minidom . parse ( os . path . join ( args . save_path , img_name + '.xml' ) ) xmlBuilder = Document ( ) objects = root . getElementsByTagName ( "objects" ) . item ( 0 ) for label_name , value2 in value1 . items ( ) : boxes = weighted_boxes_fusion ( value2 , weights = weights , iou_thr = args . IOU_threshold , skip_box_thr = args . conf_threshold ) for box in boxes : conf = str ( box [ 0 ] ) bbox = [ str ( point ) for point in box [ 1 ] ] object = xmlBuilder . createElement ( "object" ) coordinate = xmlBuilder . createElement ( "coordinate" ) coordinateContent = xmlBuilder . createTextNode ( 'pixel' ) coordinate . appendChild ( coordinateContent ) object . appendChild ( coordinate ) type = xmlBuilder . createElement ( "type" ) typeContent = xmlBuilder . createTextNode ( 'rectangle' ) type . appendChild ( typeContent ) object . appendChild ( type ) description = xmlBuilder . createElement ( "description" ) descriptionContent = xmlBuilder . createTextNode ( 'None' ) description . appendChild ( descriptionContent ) object . appendChild ( description ) possibleresult = xmlBuilder . createElement ( "possibleresult" ) objname = xmlBuilder . createElement ( "name" ) objnameContent = xmlBuilder . createTextNode ( label_name ) objname . appendChild ( objnameContent ) possibleresult . appendChild ( objname ) probability = xmlBuilder . createElement ( "probability" ) probabilityContent = xmlBuilder . createTextNode ( conf ) probability . appendChild ( probabilityContent ) possibleresult . appendChild ( probability ) object . appendChild ( possibleresult ) points = xmlBuilder . createElement ( "points" ) point = xmlBuilder . createElement ( "point" ) pointContent = xmlBuilder . createTextNode ( bbox [ 0 ] + ',' + bbox [ 1 ] ) point . appendChild ( pointContent ) points . appendChild ( point ) point = xmlBuilder . createElement ( "point" ) pointContent = xmlBuilder . createTextNode ( bbox [ 2 ] + ',' + bbox [ 3 ] ) point . appendChild ( pointContent ) points . appendChild ( point ) point = xmlBuilder . createElement ( "point" ) pointContent = xmlBuilder . createTextNode ( bbox [ 4 ] + ',' + bbox [ 5 ] ) point . appendChild ( pointContent ) points . appendChild ( point ) point = xmlBuilder . createElement ( "point" ) pointContent = xmlBuilder . createTextNode ( bbox [ 6 ] + ',' + bbox [ 7 ] ) point . appendChild ( pointContent ) points . appendChild ( point ) point = xmlBuilder . createElement ( "point" ) pointContent = xmlBuilder . createTextNode ( bbox [ 0 ] + ',' + bbox [ 1 ] ) point . appendChild ( pointContent ) points . appendChild ( point ) object . appendChild ( points ) objects . appendChild ( object ) root . writexml ( open ( os . path . join ( args . save_path , img_name + '.xml' ) , "w" ) , encoding = 'utf-8' )
This example shows how to ensemble boxes from 2 models using WBF method
This example shows how to ensemble boxes from 2 models using WBF method
"""
    This example shows how to ensemble boxes from 2 models using WBF method
    :return:
    """ # 提取预测结果 #对每个模型 #每个图片 # 打开xml文档 # 获得root节点 # 每个结果 # 子节点下节点name的值 # object下每个点 #bbox = anchor_finetune(bbox, img_size=img_size[img_name]) # else: #     print('error!') # init output xml # if draw_image: #     show_boxes(boxes_list, scores_list, labels_list)  #画图 #  输出成DOTA格式再 # update_xml(os.path.join(args.save_path, img_name+'.xml'), label_name, conf, bbox) # label_name #  probabilityContent = xmlBuilder.createTextNode(oneline[1]) # score #  pointContent = xmlBuilder.createTextNode(oneline[2]+','+oneline[3]) # point1 #  pointContent = xmlBuilder.createTextNode(oneline[4]+','+oneline[5]) # point2 #  pointContent = xmlBuilder.createTextNode(oneline[6]+','+oneline[7]) # point3 #  pointContent = xmlBuilder.createTextNode(oneline[8]+','+oneline[9]) # points #  pointContent = xmlBuilder.createTextNode(oneline[2]+','+oneline[3]) # #objects.appendChild(object)
None
args
iou_thr
draw_image