Add support for optional conditioning in PatchInferer, SliceInferer, and SlidingWindowInferer
Fixes #8220
Description
This PR adds support for optional conditioning in MONAI’s inferers, allowing models to receive auxiliary inputs for conditioning that are processed (patched, sliced) the same way as the inputs. This is particularly relevant for generative models like conditional GANs or DMs.
Example Usage:
# Given a conditioned model, inputs of shape (1, C, H, W, D) and condition of shape (1, C, H, W, D)
output = SliceInferer(...)(inputs, model, condition=cond_tensor)
Types of changes
- Extended
PatchInferer,SliceInferer, andSlidingWindowInfererto optionally accept aconditiontensor (passed as a kwarg). - The
conditioncan now be:None(default)- A tensor of the same shape as
inputs
- The inferers now slice/patch the conditions alongside the corresponding inputs and feed them to the network.
- Updated unit tests for each inferer:
- Verified with and without conditioning
- [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).
- [x] New tests added to cover the changes.
- [x] 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 htmlcommand in thedocs/folder.
Additional extensions such as support for dense vector conditioning (e.g., (1, C, Z), with Z being the conditional dimension) could be explored in a follow-up PR if there’s interest.
Thanks a lot for the suggestions! I just pushed the updated changes.
Walkthrough
The code introduces support for an optional condition tensor to the PatchInferer, SlidingWindowInferer, and SliceInferer classes, as well as the sliding_window_inference utility. This allows conditional inference by synchronously processing the condition tensor alongside inputs and passing it to the network. Comprehensive tests are added for this new conditional mechanism.
Changes
| File(s) | Change Summary |
|---|---|
| monai/inferers/inferer.py | Added optional condition argument to PatchInferer, SlidingWindowInferer, and SliceInferer methods; updated control flow to validate and propagate condition. |
| monai/inferers/utils.py | Updated sliding_window_inference to accept and process an optional condition tensor in sync with inputs. |
| tests/inferers/test_patch_inferer.py | Added new test class and cases to verify PatchInferer with condition argument. |
| tests/inferers/test_slice_inferer.py | Added new test class to verify SliceInferer with condition support. |
| tests/inferers/test_sliding_window_inference.py | Added new test class to verify sliding_window_inference and SlidingWindowInferer with condition. |
Sequence Diagram(s)
sequenceDiagram
participant User
participant Inferer (Patch/SlidingWindow/Slice)
participant Network
User->>Inferer: call(inputs, network, condition=cond)
Inferer->>Inferer: Validate shapes/types of inputs and condition
loop For each patch/slice/window
Inferer->>Network: network(input_patch, condition=cond_patch)
Network-->>Inferer: output_patch
end
Inferer-->>User: aggregated_output
Assessment against linked issues
| Objective | Addressed | Explanation |
|---|---|---|
| Add support for conditional-based models in SliceInferer (#8220) | ✅ | |
| Ensure condition tensor is processed slice-by-slice in sync with inputs (#8220) | ✅ | |
| Validate shape and type matching between condition and inputs for correct inference (#8220) | ✅ | |
| Add tests verifying SliceInferer and related inferers handle condition argument correctly (#8220) | ✅ |
Poem
A rabbit with code in its paws,
Adds "condition"—and earns applause!
Now slices and patches, in sync they go,
Through inferers, the tensors flow.
With tests that hop and outputs that gleam,
This conditional leap fulfills the dream!
🐇✨
✨ Finishing Touches
- [ ] 📝 Generate Docstrings
🪧 Tips
Chat
There are 3 ways to chat with CodeRabbit:
- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
I pushed a fix in commit <commit_id>, please review it.Explain this complex logic.Open a follow-up GitHub issue for this discussion.
- Files and specific lines of code (under the "Files changed" tab): Tag
@coderabbitaiin a new review comment at the desired location with your query. Examples:@coderabbitai explain this code block.@coderabbitai modularize this function.
- PR comments: Tag
@coderabbitaiin a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.@coderabbitai read src/utils.ts and explain its main purpose.@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.@coderabbitai help me debug CodeRabbit configuration file.
Support
Need help? Create a ticket on our support page for assistance with any issues or questions.
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.
CodeRabbit Commands (Invoked using PR comments)
@coderabbitai pauseto pause the reviews on a PR.@coderabbitai resumeto resume the paused reviews.@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository.@coderabbitai full reviewto do a full review from scratch and review all the files again.@coderabbitai summaryto regenerate the summary of the PR.@coderabbitai generate docstringsto generate docstrings for this PR.@coderabbitai generate sequence diagramto generate a sequence diagram of the changes in this PR.@coderabbitai resolveresolve all the CodeRabbit review comments.@coderabbitai configurationto show the current CodeRabbit configuration for the repository.@coderabbitai helpto get help.
Other keywords and placeholders
- Add
@coderabbitai ignoreanywhere in the PR description to prevent this PR from being reviewed. - Add
@coderabbitai summaryto generate the high-level summary at a specific location in the PR description. - Add
@coderabbitaianywhere in the PR title to generate the title automatically.
CodeRabbit Configuration File (.coderabbit.yaml)
- You can programmatically configure CodeRabbit by adding a
.coderabbit.yamlfile to the root of your repository. - Please see the configuration documentation for more information.
- If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation:
# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
Documentation and Community
- Visit our Documentation for detailed information on how to use CodeRabbit.
- Join our Discord Community to get help, request features, and share feedback.
- Follow us on X/Twitter for updates and announcements.
/build