question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

torch 1.6 unit test test_keep_largest_connected_component

See original GitHub issue

Describe the bug

12:01:15  ======================================================================
12:01:15  FAIL: test_correct_results_28_onehot_independent_batch_2_apply_label_1_2_connect_2 (tests.test_keep_largest_connected_component.TestKeepLargestConnectedComponent)
12:01:15  ----------------------------------------------------------------------
12:01:15  Traceback (most recent call last):
12:01:15    File "/usr/local/lib/python3.8/dist-packages/parameterized/parameterized.py", line 533, in standalone_func
12:01:15      return func(*(a + p.args), **p.kwargs)
12:01:15    File "/home/jenkins/agent/workspace/Monai-pytorch-versions/tests/test_keep_largest_connected_component.py", line 365, in test_correct_results
12:01:15      assert_allclose(result.argmax(0)[None], result2)
12:01:15    File "/home/jenkins/agent/workspace/Monai-pytorch-versions/tests/utils.py", line 107, in assert_allclose
12:01:15      np.testing.assert_allclose(actual, desired, *args, **kwargs)
12:01:15    File "/usr/local/lib/python3.8/dist-packages/numpy/testing/_private/utils.py", line 1530, in assert_allclose
12:01:15      assert_array_compare(compare, actual, desired, err_msg=str(err_msg),
12:01:15    File "/usr/local/lib/python3.8/dist-packages/numpy/testing/_private/utils.py", line 844, in assert_array_compare
12:01:15      raise AssertionError(msg)
12:01:15  AssertionError: 
12:01:15  Not equal to tolerance rtol=1e-07, atol=0
12:01:15  
12:01:15  Mismatched elements: 1 / 25 (4%)
12:01:15  Max absolute difference: 2
12:01:15  Max relative difference: 0.
12:01:15   x: array([[[0, 0, 1, 0, 0],
12:01:15          [0, 2, 1, 1, 1],
12:01:15          [2, 2, 1, 0, 0],...
12:01:15   y: array([[[0, 0, 1, 0, 0],
12:01:15          [0, 2, 1, 1, 1],
12:01:15          [2, 2, 1, 0, 0],...
12:01:15  ======================================================================
12:01:15  FAIL: test_correct_results_28_onehot_independent_batch_2_apply_label_1_2_connect_2 (tests.test_keep_largest_connected_component.TestKeepLargestConnectedComponent)
12:01:15  ----------------------------------------------------------------------
12:01:15  Traceback (most recent call last):
12:01:15    File "/usr/local/lib/python3.8/dist-packages/parameterized/parameterized.py", line 533, in standalone_func
12:01:15      return func(*(a + p.args), **p.kwargs)
12:01:15    File "/home/jenkins/agent/workspace/Monai-pytorch-versions/tests/test_keep_largest_connected_component.py", line 365, in test_correct_results
12:01:15      assert_allclose(result.argmax(0)[None], result2)
12:01:15    File "/home/jenkins/agent/workspace/Monai-pytorch-versions/tests/utils.py", line 107, in assert_allclose
12:01:15      np.testing.assert_allclose(actual, desired, *args, **kwargs)
12:01:15    File "/usr/local/lib/python3.8/dist-packages/numpy/testing/_private/utils.py", line 1530, in assert_allclose
12:01:15      assert_array_compare(compare, actual, desired, err_msg=str(err_msg),
12:01:15    File "/usr/local/lib/python3.8/dist-packages/numpy/testing/_private/utils.py", line 844, in assert_array_compare
12:01:15      raise AssertionError(msg)
12:01:15  AssertionError: 
12:01:15  Not equal to tolerance rtol=1e-07, atol=0
12:01:15  
12:01:15  Mismatched elements: 1 / 25 (4%)
12:01:15  Max absolute difference: 2
12:01:15  Max relative difference: 0.
12:01:15   x: array([[[0, 0, 1, 0, 0],
12:01:15          [0, 2, 1, 1, 1],
12:01:15          [2, 2, 1, 0, 0],...
12:01:15   y: array([[[0, 0, 1, 0, 0],
12:01:15          [0, 2, 1, 1, 1],
12:01:15          [2, 2, 1, 0, 0],...
12:01:15  
12:01:15  ======================================================================
12:01:15  FAIL: test_correct_results_29_onehot_dependent_batch_2_apply_label_1_2_connect_2 (tests.test_keep_largest_connected_component.TestKeepLargestConnectedComponent)
12:01:15  ----------------------------------------------------------------------
12:01:15  Traceback (most recent call last):
12:01:15    File "/usr/local/lib/python3.8/dist-packages/parameterized/parameterized.py", line 533, in standalone_func
12:01:15      return func(*(a + p.args), **p.kwargs)
12:01:15    File "/home/jenkins/agent/workspace/Monai-pytorch-versions/tests/test_keep_largest_connected_component.py", line 365, in test_correct_results
12:01:15      assert_allclose(result.argmax(0)[None], result2)
12:01:15    File "/home/jenkins/agent/workspace/Monai-pytorch-versions/tests/utils.py", line 107, in assert_allclose
12:01:15      np.testing.assert_allclose(actual, desired, *args, **kwargs)
12:01:15    File "/usr/local/lib/python3.8/dist-packages/numpy/testing/_private/utils.py", line 1530, in assert_allclose
12:01:15      assert_array_compare(compare, actual, desired, err_msg=str(err_msg),
12:01:15    File "/usr/local/lib/python3.8/dist-packages/numpy/testing/_private/utils.py", line 844, in assert_array_compare
12:01:15      raise AssertionError(msg)
12:01:15  AssertionError: 
12:01:15  Not equal to tolerance rtol=1e-07, atol=0
12:01:15  
12:01:15  Mismatched elements: 2 / 25 (8%)
12:01:15  Max absolute difference: 2
12:01:15  Max relative difference: 0.
12:01:15   x: array([[[0, 0, 0, 0, 1],
12:01:15          [0, 0, 1, 1, 1],
12:01:15          [2, 0, 1, 1, 2],...
12:01:15   y: array([[[0, 0, 0, 0, 1],
12:01:15          [0, 0, 1, 1, 1],
12:01:15          [0, 0, 1, 1, 2],...
12:01:15  
12:01:15  ======================================================================
12:01:15  FAIL: test_correct_results_30_onehot_none_dependent_batch_2_apply_label_1_2_connect_1 (tests.test_keep_largest_connected_component.TestKeepLargestConnectedComponent)
12:01:15  ----------------------------------------------------------------------
12:01:15  Traceback (most recent call last):
12:01:15    File "/usr/local/lib/python3.8/dist-packages/parameterized/parameterized.py", line 533, in standalone_func
12:01:15      return func(*(a + p.args), **p.kwargs)
12:01:15    File "/home/jenkins/agent/workspace/Monai-pytorch-versions/tests/test_keep_largest_connected_component.py", line 365, in test_correct_results
12:01:15      assert_allclose(result.argmax(0)[None], result2)
12:01:15    File "/home/jenkins/agent/workspace/Monai-pytorch-versions/tests/utils.py", line 107, in assert_allclose
12:01:15      np.testing.assert_allclose(actual, desired, *args, **kwargs)
12:01:15    File "/usr/local/lib/python3.8/dist-packages/numpy/testing/_private/utils.py", line 1530, in assert_allclose
12:01:15      assert_array_compare(compare, actual, desired, err_msg=str(err_msg),
12:01:15    File "/usr/local/lib/python3.8/dist-packages/numpy/testing/_private/utils.py", line 844, in assert_array_compare
12:01:15      raise AssertionError(msg)
12:01:15  AssertionError: 
12:01:15  Not equal to tolerance rtol=1e-07, atol=0
12:01:15  
12:01:15  Mismatched elements: 2 / 25 (8%)
12:01:15  Max absolute difference: 2
12:01:15  Max relative difference: 0.
12:01:15   x: array([[[0, 0, 0, 0, 1],
12:01:15          [0, 0, 1, 1, 1],
12:01:15          [2, 0, 1, 1, 2],...
12:01:15   y: array([[[0, 0, 0, 0, 1],
12:01:15          [0, 0, 1, 1, 1],
12:01:15          [0, 0, 1, 1, 2],...
12:01:15  
12:01:15  ======================================================================
12:01:15  FAIL: test_correct_results_44_onehot_independent_batch_2_apply_label_1_2_connect_2 (tests.test_keep_largest_connected_component.TestKeepLargestConnectedComponent)
12:01:15  ----------------------------------------------------------------------
12:01:15  Traceback (most recent call last):
12:01:15    File "/usr/local/lib/python3.8/dist-packages/parameterized/parameterized.py", line 533, in standalone_func
12:01:15      return func(*(a + p.args), **p.kwargs)
12:01:15    File "/home/jenkins/agent/workspace/Monai-pytorch-versions/tests/test_keep_largest_connected_component.py", line 365, in test_correct_results
12:01:15      assert_allclose(result.argmax(0)[None], result2)
12:01:15    File "/home/jenkins/agent/workspace/Monai-pytorch-versions/tests/utils.py", line 107, in assert_allclose
12:01:15      np.testing.assert_allclose(actual, desired, *args, **kwargs)
12:01:15    File "/usr/local/lib/python3.8/dist-packages/numpy/testing/_private/utils.py", line 1530, in assert_allclose
12:01:15      assert_array_compare(compare, actual, desired, err_msg=str(err_msg),
12:01:15    File "/usr/local/lib/python3.8/dist-packages/numpy/testing/_private/utils.py", line 844, in assert_array_compare
12:01:15      raise AssertionError(msg)
12:01:15  AssertionError: 
12:01:15  Not equal to tolerance rtol=1e-07, atol=0
12:01:15  
12:01:15  Mismatched elements: 1 / 25 (4%)
12:01:15  Max absolute difference: 2
12:01:15  Max relative difference: 0.
12:01:15   x: array([[[0, 0, 1, 0, 0],
12:01:15          [0, 2, 1, 1, 1],
12:01:15          [2, 2, 1, 0, 0],...
12:01:15   y: array([[[0, 0, 1, 0, 0],
12:01:15          [0, 2, 1, 1, 1],
12:01:15          [2, 2, 1, 0, 0],...
12:01:15  
12:01:15  ======================================================================
12:01:15  FAIL: test_correct_results_45_onehot_dependent_batch_2_apply_label_1_2_connect_2 (tests.test_keep_largest_connected_component.TestKeepLargestConnectedComponent)
12:01:15  ----------------------------------------------------------------------
12:01:15  Traceback (most recent call last):
12:01:15    File "/usr/local/lib/python3.8/dist-packages/parameterized/parameterized.py", line 533, in standalone_func
12:01:15      return func(*(a + p.args), **p.kwargs)
12:01:15    File "/home/jenkins/agent/workspace/Monai-pytorch-versions/tests/test_keep_largest_connected_component.py", line 365, in test_correct_results
12:01:15      assert_allclose(result.argmax(0)[None], result2)
12:01:15    File "/home/jenkins/agent/workspace/Monai-pytorch-versions/tests/utils.py", line 107, in assert_allclose
12:01:15      np.testing.assert_allclose(actual, desired, *args, **kwargs)
12:01:15    File "/usr/local/lib/python3.8/dist-packages/numpy/testing/_private/utils.py", line 1530, in assert_allclose
12:01:15      assert_array_compare(compare, actual, desired, err_msg=str(err_msg),
12:01:15    File "/usr/local/lib/python3.8/dist-packages/numpy/testing/_private/utils.py", line 844, in assert_array_compare
12:01:15      raise AssertionError(msg)
12:01:15  AssertionError: 
12:01:15  Not equal to tolerance rtol=1e-07, atol=0
12:01:15  
12:01:15  Mismatched elements: 2 / 25 (8%)
12:01:15  Max absolute difference: 2
12:01:15  Max relative difference: 0.
12:01:15   x: array([[[0, 0, 0, 0, 1],
12:01:15          [0, 0, 1, 1, 1],
12:01:15          [2, 0, 1, 1, 2],...
12:01:15   y: array([[[0, 0, 0, 0, 1],
12:01:15          [0, 0, 1, 1, 1],
12:01:15          [0, 0, 1, 1, 2],...
12:01:15  
12:01:15  ======================================================================
12:01:15  FAIL: test_correct_results_46_onehot_none_dependent_batch_2_apply_label_1_2_connect_1 (tests.test_keep_largest_connected_component.TestKeepLargestConnectedComponent)
12:01:15  ----------------------------------------------------------------------
12:01:15  Traceback (most recent call last):
12:01:15    File "/usr/local/lib/python3.8/dist-packages/parameterized/parameterized.py", line 533, in standalone_func
12:01:15      return func(*(a + p.args), **p.kwargs)
12:01:15    File "/home/jenkins/agent/workspace/Monai-pytorch-versions/tests/test_keep_largest_connected_component.py", line 365, in test_correct_results
12:01:15      assert_allclose(result.argmax(0)[None], result2)
12:01:15    File "/home/jenkins/agent/workspace/Monai-pytorch-versions/tests/utils.py", line 107, in assert_allclose
12:01:15      np.testing.assert_allclose(actual, desired, *args, **kwargs)
12:01:15    File "/usr/local/lib/python3.8/dist-packages/numpy/testing/_private/utils.py", line 1530, in assert_allclose
12:01:15      assert_array_compare(compare, actual, desired, err_msg=str(err_msg),
12:01:15    File "/usr/local/lib/python3.8/dist-packages/numpy/testing/_private/utils.py", line 844, in assert_array_compare
12:01:15      raise AssertionError(msg)
12:01:15  AssertionError: 
12:01:15  Not equal to tolerance rtol=1e-07, atol=0
12:01:15  
12:01:15  Mismatched elements: 2 / 25 (8%)
12:01:15  Max absolute difference: 2
12:01:15  Max relative difference: 0.
12:01:15   x: array([[[0, 0, 0, 0, 1],
12:01:15          [0, 0, 1, 1, 1],
12:01:15          [2, 0, 1, 1, 2],...
12:01:15   y: array([[[0, 0, 0, 0, 1],
12:01:15          [0, 0, 1, 1, 1],
12:01:15          [0, 0, 1, 1, 2],...
12:01:15  
12:01:15  ----------------------------------------------------------------------

Environment

Ensuring you use the relevant python executable, please paste the output of:

11:39:59  .================================
11:39:59  Printing MONAI config...
11:39:59  ================================
11:39:59  MONAI version: 0.8.1+141.g5c56b559
11:39:59  Numpy version: 1.22.2
11:39:59  Pytorch version: 1.6.0
11:39:59  MONAI flags: HAS_EXT = True, USE_COMPILED = True
11:39:59  MONAI rev id: 5c56b5599275fc9f65f5c867c5cf2c07f89bb3d8
11:39:59  
11:39:59  Optional dependencies:
11:39:59  Pytorch Ignite version: 0.4.8
11:39:59  Nibabel version: 3.2.2
11:39:59  scikit-image version: 0.19.2
11:39:59  Pillow version: 9.0.1
11:39:59  Tensorboard version: 2.8.0
11:39:59  gdown version: 4.4.0
11:39:59  TorchVision version: 0.7.0
11:39:59  tqdm version: 4.63.0
11:39:59  lmdb version: 1.3.0
11:39:59  psutil version: 5.9.0
11:39:59  pandas version: 1.4.1
11:39:59  einops version: 0.4.0
11:39:59  transformers version: 4.16.2
11:39:59  mlflow version: 1.24.0
11:39:59  
11:39:59  For details about installing the optional dependencies, please visit:
11:39:59      https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies

Additional context Add any other context about the problem here.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
yiheng-wang-nvcommented, Mar 7, 2022

Hi @wyli , I took some time and found the root cause. It is due to the changes of torch.argmax, where after 1.7, if there are multiple maximal values then the indices of the first maximal value are returned. For example:

a = torch.tensor([[0], [0], [0], [0]])
print(a.argmax(0))

will get tensor([0]). But for torch==1.6.0, the result is tensor([3]).

Therefore, the changes in this place may raise the issue.

Another issue is that the transform KeepLargestConnectedComponent will change the original input, thus we may need to copy it first in the test class.

Let me submit a PR to fix it.

0reactions
wylicommented, Mar 7, 2022

Yes, for the test cases…

Read more comments on GitHub >

github_iconTop Results From Across the Web

torch_geometric.transforms.largest_connected_components
Args: num_components (int, optional): Number of largest components to keep (default: :obj:`1`) connection (str, optional): Type of connection to use for ...
Read more >
A PyTorch implementation of Connected Components Labeling
Follwing Block-based Union Find Algorithm from YACCLAB. Running on GPU. PyTorch Interface; Fix some bit alignment problem; little refactoring. Example. Tested ...
Read more >
How to extract the largest connected component using ...
I am using OpenCV in Python to be able to identify only the Leaf presented on the image. I already be able to...
Read more >
PyTorch Geometric Signed Directed - arXiv
When unit tests are running a coverage report by CodeCov is generated. We maintain a high test coverage rate of almost 100%. 7...
Read more >
Largest connected component in loss function - PyTorch Forums
I would like to add a loss function that only takes into account the largest connected component of the output of my network...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found