def check_pooling_zero_offset(): input = torch.randn(2, 16, 64, 64).cuda().zero_() input[0, :, 16:26, 16:26] = 1. input[1, :, 10:20, 20:30] = 2. rois = torch.tensor([ [0, 65, 65, 103, 103], [1, 81, 41, 119, 79], ]).cuda().float() pooling = DCNv2Pooling(spatial_scale=1.0 / 4, pooled_size=7, output_dim=16, no_trans=True, group_size=1, trans_std=0.0).cuda() out = pooling(input, rois, input.new()) s = ', '.join( ['%f' % out[i, :, :, :].mean().item() for i in range(rois.shape[0])]) print(s) dpooling = DCNv2Pooling(spatial_scale=1.0 / 4, pooled_size=7, output_dim=16, no_trans=False, group_size=1, trans_std=0.0).cuda() offset = torch.randn(20, 2, 7, 7).cuda().zero_() dout = dpooling(input, rois, offset) s = ', '.join( ['%f' % dout[i, :, :, :].mean().item() for i in range(rois.shape[0])]) print(s)
def example_dpooling(): from dcn_v2 import DCNv2Pooling input = torch.randn(2, 32, 64, 64).cuda() batch_inds = torch.randint(2, (20, 1)).cuda().float() x = torch.randint(256, (20, 1)).cuda().float() y = torch.randint(256, (20, 1)).cuda().float() w = torch.randint(64, (20, 1)).cuda().float() h = torch.randint(64, (20, 1)).cuda().float() rois = torch.cat((batch_inds, x, y, x + w, y + h), dim=1) offset = torch.randn(20, 2, 7, 7).cuda() # normal roi_align pooling = DCNv2Pooling(spatial_scale=1.0 / 4, pooled_size=7, output_dim=32, no_trans=True, group_size=1, trans_std=0.1).cuda() # deformable pooling dpooling = DCNv2Pooling(spatial_scale=1.0 / 4, pooled_size=7, output_dim=32, no_trans=False, group_size=1, trans_std=0.1).cuda() out = pooling(input, rois, offset) dout = dpooling(input, rois, offset) print(out.shape) print(dout.shape)
def example_dpooling(): input = torch.randn(2, 32, 64, 64).cuda() batch_inds = torch.randint(2, (20, 1)).cuda().float() x = torch.randint(256, (20, 1)).cuda().float() y = torch.randint(256, (20, 1)).cuda().float() w = torch.randint(64, (20, 1)).cuda().float() h = torch.randint(64, (20, 1)).cuda().float() rois = torch.cat((batch_inds, x, y, x + w, y + h), dim=1) offset = torch.randn(20, 2, 7, 7).cuda() input.requires_grad = True offset.requires_grad = True # normal roi_align pooling = DCNv2Pooling( spatial_scale=1.0 / 4, pooled_size=7, output_dim=32, no_trans=True, group_size=1, trans_std=0.1 ).cuda() # deformable pooling dpooling = DCNv2Pooling( spatial_scale=1.0 / 4, pooled_size=7, output_dim=32, no_trans=False, group_size=1, trans_std=0.1 ).cuda() out = pooling(input, rois, offset) dout = dpooling(input, rois, offset) print(out.shape) print(dout.shape) target_out = out.new(*out.size()) target_out.data.uniform_(-0.01, 0.01) target_dout = dout.new(*dout.size()) target_dout.data.uniform_(-0.01, 0.01) e = (target_out - out).mean() e.backward() e = (target_dout - dout).mean() e.backward()
def check_gradient_dpooling(): input = torch.randn(2, 3, 5, 5).cuda() * 0.01 N = 4 batch_inds = torch.randint(2, (N, 1)).cuda().float() x = torch.rand((N, 1)).cuda().float() * 15 y = torch.rand((N, 1)).cuda().float() * 15 w = torch.rand((N, 1)).cuda().float() * 10 h = torch.rand((N, 1)).cuda().float() * 10 rois = torch.cat((batch_inds, x, y, x + w, y + h), dim=1) offset = torch.randn(N, 2, 3, 3).cuda() dpooling = DCNv2Pooling(spatial_scale=1.0 / 4, pooled_size=3, output_dim=3, no_trans=False, group_size=1, trans_std=0.0).cuda() input.requires_grad = True offset.requires_grad = True print('check_gradient_dpooling', gradcheck(dpooling, (input, rois, offset), eps=1e-4))