code: def test_guided_anchor():
      from mmdet.registry import MODELS
  
      if torch.cuda.is_available():
          device = "cuda"
      else:
          device = "cpu"
      # head configs modified from
      # configs/guided_anchoring/ga-retinanet_r50_fpn_1x_coco.py
      bbox_head = dict(
          type="GARetinaHead",
          num_classes=8,
          in_channels=4,
          stacked_convs=1,
          feat_channels=4,
          approx_anchor_generator=dict(
              type="AnchorGenerator",
              octave_base_scale=4,
              scales_per_octave=3,
              ratios=[0.5, 1.0, 2.0],
              strides=[8, 16, 32, 64, 128],
          ),
          square_anchor_generator=dict(
              type="AnchorGenerator",
              ratios=[1.0],
              scales=[4],
              strides=[8, 16, 32, 64, 128],
          ),
      )
  
      ga_retina_head = MODELS.build(bbox_head)
      assert ga_retina_head.approx_anchor_generator is not None
  
      # use the featmap sizes in NASFPN setting to test ga_retina_head
      featmap_sizes = [
          (100, 152),
          (50, 76),
          (25, 38),
          (13, 19),
          (7, 10),
      ]
      # check base anchors
      expected_approxs = [
          torch.Tensor(
              [
                  [-22.6274, -11.3137, 22.6274, 11.3137],
                  [-28.5088, -14.2544, 28.5088, 14.2544],
                  [-35.9188, -17.9594, 35.9188, 17.9594],
                  [-16.0000, -16.0000, 16.0000, 16.0000],
                  [-20.1587, -20.1587, 20.1587, 20.1587],
                  [-25.3984, -25.3984, 25.3984, 25.3984],
                  [-11.3137, -22.6274, 11.3137, 22.6274],
                  [-14.2544, -28.5088, 14.2544, 28.5088],
                  [-17.9594, -35.9188, 17.9594, 35.9188],
              ]
          ),
          torch.Tensor(
              [
                  [-45.2548, -22.6274, 45.2548, 22.6274],
                  [-57.0175, -28.5088, 57.0175, 28.5088],
                  [-71.8376, -35.9188, 71.8376, 35.9188],
                  [-32.0000, -32.0000, 32.0000, 32.0000],
                  [-40.3175, -40.3175, 40.3175, 40.3175],
                  [-50.7968, -50.7968, 50.7968, 50.7968],
                  [-22.6274, -45.2548, 22.6274, 45.2548],
                  [-28.5088, -57.0175, 28.5088, 57.0175],
                  [-35.9188, -71.8376, 35.9188, 71.8376],
              ]
          ),
          torch.Tensor(
              [
                  [-90.5097, -45.2548, 90.5097, 45.2548],
                  [-114.0350, -57.0175, 114.0350, 57.0175],
                  [-143.6751, -71.8376, 143.6751, 71.8376],
                  [-64.0000, -64.0000, 64.0000, 64.0000],
                  [-80.6349, -80.6349, 80.6349, 80.6349],
                  [-101.5937, -101.5937, 101.5937, 101.5937],
                  [-45.2548, -90.5097, 45.2548, 90.5097],
                  [-57.0175, -114.0350, 57.0175, 114.0350],
                  [-71.8376, -143.6751, 71.8376, 143.6751],
              ]
          ),
          torch.Tensor(
              [
                  [-181.0193, -90.5097, 181.0193, 90.5097],
                  [-228.0701, -114.0350, 228.0701, 114.0350],
                  [-287.3503, -143.6751, 287.3503, 143.6751],
                  [-128.0000, -128.0000, 128.0000, 128.0000],
                  [-161.2699, -161.2699, 161.2699, 161.2699],
                  [-203.1873, -203.1873, 203.1873, 203.1873],
                  [-90.5097, -181.0193, 90.5097, 181.0193],
                  [-114.0350, -228.0701, 114.0350, 228.0701],
                  [-143.6751, -287.3503, 143.6751, 287.3503],
              ]
          ),
          torch.Tensor(
              [
                  [-362.0387, -181.0193, 362.0387, 181.0193],
                  [-456.1401, -228.0701, 456.1401, 228.0701],
                  [-574.7006, -287.3503, 574.7006, 287.3503],
                  [-256.0000, -256.0000, 256.0000, 256.0000],
                  [-322.5398, -322.5398, 322.5398, 322.5398],
                  [-406.3747, -406.3747, 406.3747, 406.3747],
                  [-181.0193, -362.0387, 181.0193, 362.0387],
                  [-228.0701, -456.1401, 228.0701, 456.1401],
                  [-287.3503, -574.7006, 287.3503, 574.7006],
              ]
          ),
      ]
      approxs = ga_retina_head.approx_anchor_generator.base_anchors
      for i, base_anchor in enumerate(approxs):
          assert base_anchor.allclose(expected_approxs[i])
  
      # check valid flags
      expected_valid_pixels = [136800, 34200, 8550, 2223, 630]
      multi_level_valid_flags = (
          ga_retina_head.approx_anchor_generator.valid_flags(
              featmap_sizes, (800, 1216), device
          )
      )
      for i, single_level_valid_flag in enumerate(
          multi_level_valid_flags
      ):
          assert (
              single_level_valid_flag.sum() == expected_valid_pixels[i]
          )
  
      # check number of base anchors for each level
      assert (
          ga_retina_head.approx_anchor_generator.num_base_anchors
          == [9, 9, 9, 9, 9]
      )
  
      # check approx generation
      squares = ga_retina_head.square_anchor_generator.grid_anchors(
          featmap_sizes, device
      )
      assert len(squares) == 5
  
      expected_squares = [
          torch.Tensor([[-16.0, -16.0, 16.0, 16.0]]),
          torch.Tensor([[-32.0, -32.0, 32.0, 32]]),
          torch.Tensor([[-64.0, -64.0, 64.0, 64.0]]),
          torch.Tensor([[-128.0, -128.0, 128.0, 128.0]]),
          torch.Tensor([[-256.0, -256.0, 256.0, 256.0]]),
      ]
      squares = ga_retina_head.square_anchor_generator.base_anchors
      for i, base_anchor in enumerate(squares):
          assert base_anchor.allclose(expected_squares[i])
  
      # square_anchor_generator does not check valid flags
      # check number of base anchors for each level
      assert (
          ga_retina_head.square_anchor_generator.num_base_anchors
          == [1, 1, 1, 1, 1]
      )
  
      # check square generation
      anchors = ga_retina_head.square_anchor_generator.grid_anchors(
          featmap_sizes, device
      )
      assert len(anchors) == 5
  
file_path: mmdetection/tests/test_models/test_task_modules/test_prior_generators/test_anchor_generator.py
imports: import pytest
  import torch
label_desc: Use image node_id 9 for calling a global function with example usage: test_guided_anchor() without return types