(object_score, bboxes_pred, encode_fn, rpn_pre_nms_top_n, rpn_post_nms_top_n, nms_threshold, rpn_min_size, is_training, data_format)
About the input:
    object_score: N x num_bboxes
    bboxes_pred: N x num_bboxes x 4
    decode_fn: accept location_pred and return the decoded bboxes in format 'batch_size, feature_h, feature_w, num_anchors, 4'
    rpn_min_size: the absolute pixels a bbox must have in both side
    data_format: specify the input format

    this function do the following process:
    1. for each (H, W) location i
      generate A anchor boxes centered on cell i
      apply predicted bbox deltas at cell i to each of the A anchors (this is done before this function)
    2. clip predicted boxes to image
    3. remove predicted boxes with either height or width < rpn_min_size
    4. sort all (proposal, score) pairs by score from highest to lowest
    5. take top pre_nms_topN rois before NMS
    6. apply NMS with threshold 0.7 to remaining rois
    7. take after_nms_topN rois after NMS (if number of bboxes if less than after_nms_topN, the upsample them)
    8. take both the top rois and all the ground truth bboxes as all_rois
    9. rematch all_rois to get regress and classification target
    10.sample all_rois as proposals
About the input:
    object_score: N x num_bboxes
    bboxes_pred: N x num_bboxes x 4
    decode_fn: accept location_pred and return the decoded bboxes in format 'batch_size, feature_h, feature_w, num_anchors, 4'
    rpn_min_size: the absolute pixels a bbox must have in both side
    data_format: specify the input format
About the input : object_score : N x num_bboxes bboxes_pred : N x num_bboxes x 4 decode_fn : accept location_pred and return the decoded bboxes in format batch_size feature_h feature_w num_anchors 4 rpn_min_size : the absolute pixels a bbox must have in both side data_format : specify the input format
def get_proposals(object_score, bboxes_pred, encode_fn, rpn_pre_nms_top_n, rpn_post_nms_top_n, nms_threshold, rpn_min_size, is_training, data_format):
    '''About the input:
    object_score: N x num_bboxes
    bboxes_pred: N x num_bboxes x 4
    decode_fn: accept location_pred and return the decoded bboxes in format 'batch_size, feature_h, feature_w, num_anchors, 4'
    rpn_min_size: the absolute pixels a bbox must have in both side
    data_format: specify the input format

    this function do the following process:
    1. for each (H, W) location i
      generate A anchor boxes centered on cell i
      apply predicted bbox deltas at cell i to each of the A anchors (this is done before this function)
    2. clip predicted boxes to image
    3. remove predicted boxes with either height or width < rpn_min_size
    4. sort all (proposal, score) pairs by score from highest to lowest
    5. take top pre_nms_topN rois before NMS
    6. apply NMS with threshold 0.7 to remaining rois
    7. take after_nms_topN rois after NMS (if number of bboxes if less than after_nms_topN, the upsample them)
    8. take both the top rois and all the ground truth bboxes as all_rois
    9. rematch all_rois to get regress and classification target
    10.sample all_rois as proposals
    '''
    # it's better to run these task on GPU to avoid lots of Host->Device memory copy
    with tf.device('/cpu:0'):
        #bboxes_pred = decode_fn(location_pred)
        bboxes_pred = tf.map_fn(lambda _bboxes : _bboxes_clip([0., 0., 1., 1.], _bboxes), bboxes_pred, back_prop=False)
        object_score, bboxes_pred = tf.map_fn(lambda _score_bboxes : _filter_and_sort_boxes(_score_bboxes[0], _score_bboxes[1], rpn_min_size, keep_topk=rpn_pre_nms_top_n), [object_score, bboxes_pred], back_prop=False, infer_shape=True)
        #object_score, bboxes_pred = tf.map_fn(lambda _score_bboxes : _bboxes_sort(_score_bboxes[0], _score_bboxes[1], top_k=rpn_pre_nms_top_n), [object_score, bboxes_pred], back_prop=False)
        # object_score.set_shape([None, rpn_pre_nms_top_n])
        # bboxes_pred.set_shape([None, rpn_pre_nms_top_n, 4])

        object_score, bboxes_pred = tf.map_fn(lambda _score_bboxes : _bboxes_nms(_score_bboxes[0], _score_bboxes[1], nms_threshold = nms_threshold, keep_top_k=rpn_post_nms_top_n, mode = 'union'), [object_score, bboxes_pred], back_prop=False)#, dtype=[tf.float32, tf.float32], infer_shape=True
        # padding to fix the size of rois
        # the object_score is not in descending order when the upsample padding happened
        #object_score = tf.Print(object_score, [object_score[0],object_score[1],object_score[2],object_score[3]], message='object_score0:', summarize=1000)
        #bboxes_pred = tf.Print(bboxes_pred, [bboxes_pred[0],bboxes_pred[1],bboxes_pred[2],bboxes_pred[3]], message='bboxes_pred0:', summarize=1000)
        object_score, bboxes_pred = tf.map_fn(lambda _score_bboxes : _upsample_rois(_score_bboxes[0], _score_bboxes[1], keep_top_k= rpn_post_nms_top_n), [object_score, bboxes_pred], back_prop=False)
        # match and sample to get proposals and targets
        #print(encode_fn(bboxes_pred))
        # object_score = tf.Print(object_score, [object_score[0],object_score[1],object_score[2],object_score[3]], message='object_score1:', summarize=1000)
        # bboxes_pred = tf.Print(bboxes_pred, [bboxes_pred[0],bboxes_pred[1],bboxes_pred[2],bboxes_pred[3]], message='bboxes_pred1:', summarize=1000)
        if not is_training:
            return tf.stop_gradient(bboxes_pred)

        proposals_bboxes, proposals_targets, proposals_labels, proposals_scores = encode_fn(bboxes_pred)

        return tf.stop_gradient(proposals_bboxes), tf.stop_gradient(proposals_targets), tf.stop_gradient(proposals_labels), tf.stop_gradient(proposals_scores)
