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() input.requires_grad = True offset.requires_grad = True spatial_scale = 1.0 / 4 pooled_size = 3 output_dim = 3 no_trans = 0 group_size = 1 trans_std = 0.0 sample_per_part = 4 part_size = pooled_size print( 'check_gradient_dpooling:', gradcheck( _DeformRoIPooling, (input, rois, offset, spatial_scale, pooled_size, output_dim, no_trans, group_size, part_size, sample_per_part, trans_std), eps=1e-4))
def check_gradient_dconv(): stride = 1 padding = 1 groups = 2 dilation = 1 im2col_step = 1 input = torch.rand(N, inC, inH, inW).double().cuda() print('max input:', input.max()) input.requires_grad = True offset = torch.randn(N, deformable_groups * 2 * kW * kH, inH, inW).double().cuda() * 2 # offset.data.zero_() # offset.data -= 0.5 offset.requires_grad = True weight = torch.randn(outC, int(inC // groups), kH, kW).double().cuda() weight.requires_grad = True bias = torch.rand(outC).double().cuda() bias.requires_grad = True # print('check_gradient_dconv: ', # gradcheck(_DeformConv, (input, offset, weight, bias, # stride, padding, dilation, groups, deformable_groups, im2col_step), # eps=1e-3, atol=1e-3, rtol=1e-2, raise_exception=True)) print( 'check_gradient_dconv: ', gradcheck(_DeformConv2d, (input, offset, weight, bias, stride, padding, dilation, groups, deformable_groups, im2col_step)))
def check_gradient_conv3d(): input = torch.rand(N, inC, inD, inH, inW).double().cuda() * 0.01 input.requires_grad = True from torch.nn.functional import conv3d weight = torch.randn(outC, inC, kD, kH, kW).double().cuda() weight.requires_grad = True bias = torch.rand(outC).double().cuda() bias.requires_grad = True stride = 1 padding = 1 dilation = 1 # print('check_gradient_conv: ', # gradcheck(conv2d, (input, weight, bias, # stride, padding, dilation, deformable_groups), # eps=1e-3, atol=1e-2, rtol=1e-2, raise_exception=True)) print('check_gradient_conv: ', gradcheck(conv3d, (input, weight, bias, stride, padding, dilation)))
def check_gradient_mdconv(): stride = 1 padding = 1 groups = 2 dilation = 1 im2col_step = 1 input = torch.rand(N, inC, inH, inW).cuda() * 0.01 input.requires_grad = True offset = torch.randn(N, deformable_groups * 2 * kW * kH, inH, inW).cuda() * 2 # offset.data.zero_() # offset.data -= 0.5 offset.requires_grad = True mask = torch.rand(N, deformable_groups * 1 * kW * kH, inH, inW).cuda() # mask.data.zero_() mask.requires_grad = True mask = torch.sigmoid(mask) weight = torch.randn(outC, int(inC // groups), kH, kW).cuda() weight.requires_grad = True bias = torch.rand(outC).cuda() bias.requires_grad = True print( 'check_gradient_mdconv: ', gradcheck(_ModulatedDeformConv2d, (input, offset, mask, weight, bias, stride, padding, dilation, groups, deformable_groups, im2col_step), eps=1e-3, atol=1e-3, rtol=1e-2, raise_exception=True))