Skip to content
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

Added center as top-left for shear X/Y ops for autoaugment #5285

Merged
merged 3 commits into from
Feb 2, 2022

Conversation

vfdev-5
Copy link
Collaborator

@vfdev-5 vfdev-5 commented Jan 26, 2022

Fixes #5204

Description:

  • Added center as top-left for shear X/Y ops for autoaugment

@datumbox do you think we should add something else here to be fully consistent ?

Here is how affine with center=(0, 0) behaves for shear X and Y vs using PIL as in TF:

from PIL import Image
import math
import torchvision # >= 0.11
import numpy as np

from torchvision.transforms import functional as F
interpolation = torchvision.transforms.InterpolationMode.NEAREST
fill = None

img = Image.fromarray(np.arange(3 * 128 * 128, dtype='uint8').reshape(128, 128, 3))
magnitude = .4

# shear_x as seen in torchvision https://github.com/pytorch/vision/blob/b5aa0915fe16e82ee4c24919032b4e7afae3ae1b/torchvision/transforms/autoaugment.py#L17
im_torch = F.affine(img, angle=0.0, translate=[0, 0], scale=1.0, shear=[math.degrees(magnitude), 0.0],                    
                    interpolation=interpolation, fill=fill, center=[0, 0])

# shear_x as seen in https://github.com/automl/trivialaugment/blob/3bfd06552336244b23b357b2c973859500328fbb/aug_lib.py#L156 and https://github.com/tensorflow/models/blob/fd34f711f319d8c6fe85110d9df6e1784cc5a6ca/research/autoaugment/augmentation_transforms.py#L290
im_pil = img.transform(img.size, Image.AFFINE, (1, magnitude, 0, 0, 1, 0))

print("Original image")
img.show()
print("Torchvision Shear X with center as top-left")
im_torch.show()
print("Shear X using PIL as TF")
im_pil.show()

# shear_x as seen in torchvision https://github.com/pytorch/vision/blob/b5aa0915fe16e82ee4c24919032b4e7afae3ae1b/torchvision/transforms/autoaugment.py#L17
im_torch = F.affine(img, angle=0.0, translate=[0, 0], scale=1.0, shear=[0.0, math.degrees(magnitude)],                    
                    interpolation=interpolation, fill=fill, center=[0, 0])

# shear_x as seen in https://github.com/automl/trivialaugment/blob/3bfd06552336244b23b357b2c973859500328fbb/aug_lib.py#L156 and https://github.com/tensorflow/models/blob/fd34f711f319d8c6fe85110d9df6e1784cc5a6ca/research/autoaugment/augmentation_transforms.py#L290
im_pil = img.transform(img.size, Image.AFFINE, (1, 0, 0, magnitude, 1, 0))


print("Torchvision Shear Y with center as top-left")
im_torch.show()
print("Shear Y using PIL as TF")
im_pil.show()

cc @SamuelGabriel

EDIT:

Links to TF implementation uses Pillow with shear parametrized such that fixed point is top-left:

However, official vision ops refers as well autoaugment paper and does shear with TF raw ops, the only difference is that they do fill_mode="reflect" by default:

Output for magnitude = .4 shear X with TF from official vision ops:
image

@facebook-github-bot
Copy link

facebook-github-bot commented Jan 26, 2022

💊 CI failures summary and remediations

As of commit 6412fb0 (more details on the Dr. CI page):


  • 3/3 failures introduced in this PR

3 failures not recognized by patterns:

Job Step Action
CircleCI cmake_macos_cpu curl -o conda.sh https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh
sh conda.sh -b
source $HOME/miniconda3/bin/activate
conda install -yq conda-build cmake
packaging/build_cmake.sh
🔁 rerun
CircleCI binary_linux_conda_py3.7_cu115 packaging/build_conda.sh 🔁 rerun
CircleCI binary_linux_conda_py3.7_cu111 packaging/build_conda.sh 🔁 rerun

This comment was automatically generated by Dr. CI (expand for details).

Please report bugs/suggestions to the (internal) Dr. CI Users group.

Click here to manually regenerate this comment.

Copy link
Contributor

@datumbox datumbox left a comment

Choose a reason for hiding this comment

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

@vfdev-5 Thanks a lot for the investigations and for sending the PR.

We had long discussions with Victor about this because it's a tough call but I'll summarize here the reasons why we believe we should merge this BC-breaking change. After looking into the original implementations of AutoAugment and RandAugment, it became clear that their Shear method was using the top-left as the center. Unfortunately TorchVision didn't support this option until it was raised by the author of TrivialAugment @SamuelGabriel at #5204. This means that our implementation deviated from the canonical and thus we consider this discrepancy a bug.

Though I don't believe that this discrepancy had major effect on the overall accuracy of the models that we trained previously, I think it's worth aligning TorchVision's behaviour with the rest of the ecosystem as the majority of frameworks and libraries we checked all implement the Shear operator with center the top-left corner.

I recommend leaving this PR unmerged for a couple of days to allow for people to comment. If there are no objections we can merge it early next week.

@datumbox datumbox merged commit 8eb9fb1 into pytorch:main Feb 2, 2022
@vfdev-5 vfdev-5 deleted the fix-shear-autoaugment branch February 2, 2022 12:00
facebook-github-bot pushed a commit that referenced this pull request Feb 11, 2022
…5285)

Summary:
Fixes #5204

Reviewed By: NicolasHug

Differential Revision: D34140243

fbshipit-source-id: 0b7c01b3479d5ef0eb9dfab64e317bb31eff0b31

Co-authored-by: Vasilis Vryniotis <datumbox@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

There is a Difference between Torchvsion Shear and PIL Shear
3 participants