Make loss weight masking conditional on loss function support#184
Merged
Make loss weight masking conditional on loss function support#184
Conversation
The deep supervision handler was unconditionally passing weight=loss_weight_mask to all loss functions, but only custom losses (WeightedMSELoss, WeightedMAELoss, SmoothL1Loss) accept this parameter. Standard losses like MONAI's DiceLoss and PyTorch's BCEWithLogitsLoss do not, causing a TypeError during validation. Add _loss_supports_weight() helper that uses inspect.signature to check if a loss function's forward method accepts a 'weight' parameter, and conditionally pass it only when supported. https://claude.ai/code/session_01XRs3k4q3869VksCMeHsMTN
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR refactors the deep supervision loss computation to conditionally apply per-voxel weight masks only to loss functions that explicitly support them, rather than unconditionally passing weight masks to all loss functions.
Key Changes
_loss_supports_weight()utility function that uses introspection to check if a loss function'sforwardmethod accepts aweightparametercompute_multitask_loss()compute_loss_for_scale()compute_standard_loss()Implementation Details
_loss_supports_weight()function usesinspect.signature()to safely check the loss function's forward method signatureFalseas a fallbackhttps://claude.ai/code/session_01XRs3k4q3869VksCMeHsMTN