def __init__(self, config, batch_size, num_bboxes, add_gt_as_proposals): super(BboxAssignSample, self).__init__() cfg = config self.batch_size = batch_size self.neg_iou_thr = Tensor(cfg.neg_iou_thr, mstype.float16) self.pos_iou_thr = Tensor(cfg.pos_iou_thr, mstype.float16) self.min_pos_iou = Tensor(cfg.min_pos_iou, mstype.float16) self.zero_thr = Tensor(0.0, mstype.float16) self.num_bboxes = num_bboxes self.num_gts = cfg.num_gts self.num_expected_pos = cfg.num_expected_pos self.num_expected_neg = cfg.num_expected_neg self.add_gt_as_proposals = add_gt_as_proposals if self.add_gt_as_proposals: self.label_inds = Tensor(np.arange(1, self.num_gts + 1)) self.concat = P.Concat(axis=0) self.max_gt = P.ArgMaxWithValue(axis=0) self.max_anchor = P.ArgMaxWithValue(axis=1) self.sum_inds = P.ReduceSum() self.iou = P.IOU() self.greaterequal = P.GreaterEqual() self.greater = P.Greater() self.select = P.Select() self.gatherND = P.GatherNd() self.squeeze = P.Squeeze() self.cast = P.Cast() self.logicaland = P.LogicalAnd() self.less = P.Less() self.random_choice_with_mask_pos = P.RandomChoiceWithMask(self.num_expected_pos) self.random_choice_with_mask_neg = P.RandomChoiceWithMask(self.num_expected_neg) self.reshape = P.Reshape() self.equal = P.Equal() self.bounding_box_encode = BoundingBoxEncode() self.scatterNdUpdate = P.ScatterNdUpdate() self.scatterNd = P.ScatterNd() self.logicalnot = P.LogicalNot() self.tile = P.Tile() self.zeros_like = P.ZerosLike() self.assigned_gt_inds = Tensor(np.array(-1 * np.ones(num_bboxes), dtype=np.int32)) self.assigned_gt_zeros = Tensor(np.array(np.zeros(num_bboxes), dtype=np.int32)) self.assigned_gt_ones = Tensor(np.array(np.ones(num_bboxes), dtype=np.int32)) self.assigned_gt_ignores = Tensor(np.array(-1 * np.ones(num_bboxes), dtype=np.int32)) self.assigned_pos_ones = Tensor(np.array(np.ones(self.num_expected_pos), dtype=np.int32)) self.check_neg_mask = Tensor(np.array(np.ones(self.num_expected_neg - self.num_expected_pos), dtype=np.bool)) self.range_pos_size = Tensor(np.arange(self.num_expected_pos).astype(np.float16)) self.check_gt_one = Tensor(np.array(-1 * np.ones((self.num_gts, 4)), dtype=np.float16)) self.check_anchor_two = Tensor(np.array(-2 * np.ones((self.num_bboxes, 4)), dtype=np.float16)) self.print = P.Print()
'desc_inputs': (Tensor(np.ones((2, 3), np.float32)), Tensor(np.ones((2, 2), np.int32)), Tensor(np.ones((2,), np.float32))), 'desc_bprop': [[2, 3]]}), ('ScatterNd', { 'block': P.ScatterNd(), 'desc_const': [(3, 3)], 'desc_inputs': (Tensor(np.ones((2, 2), np.int32)), Tensor(np.ones((2,), np.int32))), 'desc_bprop': [[3, 3]]}), ('SmoothL1Loss', { 'block': P.SmoothL1Loss(), 'desc_inputs': [[256, 4], [256, 4]], 'desc_bprop': [[256, 4]]}), ('IOU', { 'block': P.IOU(), 'desc_inputs': [Tensor(np.ones((256, 4), np.float16)), Tensor(np.ones((128, 4), np.float16))], 'desc_bprop': [[128, 256]]}), ('Summary', { 'block': SummaryNet(), 'desc_inputs': [Tensor(np.array([1.1]).astype(np.float32)), Tensor(np.array([1.2]).astype(np.float32))], 'skip': ['backward']}), ('ConfusionMulGrad_1', { 'block': P.ConfusionMulGrad(axis = [0], keep_dims = False), 'desc_inputs': [[3, 2], [3, 2], [3, 2]], 'desc_bprop': [[3, 2], [2]], 'skip': ['backward']}), ('ConfusionMulGrad_2', { 'block': P.ConfusionMulGrad(axis = [0], keep_dims = True), 'desc_inputs': [[3, 2], [3, 2], [3, 2]],
def __init__(self, config, batch_size, num_bboxes, add_gt_as_proposals): super(BboxAssignSampleForRcnn, self).__init__() cfg = config self.dtype = np.float32 self.ms_type = mstype.float32 self.batch_size = batch_size self.neg_iou_thr = cfg.neg_iou_thr_stage2 self.pos_iou_thr = cfg.pos_iou_thr_stage2 self.min_pos_iou = cfg.min_pos_iou_stage2 self.num_gts = cfg.num_gts self.num_bboxes = num_bboxes self.num_expected_pos = cfg.num_expected_pos_stage2 self.num_expected_neg = cfg.num_expected_neg_stage2 self.num_expected_total = cfg.num_expected_total_stage2 self.add_gt_as_proposals = add_gt_as_proposals self.label_inds = Tensor( np.arange(1, self.num_gts + 1).astype(np.int32)) self.add_gt_as_proposals_valid = Tensor( np.full(self.num_gts, self.add_gt_as_proposals, dtype=np.int32)) self.concat = P.Concat(axis=0) self.max_gt = P.ArgMaxWithValue(axis=0) self.max_anchor = P.ArgMaxWithValue(axis=1) self.sum_inds = P.ReduceSum() self.iou = P.IOU() self.greaterequal = P.GreaterEqual() self.greater = P.Greater() self.select = P.Select() self.gatherND = P.GatherNd() self.squeeze = P.Squeeze() self.cast = P.Cast() self.logicaland = P.LogicalAnd() self.less = P.Less() self.random_choice_with_mask_pos = P.RandomChoiceWithMask( self.num_expected_pos) self.random_choice_with_mask_neg = P.RandomChoiceWithMask( self.num_expected_neg) self.reshape = P.Reshape() self.equal = P.Equal() self.bounding_box_encode = P.BoundingBoxEncode(means=(0.0, 0.0, 0.0, 0.0), stds=(0.1, 0.1, 0.2, 0.2)) self.concat_axis1 = P.Concat(axis=1) self.logicalnot = P.LogicalNot() self.tile = P.Tile() # Check self.check_gt_one = Tensor( np.full((self.num_gts, 4), -1, dtype=self.dtype)) self.check_anchor_two = Tensor( np.full((self.num_bboxes, 4), -2, dtype=self.dtype)) # Init tensor self.assigned_gt_inds = Tensor(np.full(num_bboxes, -1, dtype=np.int32)) self.assigned_gt_zeros = Tensor( np.array(np.zeros(num_bboxes), dtype=np.int32)) self.assigned_gt_ones = Tensor( np.array(np.ones(num_bboxes), dtype=np.int32)) self.assigned_gt_ignores = Tensor( np.full(num_bboxes, -1, dtype=np.int32)) self.assigned_pos_ones = Tensor( np.array(np.ones(self.num_expected_pos), dtype=np.int32)) self.gt_ignores = Tensor(np.full(self.num_gts, -1, dtype=np.int32)) self.range_pos_size = Tensor( np.arange(self.num_expected_pos).astype(self.dtype)) self.check_neg_mask = Tensor( np.array(np.ones(self.num_expected_neg - self.num_expected_pos), dtype=np.bool)) self.bboxs_neg_mask = Tensor( np.zeros((self.num_expected_neg, 4), dtype=self.dtype)) self.labels_neg_mask = Tensor( np.array(np.zeros(self.num_expected_neg), dtype=np.uint8)) self.reshape_shape_pos = (self.num_expected_pos, 1) self.reshape_shape_neg = (self.num_expected_neg, 1) self.scalar_zero = Tensor(0.0, dtype=self.ms_type) self.scalar_neg_iou_thr = Tensor(self.neg_iou_thr, dtype=self.ms_type) self.scalar_pos_iou_thr = Tensor(self.pos_iou_thr, dtype=self.ms_type) self.scalar_min_pos_iou = Tensor(self.min_pos_iou, dtype=self.ms_type)
('ScatterNd', { 'block': P.ScatterNd(), 'desc_const': [(3, 3)], 'desc_inputs': (Tensor(np.ones( (2, 2), np.int32)), Tensor(np.ones((2, ), np.int32))), 'desc_bprop': [[3, 3]] }), ('SmoothL1Loss', { 'block': P.SmoothL1Loss(), 'desc_inputs': [[256, 4], [256, 4]], 'desc_bprop': [[256, 4]] }), ('IOU', { 'block': P.IOU(), 'desc_inputs': [ Tensor(np.ones((256, 4), np.float16)), Tensor(np.ones((128, 4), np.float16)) ], 'desc_bprop': [[128, 256]] }), ('Summary', { 'block': SummaryNet(), 'desc_inputs': [ Tensor(np.array([1.1]).astype(np.float32)), Tensor(np.array([1.2]).astype(np.float32)) ], 'skip': ['backward'] }),
def __init__(self, num_classes, anchors, anchors_mask, reduction=32, seen=0, coord_scale=1.0, no_object_scale=1.0, object_scale=1.0, class_scale=1.0, thresh=0.5, head_idx=0.0): super(YoloLoss, self).__init__() self.num_classes = num_classes self.num_anchors = len(anchors_mask) self.anchor_step = len(anchors[0]) # each scale has step anchors self.anchors = np.array(anchors, dtype=np.float32) / reduction # scale every anchor for every scale self.tensor_anchors = Tensor(self.anchors, mstype.float32) self.anchors_mask = anchors_mask anchors_w = [] anchors_h = [] for i in range(len(anchors_mask)): anchors_w.append(self.anchors[self.anchors_mask[i]][0]) anchors_h.append(self.anchors[self.anchors_mask[i]][1]) self.anchors_w = Tensor(np.array(anchors_w).reshape(len(self.anchors_mask), 1)) self.anchors_h = Tensor(np.array(anchors_h).reshape(len(self.anchors_mask), 1)) self.reduction = reduction self.seen = seen self.head_idx = head_idx self.zero = Tensor(0) self.coord_scale = coord_scale self.no_object_scale = no_object_scale self.object_scale = object_scale self.class_scale = class_scale self.thresh = thresh self.info = {'avg_iou': 0, 'class': 0, 'obj': 0, 'no_obj': 0, 'recall50': 0, 'recall75': 0, 'obj_cur': 0, 'obj_all': 0, 'coord_xy': 0, 'coord_wh': 0} self.shape = P.Shape() self.reshape = P.Reshape() self.sigmoid = P.Sigmoid() self.zeros_like = P.ZerosLike() self.concat0 = P.Concat(0) self.concat0_2 = P.Concat(0) self.concat0_3 = P.Concat(0) self.concat0_4 = P.Concat(0) self.concat1 = P.Concat(1) self.concat1_2 = P.Concat(1) self.concat1_3 = P.Concat(1) self.concat1_4 = P.Concat(1) self.concat2 = P.Concat(2) self.concat2_2 = P.Concat(2) self.concat2_3 = P.Concat(2) self.concat2_4 = P.Concat(2) self.tile = P.Tile() self.transpose = P.Transpose() self.cast = P.Cast() self.exp = P.Exp() self.sum = P.ReduceSum() self.smooth_l1_loss = P.SmoothL1Loss() self.bce = P.SigmoidCrossEntropyWithLogits() self.ce = P.SoftmaxCrossEntropyWithLogits() self.pt_linspace = PtLinspace() self.one_hot = nn.OneHot(-1, self.num_classes, 1.0, 0.0) self.squeeze_2 = P.Squeeze(2) self.reduce_sum = P.ReduceSum() self.select = P.Select() self.iou = P.IOU()
def __init__(self, config, batch_size, num_bboxes, add_gt_as_proposals): super(BboxAssignSampleForRcnn, self).__init__() cfg = config self.batch_size = batch_size self.neg_iou_thr = cfg.neg_iou_thr_stage2 self.pos_iou_thr = cfg.pos_iou_thr_stage2 self.min_pos_iou = cfg.min_pos_iou_stage2 self.num_gts = cfg.num_gts self.num_bboxes = num_bboxes self.num_expected_pos = cfg.num_expected_pos_stage2 self.num_expected_neg = cfg.num_expected_neg_stage2 self.num_expected_total = cfg.num_expected_total_stage2 self.add_gt_as_proposals = add_gt_as_proposals self.label_inds = Tensor( np.arange(1, self.num_gts + 1).astype(np.int32)) self.add_gt_as_proposals_valid = Tensor( np.array(self.add_gt_as_proposals * np.ones(self.num_gts), dtype=np.int32)) self.concat = P.Concat(axis=0) self.max_gt = P.ArgMaxWithValue(axis=0) self.max_anchor = P.ArgMaxWithValue(axis=1) self.sum_inds = P.ReduceSum() self.iou = P.IOU() self.greaterequal = P.GreaterEqual() self.greater = P.Greater() self.select = P.Select() self.gatherND = P.GatherNd() self.squeeze = P.Squeeze() self.cast = P.Cast() self.logicaland = P.LogicalAnd() self.less = P.Less() self.random_choice_with_mask_pos = P.RandomChoiceWithMask( self.num_expected_pos) self.random_choice_with_mask_neg = P.RandomChoiceWithMask( self.num_expected_neg) self.reshape = P.Reshape() self.equal = P.Equal() self.bounding_box_encode = P.BoundingBoxEncode(means=(0.0, 0.0, 0.0, 0.0), stds=(0.1, 0.1, 0.2, 0.2)) self.concat_axis1 = P.Concat(axis=1) self.logicalnot = P.LogicalNot() self.tile = P.Tile() # Check self.check_gt_one = Tensor( np.array(-1 * np.ones((self.num_gts, 4)), dtype=np.float16)) self.check_anchor_two = Tensor( np.array(-2 * np.ones((self.num_bboxes, 4)), dtype=np.float16)) # Init tensor self.assigned_gt_inds = Tensor( np.array(-1 * np.ones(num_bboxes), dtype=np.int32)) self.assigned_gt_zeros = Tensor( np.array(np.zeros(num_bboxes), dtype=np.int32)) self.assigned_gt_ones = Tensor( np.array(np.ones(num_bboxes), dtype=np.int32)) self.assigned_gt_ignores = Tensor( np.array(-1 * np.ones(num_bboxes), dtype=np.int32)) self.assigned_pos_ones = Tensor( np.array(np.ones(self.num_expected_pos), dtype=np.int32)) self.gt_ignores = Tensor( np.array(-1 * np.ones(self.num_gts), dtype=np.int32)) self.range_pos_size = Tensor( np.arange(self.num_expected_pos).astype(np.float16)) self.check_neg_mask = Tensor( np.array(np.ones(self.num_expected_neg - self.num_expected_pos), dtype=np.bool)) self.bboxs_neg_mask = Tensor( np.zeros((self.num_expected_neg, 4), dtype=np.float16)) self.labels_neg_mask = Tensor( np.array(np.zeros(self.num_expected_neg), dtype=np.uint8)) self.reshape_shape_pos = (self.num_expected_pos, 1) self.reshape_shape_neg = (self.num_expected_neg, 1) self.scalar_zero = Tensor(0.0, dtype=mstype.float16) self.scalar_neg_iou_thr = Tensor(self.neg_iou_thr, dtype=mstype.float16) self.scalar_pos_iou_thr = Tensor(self.pos_iou_thr, dtype=mstype.float16) self.scalar_min_pos_iou = Tensor(self.min_pos_iou, dtype=mstype.float16) self.expand_dims = P.ExpandDims() self.split = P.Split(axis=1, output_num=4) self.concat_last_axis = P.Concat(axis=-1) self.round = P.Round() self.image_h_w = Tensor( [cfg.img_height, cfg.img_width, cfg.img_height, cfg.img_width], dtype=mstype.float16) self.range = nn.Range(start=0, limit=cfg.num_expected_pos_stage2) self.crop_and_resize = P.CropAndResize(method="bilinear_v2") self.mask_shape = (cfg.mask_shape[0], cfg.mask_shape[1]) self.squeeze_mask_last = P.Squeeze(axis=-1)
def __init__(self, mode): super(NetIOU, self).__init__() self.encode = P.IOU(mode=mode)