Skip to content

Commit 75e02ba

Browse files
wylimingxin-zheng
andauthored
5712 update padding for torch 1.9 (#5718)
Signed-off-by: Wenqi Li <wenqil@nvidia.com> Fixes #5712 ### Description ```py >>> import torch >>> img_t = torch.zeros(1, 1, 24, 24, 24); out = torch.nn.functional.pad(img_t[None], (20, 20, 20, 20, 20, 20), mode='reflect') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/opt/conda/lib/python3.8/site-packages/torch/nn/functional.py", line 4189, in _pad raise NotImplementedError("Only 3D, 4D, 5D padding with non-constant padding are supported for now") NotImplementedError: Only 3D, 4D, 5D padding with non-constant padding are supported for now ``` ### 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`. - [x] 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: Wenqi Li <wenqil@nvidia.com> Co-authored-by: Mingxin Zheng <mingxinz@nvidia.com>
1 parent a604c15 commit 75e02ba

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

monai/transforms/croppad/array.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,14 @@ def __call__( # type: ignore
183183
)
184184
out = _pad(img_t, pad_width=to_pad_, mode=mode_, **kwargs_)
185185
except (ValueError, TypeError, RuntimeError) as err:
186-
if "supported" in str(err) or "unexpected keyword" in str(err) or "implemented" in str(err):
186+
if isinstance(err, NotImplementedError) or any(
187+
k in str(err) for k in ("supported", "unexpected keyword", "implemented")
188+
):
187189
out = self._np_pad(img_t, pad_width=to_pad_, mode=mode_, **kwargs_)
188190
else:
189-
raise ValueError(f"{mode_}, {kwargs_}, {img_t.dtype}, {img_t.device}") from err
191+
raise ValueError(
192+
f"{img_t.shape} {to_pad_} {mode_} {kwargs_} {img_t.dtype} {img_t.device}"
193+
) from err
190194
else:
191195
out = img_t
192196
if get_track_meta():

0 commit comments

Comments
 (0)