2020-06-28 23:15:47 +08:00
|
|
|
import os
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
import torch
|
|
|
|
|
|
|
|
_USING_PARROTS = True
|
|
|
|
try:
|
|
|
|
from parrots.autograd import gradcheck
|
|
|
|
except ImportError:
|
|
|
|
from torch.autograd import gradcheck
|
|
|
|
_USING_PARROTS = False
|
|
|
|
|
|
|
|
cur_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
|
|
|
inputs = [([[[[1., 2.], [3., 4.]]]], [[0., 0., 0., 1., 1.]]),
|
|
|
|
([[[[1., 2.], [3., 4.]], [[4., 3.], [2.,
|
|
|
|
1.]]]], [[0., 0., 0., 1., 1.]]),
|
|
|
|
([[[[1., 2., 5., 6.], [3., 4., 7., 8.], [9., 10., 13., 14.],
|
|
|
|
[11., 12., 15., 16.]]]], [[0., 0., 0., 3., 3.]])]
|
|
|
|
outputs = [([[[[1, 1.25], [1.5, 1.75]]]], [[[[3.0625, 0.4375],
|
|
|
|
[0.4375, 0.0625]]]]),
|
|
|
|
([[[[1., 1.25], [1.5, 1.75]], [[4, 3.75],
|
|
|
|
[3.5, 3.25]]]], [[[[3.0625, 0.4375],
|
|
|
|
[0.4375, 0.0625]],
|
|
|
|
[[3.0625, 0.4375],
|
|
|
|
[0.4375,
|
|
|
|
0.0625]]]]),
|
|
|
|
([[[[1.9375, 4.75],
|
|
|
|
[7.5625,
|
|
|
|
10.375]]]], [[[[0.47265625, 0.4296875, 0.4296875, 0.04296875],
|
|
|
|
[0.4296875, 0.390625, 0.390625, 0.0390625],
|
|
|
|
[0.4296875, 0.390625, 0.390625, 0.0390625],
|
|
|
|
[0.04296875, 0.0390625, 0.0390625,
|
|
|
|
0.00390625]]]])]
|
|
|
|
|
|
|
|
|
|
|
|
class TestDeformRoIPool(object):
|
|
|
|
|
|
|
|
def test_deform_roi_pool_gradcheck(self):
|
|
|
|
if not torch.cuda.is_available():
|
|
|
|
return
|
|
|
|
from mmcv.ops import DeformRoIPoolPack
|
|
|
|
pool_h = 2
|
|
|
|
pool_w = 2
|
|
|
|
spatial_scale = 1.0
|
|
|
|
sampling_ratio = 2
|
|
|
|
|
|
|
|
for case in inputs:
|
|
|
|
np_input = np.array(case[0])
|
|
|
|
np_rois = np.array(case[1])
|
|
|
|
|
|
|
|
x = torch.tensor(
|
|
|
|
np_input, device='cuda', dtype=torch.float, requires_grad=True)
|
|
|
|
rois = torch.tensor(np_rois, device='cuda', dtype=torch.float)
|
|
|
|
output_c = x.size(1)
|
|
|
|
|
|
|
|
droipool = DeformRoIPoolPack((pool_h, pool_w),
|
|
|
|
output_c,
|
|
|
|
spatial_scale=spatial_scale,
|
|
|
|
sampling_ratio=sampling_ratio).cuda()
|
|
|
|
|
|
|
|
if _USING_PARROTS:
|
Add new parrots extension implementation for all ops (#794)
* delete all parrots file
add bbox_overlaps new parrots op impl
* support first new impl parrts op (bbox_overlaps)(success test)
* add box_iou_rotated op, test succeed
* add carafe and carafe_naive op, test succeed (one parrots bug need fix)
* add cc_attention op, test success
* add corner_pool op, test success
* add parrots op deform_conv, test success
* add deform_roi_pool op, test success (but has question)
* add focal loss op, test success (gradcheck)
* add masked_conv2d op, test success
* add modulated_deform_conv op, test success
* add nms and nms_rotated op, test success
* add psamask op, test success
* add roi_align op, test_success
* add roi_pool op, test success
* add sync_bn op, test success
* add tin_shift op, test success
* fix test_deform_roi_pool, add parrots test
* skip test_onnx because parrots does not support onnx
* fix c++ lint
* fix python lint
* fix python lint
2021-02-26 19:05:25 +08:00
|
|
|
gradcheck(droipool, (x, rois), no_grads=[rois])
|
2020-06-28 23:15:47 +08:00
|
|
|
else:
|
|
|
|
gradcheck(droipool, (x, rois), eps=1e-2, atol=1e-2)
|
|
|
|
|
|
|
|
def test_modulated_deform_roi_pool_gradcheck(self):
|
|
|
|
if not torch.cuda.is_available():
|
|
|
|
return
|
|
|
|
from mmcv.ops import ModulatedDeformRoIPoolPack
|
|
|
|
pool_h = 2
|
|
|
|
pool_w = 2
|
|
|
|
spatial_scale = 1.0
|
|
|
|
sampling_ratio = 2
|
|
|
|
|
|
|
|
for case in inputs:
|
|
|
|
np_input = np.array(case[0])
|
|
|
|
np_rois = np.array(case[1])
|
|
|
|
|
|
|
|
x = torch.tensor(
|
|
|
|
np_input, device='cuda', dtype=torch.float, requires_grad=True)
|
|
|
|
rois = torch.tensor(np_rois, device='cuda', dtype=torch.float)
|
|
|
|
output_c = x.size(1)
|
|
|
|
|
|
|
|
droipool = ModulatedDeformRoIPoolPack(
|
|
|
|
(pool_h, pool_w),
|
|
|
|
output_c,
|
|
|
|
spatial_scale=spatial_scale,
|
|
|
|
sampling_ratio=sampling_ratio).cuda()
|
|
|
|
|
|
|
|
if _USING_PARROTS:
|
Add new parrots extension implementation for all ops (#794)
* delete all parrots file
add bbox_overlaps new parrots op impl
* support first new impl parrts op (bbox_overlaps)(success test)
* add box_iou_rotated op, test succeed
* add carafe and carafe_naive op, test succeed (one parrots bug need fix)
* add cc_attention op, test success
* add corner_pool op, test success
* add parrots op deform_conv, test success
* add deform_roi_pool op, test success (but has question)
* add focal loss op, test success (gradcheck)
* add masked_conv2d op, test success
* add modulated_deform_conv op, test success
* add nms and nms_rotated op, test success
* add psamask op, test success
* add roi_align op, test_success
* add roi_pool op, test success
* add sync_bn op, test success
* add tin_shift op, test success
* fix test_deform_roi_pool, add parrots test
* skip test_onnx because parrots does not support onnx
* fix c++ lint
* fix python lint
* fix python lint
2021-02-26 19:05:25 +08:00
|
|
|
gradcheck(droipool, (x, rois), no_grads=[rois])
|
2020-06-28 23:15:47 +08:00
|
|
|
else:
|
|
|
|
gradcheck(droipool, (x, rois), eps=1e-2, atol=1e-2)
|