Fix F.pad axis swap in pad_to_training_size#8
Open
sidd462 wants to merge 1 commit into
Open
Conversation
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.
Closes #7.
Summary
pad_to_training_size()inrsuper_train/predict_abdomenatlas.pyhad twoF.pad(...)calls whose padding tuples were ordered for the wrong axis. The z-axis branch was padding W, and the x-axis branch was padding D. When an input volume was too small on the z- or x-axis, the originally-too-small axis stayed too small, and the next layer rejected the shape — but the postprocess block above swallowed the exception with a bare-except, so the only signal was a single line ofFAILED postprocesswith no context. 25 of 901 PanTS-te cases were silently lost this way.This PR makes two changes in one file:
Why the original code was wrong
torch.nn.functional.padreads its padding tuple last-dim-first. For a 5D tensor of shape(N, C, D, H, W)the tuple has to be ordered:So the tuple controls W first, then H, then D — not the other way round.
In the original code:
z < args.training_size[0]) intended to pad D, but passed(diff, diff, 0,0, 0,0). By the last-dim-first rule that pads W (and leaves D untouched).x < args.training_size[2]) intended to pad W, but passed(0,0, 0,0, diff, diff). By the same rule, that pads D.So the two branches were each padding the other axis's branch's target. The originally-too-small axis was never enlarged, and the next layer threw on a shape mismatch.
The fix (1/2) — swap the tuples between the branches
After the swap:
(0,0, 0,0, diff, diff)→ pads D (the z axis). Correct.(diff, diff, 0,0, 0,0)→ pads W (the x axis). Correct.Each branch now pads the axis named in its own guard. The other axes get
(0, 0)so they're untouched.The fix (2/2) — surface postprocess failures instead of swallowing them
The bug was easy to miss for one reason: the postprocess block above the padding code had
except:(catches everything) and printed onlyFAILED postprocess. No error type, no traceback, no case ID. That's why an axis-order bug that triggers on ~3% of cases shipped unnoticed.Why this is bundled with the F.pad fix and not a separate PR: the bare-except is what hid this bug. Replacing it with something diagnosable closes the same root cause from a different angle — if another shape-related bug crops up in
pad_to_training_size(or anywhere else in the postprocess pipeline), it'll surface immediately instead of silently dropping cases. I'd rather land both together than ship the F.pad fix and leave the silent-swallowing scaffolding in place.If the maintainers prefer the bare-except upgrade to be a separate PR, happy to split it out.
Diff stats
1 file changed, 6 insertions(+), 4 deletions(-), all inrsuper_train/predict_abdomenatlas.py. 3 hunks total: 2 single-line F.pad swaps + the except-block expansion. No other files changed, no dependencies added.Verification
Tested with the R-Super checkpoint on PanTS-te (n=901):
FAILED postprocess. No prediction outputs were written for those cases, so a downstream evaluator that globs forpredictions/*.nii.gzsimply never saw them — exactly the loss-by-silence the bare-except was producing.predictions/<class>.nii.gztree.< 128on the z- or x-axis — i.e. they hit exactly the broken branch.python -m py_compile rsuper_train/predict_abdomenatlas.pypasses.