Skip to content

Commit

Permalink
Improve sliding-window inference (#6009)
Browse files Browse the repository at this point in the history
### Description
Improve sliding-window inference with enhanced GPU memory efficiency.

### 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: dongy <[email protected]>
  • Loading branch information
dongyang0122 authored Feb 16, 2023
1 parent 94e9e17 commit 11745a6
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions monai/inferers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,14 @@ def sliding_window_inference(

# account for any overlapping sections
for ss in range(len(output_image_list)):
output_image_list[ss] = (output_image_list[ss] / count_map_list.pop(0)).to(compute_dtype)
output_image_list[ss] = output_image_list[ss].detach()
_map = count_map_list.pop(0)
for _i in range(output_image_list[ss].shape[1]):
output_image_list[ss][:, _i : _i + 1, ...] /= _map
output_image_list[ss] = output_image_list[ss].to(compute_dtype)

# remove padding if image_size smaller than roi_size
for ss, output_i in enumerate(output_image_list):
if torch.isnan(output_i).any() or torch.isinf(output_i).any():
warnings.warn("Sliding window inference results contain NaN or Inf.")

zoom_scale = [
seg_prob_map_shape_d / roi_size_d for seg_prob_map_shape_d, roi_size_d in zip(output_i.shape[2:], roi_size)
]
Expand Down

0 comments on commit 11745a6

Please sign in to comment.