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

Fixed reparametrization for shear X/Y in autoaugment ops #5384

Merged
merged 6 commits into from
Feb 8, 2022

Conversation

vfdev-5
Copy link
Collaborator

@vfdev-5 vfdev-5 commented Feb 7, 2022

Description:

Context

In this PR we are fixing parametrization bug in affine transformation matrix that we have with shear ops when applied to autoaugmentations. In torchvision we are constructing affine transformation matrix for shear X as [1, tan(sx), 0, 0, 1, 0]

b = -math.cos(rot - sy) * math.tan(sx) / math.cos(sy) - math.sin(rot)
c = math.sin(rot - sy) / math.cos(sy)
d = -math.sin(rot - sy) * math.tan(sx) / math.cos(sy) + math.cos(rot)
# Inverted rotation matrix with scale and shear
# det([[a, b], [c, d]]) == 1, since det(rotation) = 1 and det(shear) = 1
matrix = [d, -b, 0.0, -c, a, 0.0]

and official autoaugment implementations are using [1, sx, 0, 0, 1, 0], for example:

https://github.com/tensorflow/models/blob/dd02069717128186b88afa8d857ce57d17957f03/research/autoaugment/augmentation_transforms.py#L290

The difference between two is very small in range of [-0.3, 0.3]:

import torch

print([(v.tan() - v).item() for v in torch.linspace(-0.3, 0.3, 10)])
> [-0.009336262941360474, -0.004328876733779907, -0.0015605539083480835, -0.00033467262983322144, -1.2349337339401245e-05, 1.2349337339401245e-05, 0.00033467262983322144, 0.0015605539083480835, 0.004328876733779907, 0.009336262941360474]

image

In details, 1) autoaugment policy provides shear magnitude in radians, 2) as F.affine works on shear in degrees and applies math.tan(math.radians(shear_degree)) we have to modify f(magnitude) in a form such that we have the equality:

math.tan(math.radians( f(magnitude) )) == magnitude
# let's take f as math.degrees(math.atan( . ))
= math.tan(math.radians( math.degrees(math.atan(magnitude)) ))
= math.tan( math.atan(magnitude) )
= magnitude

I do not expect a major impact of that when training with autoaugment.
Right now with the fix we'll have exactly the same results on Pillow images.

What was done

  1. Fixed shear X/Y reparametrization mismatch between torchvision and one of the official autoaugment implementations. (In TF there are two ways of implementing autoaugmentations: a) using PIL as for CIFAR10 and b) using TF/keras image ops for ImageNet. The difference is in padding. For imagenet reflect padding is used. See Added center as top-left for shear X/Y ops for autoaugment #5285)

  2. Added a test to check torchvision's implementation vs ref implementations using PIL

We are testing 4 cases:

  • PIL image input, nearest interpolation mode => exact match between PIL Image output by torchvision and expected PIL Image
  • PIL image input, bilinear interpolation mode => exact match
  • Tensor image, nearest interpolation mode => exact match
  • Tensor image, bilinear interpolation mode => almost match
    • bilinear modes produce large non-zero pixel diffs for a certain amount of pixels, others are zeros.
    • we wont do anything for this case as it can be acceptable to have a small difference between between PIL and torch.tensor backends.

@facebook-github-bot
Copy link

facebook-github-bot commented Feb 7, 2022

💊 CI failures summary and remediations

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


None of the CI failures appear to be your fault 💚



🚧 1 ongoing upstream failure:

These were probably caused by upstream breakages that are not fixed yet.


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.

@vfdev-5 vfdev-5 requested a review from datumbox February 7, 2022 17:09
@vfdev-5 vfdev-5 force-pushed the added-ref-shear-test branch from 695605e to 55e77b4 Compare February 7, 2022 17:11
@vfdev-5 vfdev-5 changed the title Added reference tests for shear X/Y in autoaugment ops Fixed reparametrization for shear X/Y in autoaugment ops Feb 8, 2022
@vfdev-5 vfdev-5 requested a review from datumbox February 8, 2022 11:36
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.

LGTM, with a minor nit.

I assume you will merge after fixing the issue surfaced on the tests on a separate PR. Right?

@vfdev-5 vfdev-5 force-pushed the added-ref-shear-test branch from f15d38d to fc3f95f Compare February 8, 2022 15:40
@vfdev-5 vfdev-5 merged commit 74a1efc into pytorch:main Feb 8, 2022
@vfdev-5 vfdev-5 deleted the added-ref-shear-test branch February 8, 2022 16:56
facebook-github-bot pushed a commit that referenced this pull request Feb 11, 2022
)

Summary:
* Added ref tests for shear X/Y

* Added PIL tests and fixed tan(level) difference

* Updated tests

* Fixed reparam for shear X/Y in autoaugment

* Fixed arc_level -> level as atan is applied internally

* Fixed links

Reviewed By: NicolasHug

Differential Revision: D34140249

fbshipit-source-id: e7d984977599bbd71e57f403c022620315b052a1
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.

3 participants