(maps, rois, scores, cfg)
return np.transpose(out_kes_d, [0, 2, 1]), d_scores
Extract predicted ke locations from heatmaps. Output has shape
    (#rois, 4, #kes) with the 4 rows corresponding to (x, y, logit, prob)
    for each ke.
Extract predicted ke locations from heatmaps. Output has shape
    (#rois, 4, #kes) with the 4 rows corresponding to (x, y, logit, prob)
    for each ke.
Extract predicted ke locations from heatmaps . Output has shape ( #rois 4 #kes ) with the 4 rows corresponding to ( x y logit prob ) for each ke .
def heatmaps_to_kes(maps, rois, scores, cfg):
    """Extract predicted ke locations from heatmaps. Output has shape
    (#rois, 4, #kes) with the 4 rows corresponding to (x, y, logit, prob)
    for each ke.
    """
    # This function converts a discrete image coordinate in a HEATMAP_SIZE x
    # HEATMAP_SIZE image to a continuous ke coordinate. We maintain
    # consistency with kes_to_heatmap_labels by using the conversion from
    # Heckbert 1990: c = d + 0.5, where d is a discrete coordinate and c is a
    # continuous coordinate.
    offset_x = rois[:, 0]
    offset_y = rois[:, 1]

    widths = rois[:, 2] - rois[:, 0]
    heights = rois[:, 3] - rois[:, 1]
    widths = np.maximum(widths, 1)
    heights = np.maximum(heights, 1)
    widths_ceil = np.ceil(widths)
    heights_ceil = np.ceil(heights)

    
    resol = cfg.MODEL.ROI_KE_HEAD.RESOLUTION # cfg.mo... 56

    if maps.shape[-2:] == (1, resol):
        xory_mode = 0 # x mode
    elif maps.shape[-2:] == (resol, 1):
        xory_mode = 1 # y mode
    else:
        assert(0), 'invalid mode.'

    # print("maps", maps.shape, maps[0,0], maps[0,1])
    # NCHW to NHWC for use with OpenCV
    maps = np.transpose(maps, [0, 2, 3, 1])
    min_size = 0 # cfg
    num_kes = int(cfg.MODEL.ROI_KE_HEAD.NUM_KES/2)+2
    d_preds = np.zeros(
        (len(rois), 2, num_kes), dtype=np.float32)
    d_scores = np.zeros(scores.shape,  dtype=np.float32)

    assert(len(rois) == maps.shape[0]), 'shape mismatch {}, {}, {}, {}'.format(str(len(rois)), \
                                                                    str(rois.shape), \
                                                                    str(maps.shape[0]), \
                                                                    str(maps.shape))

    normal = 0
    innormal = 0                                                                    
    for i in range(len(rois)):
        if min_size > 0:
            roi_map_width = int(np.maximum(widths_ceil[i], min_size))
            roi_map_height = int(np.maximum(heights_ceil[i], min_size))
        else:
            roi_map_width = widths_ceil[i]
            roi_map_height = heights_ceil[i]
        width_correction = widths[i] / roi_map_width
        height_correction = heights[i] / roi_map_height

        np.set_printoptions(suppress=True)
        # print(i, "stop", maps.shape, np.around(maps[i][0, :, :], decimals=2))

        if not xory_mode:
            roi_map = cv2.resize(
                maps[i], (roi_map_width, 1), interpolation=cv2.INTER_CUBIC)
        else:
            roi_map = cv2.resize(
                maps[i], (1, roi_map_height), interpolation=cv2.INTER_CUBIC)

        # print(roi_map.shape, np.around(roi_map[0, :, :], decimals=2))
        # Bring back to CHW
        roi_map = np.transpose(roi_map, [2, 0, 1])
        roi_map_probs = scores_to_probs(roi_map.copy())
        
        # kescore visulize.
        map_vis = np.transpose(maps[i], [2, 0, 1])
        map_vis = scores_to_probs(map_vis.copy())   

        sum_score = []

        if cfg.MODEL.ROI_KE_HEAD.RESCORING:
            for k in range(num_kes):
                if map_vis[k].shape[0] == 1:
                    x = np.arange(0, len(map_vis[k][0]), 1)  
                    y = map_vis[k][0]
                else:
                    x = np.arange(0, len(map_vis[k][:, 0]), 1)  
                    y = map_vis[k][:, 0]

                top = y.max()
                atop = y.argmax()

                # lf2&1
                lf2 = max(atop-2, 0)
                lf1 = max(atop-1, 0)
                rt2 = min(atop+2, 55)
                rt1 = min(atop+1, 55)

                sum_score.append(top+y[lf2]+y[lf1]+y[rt1]+y[rt2])

                kes_score_mean = sum(sum_score)*1.0/len(sum_score)
                gama = cfg.MODEL.ROI_KE_HEAD.RESCORING_GAMA
                final_score = (scores[i]*(2.0-gama)+gama*kes_score_mean)*0.5

                # rescore 
                d_scores[i] = final_score
        else:
            d_scores[i] = scores[i]

        w = roi_map.shape[2]
        for k in range(num_kes):
            pos = roi_map[k, :, :].argmax()
            x_int = pos % w
            y_int = (pos - x_int) // w
            assert (roi_map_probs[k, y_int, x_int] ==
                    roi_map_probs[k, :, :].max())
            x = (x_int + 0.5) * width_correction
            y = (y_int + 0.5) * height_correction
            if not xory_mode:
                d_preds[i, 0, k] = x + offset_x[i]
                d_preds[i, 1, k] = roi_map_probs[k, y_int, x_int]
            else:
                d_preds[i, 0, k] = y + offset_y[i]
                d_preds[i, 1, k] = roi_map_probs[k, y_int, x_int]


    out_kes_d = kes_decode(d_preds)
    return np.transpose(out_kes_d, [0, 2, 1]), d_scores
def heatmaps_to_kes ( maps , rois , scores , cfg ) : # This function converts a discrete image coordinate in a HEATMAP_SIZE x # HEATMAP_SIZE image to a continuous ke coordinate. We maintain # consistency with kes_to_heatmap_labels by using the conversion from # Heckbert 1990: c = d + 0.5, where d is a discrete coordinate and c is a # continuous coordinate. offset_x = rois [ : , 0 ] offset_y = rois [ : , 1 ] widths = rois [ : , 2 ] - rois [ : , 0 ] heights = rois [ : , 3 ] - rois [ : , 1 ] widths = np . maximum ( widths , 1 ) heights = np . maximum ( heights , 1 ) widths_ceil = np . ceil ( widths ) heights_ceil = np . ceil ( heights ) resol = cfg . MODEL . ROI_KE_HEAD . RESOLUTION # cfg.mo... 56 if maps . shape [ - 2 : ] == ( 1 , resol ) : xory_mode = 0 # x mode elif maps . shape [ - 2 : ] == ( resol , 1 ) : xory_mode = 1 # y mode else : assert ( 0 ) , 'invalid mode.' # print("maps", maps.shape, maps[0,0], maps[0,1]) # NCHW to NHWC for use with OpenCV maps = np . transpose ( maps , [ 0 , 2 , 3 , 1 ] ) min_size = 0 # cfg num_kes = int ( cfg . MODEL . ROI_KE_HEAD . NUM_KES / 2 ) + 2 d_preds = np . zeros ( ( len ( rois ) , 2 , num_kes ) , dtype = np . float32 ) d_scores = np . zeros ( scores . shape , dtype = np . float32 ) assert ( len ( rois ) == maps . shape [ 0 ] ) , 'shape mismatch {}, {}, {}, {}' . format ( str ( len ( rois ) ) , str ( rois . shape ) , str ( maps . shape [ 0 ] ) , str ( maps . shape ) ) normal = 0 innormal = 0 for i in range ( len ( rois ) ) : if min_size > 0 : roi_map_width = int ( np . maximum ( widths_ceil [ i ] , min_size ) ) roi_map_height = int ( np . maximum ( heights_ceil [ i ] , min_size ) ) else : roi_map_width = widths_ceil [ i ] roi_map_height = heights_ceil [ i ] width_correction = widths [ i ] / roi_map_width height_correction = heights [ i ] / roi_map_height np . set_printoptions ( suppress = True ) # print(i, "stop", maps.shape, np.around(maps[i][0, :, :], decimals=2)) if not xory_mode : roi_map = cv2 . resize ( maps [ i ] , ( roi_map_width , 1 ) , interpolation = cv2 . INTER_CUBIC ) else : roi_map = cv2 . resize ( maps [ i ] , ( 1 , roi_map_height ) , interpolation = cv2 . INTER_CUBIC ) # print(roi_map.shape, np.around(roi_map[0, :, :], decimals=2)) # Bring back to CHW roi_map = np . transpose ( roi_map , [ 2 , 0 , 1 ] ) roi_map_probs = scores_to_probs ( roi_map . copy ( ) ) # kescore visulize. map_vis = np . transpose ( maps [ i ] , [ 2 , 0 , 1 ] ) map_vis = scores_to_probs ( map_vis . copy ( ) ) sum_score = [ ] if cfg . MODEL . ROI_KE_HEAD . RESCORING : for k in range ( num_kes ) : if map_vis [ k ] . shape [ 0 ] == 1 : x = np . arange ( 0 , len ( map_vis [ k ] [ 0 ] ) , 1 ) y = map_vis [ k ] [ 0 ] else : x = np . arange ( 0 , len ( map_vis [ k ] [ : , 0 ] ) , 1 ) y = map_vis [ k ] [ : , 0 ] top = y . max ( ) atop = y . argmax ( ) # lf2&1 lf2 = max ( atop - 2 , 0 ) lf1 = max ( atop - 1 , 0 ) rt2 = min ( atop + 2 , 55 ) rt1 = min ( atop + 1 , 55 ) sum_score . append ( top + y [ lf2 ] + y [ lf1 ] + y [ rt1 ] + y [ rt2 ] ) kes_score_mean = sum ( sum_score ) * 1.0 / len ( sum_score ) gama = cfg . MODEL . ROI_KE_HEAD . RESCORING_GAMA final_score = ( scores [ i ] * ( 2.0 - gama ) + gama * kes_score_mean ) * 0.5 # rescore  d_scores [ i ] = final_score else : d_scores [ i ] = scores [ i ] w = roi_map . shape [ 2 ] for k in range ( num_kes ) : pos = roi_map [ k , : , : ] . argmax ( ) x_int = pos % w y_int = ( pos - x_int ) // w assert ( roi_map_probs [ k , y_int , x_int ] == roi_map_probs [ k , : , : ] . max ( ) ) x = ( x_int + 0.5 ) * width_correction y = ( y_int + 0.5 ) * height_correction if not xory_mode : d_preds [ i , 0 , k ] = x + offset_x [ i ] d_preds [ i , 1 , k ] = roi_map_probs [ k , y_int , x_int ] else : d_preds [ i , 0 , k ] = y + offset_y [ i ] d_preds [ i , 1 , k ] = roi_map_probs [ k , y_int , x_int ] out_kes_d = kes_decode ( d_preds ) return np . transpose ( out_kes_d , [ 0 , 2 , 1 ] ) , d_scores