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

Fixes incoherence in affine transformation with center is defined as … #2468

Merged
merged 1 commit into from
Jul 15, 2020

Conversation

vfdev-5
Copy link
Collaborator

@vfdev-5 vfdev-5 commented Jul 13, 2020

Description:

  • Fixes incoherence in the affine transformation when the center is defined as half image size + 0.5
  • Incoherence is when affine transformation is 90 degrees rotation and output contains a zero line or row

For example:
Currently:

import torchvision
from torchvision.transforms.functional import affine
print(torchvision.__version__)

import numpy as np
from PIL import Image


img = np.arange(0, 3 * 12 * 12, dtype="uint8").reshape((3, 12, 12))
pil_img = Image.fromarray(img.transpose((1, 2, 0)))
out_pil_img = affine(pil_img, -90, (0, 0), scale=1.0, shear=(0, 0))
np.asarray(out_pil_img)[:, :, 0]
0.6.1+cu101
array([[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [ 11,  23,  35,  47,  59,  71,  83,  95, 107, 119, 131, 143],
       [ 10,  22,  34,  46,  58,  70,  82,  94, 106, 118, 130, 142],
       [  9,  21,  33,  45,  57,  69,  81,  93, 105, 117, 129, 141],
       [  8,  20,  32,  44,  56,  68,  80,  92, 104, 116, 128, 140],
       [  7,  19,  31,  43,  55,  67,  79,  91, 103, 115, 127, 139],
       [  6,  18,  30,  42,  54,  66,  78,  90, 102, 114, 126, 138],
       [  5,  17,  29,  41,  53,  65,  77,  89, 101, 113, 125, 137],
       [  4,  16,  28,  40,  52,  64,  76,  88, 100, 112, 124, 136],
       [  3,  15,  27,  39,  51,  63,  75,  87,  99, 111, 123, 135],
       [  2,  14,  26,  38,  50,  62,  74,  86,  98, 110, 122, 134],
       [  1,  13,  25,  37,  49,  61,  73,  85,  97, 109, 121, 133]],
      dtype=uint8)

and with the fix it gives

0.8.0a0+9126078
array([[ 11,  23,  35,  47,  59,  71,  83,  95, 107, 119, 131, 143],
       [ 10,  22,  34,  46,  58,  70,  82,  94, 106, 118, 130, 142],
       [  9,  21,  33,  45,  57,  69,  81,  93, 105, 117, 129, 141],
       [  8,  20,  32,  44,  56,  68,  80,  92, 104, 116, 128, 140],
       [  7,  19,  31,  43,  55,  67,  79,  91, 103, 115, 127, 139],
       [  6,  18,  30,  42,  54,  66,  78,  90, 102, 114, 126, 138],
       [  5,  17,  29,  41,  53,  65,  77,  89, 101, 113, 125, 137],
       [  4,  16,  28,  40,  52,  64,  76,  88, 100, 112, 124, 136],
       [  3,  15,  27,  39,  51,  63,  75,  87,  99, 111, 123, 135],
       [  2,  14,  26,  38,  50,  62,  74,  86,  98, 110, 122, 134],
       [  1,  13,  25,  37,  49,  61,  73,  85,  97, 109, 121, 133],
       [  0,  12,  24,  36,  48,  60,  72,  84,  96, 108, 120, 132]],
      dtype=uint8)

…half image size + 0.5

Incoherence is when affine transformation is 90 degrees rotation and output contains a zero line
@vfdev-5 vfdev-5 mentioned this pull request Jul 15, 2020
16 tasks
@@ -1408,7 +1408,7 @@ def _test_transformation(a, t, s, sh):
# Test rotation, scale, translation, shear
for a in range(-90, 90, 25):
for t1 in range(-10, 10, 5):
for s in [0.75, 0.98, 1.0, 1.1, 1.2]:
for s in [0.75, 0.98, 1.0, 1.2, 1.4]:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Just replaced corner case value scale=1.1 when a=90

@vfdev-5 vfdev-5 requested a review from fmassa July 15, 2020 14:37
Copy link
Member

@fmassa fmassa 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!

@fmassa fmassa merged commit a568c7f into pytorch:master Jul 15, 2020
@vfdev-5 vfdev-5 deleted the fix-affine-center branch July 15, 2020 15:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants