Skip to content

fix: respect channel_axis in pad_to_size for channels-first images#112

Merged
copybara-service[bot] merged 2 commits into
google-deepmind:masterfrom
Nas01010101:fix/pad-to-size-channels-first
Jun 2, 2026
Merged

fix: respect channel_axis in pad_to_size for channels-first images#112
copybara-service[bot] merged 2 commits into
google-deepmind:masterfrom
Nas01010101:fix/pad-to-size-channels-first

Conversation

@Nas01010101
Copy link
Copy Markdown
Contributor

Summary

pad_to_size() ignores channel_axis when constructing the pad widths, so it pads the wrong axes for channels-first (CHW) images.

The padding amounts (top, bottom, left, right) are computed correctly via _get_dimension_values, which is channel-axis-aware. But the pad_width tuple passed to jnp.pad is hardcoded to channels-last order:

pad_width = ((top, bottom), (left, right), (0, 0))

For a CHW image (channel_axis=0) this applies the height/width padding to the channel and height axes and leaves width unpadded. Example — padding a (3, 4, 4) CHW image to a (6, 6) target yields (5, 6, 4) instead of the expected (3, 6, 6).

Every other spatial op in this module already branches on _channels_last(image, channel_axis); pad_to_size simply missed it.

Fix

Select the pad-width tuple based on layout:

if _channels_last(image, channel_axis):
    pad_width = ((top, bottom), (left, right), (0, 0))
else:
    pad_width = ((0, 0), (top, bottom), (left, right))

The batch-dimension prepend and all other logic are layout-agnostic and unchanged. Channels-last behaviour is identical to before.

Tests

Adds dm_pix/_src/pad_to_size_test.py covering HWC (unchanged), CHW shape, CHW values, and batched CHW. The CHW tests fail before the fix and pass after.

4 passed

Broader regression (interpolation_test.py + the new test): 100 passed, 24 skipped (skips are pre-existing TF-gated tests).

@claudiofantacci claudiofantacci self-assigned this May 29, 2026
@claudiofantacci claudiofantacci added the bug Something isn't working label May 29, 2026
Copy link
Copy Markdown
Collaborator

@claudiofantacci claudiofantacci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot! 🚀

Comment thread dm_pix/_src/pad_to_size_test.py Outdated
import numpy as np


class PadToSizeChannelsFirstTest(absltest.TestCase):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this test under dm_pix/_src/augment_test.py.

Comment thread dm_pix/_src/pad_to_size_test.py Outdated
def test_pad_to_size_chw_shape(self):
"""Channels-first (CHW) with channel_axis=0: output shape must be (C, H', W').

Bug: pad_to_size hardcodes pad_width as ((top,bot), (left,right), (0,0)),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this comment please.

Nas01010101 added a commit to Nas01010101/dm_pix that referenced this pull request Jun 1, 2026
Address review feedback on PR google-deepmind#112:
- Move the channels-first pad_to_size regression tests out of the
  standalone pad_to_size_test.py and into the existing TestCustom class
  in augment_test.py, matching that file's conventions.
- Drop the explanatory comments, consistent with the surrounding tests.

Delete dm_pix/_src/pad_to_size_test.py.
Copy link
Copy Markdown
Collaborator

@claudiofantacci claudiofantacci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! It will take a little while to go through and I actually need to drop support from some old Python version.

I'll approve once this is done and then go from there.

In the meantime, thanks again!

@claudiofantacci
Copy link
Copy Markdown
Collaborator

Waiting #113 to go through.

@Nas01010101
Copy link
Copy Markdown
Contributor Author

Sounds good, thanks!!

@claudiofantacci
Copy link
Copy Markdown
Collaborator

Can you please rebase on top of main after #113 and re-upload?

@Nas01010101
Copy link
Copy Markdown
Contributor Author

Of course !

pad_to_size hardcoded pad_width as ((top, bottom), (left, right), (0, 0))
which assumes channels-last (HWC) layout regardless of the channel_axis
argument. For channels-first (CHW) images, this causes the padding to be
applied along the channel dimension and leaves the width dimension unpadded,
producing a wrong output shape (e.g. (5, 6, 4) instead of (3, 6, 6)).

Fix: branch on _channels_last(image, channel_axis) and produce
((0, 0), (top, bottom), (left, right)) for CHW layout, consistent
with how every other function in this module handles channel_axis.

Add pad_to_size_test.py covering:
- HWC baseline (channel_axis=-1): shape is preserved correctly.
- CHW shape (channel_axis=0): must be (C, H', W') not (C+pad, H', W).
- CHW values (channel_axis=0): original pixels centered, border is zero.
- Batched CHW (channel_axis=1): must be (B, C, H', W').
Address review feedback on PR google-deepmind#112:
- Move the channels-first pad_to_size regression tests out of the
  standalone pad_to_size_test.py and into the existing TestCustom class
  in augment_test.py, matching that file's conventions.
- Drop the explanatory comments, consistent with the surrounding tests.

Delete dm_pix/_src/pad_to_size_test.py.
@Nas01010101 Nas01010101 force-pushed the fix/pad-to-size-channels-first branch from 6617907 to 5ab629f Compare June 1, 2026 17:07
@copybara-service copybara-service Bot merged commit d6a23f1 into google-deepmind:master Jun 2, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants