-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Fixes F.affine and F.rotate to support rectangular tensor images #2553
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
Changes from 1 commit
36fef0d
2b98bdc
c7231bd
44da86e
d72cb3d
a249f77
a2c6dd1
407e9c4
4325f67
978c4c0
0e2a3c7
6f05c3e
781747c
c862126
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -619,6 +619,22 @@ def resize(img: Tensor, size: List[int], interpolation: int = 2) -> Tensor: | |
return img | ||
|
||
|
||
def _gen_affine_grid( | ||
theta: Tensor, w: int, h: int, ow: int, oh: int, | ||
) -> Tensor: | ||
d = 0.5 | ||
x = torch.arange(ow) + d - ow * 0.5 | ||
y = torch.arange(oh) + d - oh * 0.5 | ||
|
||
y, x = torch.meshgrid(y, x) | ||
pts = torch.stack([x, y, torch.ones_like(x)], dim=-1) | ||
output_grid = torch.matmul(pts, theta.t()) | ||
|
||
output_grid = output_grid / torch.tensor([0.5 * w, 0.5 * h]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is the principal difference to x = (torch.arange(ow) + d - ow * 0.5) / (0.5 * w)
y = (torch.arange(oh) + d - oh * 0.5) / (0.5 * h) instead of |
||
|
||
return output_grid.unsqueeze(dim=0) | ||
|
||
|
||
def affine( | ||
img: Tensor, matrix: List[float], resample: int = 0, fillcolor: Optional[int] = None | ||
) -> Tensor: | ||
|
@@ -651,7 +667,12 @@ def affine( | |
|
||
theta = torch.tensor(matrix, dtype=torch.float).reshape(1, 2, 3) | ||
shape = img.shape | ||
grid = affine_grid(theta, size=(1, shape[-3], shape[-2], shape[-1]), align_corners=False) | ||
if shape[-2] == shape[-1]: | ||
# here we need normalized translation part of theta | ||
grid = affine_grid(theta, size=(1, shape[-3], shape[-2], shape[-1]), align_corners=False) | ||
else: | ||
# here we need denormalized translation part of theta | ||
grid = _gen_affine_grid(theta[0, :, :], w=shape[-1], h=shape[-2], ow=shape[-1], oh=shape[-2]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you replace everything to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't benchmarked both methods to compare the performances, I assumed that About an issue in PyTorch, I think pytorch/pytorch#24870 and pytorch/pytorch#36107 already speak about absolute pixel coords. Probably, they are related to this problem. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean to just dispatch to |
||
|
||
# make image NCHW | ||
need_squeeze = False | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, I do not like that depending on if image is square or not matrice's translation part is normalized or not...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we just make everything follow the same code-path?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is that in case of square image we use
affine_grid
and it requires rescaled translation part, so we computematrix
here with rescaledtranslate_f
. In case of rectangular images, we use custom affine grid implementation_gen_affine_grid
where coords normalization is applied a posteriori and we need to deal withmatrix
where translation part is not normalized. Online normalization,denormalization ofmatrix
is not evident neither. That's why there are two pathes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't we just use
_gen_affine_grid
everywhere?