RoIAlign.pytorch icon indicating copy to clipboard operation
RoIAlign.pytorch copied to clipboard

CropAndResize seems to have wrong outputs

Open littlespray opened this issue 4 years ago • 0 comments

Hi,

I am a pytorch beginner. Recently I am trying to implement the ROIAlign in PyTorch with CropAndResize and torch.nn.functional.max_pool2d . But the output is different from torchvision.ops.RoIAlign.

code:


import torch
import torch.nn.functional as F
from torchvision.ops import RoIAlign
from my_roi_align import CropAndResize # I rename the folder as I encountered issures#32


output_size = (3,3)
spatial_scale = 1/4 
sampling_ratio = 2 

x = torch.randn(1, 1, 6, 6)
rois = torch.tensor([[0,1.0,6.6,6.7,10.1]])

x1, y1 = rois[:,1::4] * spatial_scale, rois[:,2::4] * spatial_scale
x2, y2 = rois[:,3::4] * spatial_scale, rois[:,4::4] * spatial_scale
H, W = x.shape[2], x.shape[3]

ya = RoIAlign(output_size, spatial_scale=spatial_scale, sampling_ratio=sampling_ratio)(x, rois)
yb = CropAndResize(sampling_ratio*output_size[1], sampling_ratio*output_size[0])(x, torch.cat([y1/(H-1), x1/(W-1), y2/(H-1), x2/(W-1)], 1), rois[:, 0].int())
yb = F.avg_pool2d(yb, sampling_ratio)

print('ya:\n', ya)
print('yb:\n', yb)
print('IsEqual: ', yb.equal(ya)) 

one case of output:


ya:
 tensor([[[[-0.9476,  0.1967,  0.0017],
          [-0.9198,  0.2392, -0.0529],
          [-0.3397,  0.1514, -0.0426]]]])
yb:
 tensor([[[[-1.0746,  0.1233, -0.0497],
          [-1.3218,  0.2025, -0.1869],
          [-0.5540,  0.1371, -0.1202]]]])
IsEqual:  False

I am not sure whether I have some misunderstanding of RoIAlign or there is some problem in using CropAndResize. I could not appreciate it more if anyone offers help.

littlespray avatar Feb 07 '20 08:02 littlespray