Skip to content

add anchor generators used for detection#4336

Merged
wyli merged 45 commits intoProject-MONAI:devfrom
Can-Zhao:anchor
May 30, 2022
Merged

add anchor generators used for detection#4336
wyli merged 45 commits intoProject-MONAI:devfrom
Can-Zhao:anchor

Conversation

@Can-Zhao
Copy link
Copy Markdown
Collaborator

Signed-off-by: Can Zhao [email protected]

Fixes #3573 .

Description

add anchor generators used for detection

Status

Ready/Work in progress/Hold

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • Breaking change (fix or new feature that would cause existing functionality to change).
  • New tests added to cover the changes.
  • Integration tests passed locally by running ./runtests.sh -f -u --net --coverage.
  • Quick tests passed locally by running ./runtests.sh --quick --unittests --disttests.
  • In-line docstrings updated.
  • Documentation updated, tested make html command in the docs/ folder.

@Can-Zhao Can-Zhao marked this pull request as ready for review May 24, 2022 21:02
@Can-Zhao Can-Zhao requested a review from dongyang0122 May 24, 2022 21:02
Copy link
Copy Markdown
Contributor

@wyli wyli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please add some unit tests for this module?

@Can-Zhao
Copy link
Copy Markdown
Collaborator Author

could you please add some unit tests for this module?

Thanks. Just added

Can-Zhao and others added 12 commits May 25, 2022 14:41
Signed-off-by: Can Zhao <[email protected]>
Signed-off-by: Can Zhao <[email protected]>
Signed-off-by: Can Zhao <[email protected]>
Signed-off-by: Can Zhao <[email protected]>
Signed-off-by: Can Zhao <[email protected]>
Signed-off-by: Can Zhao <[email protected]>
Signed-off-by: Can Zhao <[email protected]>
Signed-off-by: Can Zhao <[email protected]>
Signed-off-by: Can Zhao <[email protected]>
Signed-off-by: Can Zhao <[email protected]>
Can-Zhao added 2 commits May 29, 2022 10:43
Signed-off-by: Can Zhao <[email protected]>
Signed-off-by: Can Zhao <[email protected]>
@wyli
Copy link
Copy Markdown
Contributor

wyli commented May 29, 2022

/build

@wyli wyli enabled auto-merge (squash) May 29, 2022 19:29
- torchvision compatibility
- torchscript compatibility

Signed-off-by: Wenqi Li <[email protected]>
@wyli
Copy link
Copy Markdown
Contributor

wyli commented May 30, 2022

fyi, some issues with torch 1.7/1.8 from tests:

