def __init__(self): super(NetArgmaxWithValue, self).__init__() axis1 = 0 axis2 = -1 self.argmax1 = P.ArgMaxWithValue(axis1) self.argmax2 = P.ArgMaxWithValue(axis2) self.argmax3 = P.ArgMaxWithValue()
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()
def __init__(self, tag_to_index, batch_size=1, seq_length=128, is_training=True): super(CRF, self).__init__() self.target_size = len(tag_to_index) self.is_training = is_training self.tag_to_index = tag_to_index self.batch_size = batch_size self.seq_length = seq_length self.START_TAG = "<START>" self.STOP_TAG = "<STOP>" self.START_VALUE = Tensor(self.target_size-2, dtype=mstype.int32) self.STOP_VALUE = Tensor(self.target_size-1, dtype=mstype.int32) transitions = np.random.normal(size=(self.target_size, self.target_size)).astype(np.float32) transitions[tag_to_index[self.START_TAG], :] = -10000 transitions[:, tag_to_index[self.STOP_TAG]] = -10000 self.transitions = Parameter(Tensor(transitions), name="transition_matrix") self.cat = P.Concat(axis=-1) self.argmax = P.ArgMaxWithValue(axis=-1) self.log = P.Log() self.exp = P.Exp() self.sum = P.ReduceSum() self.tile = P.Tile() self.reduce_sum = P.ReduceSum(keep_dims=True) self.reshape = P.Reshape() self.expand = P.ExpandDims() self.mean = P.ReduceMean() init_alphas = np.ones(shape=(self.batch_size, self.target_size)) * -10000.0 init_alphas[:, self.tag_to_index[self.START_TAG]] = 0. self.init_alphas = Tensor(init_alphas, dtype=mstype.float32) self.cast = P.Cast() self.reduce_max = P.ReduceMax(keep_dims=True) self.on_value = Tensor(1.0, dtype=mstype.float32) self.off_value = Tensor(0.0, dtype=mstype.float32) self.onehot = P.OneHot()
def __init__(self, backbone, generate=False): super(EvalNet, self).__init__(auto_prefix=False) self.backbone = backbone self.argmax = P.ArgMaxWithValue() self.generate = generate self.topk = P.TopK(sorted=True).shard(((1, 1),)) self.log_softmax = P.Softmax(axis=-1)
def __init__(self, config): super(CrossEntropyLoss, self).__init__() self.mean = P.ReduceMean() self.sum = P.ReduceSum().shard(((config.dp, config.mp),)) self.onehot = P.OneHot().shard(((config.dp, config.mp), (), ())) self.on_value = Tensor(1.0, mstype.float32) self.off_value = Tensor(0.0, mstype.float32) self.vocab_size = config.vocab_size self.max = P.ArgMaxWithValue(axis=-1, keep_dims=True).shard( ((config.dp, config.mp),)) self.eps_const = Tensor(1e-24, mstype.float32) self.sub = P.Sub().shard(((config.dp, config.mp), (config.dp, 1))) self.exp = P.Exp().shard(((config.dp, config.mp),)) self.div = P.RealDiv().shard(((config.dp, config.mp), (config.dp, 1))) self.log = P.Log().shard(((config.dp, config.mp),)) self.add = P.TensorAdd().shard(((config.dp, config.mp), ())) self.mul = P.Mul().shard( ((config.dp, config.mp), (config.dp, config.mp))) self.neg = P.Neg().shard(((config.dp, config.mp),)) self.sum2 = P.ReduceSum().shard(((1,),)) self.mul2 = P.Mul().shard(((1,), (1,))) self.add2 = P.TensorAdd() self.div2 = P.RealDiv() if config.stage_num > 1: self.div2.add_prim_attr("end", 0)
def __init__(self, network): super(GRUInferCell, self).__init__(auto_prefix=False) self.network = network self.argmax = P.ArgMaxWithValue(axis=2) self.transpose = P.Transpose() self.teacher_force = Tensor(np.zeros((config.eval_batch_size)), mstype.bool_)
def __init__(self, probs=None, seed=None, dtype=mstype.int32, name="Categorical"): param = dict(locals()) param['param_dict'] = {'probs': probs} valid_dtype = mstype.uint_type + mstype.int_type + mstype.float_type Validator.check_type_name("dtype", dtype, valid_dtype, type(self).__name__) super(Categorical, self).__init__(seed, dtype, name, param) self._probs = self._add_parameter(probs, 'probs') if self.probs is not None: check_rank(self.probs) check_prob(self.probs) check_sum_equal_one(probs) # update is_scalar_batch and broadcast_shape # drop one dimension if self.probs.shape[:-1] == (): self._is_scalar_batch = True self._broadcast_shape = self._broadcast_shape[:-1] self.argmax = P.ArgMaxWithValue(axis=-1) self.broadcast = broadcast_to self.cast = P.Cast() self.clip_by_value = C.clip_by_value self.concat = P.Concat(-1) self.cumsum = P.CumSum() self.dtypeop = P.DType() self.exp = exp_generic self.expand_dim = P.ExpandDims() self.fill = P.Fill() self.gather = P.GatherNd() self.greater = P.Greater() self.issubclass = P.IsSubClass() self.less = P.Less() self.log = log_generic self.log_softmax = P.LogSoftmax() self.logicor = P.LogicalOr() self.logicand = P.LogicalAnd() self.multinomial = P.Multinomial(seed=self.seed) self.reshape = P.Reshape() self.reduce_sum = P.ReduceSum(keep_dims=True) self.select = P.Select() self.shape = P.Shape() self.softmax = P.Softmax() self.squeeze = P.Squeeze() self.squeeze_first_axis = P.Squeeze(0) self.squeeze_last_axis = P.Squeeze(-1) self.square = P.Square() self.transpose = P.Transpose() self.is_nan = P.IsNan() self.index_type = mstype.int32 self.nan = np.nan
def __init__(self, backbone, config): super(WithLossCell, self).__init__(auto_prefix=False) self._backbone = backbone self.batch_size = config.batch_size self.onehot = nn.OneHot(depth=config.ch_vocab_size) self._loss_fn = NLLLoss() self.max_len = config.max_seq_length self.squeeze = P.Squeeze() self.cast = P.Cast() self.argmax = P.ArgMaxWithValue(axis=1, keep_dims=True) self.print = P.Print()
def __init__(self): super(openpose_loss, self).__init__() self.expand_dims = P.ExpandDims() self.tile = P.Tile() self.mul = P.Mul() self.l2_loss = P.L2Loss() self.square = P.Square() self.reduceMean = P.ReduceMean() self.reduceSum = P.ReduceSum() self.print = P.Print() self.shape = P.Shape() self.maxoftensor = P.ArgMaxWithValue(-1)
def __init__(self, config, is_train=True): super(Seq2Seq, self).__init__() self.max_len = config.max_seq_length self.is_train = is_train self.encoder = Encoder(config, is_train) self.decoder = Decoder(config, is_train) self.expanddims = P.ExpandDims() self.squeeze = P.Squeeze(axis=0) self.argmax = P.ArgMaxWithValue(axis=int(2), keep_dims=True) self.concat = P.Concat(axis=1) self.concat2 = P.Concat(axis=0) self.select = P.Select()
def __init__(self, config, batch_size, num_classes, target_means=(0., 0., 0., 0.), target_stds=(0.1, 0.1, 0.2, 0.2)): super(RcnnCls, self).__init__() cfg = config self.rcnn_loss_cls_weight = Tensor( np.array(cfg.rcnn_loss_cls_weight).astype(np.float16)) self.rcnn_loss_reg_weight = Tensor( np.array(cfg.rcnn_loss_reg_weight).astype(np.float16)) self.rcnn_fc_out_channels = cfg.rcnn_fc_out_channels self.target_means = target_means self.target_stds = target_stds self.num_classes = num_classes self.in_channels = cfg.rcnn_in_channels self.train_batch_size = batch_size self.test_batch_size = cfg.test_batch_size self.fpn_cls = FpnCls(self.in_channels, self.rcnn_fc_out_channels, self.num_classes, cfg.roi_layer["out_size"]) self.relu = P.ReLU() self.logicaland = P.LogicalAnd() self.loss_cls = P.SoftmaxCrossEntropyWithLogits() self.loss_bbox = P.SmoothL1Loss(beta=1.0) self.loss_mask = P.SigmoidCrossEntropyWithLogits() self.reshape = P.Reshape() self.onehot = P.OneHot() self.greater = P.Greater() self.cast = P.Cast() self.sum_loss = P.ReduceSum() self.tile = P.Tile() self.expandims = P.ExpandDims() self.gather = P.GatherNd() self.argmax = P.ArgMaxWithValue(axis=1) self.on_value = Tensor(1.0, mstype.float32) self.off_value = Tensor(0.0, mstype.float32) self.value = Tensor(1.0, mstype.float16) self.num_bboxes = (cfg.num_expected_pos_stage2 + cfg.num_expected_neg_stage2) * batch_size rmv_first = np.ones((self.num_bboxes, self.num_classes)) rmv_first[:, 0] = np.zeros((self.num_bboxes, )) self.rmv_first_tensor = Tensor(rmv_first.astype(np.float16)) self.num_bboxes_test = cfg.rpn_max_num * cfg.test_batch_size
def __init__(self, config, is_training, num_labels=2, dropout_prob=0.0, use_one_hot_embeddings=False): super(BertSquad, self).__init__() self.bert = BertSquadModel(config, is_training, num_labels, dropout_prob, use_one_hot_embeddings) self.loss = CrossEntropyCalculation(is_training) self.num_labels = num_labels self.seq_length = config.seq_length self.is_training = is_training self.total_num = Parameter(Tensor([0], mstype.float32)) self.start_num = Parameter(Tensor([0], mstype.float32)) self.end_num = Parameter(Tensor([0], mstype.float32)) self.sum = P.ReduceSum() self.equal = P.Equal() self.argmax = P.ArgMaxWithValue(axis=1) self.squeeze = P.Squeeze(axis=-1)
def __init__(self, config, is_training=True): super(Seq2Seq, self).__init__() if is_training: self.batch_size = config.batch_size else: self.batch_size = config.eval_batch_size self.encoder = Encoder(config, is_training=is_training) self.decoder = Decoder(config, is_training=is_training) self.expandims = P.ExpandDims() self.dropout = nn.Dropout() self.shape = P.Shape() self.concat = P.Concat(axis=0) self.argmax = P.ArgMaxWithValue(axis=1, keep_dims=True) self.squeeze = P.Squeeze(axis=0) self.sos = Tensor(np.ones((self.batch_size, 1)) * 2, mstype.int32) self.select = P.Select() self.text_len = config.max_length
def __init__(self, temperature=0.07, contrast_mode='all', base_temperature=0.07): super(SupConLoss, self).__init__() self.temperature = temperature self.contrast_mode = contrast_mode self.base_temperature = base_temperature self.normalize = P.L2Normalize(axis=2) self.eye = P.Eye() self.unbind = P.Unstack(axis=1) self.cat = P.Concat(axis=0) self.matmul = P.MatMul() self.div = P.Div() self.transpose = P.Transpose() self.maxes = P.ArgMaxWithValue(axis=1, keep_dims=True) self.tile = P.Tile() self.scatter = P.ScatterNd() self.oneslike = P.OnesLike() self.exp = P.Exp() self.sum = P.ReduceSum(keep_dims=True) self.log = P.Log() self.reshape = P.Reshape() self.mean = P.ReduceMean()
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)
('Split', { 'block': P.Split(1, 2), 'desc_inputs': [Tensor(np.array([[1, 1, 1, 1], [2, 2, 2, 2]]))], 'skip': ['backward']}), ('Argmax', { 'block': P.Argmax(), 'desc_inputs': [[128, 32, 32, 64]], 'desc_bprop': [0], 'skip': ['backward']}), ('Argmin', { 'block': P.Argmin(), 'desc_inputs': [[128, 32, 32, 64]], 'desc_bprop': [1], 'skip': ['backward']}), ('ArgMaxWithValue', { 'block': P.ArgMaxWithValue(), 'desc_inputs': [[128, 32, 32, 64]], 'desc_bprop': [[1], [1]], 'skip': ['backward']}), ('ArgMinWithValue', { 'block': P.ArgMinWithValue(), 'desc_inputs': [[128, 32, 32, 64]], 'desc_bprop': [[1], [1]], 'skip': ['backward']}), ('Transpose_dim3', { 'block': P.Transpose(), 'desc_const': [(0, 2, 1)], 'desc_inputs': [[1, 2, 3]], 'desc_bprop': [[1, 3, 2]]}), ('Transpose_dim4', { 'block': P.Transpose(),
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)
def __init__(self, network): super(FastTextInferCell, self).__init__(auto_prefix=False) self.network = network self.argmax = P.ArgMaxWithValue(axis=1, keep_dims=True) self.log_softmax = nn.LogSoftmax(axis=1)
def read_img(img_path): img_ori = cv2.imread(img_path) img = cv2.cvtColor(img_ori, cv2.COLOR_BGR2RGB) img = cv2.resize(img, (96, 96)) img = img.transpose(2, 0, 1) img = np.array([img]).astype(np.float32)/255. img = Tensor(img) return img, img_ori blur_soft = nn.Softmax(0) kps_soft = nn.Softmax(-1) reshape = P.Reshape() argmax = P.ArgMaxWithValue() def test_trains(args): '''test trains''' print('----eval----begin----') model_path = args.pretrained result_file = model_path.replace('.ckpt', '.txt') if os.path.exists(result_file): os.remove(result_file) epoch_result = open(result_file, 'a') epoch_result.write(model_path + '\n') network = FaceQABackbone() ckpt_path = model_path
def __init__(self, axis=-1): super(Softmax, self).__init__() self.max = P.ArgMaxWithValue(axis=axis, keep_dims=True) self.sum = P.ReduceSum(keep_dims=True) self.axis = axis
def __init__(self, axis, keep_dims): super().__init__() self.op = op.ArgMaxWithValue(axis=axis, keep_dims=keep_dims)
def __init__(self, axis=0): super(NetArgmaxWithValueBig, self).__init__() self.argmax = P.ArgMaxWithValue(axis)
def __init__(self, config, representation_size, batch_size, num_classes, target_means=(0., 0., 0., 0.), target_stds=(0.1, 0.1, 0.2, 0.2)): super(Rcnn, self).__init__() cfg = config self.rcnn_loss_cls_weight = Tensor( np.array(cfg.rcnn_loss_cls_weight).astype(np.float16)) self.rcnn_loss_reg_weight = Tensor( np.array(cfg.rcnn_loss_reg_weight).astype(np.float16)) self.rcnn_fc_out_channels = cfg.rcnn_fc_out_channels self.target_means = target_means self.target_stds = target_stds self.num_classes = num_classes self.in_channels = cfg.rcnn_in_channels self.train_batch_size = batch_size self.test_batch_size = cfg.test_batch_size self.use_ambigous_sample = cfg.use_ambigous_sample shape_0 = (self.rcnn_fc_out_channels, representation_size) weights_0 = initializer("XavierUniform", shape=shape_0[::-1], dtype=mstype.float16).to_tensor() shape_1 = (self.rcnn_fc_out_channels, self.rcnn_fc_out_channels) weights_1 = initializer("XavierUniform", shape=shape_1[::-1], dtype=mstype.float16).to_tensor() self.shared_fc_0 = DenseNoTranpose(representation_size, self.rcnn_fc_out_channels, weights_0) self.shared_fc_1 = DenseNoTranpose(self.rcnn_fc_out_channels, self.rcnn_fc_out_channels, weights_1) cls_weight = initializer( 'Normal', shape=[num_classes, self.rcnn_fc_out_channels][::-1], dtype=mstype.float16).to_tensor() reg_weight = initializer( 'Normal', shape=[num_classes * 4, self.rcnn_fc_out_channels][::-1], dtype=mstype.float16).to_tensor() self.cls_scores = DenseNoTranpose(self.rcnn_fc_out_channels, num_classes, cls_weight) self.reg_scores = DenseNoTranpose(self.rcnn_fc_out_channels, num_classes * 4, reg_weight) self.flatten = P.Flatten() self.relu = P.ReLU() self.logicaland = P.LogicalAnd() self.loss_cls = P.SoftmaxCrossEntropyWithLogits() self.loss_bbox = P.SmoothL1Loss(beta=1.0) self.reshape = P.Reshape() self.onehot = P.OneHot() self.greater = P.Greater() self.equal = P.Equal() self.cast = P.Cast() self.sum_loss = P.ReduceSum() self.tile = P.Tile() self.expandims = P.ExpandDims() self.gather = P.GatherNd() self.argmax = P.ArgMaxWithValue(axis=1) self.on_value = Tensor(1.0, mstype.float32) self.off_value = Tensor(0.0, mstype.float32) self.value = Tensor(1.0, mstype.float16) self.num_bboxes = (cfg.num_expected_pos_stage2 + cfg.num_expected_neg_stage2) * batch_size if self.use_ambigous_sample: self.num_bboxes = (cfg.num_expected_pos_stage2 + cfg.num_expected_amb_stage2 + cfg.num_expected_neg_stage2) * batch_size rmv_first = np.ones((self.num_bboxes, self.num_classes)) rmv_first[:, 0] = np.zeros((self.num_bboxes, )) self.rmv_first_tensor = Tensor(rmv_first.astype(np.float16)) self.num_bboxes_test = cfg.rpn_max_num * cfg.test_batch_size range_max = np.arange(self.num_bboxes_test).astype(np.int32) self.range_max = Tensor(range_max)
def __init__(self, strategy1, strategy2, strategy3): super().__init__() self.mul1 = P.Mul().set_strategy(strategy1) self.arg_max_with_value = P.ArgMaxWithValue( keep_dims=False, axis=-1).set_strategy(strategy2) self.mul2 = P.Mul().set_strategy(strategy3)
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)