proposal_top_layer.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # --------------------------------------------------------
  2. # Faster R-CNN
  3. # Licensed under The MIT License [see LICENSE for details]
  4. # Written by Xinlei Chen
  5. # --------------------------------------------------------
  6. from __future__ import absolute_import
  7. from __future__ import division
  8. from __future__ import print_function
  9. from segment.sheet_resolve.lib.model.config import cfg
  10. from segment.sheet_resolve.lib.model.bbox_transform import bbox_transform_inv, clip_boxes, bbox_transform_inv_tf, clip_boxes_tf
  11. import tensorflow as tf
  12. def proposal_top_layer(rpn_cls_prob, rpn_bbox_pred, im_info, _feat_stride, anchors, num_anchors):
  13. """A layer that just selects the top region proposals
  14. without using non-maximal suppression,
  15. For details please see the technical report
  16. """
  17. rpn_top_n = cfg.TEST.RPN_TOP_N
  18. scores = rpn_cls_prob[:, :, :, num_anchors:]
  19. rpn_bbox_pred = tf.reshape(rpn_bbox_pred, shape=(-1, 4))
  20. scores = tf.reshape(scores, shape=(-1,))
  21. # Do the selection here
  22. top_scores, top_inds = tf.nn.top_k(scores, k=rpn_top_n)
  23. top_scores = tf.reshape(top_scores, shape=(-1, 1))
  24. top_anchors = tf.gather(anchors, top_inds)
  25. top_rpn_bbox = tf.gather(rpn_bbox_pred, top_inds)
  26. proposals = bbox_transform_inv_tf(top_anchors, top_rpn_bbox)
  27. # Clip predicted boxes to image
  28. proposals = clip_boxes_tf(proposals, im_info[:2])
  29. # Output rois blob
  30. # Our RPN implementation only supports a single input image, so all
  31. # batch inds are 0
  32. proposals = tf.to_float(proposals)
  33. batch_inds = tf.zeros((rpn_top_n, 1))
  34. blob = tf.concat([batch_inds, proposals], 1)
  35. return blob, top_scores