docstr: Tests we're computing loss correctly.
  
  In particular, we're testing whether computing a perfect score when we
  have to.
docstr_tok: ["Tests","we","'re","computing","loss","correctly",".","In","particular",",","we","'re","testing","whether","computing","a","perfect","score","when","we","have","to","."]
code: def testLoss(self):
      """Tests we're computing loss correctly.
  
      In particular, we're testing whether computing a perfect score when we
      have to.
      """
  
      # Generate placeholders and loss_graph
      cls_score_shape = (self._num_proposals, self._num_classes + 1)
      cls_score_ph = tf.placeholder(tf.float32, cls_score_shape)
  
      cls_prob_shape = (self._num_proposals, self._num_classes + 1)
      cls_prob_ph = tf.placeholder(tf.float32, cls_prob_shape)
  
      cls_target_shape = (self._num_proposals,)
      cls_target_ph = tf.placeholder(tf.float32, cls_target_shape)
  
      bbox_offsets_shape = (self._num_proposals, self._num_classes * 4)
      bbox_offsets_ph = tf.placeholder(tf.float32, bbox_offsets_shape)
  
      bbox_offsets_target_shape = (self._num_proposals, 4)
      bbox_offsets_target_ph = tf.placeholder(
          tf.float32, bbox_offsets_target_shape
      )
  
      loss_graph = self._shared_model.loss(
          {
              "rcnn": {
                  "cls_score": cls_score_ph,
                  "cls_prob": cls_prob_ph,
                  "bbox_offsets": bbox_offsets_ph,
              },
              "target": {
                  "cls": cls_target_ph,
                  "bbox_offsets": bbox_offsets_target_ph,
              },
          }
      )
  
      # Generate values that ensure a perfect score
      # We first initialize all our values to zero.
      cls_score = np.zeros(cls_score_shape, dtype=np.float32)
      cls_prob = np.zeros(cls_prob_shape, dtype=np.float32)
      cls_target = np.zeros(cls_target_shape, dtype=np.float32)
      bbox_offsets = np.zeros(bbox_offsets_shape, dtype=np.float32)
      bbox_offsets_target = np.zeros(
          bbox_offsets_target_shape, dtype=np.float32
      )
      for i in range(self._num_proposals):
          this_class = np.random.randint(
              low=1, high=self._num_classes + 1
          )
  
          cls_score[i][this_class] = self._high_score
          cls_prob[i][this_class] = 1.0
          cls_target[i] = this_class
  
          # Find out where in the axis 1 in bbox_offsets we should
          # put the offsets, because the shape is
          # (num_proposals, num_classes * 4), and we're using
          # 1-indexed classes.
          class_place = (this_class - 1) * 4
          for j in range(4):
              this_coord = np.random.randint(
                  low=0, high=np.amax(self._image_shape)
              )
  
              bbox_offsets[i][class_place + j] = this_coord
              bbox_offsets_target[i][j] = this_coord
      # Now get the loss dict using the values we just generated.
      loss_dict = self._run_net_with_feed_dict(
          loss_graph,
          feed_dict={
              cls_score_ph: cls_score,
              cls_prob_ph: cls_prob,
              cls_target_ph: cls_target,
              bbox_offsets_ph: bbox_offsets,
              bbox_offsets_target_ph: bbox_offsets_target,
          },
      )
      # Assertions
      self.assertAlmostEqual(
          loss_dict["rcnn_cls_loss"], 0, delta=self._equality_delta
      )
      self.assertAlmostEqual(
          loss_dict["rcnn_reg_loss"], 0, delta=self._equality_delta
      )
  
file_path: luminoth/luminoth/models/fasterrcnn/rcnn_test.py
imports: import numpy
  import tensorflow
  from easydict import EasyDict
  from luminoth.models.fasterrcnn.rcnn import RCNN
label_desc: Use image node_id 7 for calling the RCNNTest obj's underlying member method code with example usage: obj.testLoss() without return types