def test_roi_align(): context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") x = Tensor( np.array([[[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30], [31, 32, 33, 34, 35, 36]]]], np.float32)) rois = Tensor(np.array([[0, -2.0, -2.0, 22.0, 22.0]], np.float32)) # test case 1 pooled_height, pooled_width, spatial_scale, sample_num = 3, 3, 0.25, 2 roi_align = P.ROIAlign(pooled_height, pooled_width, spatial_scale, sample_num) output = roi_align(x, rois) print(output) expect = [[[[2.75, 4.5, 6.5], [13.25, 15., 17.], [25.25, 27., 29.]]]] assert (output.asnumpy() == expect).all() # test case 2 pooled_height, pooled_width, spatial_scale, sample_num = 4, 4, 0.2, 3 roi_align = P.ROIAlign(pooled_height, pooled_width, spatial_scale, sample_num) output = roi_align(x, rois) print(output) expect = [[[[1.2333, 2.1000, 3.3000, 4.5000], [6.4333, 7.3000, 8.5000, 9.7000], [13.6333, 14.5000, 15.7000, 16.9000], [20.8333, 21.7000, 22.9000, 24.1000]]]] np.testing.assert_almost_equal(output.asnumpy(), expect, decimal=4) # test case 3 pooled_height, pooled_width, spatial_scale, sample_num = 3, 3, 0.3, 3 rois = Tensor( np.array([[0, -2.0, -2.0, 22.0, 22.0], [0, 1.0, 0.0, 19.0, 18.0]], np.float32)) roi_align = P.ROIAlign(pooled_height, pooled_width, spatial_scale, sample_num) output = roi_align(x, rois) print(output) expect = [[[[3.3333, 5.5000, 7.6667], [16.3333, 18.5000, 20.6667], [29.3333, 31.5000, 33.6667]]], [[[4.5000, 6.3000, 8.1000], [14.9000, 16.7000, 18.5000], [25.7000, 27.5000, 29.3000]]]] np.testing.assert_almost_equal(output.asnumpy(), expect, decimal=4) # test case 4 pooled_height, pooled_width, spatial_scale, sample_num = 2, 2, 1.0, -1 rois = Tensor(np.array([[0, -2.0, -2.0, 22.0, 22.0]], np.float32)) roi_align = P.ROIAlign(pooled_height, pooled_width, spatial_scale, sample_num) output = roi_align(x, rois) print(output) expect = [[[[8.2222, 0.], [0., 0.]]]] np.testing.assert_almost_equal(output.asnumpy(), expect, decimal=4)
def __init__(self, out_size_h, out_size_w, spatial_scale, sample_num=0): super(ROIAlign, self).__init__() self.out_size = (out_size_h, out_size_w) self.spatial_scale = float(spatial_scale) self.sample_num = int(sample_num) self.align_op = P.ROIAlign(self.out_size[0], self.out_size[1], self.spatial_scale, self.sample_num)
def roi_align_case(data_type): context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") x = Tensor( np.array([[[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30], [31, 32, 33, 34, 35, 36]]]], data_type)) # test case 1 rois = Tensor(np.array([[0, -2.0, -2.0, 21.0, 21.0]], data_type)) pooled_height, pooled_width, spatial_scale, sample_num = 3, 3, 0.25, 2 roi_align = P.ROIAlign(pooled_height, pooled_width, spatial_scale, sample_num, 1) output = roi_align(x, rois) print(output) expect = [[[[4.5, 6.5, 8.5], [16.5, 18.5, 20.5], [28.5, 30.5, 32.5]]]] assert (output.asnumpy() == expect).all() # test case 2 rois = Tensor(np.array([[0, -2.0, -2.0, 22.0, 22.0]], data_type)) pooled_height, pooled_width, spatial_scale, sample_num = 3, 3, 0.25, 2 roi_align = P.ROIAlign(pooled_height, pooled_width, spatial_scale, sample_num, 0) output = roi_align(x, rois) print(output) expect = [[[[4.5, 6.5, 8.5], [16.5, 18.5, 20.5], [28.5, 30.5, 32.5]]]] assert (output.asnumpy() == expect).all() # test case 3 pooled_height, pooled_width, spatial_scale, sample_num = 2, 2, 1.0, -1 rois = Tensor(np.array([[0, -2.0, -2.0, 22.0, 22.0]], data_type)) roi_align = P.ROIAlign(pooled_height, pooled_width, spatial_scale, sample_num, 0) output = roi_align(x, rois) print(output) expect = [[[[6.295, 0.], [0., 0.]]]] np.testing.assert_almost_equal(output.asnumpy(), expect, decimal=2)
def test_roi_align_half(): context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") x = Tensor( np.array([[[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30], [31, 32, 33, 34, 35, 36]]]], np.float16)) rois = Tensor(np.array([[0, -2.0, -2.0, 22.0, 22.0]], np.float16)) # test case 1 pooled_height, pooled_width, spatial_scale, sample_num = 4, 4, 0.2, 3 roi_align = P.ROIAlign(pooled_height, pooled_width, spatial_scale, sample_num, 0) output = roi_align(x, rois) print(output) expect = [[[[1.2333, 2.1000, 3.3000, 4.5000], [6.4333, 7.3000, 8.5000, 9.7000], [13.6333, 14.5000, 15.7000, 16.9000], [20.8333, 21.7000, 22.9000, 24.1000]]]] np.testing.assert_almost_equal(output.asnumpy(), expect, decimal=1)
def __init__(self, config): super(Deeptext_VGG16, self).__init__() self.train_batch_size = config.batch_size self.num_classes = config.num_classes self.anchor_scales = config.anchor_scales self.anchor_ratios = config.anchor_ratios self.anchor_strides = config.anchor_strides self.target_means = tuple(config.rcnn_target_means) self.target_stds = tuple(config.rcnn_target_stds) # Anchor generator anchor_base_sizes = None self.anchor_base_sizes = list( self.anchor_strides ) if anchor_base_sizes is None else anchor_base_sizes self.anchor_generators = [] for anchor_base in self.anchor_base_sizes: self.anchor_generators.append( AnchorGenerator(anchor_base, self.anchor_scales, self.anchor_ratios)) self.num_anchors = len(self.anchor_ratios) * len(self.anchor_scales) featmap_sizes = config.feature_shapes assert len(featmap_sizes) == len(self.anchor_generators) self.anchor_list = self.get_anchors(featmap_sizes) # Rpn and rpn loss self.gt_labels_stage1 = Tensor( np.ones((self.train_batch_size, config.num_gts)).astype(np.uint8)) self.rpn_with_loss = RPN(config, self.train_batch_size, config.rpn_in_channels, config.rpn_feat_channels, config.num_anchors, config.rpn_cls_out_channels) # Proposal self.proposal_generator = Proposal(config, self.train_batch_size, config.activate_num_classes, config.use_sigmoid_cls) self.proposal_generator.set_train_local(config, True) self.proposal_generator_test = Proposal(config, config.test_batch_size, config.activate_num_classes, config.use_sigmoid_cls) self.proposal_generator_test.set_train_local(config, False) # Assign and sampler stage two self.bbox_assigner_sampler_for_rcnn = BboxAssignSampleForRcnn( config, self.train_batch_size, config.num_bboxes_stage2, True) self.decode = P.BoundingBoxDecode(max_shape=(576, 960), means=self.target_means, \ stds=self.target_stds) # Rcnn self.rcnn = Rcnn( config, config.rcnn_in_channels * config.roi_layer['out_size'] * config.roi_layer['out_size'], self.train_batch_size, self.num_classes) # Op declare self.squeeze = P.Squeeze() self.cast = P.Cast() self.concat = P.Concat(axis=0) self.concat_1 = P.Concat(axis=1) self.concat_2 = P.Concat(axis=2) self.reshape = P.Reshape() self.select = P.Select() self.greater = P.Greater() self.transpose = P.Transpose() # Test mode self.test_batch_size = config.test_batch_size self.split = P.Split(axis=0, output_num=self.test_batch_size) self.split_shape = P.Split(axis=0, output_num=4) self.split_scores = P.Split(axis=1, output_num=self.num_classes) self.split_cls = P.Split(axis=0, output_num=self.num_classes - 1) self.tile = P.Tile() self.gather = P.GatherNd() self.rpn_max_num = config.rpn_max_num self.zeros_for_nms = Tensor( np.zeros((self.rpn_max_num, 3)).astype(np.float32)) self.ones_mask = np.ones((self.rpn_max_num, 1)).astype(np.bool) self.zeros_mask = np.zeros((self.rpn_max_num, 1)).astype(np.bool) self.bbox_mask = Tensor( np.concatenate((self.ones_mask, self.zeros_mask, self.ones_mask, self.zeros_mask), axis=1)) self.nms_pad_mask = Tensor( np.concatenate((self.ones_mask, self.ones_mask, self.ones_mask, self.ones_mask, self.zeros_mask), axis=1)) self.test_score_thresh = Tensor( np.ones((self.rpn_max_num, 1)).astype(np.float32) * config.test_score_thr) self.test_score_zeros = Tensor( np.ones((self.rpn_max_num, 1)).astype(np.float32) * 0) self.test_box_zeros = Tensor( np.ones((self.rpn_max_num, 4)).astype(np.float32) * -1) self.test_iou_thr = Tensor( np.ones((self.rpn_max_num, 1)).astype(np.float32) * config.test_iou_thr) self.test_max_per_img = config.test_max_per_img self.nms_test = P.NMSWithMask(config.test_iou_thr) self.softmax = P.Softmax(axis=1) self.logicand = P.LogicalAnd() self.oneslike = P.OnesLike() self.test_topk = P.TopK(sorted=True) self.test_num_proposal = self.test_batch_size * self.rpn_max_num # Improve speed self.concat_start = (self.num_classes - 2) self.concat_end = (self.num_classes - 1) # Init tensor self.use_ambigous_sample = config.use_ambigous_sample roi_align_index = [ np.array(np.ones((config.num_expected_pos_stage2 + config.num_expected_neg_stage2, 1)) * i, dtype=np.float32) for i in range(self.train_batch_size) ] if self.use_ambigous_sample: roi_align_index = [ np.array(np.ones((config.num_expected_pos_stage2 + config.num_expected_amb_stage2 + config.num_expected_neg_stage2, 1)) * i, dtype=np.float32) for i in range(self.train_batch_size) ] roi_align_index_test = [np.array(np.ones((config.rpn_max_num, 1)) * i, dtype=np.float32) \ for i in range(self.test_batch_size)] self.roi_align_index_tensor = Tensor(np.concatenate(roi_align_index)) self.roi_align_index_test_tensor = Tensor( np.concatenate(roi_align_index_test)) self.roi_align4 = P.ROIAlign(pooled_width=7, pooled_height=7, spatial_scale=0.125) self.roi_align5 = P.ROIAlign(pooled_width=7, pooled_height=7, spatial_scale=0.0625) self.concat1 = P.Concat(axis=1) self.roi_align_fuse = _conv(in_channels=1024, out_channels=512, kernel_size=1, padding=0, stride=1) self.vgg16_feature_extractor = VGG16FeatureExtraction()