def get_proposals ( object_score , bboxes_pred , encode_fn , rpn_pre_nms_top_n , rpn_post_nms_top_n , nms_threshold , rpn_min_size , is_training , data_format ) : # it's better to run these task on GPU to avoid lots of Host->Device memory copy with tf . device ( '/cpu:0' ) : #bboxes_pred = decode_fn(location_pred) bboxes_pred = tf . map_fn ( lambda _bboxes : _bboxes_clip ( [ 0. , 0. , 1. , 1. ] , _bboxes ) , bboxes_pred , back_prop = False ) object_score , bboxes_pred = tf . map_fn ( lambda _score_bboxes : _filter_and_sort_boxes ( _score_bboxes [ 0 ] , _score_bboxes [ 1 ] , rpn_min_size , keep_topk = rpn_pre_nms_top_n ) , [ object_score , bboxes_pred ] , back_prop = False , infer_shape = True ) #object_score, bboxes_pred = tf.map_fn(lambda _score_bboxes : _bboxes_sort(_score_bboxes[0], _score_bboxes[1], top_k=rpn_pre_nms_top_n), [object_score, bboxes_pred], back_prop=False) # object_score.set_shape([None, rpn_pre_nms_top_n]) # bboxes_pred.set_shape([None, rpn_pre_nms_top_n, 4]) object_score , bboxes_pred = tf . map_fn ( lambda _score_bboxes : _bboxes_nms ( _score_bboxes [ 0 ] , _score_bboxes [ 1 ] , nms_threshold = nms_threshold , keep_top_k = rpn_post_nms_top_n , mode = 'union' ) , [ object_score , bboxes_pred ] , back_prop = False ) #, dtype=[tf.float32, tf.float32], infer_shape=True # padding to fix the size of rois # the object_score is not in descending order when the upsample padding happened #object_score = tf.Print(object_score, [object_score[0],object_score[1],object_score[2],object_score[3]], message='object_score0:', summarize=1000) #bboxes_pred = tf.Print(bboxes_pred, [bboxes_pred[0],bboxes_pred[1],bboxes_pred[2],bboxes_pred[3]], message='bboxes_pred0:', summarize=1000) object_score , bboxes_pred = tf . map_fn ( lambda _score_bboxes : _upsample_rois ( _score_bboxes [ 0 ] , _score_bboxes [ 1 ] , keep_top_k = rpn_post_nms_top_n ) , [ object_score , bboxes_pred ] , back_prop = False ) # match and sample to get proposals and targets #print(encode_fn(bboxes_pred)) # object_score = tf.Print(object_score, [object_score[0],object_score[1],object_score[2],object_score[3]], message='object_score1:', summarize=1000) # bboxes_pred = tf.Print(bboxes_pred, [bboxes_pred[0],bboxes_pred[1],bboxes_pred[2],bboxes_pred[3]], message='bboxes_pred1:', summarize=1000) if not is_training : return tf . stop_gradient ( bboxes_pred ) proposals_bboxes , proposals_targets , proposals_labels , proposals_scores = encode_fn ( bboxes_pred ) return tf . stop_gradient ( proposals_bboxes ) , tf . stop_gradient ( proposals_targets ) , tf . stop_gradient ( proposals_labels ) , tf . stop_gradient ( proposals_scores )