def test_roi_align_aligned(self): x = torch.rand(1, 1, 10, 10, dtype=torch.float32) single_roi = torch.tensor([[0, 1.5, 1.5, 3, 3]], dtype=torch.float32) model = ops.RoIAlign((5, 5), 1, 2, aligned=True) self.run_model(model, [(x, single_roi)]) x = torch.rand(1, 1, 10, 10, dtype=torch.float32) single_roi = torch.tensor([[0, 0.2, 0.3, 4.5, 3.5]], dtype=torch.float32) model = ops.RoIAlign((5, 5), 0.5, 3, aligned=True) self.run_model(model, [(x, single_roi)]) x = torch.rand(1, 1, 10, 10, dtype=torch.float32) single_roi = torch.tensor([[0, 0.2, 0.3, 4.5, 3.5]], dtype=torch.float32) model = ops.RoIAlign((5, 5), 1.8, 2, aligned=True) self.run_model(model, [(x, single_roi)]) x = torch.rand(1, 1, 10, 10, dtype=torch.float32) single_roi = torch.tensor([[0, 0.2, 0.3, 4.5, 3.5]], dtype=torch.float32) model = ops.RoIAlign((2, 2), 2.5, 0, aligned=True) self.run_model(model, [(x, single_roi)]) x = torch.rand(1, 1, 10, 10, dtype=torch.float32) single_roi = torch.tensor([[0, 0.2, 0.3, 4.5, 3.5]], dtype=torch.float32) model = ops.RoIAlign((2, 2), 2.5, -1, aligned=True) self.run_model(model, [(x, single_roi)])
def test_roi_align(self): x = torch.rand(1, 1, 10, 10, dtype=torch.float32) single_roi = torch.tensor([[0, 0, 0, 4, 4]], dtype=torch.float32) model = ops.RoIAlign((5, 5), 1, 2) self.run_model(model, [(x, single_roi)]) x = torch.rand(1, 1, 10, 10, dtype=torch.float32) single_roi = torch.tensor([[0, 0, 0, 4, 4]], dtype=torch.float32) model = ops.RoIAlign((5, 5), 1, -1) self.run_model(model, [(x, single_roi)])
def test_roi_align_gradcheck_cuda(self): dtype = torch.float64 device = torch.device('cuda') m = ops.RoIAlign((5, 5), 0.5, 1).to(dtype=dtype, device=device) x = torch.rand(1, 1, 10, 10, dtype=dtype, device=device, requires_grad=True) rois = self.rois.to(device=device, dtype=dtype) def func(input): return m(input, rois) assert gradcheck(func, (x, )), 'gradcheck failed for RoIAlign CUDA' assert gradcheck( func, (x.transpose(2, 3), )), 'gradcheck failed for RoIAlign CUDA' @torch.jit.script def script_func(input, rois): return ops.roi_align(input, rois, 5, 0.5, 1)[0] assert gradcheck( lambda x: script_func(x, rois), (x, )), 'gradcheck failed for scripted roi_align on CUDA'
def fn(self, x, rois, pool_h, pool_w, spatial_scale=1, sampling_ratio=-1, **kwargs): return ops.RoIAlign((pool_h, pool_w), spatial_scale=spatial_scale, sampling_ratio=sampling_ratio)(x, rois)
def test_roi_align_gradcheck_cpu(self): dtype = torch.float64 device = torch.device('cpu') m = ops.RoIAlign((5, 5), 0.5, 1).to(dtype=dtype, device=device) x = torch.rand(1, 1, 10, 10, dtype=dtype, device=device, requires_grad=True) rois = self.rois.to(device=device, dtype=dtype) def func(input): return m(input, rois) assert gradcheck(func, (x,)), 'gradcheck failed for RoIAlign CPU' assert gradcheck(func, (x.transpose(2, 3),)), 'gradcheck failed for RoIAlign CPU'
def test_roi_align_cuda(self): device = torch.device('cuda') x = self.x.to(device) rois = self.rois.to(device) gt_y_multiple = self.gt_y_multiple.to(device) pool_h, pool_w = (5, 5) roi_align = ops.RoIAlign((pool_h, pool_w), spatial_scale=1, sampling_ratio=2).to(device=device) y = roi_align(x, rois) assert torch.allclose(gt_y_multiple, y), 'RoIAlign layer incorrect for multiple ROIs on CUDA' y = roi_align(x.transpose(2, 3).contiguous().transpose(2, 3), rois) assert torch.allclose(gt_y_multiple, y), 'RoIAlign layer incorrect for multiple ROIs on CUDA'
def test_roi_align_basic_cuda(self): device = torch.device('cuda') x = self.x.to(device) single_roi = self.single_roi.to(device) gt_y_single = self.gt_y_single.to(device) pool_h, pool_w = (5, 5) roi_align = ops.RoIAlign((pool_h, pool_w), spatial_scale=1, sampling_ratio=2).to(device=device) y = roi_align(x, single_roi) assert torch.allclose(gt_y_single, y), 'RoIAlign layer incorrect for single ROI on CUDA' y = roi_align(x.transpose(2, 3).contiguous().transpose(2, 3), single_roi) assert torch.allclose(gt_y_single, y), 'RoIAlign layer incorrect for single ROI on CUDA'
def test_roi_align_gradient_cuda(self): """ Compute gradients for RoIAlign with multiple bounding boxes on the GPU """ device = torch.device('cuda') pool_h, pool_w = (5, 5) roi_align = ops.RoIAlign((pool_h, pool_w), spatial_scale=1, sampling_ratio=2).to(device=device) x = self.x.to(device).clone() rois = self.rois.to(device) gt_grad = self.x_grad.to(device) x.requires_grad = True y = roi_align(x, rois) s = y.sum() s.backward() assert torch.allclose(x.grad, gt_grad), 'gradient incorrect for RoIAlign CUDA'
def __init__(self, name, proj_head_kwargs, scrl_kwargs, trainable=True): super(Backbone, self).__init__() assert name in ['resnet50', 'resnet101'], 'only supports resnet50 and resnet101 for now.' self.scrl_enabled = scrl_kwargs.enabled self.trainable = trainable # encoder network = eval(f"models.{name}")() self.encoder = torch.nn.Sequential(*list(network.children())[:-1]) # RoI pooling layer for SCRL if self.trainable and self.scrl_enabled: roi_out_size = (scrl_kwargs.pool_size, ) * 2 self.roi_align = ops.RoIAlign(output_size=roi_out_size, sampling_ratio=scrl_kwargs.sampling_ratio, spatial_scale=scrl_kwargs.spatial_scale, aligned=scrl_kwargs.detectron_aligned) # projection head if self.trainable: self.projector = TwoLayerLinearHead(**proj_head_kwargs)
def __init__(self, pooler_size): super(Blender, self).__init__() self.pooler_size = pooler_size self.pooler = cv_ops.RoIAlign(output_size=pooler_size, spatial_scale=0.25, sampling_ratio=1)
def test_roi_align_malformed_boxes(self): x = torch.randn(1, 1, 10, 10, dtype=torch.float32) single_roi = torch.tensor([[0, 2, 0.3, 1.5, 1.5]], dtype=torch.float32) model = ops.RoIAlign((5, 5), 1, 1, aligned=True) self.run_model(model, [(x, single_roi)])