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(RcnnMask, self).__init__() cfg = config self.rcnn_loss_mask_fb_weight = Tensor(np.array(cfg.rcnn_loss_mask_fb_weight).astype(np.float16)) self.rcnn_mask_out_channels = cfg.rcnn_mask_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.fpn_mask = FpnMask(self.in_channels, self.rcnn_mask_out_channels, self.num_classes) self.logicaland = P.LogicalAnd() self.loss_mask = P.SigmoidCrossEntropyWithLogits() 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.on_value = Tensor(1.0, mstype.float32) self.off_value = Tensor(0.0, mstype.float32) self.num_bboxes = cfg.num_expected_pos_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.mean_loss = P.ReduceMean()
def __init__(self, beam_width, compute_type=mstype.float32): super(TileBeam, self).__init__() self.beam_width = beam_width self.expand = P.ExpandDims() self.tile = P.Tile() self.reshape = P.Reshape() self.shape = P.Shape()
def construct(self, input_ids, input_mask, layer_past=None): """GPT model""" if not self.use_past: layer_past = self.past input_embedding, embedding_table = self.word_embedding(input_ids) batch_size, seq_length = F.shape(input_ids) input_position = F.tuple_to_array(F.make_range(seq_length)) input_position = P.Tile()(input_position, (batch_size, 1)) position_embedding = self.position_embedding(input_position) hidden_states = input_embedding + position_embedding hidden_states = P.Cast()(hidden_states, mstype.float16) attention_mask = self.get_attention_mask(input_mask) attention_mask = P.ExpandDims()(attention_mask, 1) present_layer = () for i in range(self.num_layers): hidden_states, present = self.blocks[i](hidden_states, attention_mask, layer_past) present_layer = present_layer + (present,) output_state = self.layernorm(hidden_states) return output_state, present_layer, embedding_table
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 construct(self, box_xy, box_wh, box_confidence, box_probs, image_shape): batch_size = F.shape(box_xy)[0] x = box_xy[:, :, :, :, 0:1] y = box_xy[:, :, :, :, 1:2] box_yx = P.Concat(-1)((y, x)) w = box_wh[:, :, :, :, 0:1] h = box_wh[:, :, :, :, 1:2] box_hw = P.Concat(-1)((h, w)) new_shape = P.Round()(image_shape * P.ReduceMin()(self.input_shape / image_shape)) offset = (self.input_shape - new_shape) / 2.0 / self.input_shape scale = self.input_shape / new_shape box_yx = (box_yx - offset) * scale box_hw = box_hw * scale box_min = box_yx - box_hw / 2.0 box_max = box_yx + box_hw / 2.0 boxes = P.Concat(-1)( (box_min[:, :, :, :, 0:1], box_min[:, :, :, :, 1:2], box_max[:, :, :, :, 0:1], box_max[:, :, :, :, 1:2])) image_scale = P.Tile()(image_shape, (1, 2)) boxes = boxes * image_scale boxes = F.reshape(boxes, (batch_size, -1, 4)) boxes_scores = box_confidence * box_probs boxes_scores = F.reshape(boxes_scores, (batch_size, -1, self.num_classes)) return boxes, boxes_scores
def __init__(self, scale, config=ConfigYOLOV3DarkNet53(), is_training=True): super(DetectionBlock, self).__init__() self.config = config if scale == 's': idx = (0, 1, 2) elif scale == 'm': idx = (3, 4, 5) elif scale == 'l': idx = (6, 7, 8) else: raise KeyError("Invalid scale value for DetectionBlock") self.anchors = Tensor([self.config.anchor_scales[i] for i in idx], ms.float32) self.num_anchors_per_scale = 3 self.num_attrib = 4 + 1 + self.config.num_classes self.lambda_coord = 1 self.sigmoid = nn.Sigmoid() self.reshape = P.Reshape() self.tile = P.Tile() self.concat = P.Concat(axis=-1) self.conf_training = is_training
def __init__(self, config): super(WideDeepModel, self).__init__() self.batch_size = config.batch_size self.field_size = config.field_size self.vocab_size = config.vocab_size self.emb_dim = config.emb_dim self.deep_layer_args = config.deep_layer_args self.deep_layer_dims_list, self.deep_layer_act = self.deep_layer_args self.init_args = config.init_args self.weight_init, self.bias_init = config.weight_bias_init self.weight_bias_init = config.weight_bias_init self.emb_init = config.emb_init self.drop_out = config.dropout_flag self.keep_prob = config.keep_prob self.deep_input_dims = self.field_size * self.emb_dim self.layer_dims = self.deep_layer_dims_list + [1] self.all_dim_list = [self.deep_input_dims] + self.layer_dims init_acts = [('Wide_w', [self.vocab_size, 1], self.emb_init), ('V_l2', [self.vocab_size, self.emb_dim], self.emb_init), ('Wide_b', [1], self.emb_init)] var_map = init_var_dict(self.init_args, init_acts) self.wide_w = var_map["Wide_w"] self.wide_b = var_map["Wide_b"] self.embedding_table = var_map["V_l2"] self.dense_layer_1 = DenseLayer(self.all_dim_list[0], self.all_dim_list[1], self.weight_bias_init, self.deep_layer_act, convert_dtype=True) self.dense_layer_2 = DenseLayer(self.all_dim_list[1], self.all_dim_list[2], self.weight_bias_init, self.deep_layer_act, convert_dtype=True) self.dense_layer_3 = DenseLayer(self.all_dim_list[2], self.all_dim_list[3], self.weight_bias_init, self.deep_layer_act, convert_dtype=True) self.dense_layer_4 = DenseLayer(self.all_dim_list[3], self.all_dim_list[4], self.weight_bias_init, self.deep_layer_act, convert_dtype=True) self.dense_layer_5 = DenseLayer(self.all_dim_list[4], self.all_dim_list[5], self.weight_bias_init, self.deep_layer_act, convert_dtype=True) self.gather_v2 = P.GatherV2() self.mul = P.Mul() self.reduce_sum = P.ReduceSum(keep_dims=False) self.reshape = P.Reshape() self.square = P.Square() self.shape = P.Shape() self.tile = P.Tile() self.concat = P.Concat(axis=1) self.cast = P.Cast()
def __init__(self, img_dim, patch_dim, num_channels, embedding_dim, num_heads, num_layers, hidden_dim, num_queries, dropout_rate=0, norm=False, mlp=False, pos_every=False, no_pos=False, con_loss=False): super(VisionTransformer, self).__init__() self.norm = norm self.mlp = mlp self.embedding_dim = embedding_dim self.num_heads = num_heads self.patch_dim = patch_dim self.num_channels = num_channels self.img_dim = img_dim self.pos_every = pos_every self.num_patches = int((img_dim // patch_dim) ** 2) self.seq_length = self.num_patches self.flatten_dim = patch_dim * patch_dim * num_channels self.out_dim = patch_dim * patch_dim * num_channels self.no_pos = no_pos self.unf = _unfold_(patch_dim) self.fold = _fold_(patch_dim, output_shape=(img_dim, img_dim)) if self.mlp is not True: self.linear_encoding = nn.Dense( self.flatten_dim, embedding_dim) self.mlp_head = nn.SequentialCell( nn.Dense(embedding_dim, hidden_dim), nn.Dropout(1. - dropout_rate), nn.ReLU(), nn.Dense(hidden_dim, self.out_dim), nn.Dropout(1. - dropout_rate)) self.query_embed = nn.Embedding( num_queries, embedding_dim * self.seq_length) encoder_layer = TransformerEncoderLayer( embedding_dim, num_heads, hidden_dim, dropout_rate) self.encoder = TransformerEncoder(encoder_layer, num_layers) decoder_layer = TransformerDecoderLayer( embedding_dim, num_heads, hidden_dim, dropout_rate) self.decoder = TransformerDecoder(decoder_layer, num_layers) self.reshape = P.Reshape() self.tile = P.Tile() self.transpose = P.Transpose() if not self.no_pos: self.position_encoding = LearnedPositionalEncoding(self.seq_length, self.embedding_dim, self.seq_length) self.dropout_layer1 = nn.Dropout(1. - dropout_rate) self.con_loss = con_loss
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 __init__(self, config, batch_size, in_channels, feat_channels, num_anchors, cls_out_channels): super(RPN, self).__init__() cfg_rpn = config self.dtype = np.float32 self.ms_type = mstype.float32 self.num_bboxes = cfg_rpn.num_bboxes self.slice_index = () self.feature_anchor_shape = () self.slice_index += (0, ) index = 0 for shape in cfg_rpn.feature_shapes: self.slice_index += (self.slice_index[index] + shape[0] * shape[1] * num_anchors, ) self.feature_anchor_shape += (shape[0] * shape[1] * num_anchors * batch_size, ) index += 1 self.num_anchors = num_anchors self.batch_size = batch_size self.test_batch_size = cfg_rpn.test_batch_size self.num_layers = 5 self.real_ratio = Tensor(np.ones((1, 1)).astype(self.dtype)) self.rpn_convs_list = nn.layer.CellList( self._make_rpn_layer(self.num_layers, in_channels, feat_channels, num_anchors, cls_out_channels)) self.transpose = P.Transpose() self.reshape = P.Reshape() self.concat = P.Concat(axis=0) self.fill = P.Fill() self.placeh1 = Tensor(np.ones((1, )).astype(self.dtype)) self.trans_shape = (0, 2, 3, 1) self.reshape_shape_reg = (-1, 4) self.reshape_shape_cls = (-1, ) self.rpn_loss_reg_weight = Tensor( np.array(cfg_rpn.rpn_loss_reg_weight).astype(self.dtype)) self.rpn_loss_cls_weight = Tensor( np.array(cfg_rpn.rpn_loss_cls_weight).astype(self.dtype)) self.num_expected_total = Tensor( np.array(cfg_rpn.num_expected_neg * self.batch_size).astype( self.dtype)) self.num_bboxes = cfg_rpn.num_bboxes self.get_targets = BboxAssignSample(cfg_rpn, self.batch_size, self.num_bboxes, False) self.CheckValid = P.CheckValid() self.sum_loss = P.ReduceSum() self.loss_cls = P.SigmoidCrossEntropyWithLogits() self.loss_bbox = P.SmoothL1Loss(beta=1.0 / 9.0) self.squeeze = P.Squeeze() self.cast = P.Cast() self.tile = P.Tile() self.zeros_like = P.ZerosLike() self.loss = Tensor(np.zeros((1, )).astype(self.dtype)) self.clsloss = Tensor(np.zeros((1, )).astype(self.dtype)) self.regloss = Tensor(np.zeros((1, )).astype(self.dtype))
def __init__(self, config, batch_size, in_channels, feat_channels, num_anchors, cls_out_channels): super(RPN, self).__init__() cfg_rpn = config self.cfg = config self.num_bboxes = cfg_rpn.num_bboxes self.feature_anchor_shape = cfg_rpn.feature_shapes self.feature_anchor_shape = self.feature_anchor_shape[0] * \ self.feature_anchor_shape[1] * num_anchors * batch_size self.num_anchors = num_anchors self.batch_size = batch_size self.test_batch_size = cfg_rpn.test_batch_size self.num_layers = 1 self.real_ratio = Tensor(np.ones((1, 1)).astype(np.float16)) self.use_sigmoid_cls = config.use_sigmoid_cls if config.use_sigmoid_cls: self.reshape_shape_cls = (-1, ) self.loss_cls = P.SigmoidCrossEntropyWithLogits() cls_out_channels = 1 else: self.reshape_shape_cls = (-1, cls_out_channels) self.loss_cls = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="none") self.rpn_convs_list = self._make_rpn_layer(self.num_layers, in_channels, feat_channels,\ num_anchors, cls_out_channels) self.transpose = P.Transpose() self.reshape = P.Reshape() self.concat = P.Concat(axis=0) self.fill = P.Fill() self.placeh1 = Tensor(np.ones((1, )).astype(np.float16)) self.trans_shape = (0, 2, 3, 1) self.reshape_shape_reg = (-1, 4) self.softmax = nn.Softmax() self.rpn_loss_reg_weight = Tensor( np.array(cfg_rpn.rpn_loss_reg_weight).astype(np.float16)) self.rpn_loss_cls_weight = Tensor( np.array(cfg_rpn.rpn_loss_cls_weight).astype(np.float16)) self.num_expected_total = Tensor( np.array(cfg_rpn.num_expected_neg * self.batch_size).astype( np.float16)) self.num_bboxes = cfg_rpn.num_bboxes self.get_targets = BboxAssignSample(cfg_rpn, self.batch_size, self.num_bboxes, False) self.CheckValid = P.CheckValid() self.sum_loss = P.ReduceSum() self.loss_bbox = P.SmoothL1Loss(beta=1.0 / 9.0) self.squeeze = P.Squeeze() self.cast = P.Cast() self.tile = P.Tile() self.zeros_like = P.ZerosLike() self.loss = Tensor(np.zeros((1, )).astype(np.float16)) self.clsloss = Tensor(np.zeros((1, )).astype(np.float16)) self.regloss = Tensor(np.zeros((1, )).astype(np.float16)) self.print = P.Print()
def __init__(self, weight, weight2, strategy1=None, strategy2=None, is_parameter=True): super().__init__() self.mul = P.Mul().shard(strategy1) self.tile = P.Tile().shard(strategy2) if is_parameter: self.weight = Parameter(weight, "w1") else: self.weight = weight self.mul2 = P.Mul() self.weight2 = Parameter(weight2, "w2")
def __init__(self, network, config): super(SSDWithLossCell, self).__init__() self.network = network self.less = P.Less() self.tile = P.Tile() self.reduce_sum = P.ReduceSum() self.expand_dims = P.ExpandDims() self.class_loss = SigmoidFocalClassificationLoss( config.gamma, config.alpha) self.loc_loss = nn.SmoothL1Loss()
def __init__(self, is_training, query_size, key_size, num_units, normalize=False, initializer_range=0.1, compute_type=mstype.float16): super(BahdanauAttention, self).__init__() self.is_training = is_training self.mask = None self.query_size = query_size self.key_size = key_size self.normalize = normalize self.num_units = num_units self.linear_att = Parameter(Tensor(np.random.uniform( -initializer_range, initializer_range, size=[num_units]), dtype=mstype.float32), name='linear_att') if self.normalize: self.normalize_scalar = Parameter(Tensor(np.array( [1.0 / num_units]), dtype=mstype.float32), name='normalize_scalar') self.normalize_bias = Parameter(Tensor(np.zeros(num_units), dtype=mstype.float32), name='normalize_bias') self.transpose = P.Transpose() self.transpose_orders = (1, 0, 2) self.shape_op = P.Shape() self.linear_q = nn.Dense( query_size, num_units, has_bias=False, weight_init=Uniform(initializer_range)).to_float(compute_type) self.linear_k = nn.Dense( key_size, num_units, has_bias=False, weight_init=Uniform(initializer_range)).to_float(compute_type) self.expand = P.ExpandDims() self.tile = P.Tile() self.norm = nn.Norm(axis=-1) self.mul = P.Mul() self.matmul = P.MatMul() self.batchMatmul = P.BatchMatMul() self.tanh = nn.Tanh() self.matmul_trans_b = P.BatchMatMul(transpose_b=True) self.softmax = nn.Softmax(axis=-1) self.reshape = P.Reshape() self.cast = P.Cast()
def __init__(self, model, args): super(IPT_post, self).__init__() self.model = model self.args = args self.scale_idx = 0 self.reshape = P.Reshape() self.tile = P.Tile() self.transpose = P.Transpose() self.cc_0 = P.Concat(axis=0) self.cc_2 = P.Concat(axis=2) self.cc_3 = P.Concat(axis=3)
def __init__(self, length, max_relative_position): super(RelaPosMatrixGenerator, self).__init__() self._length = length self._max_relative_position = Tensor(max_relative_position, dtype=mstype.int32) self._min_relative_position = Tensor(-max_relative_position, dtype=mstype.int32) self.range_length = -length + 1 self.tile = P.Tile() self.range_mat = P.Reshape() self.sub = P.Sub() self.expanddims = P.ExpandDims() self.cast = P.Cast()
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 construct(self, img1, img2): max_val = _convert_img_dtype_to_float32(self.max_val, self.max_val) img1 = _convert_img_dtype_to_float32(img1, self.max_val) img2 = _convert_img_dtype_to_float32(img2, self.max_val) kernel = self._fspecial_gauss(self.filter_size, self.filter_sigma) kernel = P.Tile()(kernel, (1, P.Shape()(img1)[1], 1, 1)) mean_ssim = self._calculate_mean_ssim(img1, img2, kernel, max_val, self.k1, self.k2) return mean_ssim
def __init__(self, weight=None, gamma=2.0, reduction='mean'): super(FocalLoss, self).__init__(reduction=reduction) self.gamma = validator.check_value_type("gamma", gamma, [float]) if weight is not None and not isinstance(weight, Tensor): raise TypeError("The type of weight should be Tensor, but got {}.".format(type(weight))) self.weight = weight self.expand_dims = P.ExpandDims() self.gather_d = P.GatherD() self.squeeze = P.Squeeze(axis=1) self.tile = P.Tile() self.cast = P.Cast()
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): super(DeepFMModel, self).__init__() self.batch_size = config.batch_size self.field_size = config.data_field_size self.vocab_size = config.data_vocab_size self.emb_dim = config.data_emb_dim self.deep_layer_dims_list, self.deep_layer_act = config.deep_layer_args self.init_args = config.init_args self.weight_bias_init = config.weight_bias_init self.keep_prob = config.keep_prob init_acts = [('W_l2', [self.vocab_size, 1], 'normal'), ('V_l2', [self.vocab_size, self.emb_dim], 'normal'), ('b', [1], 'normal')] var_map = init_var_dict(self.init_args, init_acts) self.fm_w = var_map["W_l2"] self.fm_b = var_map["b"] self.embedding_table = var_map["V_l2"] # Deep Layers self.deep_input_dims = self.field_size * self.emb_dim + 1 self.all_dim_list = [self.deep_input_dims ] + self.deep_layer_dims_list + [1] self.dense_layer_1 = DenseLayer(self.all_dim_list[0], self.all_dim_list[1], self.weight_bias_init, self.deep_layer_act, self.keep_prob) self.dense_layer_2 = DenseLayer(self.all_dim_list[1], self.all_dim_list[2], self.weight_bias_init, self.deep_layer_act, self.keep_prob) self.dense_layer_3 = DenseLayer(self.all_dim_list[2], self.all_dim_list[3], self.weight_bias_init, self.deep_layer_act, self.keep_prob) self.dense_layer_4 = DenseLayer(self.all_dim_list[3], self.all_dim_list[4], self.weight_bias_init, self.deep_layer_act, self.keep_prob) # Cross Layer self.cross_layer_1 = CrossLayer(self.field_size * self.emb_dim, self.weight_bias_init) self.cross_layer_2 = CrossLayer(self.field_size * self.emb_dim, self.weight_bias_init) # FM, linear Layers self.Gatherv2 = P.GatherV2() self.Mul = P.Mul() self.ReduceSum = P.ReduceSum(keep_dims=False) self.Reshape = P.Reshape() self.Square = P.Square() self.Shape = P.Shape() self.Tile = P.Tile() self.Concat = P.Concat(axis=1) self.Cast = P.Cast()
def affine_grid_generator(self, height, width, theta): """ This function returns a sampling grid, which when used with the bilinear sampler on the input feature map, will create an output feature map that is an affine transformation [1] of the input feature map. zero = Tensor(np.zeros([]), mindspore.float32) Input ----- - height: desired height of grid/output. Used to downsample or upsample. - width: desired width of grid/output. Used to downsample or upsample. - theta: affine transform matrices of shape (num_batch, 2, 3). For each image in the batch, we have 6 theta parameters of the form (2x3) that define the affine transformation T. Returns ------- - normalized grid (-1, 1) of shape (num_batch, 2, H, W). The 2nd dimension has 2 components: (x, y) which are the sampling points of the original image for each point in the target image. Note ---- [1]: the affine transformation allows cropping, translation, and isotropic scaling. """ shape = P.Shape() num_batch = shape(theta)[0] cast = P.Cast() theta = cast(theta, mindspore.float32) # transform the sampling grid - batch multiply matmul = P.BatchMatMul() tile = P.Tile() sampling_grid = tile(self.sampling_grid, (num_batch, 1, 1)) cast = P.Cast() sampling_grid = cast(sampling_grid, mindspore.float32) batch_grids = matmul(theta, sampling_grid) # batch grid has shape (num_batch, 2, H*W) # reshape to (num_batch, H, W, 2) reshape = P.Reshape() batch_grids = reshape(batch_grids, (num_batch, 2, height, width)) return batch_grids
def construct(self, rois, feat1, feat2, feat3, feat4): feats = (feat1, feat2, feat3, feat4) res = self.res_ target_lvls = self._c_map_roi_levels(rois) for i in range(self.num_levels): mask = self.equal(target_lvls, P.ScalarToArray()(i)) mask = P.Reshape()(mask, (-1, 1, 1, 1)) roi_feats_t = self.roi_layers[i](feats[i], rois) mask = self.cast(P.Tile()(self.cast(mask, mstype.int32), (1, 256, self.out_size, self.out_size)), mstype.bool_) res = self.select(mask, roi_feats_t, res) return res
def __init__(self, input_dim, weight_bias_init): super(CrossLayer, self).__init__() self.input_dim = input_dim weight_init, bias_init = weight_bias_init self.weight = init_method(weight_init, [input_dim, 1], name="weight") self.bias = init_method(bias_init, [input_dim], name="bias") self.Tile = P.Tile() self.Transpose = P.TransPose() self.Mul = P.Mul() self.ReduceSum = P.ReduceSum(keep_dims=False) self.ExpandDims = P.ExpandDims() self.Matmul = P.MatMul(transpose_b=False) self.Reshape = P.Reshape()
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): super(ClassificationLoss, self).__init__() self.num_classes = config.NUM_CLASSES self.num_boxes = config.NUM_SSD_BOXES self.neg_pre_positive = config.NEG_PRE_POSITIVE self.minimum = P.Minimum() self.less = P.Less() self.sort = P.TopK() self.tile = P.Tile() self.reduce_sum = P.ReduceSum() self.reduce_mean = P.ReduceMean() self.expand_dims = P.ExpandDims() self.sort_descend = P.TopK(True) self.cross_entropy = nn.SoftmaxCrossEntropyWithLogits(sparse=True)
def construct(self, img1, img2): _check_input_4d(F.shape(img1), "img1", "SSIM") _check_input_4d(F.shape(img2), "img2", "SSIM") P.SameTypeShape()(img1, img2) max_val = _convert_img_dtype_to_float32(self.max_val, self.max_val) img1 = _convert_img_dtype_to_float32(img1, self.max_val) img2 = _convert_img_dtype_to_float32(img2, self.max_val) kernel = self._fspecial_gauss(self.filter_size, self.filter_sigma) kernel = P.Tile()(kernel, (1, P.Shape()(img1)[1], 1, 1)) mean_ssim = self._calculate_mean_ssim(img1, img2, kernel, max_val, self.k1, self.k2) return mean_ssim
def test_elim_tile_multiply_one(tag): """ test_elim_tile_multiply_one """ fns = FnDict() tile = P.Tile() all_one = (1, 1, 1) @fns def before(x): return tile(x, all_one) @fns def after(x): return x return fns[tag]
def __init__(self, network, config): super(retinanetWithLossCell, self).__init__() self.network = network self.less = P.Less() self.tile = P.Tile() self.reduce_sum = P.ReduceSum() self.reduce_mean = P.ReduceMean() self.expand_dims = P.ExpandDims() self.class_loss = SigmoidFocalClassificationLoss( config.gamma, config.alpha) self.loc_loss = nn.SmoothL1Loss() self.cast = P.Cast() self.network.to_float(mstype.float16)
def __init__(self, config): super(Attention, self).__init__() self.text_len = config.max_length self.attn = nn.Dense(in_channels=config.hidden_size * 3, out_channels=config.hidden_size).to_float( mstype.float16) self.fc = nn.Dense(config.hidden_size, 1, has_bias=False).to_float(mstype.float16) self.expandims = P.ExpandDims() self.tanh = P.Tanh() self.softmax = P.Softmax() self.tile = P.Tile() self.transpose = P.Transpose() self.concat = P.Concat(axis=2) self.squeeze = P.Squeeze(axis=2) self.cast = P.Cast()