class_docstr: Faster RCNN Network module
  
  Builds the Faster RCNN network architecture using different submodules.
  Calculates the total loss of the model based on the different losses by
  each of the submodules.
  
  It is also responsible for building the anchor reference which is used in
  graph for generating the dynamic anchors.
class_docstr_tok: ["Faster","RCNN","Network","module","Builds","the","Faster","RCNN","network","architecture","using","different","submodules",".","Calculates","the","total","loss","of","the","model","based","on","the","different","losses","by","each","of","the","submodules",".","It","is","also","responsible","for","building","the","anchor","reference","which","is","used","in","graph","for","generating","the","dynamic","anchors","."]
docstr: Compute the joint training loss for Faster RCNN.
  
  Args:
      prediction_dict: The output dictionary of the _build method from
          which we use two different main keys:
  
          rpn_prediction: A dictionary with the output Tensors from the
              RPN.
          classification_prediction: A dictionary with the output Tensors
              from the RCNN.
  
  Returns:
      If `return_all` is False, a tensor for the total loss. If True, a
      dict with all the internal losses (RPN's, RCNN's, regularization
      and total loss).
docstr_tok: ["Compute","the","joint","training","loss","for","Faster","RCNN",".","Args",":","prediction_dict",":","The","output","dictionary","of","the","_build","method","from","which","we","use","two","different","main","keys",":","rpn_prediction",":","A","dictionary","with","the","output","Tensors","from","the","RPN",".","classification_prediction",":","A","dictionary","with","the","output","Tensors","from","the","RCNN",".","Returns",":","If","`","return_all","`","is","False",",","a","tensor","for","the","total","loss",".","If","True",",","a","dict","with","all","the","internal","losses","(","RPN","'s",",","RCNN","'s",",","regularization","and","total","loss",")","."]
code: def loss(self, prediction_dict, return_all=False):
      """Compute the joint training loss for Faster RCNN.
  
      Args:
          prediction_dict: The output dictionary of the _build method from
              which we use two different main keys:
  
              rpn_prediction: A dictionary with the output Tensors from the
                  RPN.
              classification_prediction: A dictionary with the output Tensors
                  from the RCNN.
  
      Returns:
          If `return_all` is False, a tensor for the total loss. If True, a
          dict with all the internal losses (RPN's, RCNN's, regularization
          and total loss).
      """
  
      with tf.name_scope("losses"):
          rpn_loss_dict = self._rpn.loss(
              prediction_dict["rpn_prediction"]
          )
  
          # Losses have a weight assigned, we multiply by them before saving
          # them.
          rpn_loss_dict["rpn_cls_loss"] = (
              rpn_loss_dict["rpn_cls_loss"] * self._rpn_cls_loss_weight
          )
          rpn_loss_dict["rpn_reg_loss"] = (
              rpn_loss_dict["rpn_reg_loss"] * self._rpn_reg_loss_weight
          )
  
          prediction_dict["rpn_loss_dict"] = rpn_loss_dict
  
          if self._with_rcnn:
              rcnn_loss_dict = self._rcnn.loss(
                  prediction_dict["classification_prediction"]
              )
  
              rcnn_loss_dict["rcnn_cls_loss"] = (
                  rcnn_loss_dict["rcnn_cls_loss"]
                  * self._rcnn_cls_loss_weight
              )
              rcnn_loss_dict["rcnn_reg_loss"] = (
                  rcnn_loss_dict["rcnn_reg_loss"]
                  * self._rcnn_reg_loss_weight
              )
  
              prediction_dict["rcnn_loss_dict"] = rcnn_loss_dict
          else:
              rcnn_loss_dict = {}
  
          all_losses_items = list(rpn_loss_dict.items()) + list(
              rcnn_loss_dict.items()
          )
  
          for loss_name, loss_tensor in all_losses_items:
              tf.summary.scalar(
                  loss_name,
                  loss_tensor,
                  collections=self._losses_collections,
              )
              # We add losses to the losses collection instead of manually
              # summing them just in case somebody wants to use it in another
              # place.
              tf.losses.add_loss(loss_tensor)
  
          # Regularization loss is automatically saved by TensorFlow, we log
          # it differently so we can visualize it independently.
          regularization_loss = tf.losses.get_regularization_loss()
          # Total loss without regularization
          no_reg_loss = tf.losses.get_total_loss(
              add_regularization_losses=False
          )
          total_loss = tf.losses.get_total_loss()
  
          tf.summary.scalar(
              "total_loss",
              total_loss,
              collections=self._losses_collections,
          )
          tf.summary.scalar(
              "no_reg_loss",
              no_reg_loss,
              collections=self._losses_collections,
          )
          tf.summary.scalar(
              "regularization_loss",
              regularization_loss,
              collections=self._losses_collections,
          )
  
          if return_all:
              loss_dict = {
                  "total_loss": total_loss,
                  "no_reg_loss": no_reg_loss,
                  "regularization_loss": regularization_loss,
              }
  
              for loss_name, loss_tensor in all_losses_items:
                  loss_dict[loss_name] = loss_tensor
  
              return loss_dict
  
          # We return the total loss, which includes:
          # - rpn loss
          # - rcnn loss (if activated)
          # - regularization loss
          return total_loss
  
file_path: luminoth/luminoth/models/fasterrcnn/fasterrcnn.py
imports: import numpy
  import sonnet
  import tensorflow
  from luminoth.models.fasterrcnn.rcnn import RCNN
  from luminoth.models.fasterrcnn.rpn import RPN
  from luminoth.models.base import TruncatedBaseNetwork
  from luminoth.utils.anchors import generate_anchors_reference
  from luminoth.utils.vars import VAR_LOG_LEVELS, variable_summaries
label_desc: Use image node_id 3 for calling the FasterRCNN obj's underlying member method code with example usage: obj.loss(prediction_dict, return_all) and returns: total_loss, loss_dict