docstr: Tests guided anchor loss when truth is empty and non-empty.
code: def test_guided_anchor_head_loss(self):
      """Tests guided anchor loss when truth is empty and non-empty."""
      s = 256
      img_metas = [
          {
              "img_shape": (s, s),
              "pad_shape": (s, s),
              "scale_factor": (1, 1),
          }
      ]
      guided_anchor_head = GuidedAnchorHead(**guided_anchor_head_config)
  
      feats = (
          torch.rand(1, 4, s // stride[1], s // stride[0])
          for stride in guided_anchor_head.square_anchor_generator.strides
      )
      outs = guided_anchor_head(feats)
  
      # Test that empty ground truth encourages the network to
      # predict background
      gt_instances = InstanceData()
      gt_instances.bboxes = torch.empty((0, 4))
      gt_instances.labels = torch.LongTensor([])
  
      empty_gt_losses = guided_anchor_head.loss_by_feat(
          *outs, [gt_instances], img_metas
      )
      # When there is no truth, the cls loss should be nonzero but
      # box shape and location loss should be zero
      empty_cls_loss = sum(empty_gt_losses["loss_cls"]).item()
      empty_box_loss = sum(empty_gt_losses["loss_bbox"]).item()
      empty_shape_loss = sum(empty_gt_losses["loss_shape"]).item()
      empty_loc_loss = sum(empty_gt_losses["loss_loc"]).item()
      self.assertGreater(
          empty_cls_loss, 0, "cls loss should be non-zero"
      )
      self.assertGreater(
          empty_loc_loss, 0, "location loss should be non-zero"
      )
      self.assertEqual(
          empty_box_loss,
          0,
          "there should be no box loss when there are no true boxes",
      )
      self.assertEqual(
          empty_shape_loss,
          0,
          "there should be no shape loss when there are no true boxes",
      )
  
      # When truth is non-empty then all cls, box loss and centerness loss
      # should be nonzero for random inputs
      gt_instances = InstanceData()
      gt_instances.bboxes = torch.Tensor(
          [[23.6667, 23.8757, 238.6326, 151.8874]]
      )
      gt_instances.labels = torch.LongTensor([2])
  
      one_gt_losses = guided_anchor_head.loss_by_feat(
          *outs, [gt_instances], img_metas
      )
      onegt_cls_loss = sum(one_gt_losses["loss_cls"]).item()
      onegt_box_loss = sum(one_gt_losses["loss_bbox"]).item()
      onegt_shape_loss = sum(one_gt_losses["loss_shape"]).item()
      onegt_loc_loss = sum(one_gt_losses["loss_loc"]).item()
      self.assertGreater(
          onegt_cls_loss, 0, "cls loss should be non-zero"
      )
      self.assertGreater(
          onegt_box_loss, 0, "box loss should be non-zero"
      )
      self.assertGreater(
          onegt_shape_loss, 0, "shape loss should be non-zero"
      )
      self.assertGreater(
          onegt_loc_loss, 0, "location loss should be non-zero"
      )
  
file_path: mmdetection/tests/test_models/test_dense_heads/test_guided_anchor_head.py
imports: from unittest import TestCase
  import torch
  from mmengine.config import ConfigDict
  from mmengine.structures import InstanceData
  from mmdet.models.dense_heads import GuidedAnchorHead
label_desc: Use image node_id 1 for calling the TestGuidedAnchorHead obj's underlying member method code with example usage: obj.test_guided_anchor_head_loss() without return types