code: def test_ssd_anchor_generator():
      from mmdet.models.task_modules import build_anchor_generator
  
      if torch.cuda.is_available():
          device = "cuda"
      else:
          device = "cpu"
  
      # min_sizes max_sizes must set at the same time
      with pytest.raises(AssertionError):
          anchor_generator_cfg = dict(
              type="SSDAnchorGenerator",
              scale_major=False,
              min_sizes=[48, 100, 150, 202, 253, 300],
              max_sizes=None,
              strides=[8, 16, 32, 64, 100, 300],
              ratios=[[2], [2, 3], [2, 3], [2, 3], [2], [2]],
          )
          build_anchor_generator(anchor_generator_cfg)
  
      # length of min_sizes max_sizes must be the same
      with pytest.raises(AssertionError):
          anchor_generator_cfg = dict(
              type="SSDAnchorGenerator",
              scale_major=False,
              min_sizes=[48, 100, 150, 202, 253, 300],
              max_sizes=[100, 150, 202, 253],
              strides=[8, 16, 32, 64, 100, 300],
              ratios=[[2], [2, 3], [2, 3], [2, 3], [2], [2]],
          )
          build_anchor_generator(anchor_generator_cfg)
  
      # test setting anchor size manually
      anchor_generator_cfg = dict(
          type="SSDAnchorGenerator",
          scale_major=False,
          min_sizes=[48, 100, 150, 202, 253, 304],
          max_sizes=[100, 150, 202, 253, 304, 320],
          strides=[16, 32, 64, 107, 160, 320],
          ratios=[[2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3]],
      )
  
      featmap_sizes = [
          (38, 38),
          (19, 19),
          (10, 10),
          (5, 5),
          (3, 3),
          (1, 1),
      ]
      anchor_generator = build_anchor_generator(anchor_generator_cfg)
  
      expected_base_anchors = [
          torch.Tensor(
              [
                  [-16.0000, -16.0000, 32.0000, 32.0000],
                  [-26.6410, -26.6410, 42.6410, 42.6410],
                  [-25.9411, -8.9706, 41.9411, 24.9706],
                  [-8.9706, -25.9411, 24.9706, 41.9411],
                  [-33.5692, -5.8564, 49.5692, 21.8564],
                  [-5.8564, -33.5692, 21.8564, 49.5692],
              ]
          ),
          torch.Tensor(
              [
                  [-34.0000, -34.0000, 66.0000, 66.0000],
                  [-45.2372, -45.2372, 77.2372, 77.2372],
                  [-54.7107, -19.3553, 86.7107, 51.3553],
                  [-19.3553, -54.7107, 51.3553, 86.7107],
                  [-70.6025, -12.8675, 102.6025, 44.8675],
                  [-12.8675, -70.6025, 44.8675, 102.6025],
              ]
          ),
          torch.Tensor(
              [
                  [-43.0000, -43.0000, 107.0000, 107.0000],
                  [-55.0345, -55.0345, 119.0345, 119.0345],
                  [-74.0660, -21.0330, 138.0660, 85.0330],
                  [-21.0330, -74.0660, 85.0330, 138.0660],
                  [-97.9038, -11.3013, 161.9038, 75.3013],
                  [-11.3013, -97.9038, 75.3013, 161.9038],
              ]
          ),
          torch.Tensor(
              [
                  [-47.5000, -47.5000, 154.5000, 154.5000],
                  [-59.5332, -59.5332, 166.5332, 166.5332],
                  [-89.3356, -17.9178, 196.3356, 124.9178],
                  [-17.9178, -89.3356, 124.9178, 196.3356],
                  [-121.4371, -4.8124, 228.4371, 111.8124],
                  [-4.8124, -121.4371, 111.8124, 228.4371],
              ]
          ),
          torch.Tensor(
              [
                  [-46.5000, -46.5000, 206.5000, 206.5000],
                  [-58.6651, -58.6651, 218.6651, 218.6651],
                  [-98.8980, -9.4490, 258.8980, 169.4490],
                  [-9.4490, -98.8980, 169.4490, 258.8980],
                  [-139.1044, 6.9652, 299.1044, 153.0348],
                  [6.9652, -139.1044, 153.0348, 299.1044],
              ]
          ),
          torch.Tensor(
              [
                  [8.0000, 8.0000, 312.0000, 312.0000],
                  [4.0513, 4.0513, 315.9487, 315.9487],
                  [-54.9605, 52.5198, 374.9604, 267.4802],
                  [52.5198, -54.9605, 267.4802, 374.9604],
                  [-103.2717, 72.2428, 423.2717, 247.7572],
                  [72.2428, -103.2717, 247.7572, 423.2717],
              ]
          ),
      ]
  
      base_anchors = anchor_generator.base_anchors
      for i, base_anchor in enumerate(base_anchors):
          assert base_anchor.allclose(expected_base_anchors[i])
  
      # check valid flags
      expected_valid_pixels = [2400, 600, 150, 54, 24, 6]
      multi_level_valid_flags = anchor_generator.valid_flags(
          featmap_sizes, (320, 320), 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 anchor_generator.num_base_anchors == [6, 6, 6, 6, 6, 6]
  
      # check anchor generation
      anchors = anchor_generator.grid_anchors(featmap_sizes, device)
      assert len(anchors) == 6
  
      # test vgg ssd anchor setting
      anchor_generator_cfg = dict(
          type="SSDAnchorGenerator",
          scale_major=False,
          input_size=300,
          basesize_ratio_range=(0.15, 0.9),
          strides=[8, 16, 32, 64, 100, 300],
          ratios=[[2], [2, 3], [2, 3], [2, 3], [2], [2]],
      )
  
      featmap_sizes = [
          (38, 38),
          (19, 19),
          (10, 10),
          (5, 5),
          (3, 3),
          (1, 1),
      ]
      anchor_generator = build_anchor_generator(anchor_generator_cfg)
  
      # check base anchors
      expected_base_anchors = [
          torch.Tensor(
              [
                  [-6.5000, -6.5000, 14.5000, 14.5000],
                  [-11.3704, -11.3704, 19.3704, 19.3704],
                  [-10.8492, -3.4246, 18.8492, 11.4246],
                  [-3.4246, -10.8492, 11.4246, 18.8492],
              ]
          ),
          torch.Tensor(
              [
                  [-14.5000, -14.5000, 30.5000, 30.5000],
                  [-25.3729, -25.3729, 41.3729, 41.3729],
                  [-23.8198, -7.9099, 39.8198, 23.9099],
                  [-7.9099, -23.8198, 23.9099, 39.8198],
                  [-30.9711, -4.9904, 46.9711, 20.9904],
                  [-4.9904, -30.9711, 20.9904, 46.9711],
              ]
          ),
          torch.Tensor(
              [
                  [-33.5000, -33.5000, 65.5000, 65.5000],
                  [-45.5366, -45.5366, 77.5366, 77.5366],
                  [-54.0036, -19.0018, 86.0036, 51.0018],
                  [-19.0018, -54.0036, 51.0018, 86.0036],
                  [-69.7365, -12.5788, 101.7365, 44.5788],
                  [-12.5788, -69.7365, 44.5788, 101.7365],
              ]
          ),
          torch.Tensor(
              [
                  [-44.5000, -44.5000, 108.5000, 108.5000],
                  [-56.9817, -56.9817, 120.9817, 120.9817],
                  [-76.1873, -22.0937, 140.1873, 86.0937],
                  [-22.0937, -76.1873, 86.0937, 140.1873],
                  [-100.5019, -12.1673, 164.5019, 76.1673],
                  [-12.1673, -100.5019, 76.1673, 164.5019],
              ]
          ),
          torch.Tensor(
              [
                  [-53.5000, -53.5000, 153.5000, 153.5000],
                  [-66.2185, -66.2185, 166.2185, 166.2185],
                  [-96.3711, -23.1855, 196.3711, 123.1855],
                  [-23.1855, -96.3711, 123.1855, 196.3711],
              ]
          ),
          torch.Tensor(
              [
                  [19.5000, 19.5000, 280.5000, 280.5000],
                  [6.6342, 6.6342, 293.3658, 293.3658],
                  [-34.5549, 57.7226, 334.5549, 242.2774],
                  [57.7226, -34.5549, 242.2774, 334.5549],
              ]
          ),
      ]
      base_anchors = anchor_generator.base_anchors
      for i, base_anchor in enumerate(base_anchors):
          assert base_anchor.allclose(expected_base_anchors[i])
  
      # check valid flags
      expected_valid_pixels = [5776, 2166, 600, 150, 36, 4]
      multi_level_valid_flags = anchor_generator.valid_flags(
          featmap_sizes, (300, 300), 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 anchor_generator.num_base_anchors == [4, 6, 6, 6, 4, 4]
  
      # check anchor generation
      anchors = anchor_generator.grid_anchors(featmap_sizes, device)
      assert len(anchors) == 6
  
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 5 for calling a global function with example usage: test_ssd_anchor_generator() without return types