I write an example PyTorch code for wrapping a gray-scale image with a certain transformation.
import torch
import numpy as np
# Gray Scale Image
image = torch.tensor([[[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]]]
).unsqueeze(0).float()
# Define a simple grid with some shifts and rotations
grid_x, grid_y = torch.meshgrid(torch.arange(4), torch.arange(4))
grid_x = grid_x.float()
grid_y = grid_y.float()
new_locs = torch.stack([grid_x + 0.2 * torch.sin(grid_y), grid_y - 0.1 * torch.cos(grid_x)], dim=2).unsqueeze(0).float()
# Warp the image using grid_sample
import torch.nn.functional as F
warped_image = F.grid_sample(image, new_locs, align_corners=True, mode='nearest')
This code is written following the documentation of grid_sample package. But I am not able to properly understand what F.grid_sample actually do. My first confusion is, while I was debugging the code I noticed that the values inside the new_locs variable contain values between [-1,1]. This makes me confused because I am not able to understand how the pixel location of the new output can be negative. In the case of standard image processing, we normally represent the top left pixel location as (0,0), thus all the other pixel locations are positive, meaning there is no negative pixel in that case. Secondly, I am also confused regarding the purpose of interpolation mode, in the above case nearest interpolation. Why do we need interpolation in F.grid_sample? I would really appreciate if anyone can help me understanding the F.grid_sample module better.