Skip to content

Commit f965d49

Browse files
wylinsrivathsa
authored andcommitted
1965 - register_backward_hook (Project-MONAI#1966)
* fixes Project-MONAI#1965 Signed-off-by: Wenqi Li <[email protected]> * adds docstring Signed-off-by: Wenqi Li <[email protected]> Signed-off-by: Neha Srivathsa <[email protected]>
1 parent b1703b2 commit f965d49

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

monai/handlers/parameter_scheduler.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
# Copyright 2020 - 2021 MONAI Consortium
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
# Unless required by applicable law or agreed to in writing, software
7+
# distributed under the License is distributed on an "AS IS" BASIS,
8+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
# See the License for the specific language governing permissions and
10+
# limitations under the License.
11+
112
import logging
213
from bisect import bisect_right
314
from typing import TYPE_CHECKING, Callable, Dict, List, Optional, Union

monai/networks/blocks/crf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2020 MONAI Consortium
1+
# Copyright 2020 - 2021 MONAI Consortium
22
# Licensed under the Apache License, Version 2.0 (the "License");
33
# you may not use this file except in compliance with the License.
44
# You may obtain a copy of the License at

monai/optimizers/lr_scheduler.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
# Copyright 2020 - 2021 MONAI Consortium
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
# Unless required by applicable law or agreed to in writing, software
7+
# distributed under the License is distributed on an "AS IS" BASIS,
8+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
# See the License for the specific language governing permissions and
10+
# limitations under the License.
11+
112
from torch.optim import Optimizer
213
from torch.optim.lr_scheduler import _LRScheduler
314

monai/visualize/class_activation_maps.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import torch.nn.functional as F
1919

2020
from monai.transforms import ScaleIntensity
21-
from monai.utils import ensure_tuple
21+
from monai.utils import ensure_tuple, get_torch_version_tuple
2222
from monai.visualize.visualizer import default_upsampler
2323

2424
__all__ = ["CAM", "GradCAM", "GradCAMpp", "ModelWithHooks", "default_normalizer"]
@@ -73,7 +73,13 @@ def __init__(
7373
continue
7474
_registered.append(name)
7575
if self.register_backward:
76-
mod.register_backward_hook(self.backward_hook(name))
76+
if get_torch_version_tuple() < (1, 8):
77+
mod.register_backward_hook(self.backward_hook(name))
78+
else:
79+
if "inplace" in mod.__dict__ and mod.__dict__["inplace"]:
80+
# inplace=True causes errors for register_full_backward_hook
81+
mod.__dict__["inplace"] = False
82+
mod.register_full_backward_hook(self.backward_hook(name))
7783
if self.register_forward:
7884
mod.register_forward_hook(self.forward_hook(name))
7985
if len(_registered) != len(self.target_layers):

0 commit comments

Comments
 (0)