8627 perceptual loss errors out after hitting the maximum number of downloads
Fixes #8627. Moves the perceptual loss code to MONAI repository https://github.com/Project-MONAI/perceptual-models and the checkpoints to Huggingface.
Description
This PR changes and simplifies the torch.hub loading process and gets the models from Huggingface lirbary. A few sentences describing the changes proposed in this pull request.
Types of changes
- [x] Non-breaking change (fix or new feature that would not break existing functionality).
- [x] Breaking change (fix or new feature that would cause existing functionality to change).
- [N/A] New tests added to cover the changes.
- [N/A] Integration tests passed locally by running
./runtests.sh -f -u --net --coverage. - [x] Quick tests passed locally by running
./runtests.sh --quick --unittests --disttests. - [x] In-line docstrings updated.
- [x] Documentation updated, tested
make htmlcommand in thedocs/folder.
Walkthrough
Perceptual loss backbones now load from Project-MONAI/perceptual-models via torch.hub. Added HF_MONAI_MODELS constant and renamed/expanded the enum to PerceptualNetworkType with new members (radimagenet_resnet50, medicalnet_resnet10_23datasets, medicalnet_resnet50_23datasets, resnet50). PerceptualLoss, MedicalNetPerceptualSimilarity, and RadImageNetPerceptualSimilarity accept a new cache_dir parameter and propagate it to constructors and torch.hub.load calls. MedicalNet models now enforce spatial_dims==3 and is_fake_3d==False; channel_wise is restricted to MedicalNet (non-MedicalNet with channel_wise raises). Model-name inputs are validated against HF_MONAI_MODELS. Axis/slicing bug in channel-wise feature differencing fixed (slice end index corrected). Docstrings and comments updated to reflect new hub sources and cache_dir behavior.
Estimated code review effort
๐ฏ 3 (Moderate) | โฑ๏ธ ~25 minutes
Areas needing extra attention:
- MedicalNet validation: enforcement of spatial_dims==3, is_fake_3d==False, and restriction of channel_wise to MedicalNet
- cache_dir propagation through PerceptualLoss โ MedicalNetPerceptualSimilarity / RadImageNetPerceptualSimilarity โ torch.hub.load
- torch.hub.load targets changed to Project-MONAI/perceptual-models and model-name validation against HF_MONAI_MODELS
- Channel-wise computation fix: corrected slice indexing for feats_diff
- Public API changes: new
cache_dirparameters and renamed enum PerceptualNetworkType (check imports and backward-compatibility)
Pre-merge checks and finishing touches
โ Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | โ ๏ธ Warning | Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. | You can run @coderabbitai generate docstrings to improve docstring coverage. |
โ Passed checks (4 passed)
| Check name | Status | Explanation |
|---|---|---|
| Title check | โ Passed | Title clearly identifies the bug fix (issue #8627) by referencing the specific problem with perceptual loss download limits. |
| Description check | โ Passed | Description links to the issue, explains the solution (moving to MONAI repo and HuggingFace), and documents the testing and documentation updates performed. |
| Linked Issues check | โ Passed | Changes fully address issue #8627 by migrating models from Google Drive to HuggingFace and updating the torch.hub loading process to prevent download quota failures. |
| Out of Scope Changes check | โ Passed | All changes in perceptual.py directly support the migration objective: new model sources, cache_dir propagation, validation enums, and strict MedicalNet handling are all within scope. |
โจ Finishing touches
- [ ] ๐ Generate docstrings
๐งช Generate unit tests (beta)
- [ ] Create PR with unit tests
- [ ] Post copyable unit tests in a comment
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 @coderabbitai help to get the list of available commands and usage tips.