Skip to content

Commit 7b9a523

Browse files
Bala93pre-commit-ci[bot]ericspodKumoLiu
authored
NACLLoss memory management (Project-MONAI#8020)
Fixes # . ### Description Calling contiguous after applying the permute option to work with view operation in apply_filter (https://github.com/Project-MONAI/MONAI/blob/59a7211070538586369afd4a01eca0a7fe2e742e/monai/networks/layers/simplelayers.py#L293). ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] 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. --------- Signed-off-by: Balamurali <balamuralim.1993@gmail.com> Signed-off-by: bala93 <balamuralim.1993@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com> Co-authored-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
1 parent 77304dd commit 7b9a523

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

monai/losses/nacl_loss.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ def get_constr_target(self, mask: torch.Tensor) -> torch.Tensor:
9595
rmask: torch.Tensor
9696

9797
if self.dim == 2:
98-
oh_labels = F.one_hot(mask.to(torch.int64), num_classes=self.nc).contiguous().permute(0, 3, 1, 2).float()
98+
oh_labels = F.one_hot(mask.to(torch.int64), num_classes=self.nc).permute(0, 3, 1, 2).contiguous().float()
9999
rmask = self.svls_layer(oh_labels)
100100

101101
if self.dim == 3:
102-
oh_labels = F.one_hot(mask.to(torch.int64), num_classes=self.nc).contiguous().permute(0, 4, 1, 2, 3).float()
102+
oh_labels = F.one_hot(mask.to(torch.int64), num_classes=self.nc).permute(0, 4, 1, 2, 3).contiguous().float()
103103
rmask = self.svls_layer(oh_labels)
104104

105105
return rmask

tests/test_nacl_loss.py

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
TEST_CASES = [
4949
[{"classes": 3, "dim": 2}, {"inputs": inputs, "targets": targets}, 1.1442],
50+
[{"classes": 3, "dim": 2}, {"inputs": inputs.repeat(4, 1, 1, 1), "targets": targets.repeat(4, 1, 1)}, 1.1442],
5051
[{"classes": 3, "dim": 2, "kernel_ops": "gaussian"}, {"inputs": inputs, "targets": targets}, 1.1433],
5152
[{"classes": 3, "dim": 2, "kernel_ops": "gaussian", "sigma": 0.5}, {"inputs": inputs, "targets": targets}, 1.1469],
5253
[{"classes": 3, "dim": 2, "distance_type": "l2"}, {"inputs": inputs, "targets": targets}, 1.1269],

0 commit comments

Comments
 (0)