docstr: Tests center head loss when truth is empty and non-empty.
docstr_tok: ["Tests","center","head","loss","when","truth","is","empty","and","non-empty","."]
code: def test_center_head_loss(self):
      """Tests center head loss when truth is empty and non-empty."""
      s = 256
      img_metas = [{"batch_input_shape": (s, s, 3)}]
      test_cfg = dict(topK=100, max_per_img=100)
      centernet_head = CenterNetHead(
          num_classes=4,
          in_channels=1,
          feat_channels=4,
          test_cfg=test_cfg,
      )
  
      feat = [torch.rand(1, 1, s, s)]
      center_out, wh_out, offset_out = centernet_head.forward(feat)
      # 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 = centernet_head.loss_by_feat(
          center_out, wh_out, offset_out, [gt_instances], img_metas
      )
      loss_center = empty_gt_losses["loss_center_heatmap"]
      loss_wh = empty_gt_losses["loss_wh"]
      loss_offset = empty_gt_losses["loss_offset"]
      assert loss_center.item() > 0, "loss_center should be non-zero"
      assert (
          loss_wh.item() == 0
      ), "there should be no loss_wh when there are no true boxes"
      assert (
          loss_offset.item() == 0
      ), "there should be no loss_offset when there are no true boxes"
  
      # When truth is non-empty then both cls and box 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 = centernet_head.loss_by_feat(
          center_out, wh_out, offset_out, [gt_instances], img_metas
      )
      loss_center = one_gt_losses["loss_center_heatmap"]
      loss_wh = one_gt_losses["loss_wh"]
      loss_offset = one_gt_losses["loss_offset"]
      assert loss_center.item() > 0, "loss_center should be non-zero"
      assert loss_wh.item() > 0, "loss_wh should be non-zero"
      assert loss_offset.item() > 0, "loss_offset should be non-zero"
  
file_path: mmdetection/tests/test_models/test_dense_heads/test_centernet_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 CenterNetHead
label_desc: Use image node_id 1 for calling the TestCenterNetHead obj's underlying member method code with example usage: obj.test_center_head_loss() without return types