question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

grid_sample() and align_corners parameter - new default value in pytorch v1.4.0

See original GitHub issue

It is said pytorch devs are going to change the default behavior of grid_sample() in pytorch v1.4.0: from align_corners=True to align_corners=False

Here is the link: https://pytorch.org/docs/stable/nn.functional.html#grid-sample

Снимок экрана 2019-12-22 в 2 39 28

What do you think about explicitly adding align_corners=True everywhere in grid_sample() so as not to ruin the current behavior of kornia (and your tests of course)?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
poxyucommented, Apr 30, 2020

@edgarriba I just don’t have time right now to write tests. But the main part is ready and used since that time…

def create_meshgrid(
        height: int,
        width: int,
        normalized_coordinates: Optional[bool] = True,
        align_corners: Optional[bool] = True,
        device: Optional[torch.device] = torch.device("cpu")) -> torch.Tensor:
    """Generates a coordinate grid for an image.
    When the flag ``normalized_coordinates`` is set to True, the grid is
    normalized to be in the range [-1, 1] to be consistent with the pytorch
    function grid_sample.
    http://pytorch.org/docs/master/nn.html#torch.nn.functional.grid_sample
    Args:
        height (int): the image height (rows).
        width (int): the image width (cols).
        normalized_coordinates (Optional[bool]): whether to normalize
          coordinates in the range [-1, 1] in order to be consistent with the
          PyTorch function grid_sample.
        align_corners (Optional[bool]): if set to ``True``, the extrema [-1, 1]
          are considered as referring to the center points of the input's corner pixels;
          if set to ``False``, they are instead considered as referring to the corner points
          of the input's corner pixels, making the sampling more resolution agnostic 
          (if ``normalized_coordinates`` is set to ``True``).
    Return:
        torch.Tensor: returns a grid tensor with shape :math:`(1, H, W, 2)`.
    """
    # generate coordinates
    xs: Optional[torch.Tensor] = None
    ys: Optional[torch.Tensor] = None
    if normalized_coordinates:
        # extrema [-1, 1]
        x_start, x_stop = -1., 1.
        y_start, y_stop = -1., 1.
        # calculate corner points
        if not align_corners:
            x_half_pixel_size = (x_stop - x_start) / float(width) / 2.
            y_half_pixel_size = (y_stop - y_start) / float(height) / 2.
            # update extrema
            x_start, x_stop = x_start + x_half_pixel_size, x_stop - x_half_pixel_size
            y_start, y_stop = y_start + y_half_pixel_size, y_stop - y_half_pixel_size
        # generate coordinates
        xs = torch.linspace(x_start, x_stop, width, device=device, dtype=torch.float)
        ys = torch.linspace(y_start, y_stop, height, device=device, dtype=torch.float)
    else:
        # generate coordinates
        xs = torch.linspace(0, width - 1, width, device=device, dtype=torch.float)
        ys = torch.linspace(0, height - 1, height, device=device, dtype=torch.float)
    # generate grid by stacking coordinates
    base_grid: torch.Tensor = torch.stack(
        torch.meshgrid([xs, ys])).transpose(1, 2)  # 2xHxW
    return torch.unsqueeze(base_grid, dim=0).permute(0, 2, 3, 1)  # 1xHxWx2
0reactions
stale[bot]commented, Oct 8, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions, and happy coding day 😎

Read more comments on GitHub >

github_iconTop Results From Across the Web

Suggest to add more tips in F.grid_sample() function - vision
Hi, I notice that in the newest version Pytorch, when query the coords in ... will assign the values of the padding part...
Read more >
Module — PyTorch 1.13 documentation
For example, BatchNorm's running_mean is not a parameter, but is part of the module's state. Buffers, by default, are persistent and will be...
Read more >
torch.nn.functional.affine_grid — PyTorch 1.13 documentation
A grid generated by affine_grid() should be passed to grid_sample() with the same setting for this option. Default: False. Returns:.
Read more >
Grid sample and align corners - PyTorch Forums
I want to understand this, as I prefer to leave default values as is in practice. Here's my example: image = torch.arange(0., 10.,...
Read more >
Using Torch-TensorRT in C++ - PyTorch
This new TorchScript file can be loaded into Python (note: you need to import torch_tensorrt before loading these compiled modules because the compiler...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found