def _test_roialign_rotated_allclose(device, dtype): if not torch.cuda.is_available() and device == 'cuda': pytest.skip('unittest does not support GPU yet.') try: from mmcv.ops import RoIAlignRotated, roi_align_rotated except ModuleNotFoundError: pytest.skip('test requires compilation') pool_h = 2 pool_w = 2 spatial_scale = 1.0 sampling_ratio = 2 for case, output in zip(inputs, outputs): np_input = np.array(case[0]) np_rois = np.array(case[1]) np_output = np.array(output[0]) np_grad = np.array(output[1]) x = torch.tensor(np_input, dtype=dtype, device=device, requires_grad=True) rois = torch.tensor(np_rois, dtype=dtype, device=device) output = roi_align_rotated(x, rois, (pool_h, pool_w), spatial_scale, sampling_ratio, True) output.backward(torch.ones_like(output)) assert np.allclose(output.data.type(torch.float).cpu().numpy(), np_output, atol=1e-3) assert np.allclose(x.grad.data.type(torch.float).cpu().numpy(), np_grad, atol=1e-3) # Test deprecated parameters roi_align_rotated_module_deprecated = RoIAlignRotated( out_size=(pool_h, pool_w), spatial_scale=spatial_scale, sample_num=sampling_ratio) output_1 = roi_align_rotated_module_deprecated(x, rois) roi_align_rotated_module_new = RoIAlignRotated( output_size=(pool_h, pool_w), spatial_scale=spatial_scale, sampling_ratio=sampling_ratio) output_2 = roi_align_rotated_module_new(x, rois) assert np.allclose( output_1.data.type(torch.float).cpu().numpy(), output_2.data.type(torch.float).cpu().numpy())
def __init__(self, k_at_hops, num_adjacent_linkages, node_geo_feat_len, pooling_scale, pooling_output_size, nms_thr, min_width, max_width, comp_shrink_ratio, comp_w_h_ratio, comp_score_thr, text_region_thr, center_region_thr, center_region_area_thr): assert len(k_at_hops) == 2 assert isinstance(k_at_hops, tuple) assert isinstance(num_adjacent_linkages, int) assert isinstance(node_geo_feat_len, int) assert isinstance(pooling_scale, float) assert isinstance(pooling_output_size, tuple) assert isinstance(nms_thr, float) assert isinstance(min_width, float) assert isinstance(max_width, float) assert isinstance(comp_shrink_ratio, float) assert isinstance(comp_w_h_ratio, float) assert isinstance(comp_score_thr, float) assert isinstance(text_region_thr, float) assert isinstance(center_region_thr, float) assert isinstance(center_region_area_thr, int) self.k_at_hops = k_at_hops self.active_connection = num_adjacent_linkages self.local_graph_depth = len(self.k_at_hops) self.node_geo_feat_dim = node_geo_feat_len self.pooling = RoIAlignRotated(pooling_output_size, pooling_scale) self.nms_thr = nms_thr self.min_width = min_width self.max_width = max_width self.comp_shrink_ratio = comp_shrink_ratio self.comp_w_h_ratio = comp_w_h_ratio self.comp_score_thr = comp_score_thr self.text_region_thr = text_region_thr self.center_region_thr = center_region_thr self.center_region_area_thr = center_region_area_thr
def _test_roialign_rotated_gradcheck(device, dtype): if not torch.cuda.is_available() and device == 'cuda': pytest.skip('unittest does not support GPU yet.') try: from mmcv.ops import RoIAlignRotated except ModuleNotFoundError: pytest.skip('RoIAlignRotated op is not successfully compiled') if dtype is torch.half: pytest.skip('grad check does not support fp16') for case in inputs: np_input = np.array(case[0]) np_rois = np.array(case[1]) x = torch.tensor(np_input, dtype=dtype, device=device, requires_grad=True) rois = torch.tensor(np_rois, dtype=dtype, device=device) froipool = RoIAlignRotated((pool_h, pool_w), spatial_scale, sampling_ratio) if torch.__version__ == 'parrots': gradcheck(froipool, (x, rois), no_grads=[rois], delta=1e-5, pt_atol=1e-5) else: gradcheck(froipool, (x, rois), eps=1e-5, atol=1e-5)
def __init__(self, k_at_hops, num_adjacent_linkages, node_geo_feat_len, pooling_scale, pooling_output_size, local_graph_thr): assert len(k_at_hops) == 2 assert all(isinstance(n, int) for n in k_at_hops) assert isinstance(num_adjacent_linkages, int) assert isinstance(node_geo_feat_len, int) assert isinstance(pooling_scale, float) assert all(isinstance(n, int) for n in pooling_output_size) assert isinstance(local_graph_thr, float) self.k_at_hops = k_at_hops self.num_adjacent_linkages = num_adjacent_linkages self.node_geo_feat_dim = node_geo_feat_len self.pooling = RoIAlignRotated(pooling_output_size, pooling_scale) self.local_graph_thr = local_graph_thr