docstr: Builds the RPN model subgraph.
  
  Args:
      conv_feature_map: A Tensor with the output of some pretrained
          network. Its dimensions should be
          `[1, feature_map_height, feature_map_width, depth]` where depth
          is 512 for the default layer in VGG and 1024 for the default
          layer in ResNet.
      im_shape: A Tensor with the shape of the original image.
      all_anchors: A Tensor with all the anchor bounding boxes. Its shape
          should be
          [feature_map_height * feature_map_width * total_anchors, 4]
      gt_boxes: A Tensor with the ground-truth boxes for the image.
          Its dimensions should be `[total_gt_boxes, 5]`, and it should
          consist of [x1, y1, x2, y2, label], being (x1, y1) -> top left
          point, and (x2, y2) -> bottom right point of the bounding box.
  
  Returns:
      prediction_dict: A dict with the following keys:
          proposals: A Tensor with a variable number of proposals for
              objects on the image.
          scores: A Tensor with a "objectness" probability for each
              proposal. The score should be the output of the softmax for
              object.
  
          If training is True, then some more Tensors are added to the
          prediction dictionary to be used for calculating the loss.
  
          rpn_cls_prob: A Tensor with the probability of being
              background and foreground for each anchor.
          rpn_cls_score: A Tensor with the cls score of being background
              and foreground for each anchor (the input for the softmax).
          rpn_bbox_pred: A Tensor with the bounding box regression for
              each anchor.
          rpn_cls_target: A Tensor with the target for each of the
              anchors. The shape is [num_anchors,].
          rpn_bbox_target: A Tensor with the target for each of the
              anchors. In case of ignoring the anchor for the target then
              we still have a bbox target for each anchors, and it's
              filled with zeroes when ignored.