Details
[2022-05-29T19:56:23.845Z] ======================================================================
[2022-05-29T19:56:23.845Z] ERROR: test_anchor_2d_0 (tests.test_anchor_box.TestAnchorGenerator)
[2022-05-29T19:56:23.845Z] ----------------------------------------------------------------------
[2022-05-29T19:56:23.845Z] Traceback (most recent call last):
[2022-05-29T19:56:23.845Z]   File "/usr/local/lib/python3.7/dist-packages/parameterized/parameterized.py", line 533, in standalone_func
[2022-05-29T19:56:23.845Z]     return func(*(a + p.args), **p.kwargs)
[2022-05-29T19:56:23.845Z]   File "/home/jenkins/agent/workspace/MONAI-premerge/monai/tests/test_anchor_box.py", line 53, in test_anchor_2d
[2022-05-29T19:56:23.845Z]     for a, a_f in zip(anchor.cell_anchors, anchor_ref.cell_anchors):
[2022-05-29T19:56:23.845Z] TypeError: zip argument #2 must support iteration
[2022-05-29T19:56:23.845Z] 
[2022-05-29T19:56:23.845Z] ======================================================================
[2022-05-29T19:56:23.845Z] ERROR: test_script_2d_0 (tests.test_anchor_box.TestAnchorGenerator)
[2022-05-29T19:56:23.845Z] ----------------------------------------------------------------------
[2022-05-29T19:56:23.845Z] Traceback (most recent call last):
[2022-05-29T19:56:23.845Z]   File "/usr/local/lib/python3.7/dist-packages/parameterized/parameterized.py", line 533, in standalone_func
[2022-05-29T19:56:23.845Z]     return func(*(a + p.args), **p.kwargs)
[2022-05-29T19:56:23.845Z]   File "/home/jenkins/agent/workspace/MONAI-premerge/monai/tests/test_anchor_box.py", line 76, in test_script_2d
[2022-05-29T19:56:23.845Z]     test_script_save(anchor, images, feature_maps)
[2022-05-29T19:56:23.845Z]   File "/home/jenkins/agent/workspace/MONAI-premerge/monai/tests/utils.py", line 682, in test_script_save
[2022-05-29T19:56:23.845Z]     atol=atol,
[2022-05-29T19:56:23.845Z]   File "/home/jenkins/agent/workspace/MONAI-premerge/monai/monai/networks/utils.py", line 539, in convert_to_torchscript
[2022-05-29T19:56:23.845Z]     script_module = torch.jit.script(model, **kwargs)
[2022-05-29T19:56:23.845Z]   File "/usr/local/lib/python3.7/dist-packages/torch/jit/_script.py", line 898, in script
[2022-05-29T19:56:23.845Z]     obj, torch.jit._recursive.infer_methods_to_compile
[2022-05-29T19:56:23.845Z]   File "/usr/local/lib/python3.7/dist-packages/torch/jit/_recursive.py", line 352, in create_script_module
[2022-05-29T19:56:23.845Z]     return create_script_module_impl(nn_module, concrete_type, stubs_fn)
[2022-05-29T19:56:23.845Z]   File "/usr/local/lib/python3.7/dist-packages/torch/jit/_recursive.py", line 410, in create_script_module_impl
[2022-05-29T19:56:23.845Z]     create_methods_and_properties_from_stubs(concrete_type, method_stubs, property_stubs)
[2022-05-29T19:56:23.845Z]   File "/usr/local/lib/python3.7/dist-packages/torch/jit/_recursive.py", line 304, in create_methods_and_properties_from_stubs
[2022-05-29T19:56:23.845Z]     concrete_type._create_methods_and_properties(property_defs, property_rcbs, method_defs, method_rcbs, method_defaults)
[2022-05-29T19:56:23.846Z]   File "/usr/local/lib/python3.7/dist-packages/torch/jit/_recursive.py", line 680, in compile_unbound_method
[2022-05-29T19:56:23.846Z]     create_methods_and_properties_from_stubs(concrete_type, (stub,), ())
[2022-05-29T19:56:23.846Z]   File "/usr/local/lib/python3.7/dist-packages/torch/jit/_recursive.py", line 304, in create_methods_and_properties_from_stubs
[2022-05-29T19:56:23.846Z]     concrete_type._create_methods_and_properties(property_defs, property_rcbs, method_defs, method_rcbs, method_defaults)
[2022-05-29T19:56:23.846Z] RuntimeError: 
[2022-05-29T19:56:23.846Z] 
[2022-05-29T19:56:23.846Z] aten::meshgrid(Tensor[] tensors) -> (Tensor[]):
[2022-05-29T19:56:23.846Z] Keyword argument indexing unknown.
[2022-05-29T19:56:23.846Z] :
[2022-05-29T19:56:23.846Z]   File "/home/jenkins/agent/workspace/MONAI-premerge/monai/monai/apps/detection/utils/anchor_utils.py", line 256
[2022-05-29T19:56:23.846Z]             if self.spatial_dims == 2:
[2022-05-29T19:56:23.846Z]                 shifts_centers = list(
[2022-05-29T19:56:23.846Z]                     torch.meshgrid([shifts_centers[0], shifts_centers[1]], indexing="ij")
[2022-05-29T19:56:23.846Z]                     ~~~~~~~~~~~~~~ <--- HERE
[2022-05-29T19:56:23.846Z]                 )  # indexing="ij"
[2022-05-29T19:56:23.846Z]             else:
[2022-05-29T19:56:23.846Z] 'AnchorGenerator.grid_anchors' is being compiled since it was called from 'AnchorGenerator.forward'
[2022-05-29T19:56:23.846Z]   File "/home/jenkins/agent/workspace/MONAI-premerge/monai/monai/apps/detection/utils/anchor_utils.py", line 318
[2022-05-29T19:56:23.846Z]         # Code below come from torchvision.models.detection.anchor_utils.AnchorGenerator.forward()
[2022-05-29T19:56:23.846Z]         self.set_cell_anchors(dtype, device)
[2022-05-29T19:56:23.846Z]         anchors_over_all_feature_maps = self.grid_anchors(grid_sizes, strides)
[2022-05-29T19:56:23.846Z]         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
[2022-05-29T19:56:23.846Z] 
[2022-05-29T19:56:23.846Z]         anchors: List[List[torch.Tensor]] = []
[2022-05-29T19:56:23.846Z] 
[2022-05-29T19:56:23.846Z] 
[2022-05-29T19:56:23.846Z] ======================================================================
[2022-05-29T19:56:23.846Z] ERROR: test_script_3d_0 (tests.test_anchor_box.TestAnchorGenerator)
[2022-05-29T19:56:23.846Z] ----------------------------------------------------------------------
[2022-05-29T19:56:23.846Z] Traceback (most recent call last):
[2022-05-29T19:56:23.846Z]   File "/usr/local/lib/python3.7/dist-packages/parameterized/parameterized.py", line 533, in standalone_func
[2022-05-29T19:56:23.846Z]     return func(*(a + p.args), **p.kwargs)
[2022-05-29T19:56:23.846Z]   File "/home/jenkins/agent/workspace/MONAI-premerge/monai/tests/test_anchor_box.py", line 84, in test_script_3d
[2022-05-29T19:56:23.846Z]     test_script_save(anchor, images, feature_maps)
[2022-05-29T19:56:23.846Z]   File "/home/jenkins/agent/workspace/MONAI-premerge/monai/tests/utils.py", line 682, in test_script_save
[2022-05-29T19:56:23.846Z]     atol=atol,
[2022-05-29T19:56:23.846Z]   File "/home/jenkins/agent/workspace/MONAI-premerge/monai/monai/networks/utils.py", line 539, in convert_to_torchscript
[2022-05-29T19:56:23.846Z]     script_module = torch.jit.script(model, **kwargs)
[2022-05-29T19:56:23.846Z]   File "/usr/local/lib/python3.7/dist-packages/torch/jit/_script.py", line 898, in script
[2022-05-29T19:56:23.846Z]     obj, torch.jit._recursive.infer_methods_to_compile
[2022-05-29T19:56:23.846Z]   File "/usr/local/lib/python3.7/dist-packages/torch/jit/_recursive.py", line 352, in create_script_module
[2022-05-29T19:56:23.846Z]     return create_script_module_impl(nn_module, concrete_type, stubs_fn)
[2022-05-29T19:56:23.846Z]   File "/usr/local/lib/python3.7/dist-packages/torch/jit/_recursive.py", line 410, in create_script_module_impl
[2022-05-29T19:56:23.846Z]     create_methods_and_properties_from_stubs(concrete_type, method_stubs, property_stubs)
[2022-05-29T19:56:23.846Z]   File "/usr/local/lib/python3.7/dist-packages/torch/jit/_recursive.py", line 304, in create_methods_and_properties_from_stubs
[2022-05-29T19:56:23.846Z]     concrete_type._create_methods_and_properties(property_defs, property_rcbs, method_defs, method_rcbs, method_defaults)
[2022-05-29T19:56:23.846Z]   File "/usr/local/lib/python3.7/dist-packages/torch/jit/_recursive.py", line 680, in compile_unbound_method
[2022-05-29T19:56:23.846Z]     create_methods_and_properties_from_stubs(concrete_type, (stub,), ())
[2022-05-29T19:56:23.846Z]   File "/usr/local/lib/python3.7/dist-packages/torch/jit/_recursive.py", line 304, in create_methods_and_properties_from_stubs
[2022-05-29T19:56:23.846Z]     concrete_type._create_methods_and_properties(property_defs, property_rcbs, method_defs, method_rcbs, method_defaults)
[2022-05-29T19:56:23.846Z] RuntimeError: 
[2022-05-29T19:56:23.846Z] 
[2022-05-29T19:56:23.846Z] aten::meshgrid(Tensor[] tensors) -> (Tensor[]):
[2022-05-29T19:56:23.846Z] Keyword argument indexing unknown.
[2022-05-29T19:56:23.846Z] :
[2022-05-29T19:56:23.846Z]   File "/home/jenkins/agent/workspace/MONAI-premerge/monai/monai/apps/detection/utils/anchor_utils.py", line 256
[2022-05-29T19:56:23.846Z]             if self.spatial_dims == 2:
[2022-05-29T19:56:23.846Z]                 shifts_centers = list(
[2022-05-29T19:56:23.846Z]                     torch.meshgrid([shifts_centers[0], shifts_centers[1]], indexing="ij")
[2022-05-29T19:56:23.846Z]                     ~~~~~~~~~~~~~~ <--- HERE
[2022-05-29T19:56:23.846Z]                 )  # indexing="ij"
[2022-05-29T19:56:23.846Z]             else:
[2022-05-29T19:56:23.846Z] 'AnchorGeneratorWithAnchorShape.grid_anchors' is being compiled since it was called from 'AnchorGeneratorWithAnchorShape.forward'
[2022-05-29T19:56:23.846Z]   File "/home/jenkins/agent/workspace/MONAI-premerge/monai/monai/apps/detection/utils/anchor_utils.py", line 318
[2022-05-29T19:56:23.846Z]         # Code below come from torchvision.models.detection.anchor_utils.AnchorGenerator.forward()
[2022-05-29T19:56:23.846Z]         self.set_cell_anchors(dtype, device)
[2022-05-29T19:56:23.846Z]         anchors_over_all_feature_maps = self.grid_anchors(grid_sizes, strides)
[2022-05-29T19:56:23.846Z]         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
[2022-05-29T19:56:23.846Z] 
[2022-05-29T19:56:23.846Z]         anchors: List[List[torch.Tensor]] = []
[2022-05-29T19:56:23.846Z] 
[2022-05-29T19:56:23.846Z] 
[2022-05-29T19:56:23.846Z] ----------------------------------------------------------------------

