def __init__(self, H, W): super(STN, self).__init__() batch_size = 1 x = np.linspace(-1.0, 1.0, H) y = np.linspace(-1.0, 1.0, W) x_t, y_t = np.meshgrid(x, y) x_t = Tensor(x_t, mindspore.float32) y_t = Tensor(y_t, mindspore.float32) expand_dims = P.ExpandDims() x_t = expand_dims(x_t, 0) y_t = expand_dims(y_t, 0) flatten = P.Flatten() x_t_flat = flatten(x_t) y_t_flat = flatten(y_t) oneslike = P.OnesLike() ones = oneslike(x_t_flat) concat = P.Concat() sampling_grid = concat((x_t_flat, y_t_flat, ones)) self.sampling_grid = expand_dims(sampling_grid, 0) batch_size = 128 batch_idx = np.arange(batch_size) batch_idx = batch_idx.reshape((batch_size, 1, 1)) self.batch_idx = Tensor(batch_idx, mindspore.float32) self.zero = Tensor(np.zeros([]), mindspore.float32)
def __init__(self, scale=1.0, shift=0.0, name='ScalarAffine'): """ Constructor of ScalarAffine Bijector. """ param = dict(locals()) validator.check_value_type( 'scale', scale, [int, float], type(self).__name__) validator.check_value_type( 'shift', shift, [int, float], type(self).__name__) super(ScalarAffine, self).__init__( is_constant_jacobian=True, is_injective=True, name=name, dtype=None, param=param) self._scale = cast_to_tensor(scale) self._shift = cast_to_tensor(shift) self.abs = P.Abs() self.oneslike = P.OnesLike() self.dtypeop = P.DType() self.cast = P.Cast() self.log = log_generic
def __init__(self, num_sampled, num_classes, num_true=1, sampled_values=None, remove_accidental_hits=True, seed=0, reduction='none'): super(SampledSoftmaxLoss, self).__init__() self.num_sampled = num_sampled self.num_classes = num_classes self.num_true = num_true self.sampled_values = sampled_values self.remove_accidental_hits = remove_accidental_hits self.seed = seed self.sampler = P.UniformSampler( num_true, num_sampled, True, num_classes, seed, remove_accidental_hits) self.cast = P.Cast() self.reshape = P.Reshape() self.shape = P.Shape() self.exp = P.Exp() self.log = P.Log() self.slice_op = P.Slice() self.matmul = P.MatMul(False, True) self.gather_v2 = P.GatherV2() self.reduce_max_true = P.ReduceMax(True) self.reduce_sum = P.ReduceSum() self.reduce_sum_true = P.ReduceSum(True) self.concat_dim0 = P.Concat(0) self.concat_dim1 = P.Concat(1) self.ones_like = P.OnesLike() self.zeros_like = P.ZerosLike() self.mul = P.Mul() self.expand_dims = P.ExpandDims()
def __init__(self, weight=None, reduction='none'): super(BCELoss, self).__init__() self.binary_cross_entropy = P.BinaryCrossEntropy(reduction=reduction) self.weight_one = weight is None if not self.weight_one: self.weight = weight else: self.ones = P.OnesLike()
def __init__(self, num_sampled, num_classes, num_true=1, sampled_values=None, remove_accidental_hits=True, seed=0, reduction='none'): super(SampledSoftmaxLoss, self).__init__(reduction) if num_true < 1: raise ValueError(f"num_true {num_true} is less than 1.") if seed < 0: raise ValueError(f"seed {seed} is less than 0.") if num_sampled > num_classes: raise ValueError( f"num_sampled {num_sampled} is great than num_classes {num_classes}." ) if num_true > num_classes: raise ValueError( f"num_true {num_true} is great than num_classes {num_classes}." ) if sampled_values is not None: if not isinstance(sampled_values, (list, tuple)): raise TypeError( f"sampled_values {sampled_values} is not a list.") if len(sampled_values) != 3: raise ValueError( f"sampled_values size {len(sampled_values)} is not 3.") self.num_sampled = num_sampled self.num_classes = num_classes self.num_true = num_true self.sampled_values = sampled_values self.remove_accidental_hits = remove_accidental_hits self.seed = seed self.sampler = P.LogUniformCandidateSampler(num_true, num_sampled, True, num_classes, seed) self.cast = P.Cast() self.reshape = P.Reshape() self.shape = P.Shape() self.exp = P.Exp() self.log = P.Log() self.slice_op = P.Slice() self.matmul = P.MatMul(False, True) self.gather_v2 = P.Gather() self.reduce_max_true = P.ReduceMax(True) self.reduce_sum = P.ReduceSum() self.reduce_sum_true = P.ReduceSum(True) self.concat_dim0 = P.Concat(0) self.concat_dim1 = P.Concat(1) self.ones_like = P.OnesLike() self.zeros_like = P.ZerosLike() self.mul = P.Mul() self.expand_dims = P.ExpandDims() self.dtype = P.DType() self.compute_accidental_hits = P.ComputeAccidentalHits(num_true) self.scatter_nd = P.ScatterNd()
def __init__(self, reduction='mean', weight=None, pos_weight=None): super(BCEWithLogitsLoss, self).__init__() self.bce_with_logits_loss = P.BCEWithLogitsLoss(reduction=reduction) if isinstance(weight, Parameter): raise TypeError(f"For {self.cls_name}, weight can not be Parameter.") if isinstance(pos_weight, Parameter): raise TypeError(f"For {self.cls_name}, pos_weight can not be Parameter.") self.weight = weight self.pos_weight = pos_weight self.ones = P.OnesLike()
def __init__(self): super(CEWithIgnoreIndex3D, self).__init__() self.exp = P.Exp() self.sum = P.ReduceSum() self.reshape = P.Reshape() self.log = P.Log() self.cast = P.Cast() self.eps_const = Tensor(eps, dtype=mstype.float32) self.ones = P.OnesLike() self.onehot = P.OneHot() self.on_value = Tensor(1.0, mstype.float32) self.off_value = Tensor(0.0, mstype.float32) self.relu = P.ReLU() self.resum = P.ReduceSum(keep_dims=False)
def __init__(self, scale=1.0, shift=0.0, name='ScalarAffine'): """ Constructor of scalar affine bijector. """ param = dict(locals()) validator.check_value_type('scale', scale, [float], name) validator.check_value_type('shift', shift, [float], name) self._scale = cast_to_tensor(scale) self._shift = cast_to_tensor(shift) super(ScalarAffine, self).__init__(is_constant_jacobian=True, is_injective=True, name=name, dtype=None, param=param) self.log = P.Log() self.oneslike = P.OnesLike()
def __init__( self, d_min=1e-3, d_max=1.0, num_rbf=32, sigma=None, trainable=False, min_cutoff=False, max_cutoff=False, ): super().__init__() if d_max <= d_min: raise ValueError( 'The argument "d_max" must be larger' + 'than the argument "d_min" in LogGaussianDistribution!') if d_min <= 0: raise ValueError('The argument "d_min" must be ' + ' larger than 0 in LogGaussianDistribution!') self.d_max = d_max self.d_min = d_min / d_max self.min_cutoff = min_cutoff self.max_cutoff = max_cutoff self.log = P.Log() self.exp = P.Exp() self.max = P.Maximum() self.min = P.Minimum() self.zeroslike = P.ZerosLike() self.oneslike = P.OnesLike() # linspace = nn.LinSpace(log_dmin,0,n_gaussians) log_dmin = math.log(self.d_min) # self.centers = linspace() # self.ones = self.oneslike(self.centers) centers = np.linspace(log_dmin, 0, num_rbf) self.centers = Tensor(centers, ms.float32) ones = np.ones_like(centers) self.ones = Tensor(ones, ms.float32) if sigma is None: sigma = -log_dmin / (num_rbf - 1) self.rescale = -0.5 / (sigma * sigma)
def __init__(self, config, num_hidden_layers, attn_embed_dim, num_attn_heads=12, seq_length=64, intermediate_size=3072, attn_dropout_prob=0.1, initializer_range=0.02, hidden_dropout_prob=0.1, hidden_act="relu", compute_type=mstype.float32, embedding_lookup=None, positional_embedding=None, projection=None): super(TransformerDecoderStep, self).__init__(auto_prefix=False) self.embedding_lookup = embedding_lookup self.positional_embedding = positional_embedding self.projection = projection self.seq_length = seq_length self.decoder = TransformerDecoder(attn_embed_dim=attn_embed_dim, num_attn_heads=num_attn_heads, decoder_layers=num_hidden_layers, intermediate_size=intermediate_size, attn_dropout_prob=attn_dropout_prob, initializer_range=initializer_range, dropout_prob=hidden_dropout_prob, hidden_act=hidden_act, compute_type=compute_type) self.ones_like = P.OnesLike() self.shape = P.Shape() self._create_attention_mask_from_input_mask = CreateAttentionMaskFromInputMask( config) self.expand = P.ExpandDims() self.multiply = P.Mul() ones = np.ones(shape=(seq_length, seq_length)) self.future_mask = Tensor(np.tril(ones), dtype=mstype.float32) self.cast_compute_type = SaturateCast(dst_type=compute_type) self.scale = Tensor([math.sqrt(float(attn_embed_dim))], dtype=mstype.float32)
def __init__(self, scale=1.0, shift=0.0, name='ScalarAffine'): """ Constructor of ScalarAffine Bijector. """ param = dict(locals()) param['param_dict'] = {'scale': scale, 'shift': shift} super(ScalarAffine, self).__init__(is_constant_jacobian=True, is_injective=True, name=name, dtype=None, param=param) self._scale = self._add_parameter(scale, 'scale') self._shift = self._add_parameter(shift, 'shift') self.abs = P.Abs() self.oneslike = P.OnesLike() self.dtypeop = P.DType() self.cast = P.Cast() self.log = log_generic
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
def __init__(self, config, use_one_hot_embeddings, compute_type=mstype.float32): super(BeamDecoderStep, self).__init__(auto_prefix=True) self.vocab_size = config.vocab_size self.word_embed_dim = config.hidden_size self.embedding_lookup = EmbeddingLookup( is_training=False, vocab_size=config.vocab_size, embed_dim=self.word_embed_dim, use_one_hot_embeddings=use_one_hot_embeddings) self.projection = PredLogProbs(batch_size=config.batch_size * config.beam_width, seq_length=1, width=config.vocab_size, compute_type=config.compute_type) self.seq_length = config.max_decode_length self.decoder = GNMTDecoder(config, is_training=False, infer_beam_width=config.beam_width) self.ones_like = P.OnesLike() self.shape = P.Shape() self.create_att_paddings_from_input_paddings = CreateAttentionPaddingsFromInputPaddings( config, is_training=False) self.expand = P.ExpandDims() self.multiply = P.Mul() ones = np.ones(shape=(config.max_decode_length, config.max_decode_length)) self.future_mask = Tensor(np.tril(ones), dtype=mstype.float32) self.cast_compute_type = SaturateCast(dst_type=compute_type) self.transpose = P.Transpose() self.transpose_orders = (1, 0, 2)
def construct(self, x, mean, variance, global_step): if self.is_gpu: if self.training: batch_mean, batch_std, running_mean, running_std = self.bn_train( x, mean, variance, global_step) else: batch_mean, batch_std, running_mean, running_std = self.bn_infer( x, mean, variance, global_step) else: if self.training: x_sum, x_square_sum = self.bn_reduce(x) _, batch_mean, batch_std, running_mean, running_std, mean_updated, variance_updated = \ self.bn_update(x, x_sum, x_square_sum, mean, variance) P.Assign()(mean, mean_updated) P.Assign()(variance, variance_updated) else: batch_mean = P.ZerosLike()(variance) batch_std = P.OnesLike()(variance) running_mean = P.TensorAdd()(mean, 0.) running_std = P.Sqrt()(P.TensorAdd()(variance, self.epsilon)) return batch_mean, batch_std, running_mean, running_std
def __init__(self): super(ComputeDescriptor, self).__init__() self.reshape = P.Reshape() self.transpose = P.Transpose() self.cast = P.Cast() self.rsum = P.ReduceSum() self.broadcastto = P.BroadcastTo((1, 192 * 138)) self.broadcastto1 = P.BroadcastTo((1, 192, 138, 3)) self.broadcastto2 = P.BroadcastTo((1, 192, 138, 3, 3)) self.broadcastto3 = P.BroadcastTo((1, 192, 138, 4)) self.broadcastto4 = P.BroadcastTo((1, 192, 138, 4, 3)) self.expdims = P.ExpandDims() self.concat = P.Concat(axis=3) self.gather = P.GatherV2() self.mul = P.Mul() self.slice = P.Slice() self.square = P.Square() self.inv = P.Inv() self.sqrt = P.Sqrt() self.ones = P.OnesLike() self.eye = P.Eye()
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, dim_atom_embed, num_rbf, n_heads=8, activation=Swish(), max_cycles=10, time_embedding=0, use_pondering=True, fixed_cycles=False, use_filter=True, inside_filter=None, act_threshold=0.9, fixed_neigh=False, ): super().__init__(gather_dim=dim_atom_embed, fixed_neigh=fixed_neigh) if dim_atom_embed % n_heads != 0: raise ValueError('The term "dim_atom_embed" cannot be divisible ' + 'by the term "n_heads" in AirNetIneteraction! ') self.n_heads = n_heads self.max_cycles = max_cycles self.dim_atom_embed = dim_atom_embed self.num_rbf = num_rbf self.time_embedding = time_embedding if fixed_cycles: self.flexable_cycels = False else: self.flexable_cycels = True self.use_filter = use_filter if self.use_filter: # self.filter = Filter(num_rbf,dim_atom_embed,activation) self.filter = Dense(num_rbf, dim_atom_embed, has_bias=True, activation=None) self.positional_embedding = PositionalEmbedding(dim_atom_embed) self.multi_head_attention = MultiheadAttention(dim_atom_embed, n_heads) self.act_threshold = act_threshold self.act_epsilon = 1.0 - act_threshold self.use_pondering = use_pondering self.pondering = None self.act_weight = None if self.max_cycles > 1: if self.use_pondering: self.pondering = Pondering(dim_atom_embed * 3, bias_const=3) self.act_weight = ACTWeight(self.act_threshold) else: if self.flexable_cycels: raise ValueError( 'The term "fixed_cycles" must be True ' + 'when the pondering network is None in AirNetIneteraction! ' ) self.fixed_weight = Tensor(1.0 / max_cycles, ms.float32) self.max = P.Maximum() self.min = P.Minimum() self.concat = P.Concat(-1) self.pack = P.Pack() self.reducesum = P.ReduceSum() self.squeeze = P.Squeeze(-1) self.ones_like = P.OnesLike() self.zeros_like = P.ZerosLike() self.zeros = P.Zeros()
'skip': ['backward']}), ('DescConst', { 'block': Tensor(np.array([2], np.float32)), 'desc_inputs': [], 'desc_bprop': [[1]], 'skip': ['backward'], 'add_fake_input': True}), ('Fill', { 'block': P.Fill(), 'desc_const': [mstype.float32, (2, 3), 1.0], 'desc_inputs': [], 'desc_bprop': [[2, 3]], 'skip': ['backward'], 'add_fake_input': True}), ('OnesLike', { 'block': P.OnesLike(), 'desc_inputs': [Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32))], 'desc_bprop': [Tensor(np.array([[1, 1], [1, 1]]).astype(np.int32))] }), ('ZerosLike', { 'block': P.ZerosLike(), 'desc_inputs': [Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32))], 'desc_bprop': [Tensor(np.array([[1, 1], [1, 1]]).astype(np.int32))] }), ('Softmax', { 'block': P.Softmax(), 'desc_inputs': [[5, 5]], 'desc_bprop': [[5, 5]]}), ('DepthwiseConv2dNative_1', { 'block': P.DepthwiseConv2dNative(3, (3, 3), pad_mode="pad", pad=1, stride=2), 'desc_inputs': [[10, 32, 32, 32], [1, 32, 3, 3]],
def __init__(self, strategy1, strategy2): super().__init__() self.matmul = P.MatMul().set_strategy(strategy1) self.oneslike = P.OnesLike().set_strategy(strategy2) self.matmul2 = P.MatMul().set_strategy(strategy1)
def __init__(self, shape, threshold=0.9): super().__init__() self.threshold = threshold self.zeros_like = P.ZerosLike() self.ones_like = P.OnesLike()
reduce_sum = ops.ReduceSum(keep_dims=False) square = ops.Square() argmin = ops.Argmin() centroid_matrix = reshape(tile(centroids, (num_pts, 1)), (num_pts, k, num_feats)) point_matrix = reshape(tile(data_points, (1, k)), (num_pts, k, num_feats)) distances = reduce_sum(square(point_matrix - centroid_matrix), 2) centroid_group = argmin(distances) return centroid_group # 计算三个堆的平均距离更新堆中新的中心点 unsorted_segment_sum = ops.UnsortedSegmentSum() ones_like = ops.OnesLike() def data_group_avg(group_ids, data): # 分组求和 sum_total = unsorted_segment_sum(data, group_ids, 3) #计算堆大小 num_total = unsorted_segment_sum(ones_like(data), group_ids, 3) #求距离均值 avg_by_group = sum_total / num_total return avg_by_group assign = ops.Assign() # 遍历循环训练,更新每组分类的中心点 for i in range(generations):
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()
def __init__(self): super(NetOnesLike, self).__init__() self.ones_like = P.OnesLike()
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)