docstr_tok: ["Builds","the","RPN","model","subgraph",".","Args",":","conv_feature_map",":","A","Tensor","with","the","output","of","some","pretrained","network",".","Its","dimensions","should","be","`","[","1",",","feature_map_height",",","feature_map_width",",","depth","]","`","where","depth","is","512","for","the","default","layer","in","VGG","and","1024","for","the","default","layer","in","ResNet",".","im_shape",":","A","Tensor","with","the","shape","of","the","original","image",".","all_anchors",":","A","Tensor","with","all","the","anchor","bounding","boxes",".","Its","shape","should","be","[","feature_map_height","*","feature_map_width","*","total_anchors",",","4","]","gt_boxes",":","A","Tensor","with","the","ground-truth","boxes","for","the","image",".","Its","dimensions","should","be","`","[","total_gt_boxes",",","5","]","`",",","and","it","should","consist","of","[","x1",",","y1",",","x2",",","y2",",","label","]",",","being","(","x1",",","y1",")","-",">","top","left","point",",","and","(","x2",",","y2",")","-",">","bottom","right","point","of","the","bounding","box",".","Returns",":","prediction_dict",":","A","dict","with","the","following","keys",":","proposals",":","A","Tensor","with","a","variable","number","of","proposals","for","objects","on","the","image",".","scores",":","A","Tensor","with","a","``","objectness","''","probability","for","each","proposal",".","The","score","should","be","the","output","of","the","softmax","for","object",".","If","training","is","True",",","then","some","more","Tensors","are","added","to","the","prediction","dictionary","to","be","used","for","calculating","the","loss",".","rpn_cls_prob",":","A","Tensor","with","the","probability","of","being","background","and","foreground","for","each","anchor",".","rpn_cls_score",":","A","Tensor","with","the","cls","score","of","being","background","and","foreground","for","each","anchor","(","the","input","for","the","softmax",")",".","rpn_bbox_pred",":","A","Tensor","with","the","bounding","box","regression","for","each","anchor",".","rpn_cls_target",":","A","Tensor","with","the","target","for","each","of","the","anchors",".","The","shape","is","[","num_anchors",",","]",".","rpn_bbox_target",":","A","Tensor","with","the","target","for","each","of","the","anchors",".","In","case","of","ignoring","the","anchor","for","the","target","then","we","still","have","a","bbox","target","for","each","anchors",",","and","it's","filled","with","zeroes","when","ignored","."]
code: def _build(
      self,
      conv_feature_map,
      im_shape,
      all_anchors,
      gt_boxes=None,
      is_training=False,
  ):
      """Builds the RPN model subgraph.
  
      Args:
          conv_feature_map: A Tensor with the output of some pretrained
              network. Its dimensions should be
              `[1, feature_map_height, feature_map_width, depth]` where depth
              is 512 for the default layer in VGG and 1024 for the default
              layer in ResNet.
          im_shape: A Tensor with the shape of the original image.
          all_anchors: A Tensor with all the anchor bounding boxes. Its shape
              should be
              [feature_map_height * feature_map_width * total_anchors, 4]
          gt_boxes: A Tensor with the ground-truth boxes for the image.
              Its dimensions should be `[total_gt_boxes, 5]`, and it should
              consist of [x1, y1, x2, y2, label], being (x1, y1) -> top left
              point, and (x2, y2) -> bottom right point of the bounding box.
  
      Returns:
          prediction_dict: A dict with the following keys:
              proposals: A Tensor with a variable number of proposals for
                  objects on the image.
              scores: A Tensor with a "objectness" probability for each
                  proposal. The score should be the output of the softmax for
                  object.
  
              If training is True, then some more Tensors are added to the
              prediction dictionary to be used for calculating the loss.
  
              rpn_cls_prob: A Tensor with the probability of being
                  background and foreground for each anchor.
              rpn_cls_score: A Tensor with the cls score of being background
                  and foreground for each anchor (the input for the softmax).
              rpn_bbox_pred: A Tensor with the bounding box regression for
                  each anchor.
              rpn_cls_target: A Tensor with the target for each of the
                  anchors. The shape is [num_anchors,].
              rpn_bbox_target: A Tensor with the target for each of the
                  anchors. In case of ignoring the anchor for the target then
                  we still have a bbox target for each anchors, and it's
                  filled with zeroes when ignored.
      """
      # We start with a common conv layer applied to the feature map.
      self._instantiate_layers()
      self._proposal = RPNProposal(
          self._num_anchors, self._config.proposals, debug=self._debug
      )
      self._anchor_target = RPNTarget(
          self._num_anchors, self._config.target, seed=self._seed
      )
  
      prediction_dict = {}
  
      # Get the RPN feature using a simple conv net. Activation function
      # can be set to empty.
      rpn_conv_feature = self._rpn(conv_feature_map)
      rpn_feature = self._rpn_activation(rpn_conv_feature)
  
      # Then we apply separate conv layers for classification and regression.
      rpn_cls_score_original = self._rpn_cls(rpn_feature)
      rpn_bbox_pred_original = self._rpn_bbox(rpn_feature)
      # rpn_cls_score_original has shape (1, H, W, num_anchors * 2)
      # rpn_bbox_pred_original has shape (1, H, W, num_anchors * 4)
      # where H, W are height and width of the pretrained feature map.
  
      # Convert (flatten) `rpn_cls_score_original` which has two scalars per
      # anchor per location to be able to apply softmax.
      rpn_cls_score = tf.reshape(rpn_cls_score_original, [-1, 2])
      # Now that `rpn_cls_score` has shape (H * W * num_anchors, 2), we apply
      # softmax to the last dim.
      rpn_cls_prob = tf.nn.softmax(rpn_cls_score)
  
      prediction_dict["rpn_cls_prob"] = rpn_cls_prob
      prediction_dict["rpn_cls_score"] = rpn_cls_score
  
      # Flatten bounding box delta prediction for easy manipulation.
      # We end up with `rpn_bbox_pred` having shape (H * W * num_anchors, 4).
      rpn_bbox_pred = tf.reshape(rpn_bbox_pred_original, [-1, 4])
  
      prediction_dict["rpn_bbox_pred"] = rpn_bbox_pred
  
      # We have to convert bbox deltas to usable bounding boxes and remove
      # redundant ones using Non Maximum Suppression (NMS).
      proposal_prediction = self._proposal(
          rpn_cls_prob, rpn_bbox_pred, all_anchors, im_shape
      )
  
      prediction_dict["proposals"] = proposal_prediction["proposals"]
      prediction_dict["scores"] = proposal_prediction["scores"]
  
      if self._debug:
          prediction_dict["proposal_prediction"] = proposal_prediction
  
      if gt_boxes is not None:
          # When training we use a separate module to calculate the target
          # values we want to output.
          (
              rpn_cls_target,
              rpn_bbox_target,
              rpn_max_overlap,
          ) = self._anchor_target(all_anchors, gt_boxes, im_shape)
  
          prediction_dict["rpn_cls_target"] = rpn_cls_target
          prediction_dict["rpn_bbox_target"] = rpn_bbox_target
  
          if self._debug:
              prediction_dict["rpn_max_overlap"] = rpn_max_overlap
              variable_summaries(
                  rpn_bbox_target, "rpn_bbox_target", "full"
              )
  
      # Variables summaries.
      variable_summaries(
          prediction_dict["scores"], "rpn_scores", "reduced"
      )
      variable_summaries(rpn_cls_prob, "rpn_cls_prob", "reduced")
      variable_summaries(rpn_bbox_pred, "rpn_bbox_pred", "reduced")
  
      if self._debug:
          variable_summaries(rpn_feature, "rpn_feature", "full")
          variable_summaries(
              rpn_cls_score_original, "rpn_cls_score_original", "full"
          )
          variable_summaries(
              rpn_bbox_pred_original, "rpn_bbox_pred_original", "full"
          )
  
          # Layer summaries.
          layer_summaries(self._rpn, "full")
          layer_summaries(self._rpn_cls, "full")
          layer_summaries(self._rpn_bbox, "full")
  
      return prediction_dict
  
file_path: luminoth/luminoth/models/fasterrcnn/rpn.py
imports: import sonnet
  import tensorflow
  from sonnet.python.modules.conv import Conv2D
  from .rpn_target import RPNTarget
  from .rpn_proposal import RPNProposal
  from luminoth.utils.losses import smooth_l1_loss
  from luminoth.utils.vars import get_initializer, layer_summaries, variable_summaries, get_activation_function
label_desc: Use image node_id 3 for calling the RPN obj's underlying member method code with example usage: obj._build(conv_feature_map, im_shape, all_anchors, gt_boxes, is_training) and returns: prediction_dict