@wyli
Copy link
Copy Markdown
Contributor

wyli commented May 30, 2022

/build

Signed-off-by: Wenqi Li <[email protected]>
@wyli
Copy link
Copy Markdown
Contributor

wyli commented May 30, 2022

/build

wyli added 2 commits May 30, 2022 10:48
Signed-off-by: Wenqi Li <[email protected]>
Signed-off-by: Wenqi Li <[email protected]>
@wyli
Copy link
Copy Markdown
Contributor

wyli commented May 30, 2022

/build

Signed-off-by: Wenqi Li <[email protected]>
@wyli
Copy link
Copy Markdown
Contributor

wyli commented May 30, 2022

/build

Signed-off-by: Wenqi Li <[email protected]>
@wyli
Copy link
Copy Markdown
Contributor

wyli commented May 30, 2022

/build

Copy link
Copy Markdown
Collaborator

@mibaumgartner mibaumgartner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some small notes, the rest looks good to me.

@wyli wyli disabled auto-merge May 30, 2022 14:34
@wyli
Copy link
Copy Markdown
Contributor

wyli commented May 30, 2022

/build

@wyli wyli merged commit d635339 into Project-MONAI:dev May 30, 2022
@Can-Zhao Can-Zhao deleted the anchor branch May 30, 2022 20:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anchor generator

4 participants