Fix #421: Update UNETR to support proj_type in newer MONAI versions#429
Fix #421: Update UNETR to support proj_type in newer MONAI versions#429aman0311x wants to merge 2 commits into
Conversation
….0 compatibility
WalkthroughUNETR now exposes ChangesUNETR compatibility updates
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
UNETR/BTCV/networks/unetr.py (1)
211-215: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick winCheckpoint bias key is missing
patch_embedding.prefix — inconsistent with weight key.Line 212 reads the weight from
module.transformer.patch_embedding.patch_embeddings.1.weight, but line 215 reads the bias frommodule.transformer.patch_embeddings.1.bias. The bias key is missing thepatch_embedding.segment, making it inconsistent with the weight key. Since both tensors belong to the samepatch_embeddings[1]module in the checkpoint, the bias key should follow the same path prefix.🐛 Proposed fix: restore the `patch_embedding.` prefix
self.vit.patch_embedding.patch_embeddings[1].bias.copy_( - weights["state_dict"]["module.transformer.patch_embeddings.1.bias"] + weights["state_dict"]["module.transformer.patch_embedding.patch_embeddings.1.bias"] )🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@UNETR/BTCV/networks/unetr.py` around lines 211 - 215, Update the bias checkpoint lookup in the weight-loading block to use the same module path as the corresponding weight lookup, including the patch_embedding segment before patch_embeddings.1.bias. Keep the target module self.vit.patch_embedding.patch_embeddings[1] unchanged.
🧹 Nitpick comments (1)
UNETR/BTCV/networks/unetr.py (1)
102-102: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueMove
import inspectto module top.The
inspectimport inside__init__runs on every instantiation. Move it to the top of the file with the other imports.♻️ Proposed refactor
+import inspect + # ... other top-level imports ... # MONAI >= 1.3.0 backward compatibility handle - import inspect vit_params = inspect.signature(ViT.__init__).parameters🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@UNETR/BTCV/networks/unetr.py` at line 102, Move the inspect import from the __init__ method to the module-level import section in unetr.py, alongside the other imports, and remove the local import while leaving the existing inspect usage unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@UNETR/BTCV/networks/unetr.py`:
- Line 37: Update the UNETR constructor around the proj_type parameter to accept
the legacy pos_embed keyword as an alias while retaining the new proj_type API.
Resolve both inputs consistently, with the explicitly provided compatibility
value taking effect when appropriate, and pass the resolved projection type
through the existing initialization path so callers in main.py and test.py
remain valid.
---
Outside diff comments:
In `@UNETR/BTCV/networks/unetr.py`:
- Around line 211-215: Update the bias checkpoint lookup in the weight-loading
block to use the same module path as the corresponding weight lookup, including
the patch_embedding segment before patch_embeddings.1.bias. Keep the target
module self.vit.patch_embedding.patch_embeddings[1] unchanged.
---
Nitpick comments:
In `@UNETR/BTCV/networks/unetr.py`:
- Line 102: Move the inspect import from the __init__ method to the module-level
import section in unetr.py, alongside the other imports, and remove the local
import while leaving the existing inspect usage unchanged.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 987ebf72-a466-419c-b597-3a3cf445510a
📒 Files selected for processing (1)
UNETR/BTCV/networks/unetr.py
Description
This PR fixes issue #421 where the UNETR model crashes on MONAI >= 1.3.0 due to the deprecation of the pos_embed parameter in the ViT class, which was renamed to proj_type.
Changes Made
Updated the parameter name from pos_embed to proj_type in UNETR.init arguments, docstrings, and examples.
Added a backward compatibility check using Python's inspect module to dynamically pass either proj_type or pos_embed based on the installed MONAI version.
Fixes #421
Summary by CodeRabbit
Bug Fixes
Documentation