Skip to content

remove fill blending for bilinear affine #8098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions test/test_transforms_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,28 @@ def test_transform_unknown_fill_error(self):
with pytest.raises(TypeError, match="Got inappropriate fill arg"):
transforms.RandomAffine(degrees=0, fill="fill")

@pytest.mark.parametrize("dtype", [torch.uint8, torch.float32])
def test_bilinear_no_blend_artifacts(self, dtype):
# Regression test for https://github.com/pytorch/vision/issues/8083
value = 0.5
if not dtype.is_floating_point:
value = int(value * get_max_value(dtype))

input = torch.full((1, 200, 200), value, dtype=dtype)

output = F.affine(
input,
angle=30.0,
translate=(0, 0),
scale=1.0,
shear=(0, 0),
interpolation=F.InterpolationMode.BILINEAR,
fill=value,
)

# Since the fill color is the same as the input, affine should be a no-op
assert_equal(output, input)


class TestVerticalFlip:
@pytest.mark.parametrize("dtype", [torch.float32, torch.uint8])
Expand Down
9 changes: 2 additions & 7 deletions torchvision/transforms/v2/functional/_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,13 +585,8 @@ def _apply_grid_transform(img: torch.Tensor, grid: torch.Tensor, mode: str, fill
mask = mask.expand_as(float_img)
fill_list = fill if isinstance(fill, (tuple, list)) else [float(fill)] # type: ignore[arg-type]
fill_img = torch.tensor(fill_list, dtype=float_img.dtype, device=float_img.device).view(1, -1, 1, 1)
if mode == "nearest":
bool_mask = mask < 0.5
float_img[bool_mask] = fill_img.expand_as(float_img)[bool_mask]
else: # 'bilinear'
# The following is mathematically equivalent to:
# img * mask + (1.0 - mask) * fill = img * mask - fill * mask + fill = mask * (img - fill) + fill
float_img = float_img.sub_(fill_img).mul_(mask).add_(fill_img)
bool_mask = mask < 1
float_img[bool_mask] = fill_img.expand_as(float_img)[bool_mask]
Comment on lines +588 to +589
Copy link
Member

Choose a reason for hiding this comment

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

This is only equivalent to the 'bilinear' block if mask was a boolean mask, right?

Is that the case? And if it is, why was there even a distinction between bilinear and nearest mode in the first place?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is only equivalent to the 'bilinear' block if mask was a boolean mask, right?

Yes.

Is that the case?

No.

And if it is, why was there even a distinction between bilinear and nearest mode in the first place?

I would need to dig, but maybe @vfdev-5 knows? My guess is that the blend strategy we implemented was to have a smooth transition from fill to image. And that works well if we fill with zeros, but creates a shadow artifact if we use a different fill color with bilinear interpolation. See #8083.

Meaning, this PR is BC breaking, but I consider it a bug fix since the blending behavior seems to be "nice to have" while the shadow is an actual issue.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it was an incorrect implementation. At the moment of PR (#2904), I thought it could be ok as assumption to linearly interpolate around the boundaries: #2904 (comment)


img = float_img.round_().to(img.dtype) if not fp else float_img

Expand Down