Reshape per-channel ImageType.scale to broadcast over channel-first input (fixes #2461)#2709
Open
LeSingh1 wants to merge 1 commit into
Open
Reshape per-channel ImageType.scale to broadcast over channel-first input (fixes #2461)#2709LeSingh1 wants to merge 1 commit into
LeSingh1 wants to merge 1 commit into
Conversation
`ImageType.scale` is documented as "float or list of floats", but when a
user supplied a list (e.g. `[1/127.5, 1/127.5, 1/127.5]` for RGB) the
preprocessing pass dropped it through `np.array(...)` and emitted an
`mb.mul` whose scale tensor still had shape `(3,)`. That then tried to
broadcast against the channel-first input `(N, 3, H, W)` and failed in
`backend_mlprogram` with:
ValueError: Incompatible dim 3 in shapes (1, 3, 224, 224) vs. (1, 1, 1, 3)
The neighbouring per-channel `bias` path already reshapes to `(3, 1, 1)`
for rank-3 inputs and `(1, 3, 1, 1)` for rank-4 inputs; scale should do
the same. Scalar scales and grayscale layouts keep the existing shape so
no other paths change.
Adds two regression tests (rank-3 and rank-4) covering the `(3,) →
(3,1,1)` / `(1,3,1,1)` reshape on RGB images. Both tests pass; existing
`TestImagePreprocessingPass` cases continue to pass.
Fixes apple#2461
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
ImageType.scaleis documented as…matching
bias. But when a user actually supplied a per-channel list (e.g.[1/127.5, 1/127.5, 1/127.5]for an RGB MobileNetV2 input), themil_backend::insert_image_preprocessing_opspass dropped it throughnp.array(...)and emitted anmb.mulwhose scale tensor still had shape(3,). That tensor then tried to broadcast against the channel-first input(N, 3, H, W)and failed inbackend_mlprogramwith:Fix
The neighbouring per-channel
biaspath already reshapes the constant to(3, 1, 1)for rank-3 inputs and(1, 3, 1, 1)for rank-4 inputs. Apply the same reshape toscale:Scalar scales (
scale_arr.ndim == 0) and grayscale layouts skip the reshape — no other code paths change.Tests
Two new regression tests under
TestImagePreprocessingPass:test_program_rgb_per_channel_scale— rank-4 RGB input withscale=[1/127.5]*3, bias=[-1, -1, -1](the exact pattern from ImageType causes an error in TensorFlow 2 model conversion with a scale parameter provided as list #2461); asserts the insertedmul.yhas shape(1, 3, 1, 1)and values match the input list.test_program_rgb_per_channel_scale_rank3— rank-3 sibling; asserts the(3, 1, 1)reshape path mirroring the rank-3 bias case.Both pass locally. The full
TestImagePreprocessingPasssuite (14 tests including the existing scalar-scale and per-channel-bias cases) also continues to pass:Issue
Fixes #2461