def __init__(self, config, batch_size, num_classes, use_sigmoid_cls, target_means=(.0, .0, .0, .0), target_stds=(1.0, 1.0, 1.0, 1.0) ): super(Proposal, self).__init__() cfg = config self.batch_size = batch_size self.num_classes = num_classes self.target_means = target_means self.target_stds = target_stds self.use_sigmoid_cls = config.use_sigmoid_cls if self.use_sigmoid_cls: self.cls_out_channels = 1 self.activation = P.Sigmoid() self.reshape_shape = (-1, 1) else: self.cls_out_channels = num_classes self.activation = P.Softmax(axis=1) self.reshape_shape = (-1, 2) if self.cls_out_channels <= 0: raise ValueError('num_classes={} is too small'.format(num_classes)) self.num_pre = cfg.rpn_proposal_nms_pre self.min_box_size = cfg.rpn_proposal_min_bbox_size self.nms_thr = cfg.rpn_proposal_nms_thr self.nms_post = cfg.rpn_proposal_nms_post self.nms_across_levels = cfg.rpn_proposal_nms_across_levels self.max_num = cfg.rpn_proposal_max_num # Op Define self.squeeze = P.Squeeze() self.reshape = P.Reshape() self.cast = P.Cast() self.feature_shapes = cfg.feature_shapes self.transpose_shape = (1, 2, 0) self.decode = BoundingBoxDecode() self.nms = P.NMSWithMask(self.nms_thr) self.concat_axis0 = P.Concat(axis=0) self.concat_axis1 = P.Concat(axis=1) self.split = P.Split(axis=1, output_num=5) self.min = P.Minimum() self.gatherND = P.GatherNd() self.slice = P.Slice() self.select = P.Select() self.greater = P.Greater() self.transpose = P.Transpose() self.tile = P.Tile() self.set_train_local(config, training=True) self.multi_10 = Tensor(10.0, mstype.float16)
def test_nms_with_mask_edge_case_3(): context.set_context(mode=context.GRAPH_MODE, device_target="GPU") # CASE 3 - x2/x1 and y2/y1 sequence out of place nms_op3 = P.NMSWithMask(0.7) bbox3 = [[70, 70, 45, 75, 0.6], [30, 33, 43, 29, 0.1]] expected_bbox = np.array([[70., 70., 45., 75.], [30., 33., 43., 29.]]) expected_score = np.array([0.6, 0.1]) sel_rows, sel_score = runMSRun(nms_op3, bbox3) np.testing.assert_almost_equal(sel_rows, expected_bbox) np.testing.assert_almost_equal(sel_score, expected_score)
def test_nms_with_mask_edge_case_2(): context.set_context(mode=context.GRAPH_MODE, device_target="GPU") # CASE 2 - 0 value boxes - with valid scores nms_op2 = P.NMSWithMask(0.5) bbox2 = [[0, 0, 0, 0, 0.6], [0, 0, 0, 0, 0.1]] expected_bbox = np.array([[0., 0., 0., 0.], [0., 0., 0., 0.]]) expected_score = np.array([0.6, 0.1]) sel_rows, sel_score = runMSRun(nms_op2, bbox2) np.testing.assert_almost_equal(sel_rows, expected_bbox) np.testing.assert_almost_equal(sel_score, expected_score)
def test_nms_with_mask_edge_case_1(): context.set_context(mode=context.GRAPH_MODE, device_target="GPU") # CASE 1 - FULL OVERLAP BOXES - Every box is duplicated and has a different score nms_op1 = P.NMSWithMask(0.3) bbox1 = [[12, 4, 33, 17, 0.6], [20, 11, 38, 23, 0.1], [20, 10, 45, 26, 0.9], [15, 17, 35, 38, 0.5], [10, 20, 30, 40, 0.4], [35, 35, 89, 90, 0.8], [12, 4, 33, 17, 0.3], [20, 11, 38, 23, 0.2], [20, 10, 45, 26, 0.1], [15, 17, 35, 38, 0.8], [10, 20, 30, 40, 0.41], [35, 35, 89, 90, 0.82]] expected_bbox = np.array([[20., 10., 45., 26.], [35., 35., 89., 90.], [15., 17., 35., 38.], [12., 4., 33., 17.]]) expected_score = np.array([0.9, 0.82, 0.8, 0.6]) sel_rows, sel_score = runMSRun(nms_op1, bbox1) np.testing.assert_almost_equal(sel_rows, expected_bbox) np.testing.assert_almost_equal(sel_score, expected_score)
def test_nms_with_mask_check_order(): context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") nms_op = P.NMSWithMask(0.5) for _ in range(10): count = 4000 box = np.random.randint(1, 100, size=(count, 4)) box[:, 2] = box[:, 0] + box[:, 2] box[:, 3] = box[:, 1] + box[:, 3] unsorted_scores = np.random.rand(count, 1) bbox = np.hstack((box, unsorted_scores)) bbox = Tensor(bbox, dtype=mindspore.float32) prop, _, _ = nms_op(bbox) ms_sorted_scores = (prop.asnumpy()[:, -1]) # select just scores np_sorted_scores = (np.sort(unsorted_scores, axis=0)[::-1][:, 0]) # sort manually np.testing.assert_array_almost_equal( ms_sorted_scores, np_sorted_scores)
def test_nms_with_masl_check_result(): context.set_context(mode=context.GRAPH_MODE, device_target="GPU") test_count = 500 for x in range(1, test_count + 1): count = 20 # size of bbox lists nms_op = P.NMSWithMask(x * 0.002) # will test full range b/w 0 and 1 box = np.random.randint(1, 100, size=(count, 4)) box[:, 2] = box[:, 0] + box[:, 2] box[:, 3] = box[:, 1] + box[:, 3] unsorted_scores = np.random.rand(count, 1) sorted_scores = np.sort(unsorted_scores, axis=0)[::-1] bbox = np.hstack((box, sorted_scores)) bbox = Tensor(bbox, dtype=mindspore.float32) _, _, mask = nms_op(bbox) mask = mask.asnumpy() manual_mask = manualNMS(box, x * 0.002) np.testing.assert_array_equal(mask, np.array(manual_mask))
def test_mode_init(self, config): 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(self.dtype)) 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(self.dtype) * config.test_score_thr) self.test_score_zeros = Tensor( np.ones((self.rpn_max_num, 1)).astype(self.dtype) * 0) self.test_box_zeros = Tensor( np.ones((self.rpn_max_num, 4)).astype(self.dtype) * -1) self.test_iou_thr = Tensor( np.ones((self.rpn_max_num, 1)).astype(self.dtype) * 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
'block': P.NPUGetFloatStatus(), 'desc_inputs': [Tensor(np.zeros([8]).astype(np.float32))], 'desc_bprop': [Tensor(np.zeros([8]).astype(np.float32))], 'skip': ['backward']}), ('NpuClearFloatStatus', { 'block': P.NPUClearFloatStatus(), 'desc_inputs': [Tensor(np.zeros([8]).astype(np.float32))], 'desc_bprop': [Tensor(np.zeros([8]).astype(np.float32))], 'skip': ['backward']}), ('CheckValid', { 'block': P.CheckValid(), 'desc_inputs': [[20000, 4], [3]], 'desc_bprop': [[20000]], 'skip': ['backward']}), ('NMSWithMask', { 'block': P.NMSWithMask(0.5), 'desc_inputs': [[128, 5]], 'desc_bprop': [[128, 5], [128], [128]], 'skip': ['backward']}), ('Abs', { 'block': P.Abs(), 'desc_inputs': [[4]], 'desc_bprop': [[4]]}), ('CumSum', { 'block': P.CumSum(), 'desc_const': [0], 'desc_inputs': [Tensor(np.array([[3, 4],[1, 6]]).astype(np.float16))], 'desc_bprop': [Tensor(np.array([[3, 4],[4, 10]]).astype(np.float16))]}), ('ReduceSum_3', { 'block': P.ReduceSum(), 'desc_const': [0],
'skip': ['backward']}), # input is not tensor ('Sin0', { 'block': (P.Sin(), {'exception': TypeError, 'error_keywords': ['Sin']}), 'desc_inputs': [5.0], 'skip': ['backward']}), # input is Tensor(bool) ('Sin1', { 'block': (P.Sin(), {'exception': TypeError, 'error_keywords': ['Sin']}), 'desc_inputs': [Tensor(np.ones([3, 4]).astype(np.bool_))], 'skip': ['backward']}), # input is not tensor ('NMSWithMask0', { 'block': (P.NMSWithMask(), {'exception': TypeError, 'error_keywords': ['NMSWithMask']}), 'desc_inputs': [5.0], 'skip': ['backward']}), # input is not Tensor(float16) or Tensor(float32) ('NMSWithMask1', { 'block': (P.NMSWithMask(), {'exception': TypeError, 'error_keywords': ['NMSWithMask']}), 'desc_inputs': [Tensor(np.ones([3, 4]).astype(np.int32))], 'skip': ['backward']}), # dims is not 2 ('NMSWithMask2', { 'block': (P.NMSWithMask(), {'exception': ValueError, 'error_keywords': ['NMSWithMask']}), 'desc_inputs': [Tensor(np.ones([3, 4, 2]).astype(np.float32))], 'skip': ['backward']}), # shape[1] is not 5 ('NMSWithMask3', { 'block': (P.NMSWithMask(), {'exception': ValueError, 'error_keywords': ['NMSWithMask']}),
'desc_inputs': [5.0], 'skip': ['backward'] }), # input is Tensor(bool) ('Sin1', { 'block': (P.Sin(), { 'exception': TypeError, 'error_keywords': ['Sin'] }), 'desc_inputs': [Tensor(np.ones([3, 4]).astype(np.bool_))], 'skip': ['backward'] }), # input is not tensor ('NMSWithMask0', { 'block': (P.NMSWithMask(), { 'exception': TypeError, 'error_keywords': ['NMSWithMask'] }), 'desc_inputs': [5.0], 'skip': ['backward'] }), # input is not Tensor(float16) or Tensor(float32) ('NMSWithMask1', { 'block': (P.NMSWithMask(), { 'exception': TypeError, 'error_keywords': ['NMSWithMask'] }), 'desc_inputs': [Tensor(np.ones([3, 4]).astype(np.int32))], 'skip': ['backward'] }),
def __init__(self, config): super(Mask_Rcnn_Resnet50, 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) # Backbone resnet50 self.backbone = ResNetFea(ResidualBlockUsing, config.resnet_block, config.resnet_in_channels, config.resnet_out_channels, False) # Fpn self.fpn_ncek = FeatPyramidNeck(config.fpn_in_channels, config.fpn_out_channels, config.fpn_num_outs) # 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=(768, 1280), means=self.target_means, \ stds=self.target_stds) # Roi self.roi_align = SingleRoIExtractor(config, config.roi_layer, config.roi_align_out_channels, config.roi_align_featmap_strides, self.train_batch_size, config.roi_align_finest_scale, mask=False) self.roi_align.set_train_local(config, True) self.roi_align_mask = SingleRoIExtractor(config, config.roi_layer, config.roi_align_out_channels, config.roi_align_featmap_strides, self.train_batch_size, config.roi_align_finest_scale, mask=True) self.roi_align_mask.set_train_local(config, True) self.roi_align_test = SingleRoIExtractor(config, config.roi_layer, config.roi_align_out_channels, config.roi_align_featmap_strides, 1, config.roi_align_finest_scale, mask=False) self.roi_align_test.set_train_local(config, False) self.roi_align_mask_test = SingleRoIExtractor(config, config.roi_layer, config.roi_align_out_channels, config.roi_align_featmap_strides, 1, config.roi_align_finest_scale, mask=True) self.roi_align_mask_test.set_train_local(config, False) # Rcnn self.rcnn_cls = RcnnCls(config, self.train_batch_size, self.num_classes) self.rcnn_mask = RcnnMask(config, 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_fb_mask = 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.float16)) 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.float16) * config.test_score_thr) self.test_score_zeros = Tensor(np.ones((self.rpn_max_num, 1)).astype(np.float16) * 0) self.test_box_zeros = Tensor(np.ones((self.rpn_max_num, 4)).astype(np.float16) * -1) self.test_iou_thr = Tensor(np.ones((self.rpn_max_num, 1)).astype(np.float16) * 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 = min(self.num_classes - 2, 55) self.concat_end = (self.num_classes - 1) # Init tensor roi_align_index = [np.array(np.ones((config.num_expected_pos_stage2 + config.num_expected_neg_stage2, 1)) * i, dtype=np.float16) for i in range(self.train_batch_size)] roi_align_index_test = [np.array(np.ones((config.rpn_max_num, 1)) * i, dtype=np.float16) \ 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)) roi_align_index_pos = [np.array(np.ones((config.num_expected_pos_stage2, 1)) * i, dtype=np.float16) for i in range(self.train_batch_size)] self.roi_align_index_tensor_pos = Tensor(np.concatenate(roi_align_index_pos)) self.rcnn_loss_cls_weight = Tensor(np.array(config.rcnn_loss_cls_weight).astype(np.float16)) self.rcnn_loss_reg_weight = Tensor(np.array(config.rcnn_loss_reg_weight).astype(np.float16)) self.rcnn_loss_mask_fb_weight = Tensor(np.array(config.rcnn_loss_mask_fb_weight).astype(np.float16)) self.argmax_with_value = P.ArgMaxWithValue(axis=1) self.on_value = Tensor(1.0, mstype.float32) self.off_value = Tensor(0.0, mstype.float32) self.onehot = P.OneHot() self.reducesum = P.ReduceSum() self.sigmoid = P.Sigmoid() self.expand_dims = P.ExpandDims() self.test_mask_fb_zeros = Tensor(np.zeros((self.rpn_max_num, 28, 28)).astype(np.float16)) self.value = Tensor(1.0, mstype.float16)
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()