示例#1
0
    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()
示例#2
0
 def __init__(self,
              in_channels,
              out_channels,
              kernel_size,
              stride,
              pad_mode="pad",
              pad=0,
              groups=1,
              has_bias=False):
     super(GroupConv, self).__init__()
     assert in_channels % groups == 0 and out_channels % groups == 0
     self.groups = groups
     self.convs = nn.CellList()
     self.op_split = P.Split(axis=1, output_num=self.groups)
     self.op_concat = P.Concat(axis=1)
     self.cast = P.Cast()
     for _ in range(groups):
         self.convs.append(
             nn.Conv2d(in_channels // groups,
                       out_channels // groups,
                       kernel_size=kernel_size,
                       stride=stride,
                       has_bias=has_bias,
                       padding=pad,
                       pad_mode=pad_mode,
                       group=1,
                       weight_init='xavier_uniform'))
示例#3
0
 def __init__(self, in_channels, has_bias=False):
     super(Inception_E, self).__init__()
     self.concat = P.Concat(axis=1)
     self.branch0 = BasicConv2d(in_channels,
                                320,
                                kernel_size=1,
                                has_bias=has_bias)
     self.branch1 = BasicConv2d(in_channels,
                                384,
                                kernel_size=1,
                                has_bias=has_bias)
     self.branch1_a = BasicConv2d(384,
                                  384,
                                  kernel_size=(1, 3),
                                  has_bias=has_bias)
     self.branch1_b = BasicConv2d(384,
                                  384,
                                  kernel_size=(3, 1),
                                  has_bias=has_bias)
     self.branch2 = nn.SequentialCell([
         BasicConv2d(in_channels, 448, kernel_size=1, has_bias=has_bias),
         BasicConv2d(448, 384, kernel_size=3, has_bias=has_bias)
     ])
     self.branch2_a = BasicConv2d(384,
                                  384,
                                  kernel_size=(1, 3),
                                  has_bias=has_bias)
     self.branch2_b = BasicConv2d(384,
                                  384,
                                  kernel_size=(3, 1),
                                  has_bias=has_bias)
     self.branch_pool = nn.SequentialCell([
         nn.AvgPool2d(kernel_size=3, pad_mode='same'),
         BasicConv2d(in_channels, 192, kernel_size=1, has_bias=has_bias)
     ])
示例#4
0
 def __init__(self,
              in_channels,
              out_channels,
              num_heads=1,
              in_drop=0.0,
              coef_drop=0.0,
              activation=nn.ELU(),
              residual=False,
              output_transform='concat'):
     super(AttentionAggregator, self).__init__()
     self.num_heads = num_heads
     self.attns = []
     for _ in range(num_heads):
         self.attns.append(
             AttentionHead(in_channels,
                           out_channels,
                           in_drop_ratio=in_drop,
                           coef_drop_ratio=coef_drop,
                           activation=activation,
                           residual=residual))
     self.attns = nn.layer.CellList(self.attns)
     if output_transform == 'concat':
         self.out_trans = P.Concat(-1)
     elif output_transform == 'sum':
         self.out_trans = P.AddN()
     else:
         raise ValueError
示例#5
0
 def __init__(self,
              dim,
              heads,
              dim_head,
              causal=False,
              nb_features=None,
              qr_uniform_q=False,
              dropout=0.9):
     super(SelfAttention, self).__init__()
     assert dim % heads == 0, 'dimension must be divisible by number of heads'
     self.dim_head = dim_head
     self.fast_attention = FastAttention(dim_heads=self.dim_head,
                                         nb_features=nb_features,
                                         causal=causal,
                                         qr_uniform_q=qr_uniform_q)
     self.heads = heads
     self.to_q = Mapping(dim, dim)
     self.to_k = Mapping(dim, dim)
     self.to_v = Mapping(dim, dim)
     self.to_out = Mapping(dim, dim)
     self.dropout = nn.Dropout(dropout)
     self.view = P.Reshape()
     self.Concat = P.Concat(axis=1)
     self.Mul = P.Mul()
     self.ExpandDims = P.ExpandDims()
     self.Tile = P.Tile()
示例#6
0
    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
示例#7
0
 def __init__(self, in_channels):
     super(InceptionBlockC_2, self).__init__()
     self.branch_1 = Conv2dBlock(in_channels,
                                 out_channels=320,
                                 kernel_size=1)
     self.branch_2_a = Conv2dBlock(in_channels,
                                   out_channels=384,
                                   kernel_size=1)
     self.branch_2_b_1x3 = Conv2dBlock(in_channels=384,
                                       out_channels=384,
                                       kernel_size=(1, 3))
     self.branch_2_b_3x1 = Conv2dBlock(in_channels=384,
                                       out_channels=384,
                                       kernel_size=(3, 1))
     self.branch_3_a = nn.SequentialCell([
         Conv2dBlock(in_channels, out_channels=448, kernel_size=1),
         Conv2dBlock(in_channels=448, out_channels=384, kernel_size=3)
     ])
     self.branch_3_b_1x3 = Conv2dBlock(
         in_channels=384, out_channels=384,
         kernel_size=(1, 3))  # same with branch_2_b_1x3
     self.branch_3_b_3x1 = Conv2dBlock(
         in_channels=384, out_channels=384,
         kernel_size=(3, 1))  # same with branch_2_b_3x1
     self.branch_4 = nn.SequentialCell([
         nn.AvgPool2d(kernel_size=3, stride=1, pad_mode='same'),
         Conv2dBlock(in_channels, out_channels=192, kernel_size=1)
     ])
     self.concat = P.Concat(axis=1)
示例#8
0
    def __init__(self, batch_size=4):
        super(DiceLoss, self).__init__()

        self.threshold0 = Tensor(0.5, mstype.float32)
        self.zero_float32 = Tensor(0.0, mstype.float32)
        self.k = int(640 * 640)
        self.negative_one_int32 = Tensor(-1, mstype.int32)
        self.batch_size = batch_size
        self.concat = P.Concat()
        self.less_equal = P.LessEqual()
        self.greater = P.Greater()
        self.reduce_sum = P.ReduceSum()
        self.reduce_sum_keep_dims = P.ReduceSum(keep_dims=True)
        self.reduce_mean = P.ReduceMean()
        self.reduce_min = P.ReduceMin()
        self.cast = P.Cast()
        self.minimum = P.Minimum()
        self.expand_dims = P.ExpandDims()
        self.select = P.Select()
        self.fill = P.Fill()
        self.topk = P.TopK(sorted=True)
        self.shape = P.Shape()
        self.sigmoid = P.Sigmoid()
        self.reshape = P.Reshape()
        self.slice = P.Slice()
        self.logical_and = P.LogicalAnd()
        self.logical_or = P.LogicalOr()
        self.equal = P.Equal()
        self.zeros_like = P.ZerosLike()
        self.add = P.TensorAdd()
        self.gather = P.Gather()
示例#9
0
 def __init__(self,
              batch_size,
              conv_out_dim,
              encoder_hidden_size,
              decoder_hidden_size,
              decoder_output_size,
              max_length,
              dropout_p=0.1):
     super(AttentionOCR, self).__init__()
     self.encoder = Encoder(batch_size=batch_size,
                            conv_out_dim=conv_out_dim,
                            hidden_size=encoder_hidden_size)
     self.decoder = Decoder(hidden_size=decoder_hidden_size,
                            output_size=decoder_output_size,
                            max_length=max_length,
                            dropout_p=dropout_p)
     self.init_decoder_hidden = Tensor(
         np.zeros((1, batch_size, decoder_hidden_size), dtype=np.float16),
         mstype.float16)
     self.shape = P.Shape()
     self.split = P.Split(axis=1, output_num=max_length)
     self.concat = P.Concat()
     self.expand_dims = P.ExpandDims()
     self.argmax = P.Argmax()
     self.select = P.Select()
示例#10
0
def extract_logits(logits=None, position=None):
    """
    Args
        logits (Tensor): Tensor(batch_size,seq_length,vocab_size) e.g.(8,1024,50257)
        position (numpy.array): the array stored the fianl word position, shape with [batch_size, 2]

    Return:
        output_logits (Tensor): extract the Specified logit according to the position,
                                shape with [batch_size, vocab_size]
    """

    batch_size = logits.shape[0]

    for batch_idx in range(batch_size):
        word_logits_pos = int(position[batch_idx, 0] - 1)

        logit = logits[batch_idx:batch_idx + 1:1,
                       word_logits_pos, ::]  # [1, vocab_size]
        if batch_idx == 0:
            output_logits = logit
        else:
            output_logits = P.Concat()(
                (output_logits, logit))  # [batch_size, vocab_size]

    return output_logits
示例#11
0
    def __init__(self, vggpath=''):
        super(OpenPoseNet, self).__init__()
        self.base = Base_model()
        self.stage_1 = Stage_1()
        self.stage_2 = Stage_x()
        self.stage_3 = Stage_x()
        self.stage_4 = Stage_x()
        self.stage_5 = Stage_x()
        self.stage_6 = Stage_x()
        self.shape = P.Shape()
        self.cat = P.Concat(axis=1)
        self.print = P.Print()
        # for m in self.modules():
        # if isinstance(m, Conv2d):
        # init.constant_(m.bias, 0)
        if loadvgg and vggpath:
            param_dict = load_checkpoint(vggpath)
            param_dict_new = {}
            trans_name = 'base.vgg_base.'
            for key, values in param_dict.items():

                #print('key:',key,self.shape(values))
                if key.startswith('moments.'):
                    continue
                elif key.startswith('network.'):
                    param_dict_new[trans_name + key[17:]] = values
                # else:
                # param_dict_new[key] = values
            #print(param_dict_new)
            load_param_into_net(self.base.vgg_base, param_dict_new)
示例#12
0
 def __init__(self, config):
     super(FlattenConcat, self).__init__()
     self.sizes = config.FEATURE_SIZE
     self.length = len(self.sizes)
     self.num_default = config.NUM_DEFAULT
     self.concat = P.Concat(axis=-1)
     self.transpose = P.Transpose()
示例#13
0
    def __init__(self, feature_shape, backbone_shape, backbone, out_channel):
        super(YOLOv3, self).__init__()
        self.out_channel = out_channel
        self.net = backbone
        self.backblock0 = YoloBlock(backbone_shape[-1],
                                    out_chls=backbone_shape[-2],
                                    out_channels=out_channel)

        self.conv1 = _conv_bn_relu(in_channel=backbone_shape[-2],
                                   out_channel=backbone_shape[-2] // 2,
                                   ksize=1)
        self.upsample1 = P.ResizeNearestNeighbor(
            (feature_shape[2] // 16, feature_shape[3] // 16))
        self.backblock1 = YoloBlock(in_channels=backbone_shape[-2] +
                                    backbone_shape[-3],
                                    out_chls=backbone_shape[-3],
                                    out_channels=out_channel)

        self.conv2 = _conv_bn_relu(in_channel=backbone_shape[-3],
                                   out_channel=backbone_shape[-3] // 2,
                                   ksize=1)
        self.upsample2 = P.ResizeNearestNeighbor(
            (feature_shape[2] // 8, feature_shape[3] // 8))
        self.backblock2 = YoloBlock(in_channels=backbone_shape[-3] +
                                    backbone_shape[-4],
                                    out_chls=backbone_shape[-4],
                                    out_channels=out_channel)
        self.concat = P.Concat(axis=1)
示例#14
0
 def __init__(self, config, is_training=True):
     super(Decoder, self).__init__()
     self.hidden_size = config.hidden_size
     self.vocab_size = config.trg_vocab_size
     self.embedding_size = config.decoder_embedding_size
     self.embedding = nn.Embedding(self.vocab_size, self.embedding_size)
     self.rnn = GRU(input_size=self.embedding_size + self.hidden_size*2, \
         hidden_size=self.hidden_size).to_float(config.compute_type)
     self.text_len = config.max_length
     self.shape = P.Shape()
     self.transpose = P.Transpose()
     self.p = P.Print()
     self.cast = P.Cast()
     self.concat = P.Concat(axis=2)
     self.squeeze = P.Squeeze(axis=0)
     self.expandims = P.ExpandDims()
     self.log_softmax = P.LogSoftmax(axis=1)
     weight, bias = dense_default_state(
         self.embedding_size + self.hidden_size * 3, self.vocab_size)
     self.fc = nn.Dense(self.embedding_size + self.hidden_size * 3,
                        self.vocab_size,
                        weight_init=weight,
                        bias_init=bias).to_float(config.compute_type)
     self.attention = Attention(config)
     self.bmm = P.BatchMatMul()
     self.dropout = nn.Dropout(0.7)
     self.expandims = P.ExpandDims()
     self.dtype = config.dtype
示例#15
0
 def __init__(self):
     super(BoundingBoxEncode, self).__init__()
     self.split = P.Split(axis=1, output_num=4)
     self.ones = 1.0
     self.half = 0.5
     self.log = P.Log()
     self.concat = P.Concat(axis=1)
示例#16
0
    def __init__(self, vocab_size, embed_size, num_hiddens, num_layers,
                 bidirectional, weight, labels, batch_size):
        super(SentimentNet, self).__init__()
        self.num_hiddens = num_hiddens
        self.num_layers = num_layers
        self.bidirectional = bidirectional
        self.batch_size = batch_size

        self.embedding = nn.Embedding(vocab_size, embed_size, use_one_hot=False, embedding_table=Tensor(weight))
        self.embedding.embedding_table.requires_grad = False
        self.trans = P.Transpose()
        self.perm = (1, 0, 2)
        self.h, self.c, self.w = InitialLstmWeight(embed_size, num_hiddens, num_layers, bidirectional)
        self.encoder = P.LSTM(input_size=embed_size, hidden_size=self.num_hiddens,
                              num_layers=num_layers, has_bias=False,
                              bidirectional=self.bidirectional, dropout=0.0)
        self.concat = P.Concat(2)
        if self.bidirectional:
            self.decoder = nn.Dense(num_hiddens * 4, labels)
        else:
            self.decoder = nn.Dense(num_hiddens * 2, labels)

        self.slice1 = P.Slice()
        self.slice2 = P.Slice()
        self.reshape = P.Reshape()

        self.num_direction = 1
        if bidirectional:
            self.num_direction = 2
示例#17
0
 def __init__(self, in_channels):
     super(InceptionB, self).__init__()
     self.branch_0 = Conv2d(in_channels,
                            384,
                            1,
                            stride=1,
                            padding=0,
                            has_bias=False)
     self.branch_1 = nn.SequentialCell([
         Conv2d(in_channels, 192, 1, stride=1, padding=0, has_bias=False),
         Conv2d(192, 224, (1, 7), pad_mode='same', stride=1,
                has_bias=False),
         Conv2d(224, 256, (7, 1), pad_mode='same', stride=1,
                has_bias=False),
     ])
     self.branch_2 = nn.SequentialCell([
         Conv2d(in_channels, 192, 1, stride=1, padding=0, has_bias=False),
         Conv2d(192, 192, (7, 1), pad_mode='same', stride=1,
                has_bias=False),
         Conv2d(192, 224, (1, 7), pad_mode='same', stride=1,
                has_bias=False),
         Conv2d(224, 224, (7, 1), pad_mode='same', stride=1,
                has_bias=False),
         Conv2d(224, 256, (1, 7), pad_mode='same', stride=1, has_bias=False)
     ])
     self.branch_3 = nn.SequentialCell([
         Avgpool(kernel_size=3, stride=1, pad_mode='same'),
         Conv2d(in_channels, 128, 1, stride=1, padding=0, has_bias=False)
     ])
     self.concat = P.Concat(1)
示例#18
0
 def __init__(self,
              model,
              train_dataset,
              task_type,
              num_classes=None,
              epochs=1,
              epi_uncer_model_path=None,
              ale_uncer_model_path=None,
              save_model=False):
     self.model = model
     self.train_dataset = train_dataset
     self.task_type = task_type
     self.num_classes = check_int_positive(num_classes)
     self.epochs = epochs
     self.epi_uncer_model_path = epi_uncer_model_path
     self.ale_uncer_model_path = ale_uncer_model_path
     self.save_model = save_model
     self.epi_uncer_model = None
     self.ale_uncer_model = None
     self.concat = P.Concat(axis=0)
     self.sum = P.ReduceSum()
     self.pow = P.Pow()
     if self.task_type not in ('regression', 'classification'):
         raise ValueError(
             'The task should be regression or classification.')
     if self.task_type == 'classification':
         if self.num_classes is None:
             raise ValueError("Classification task needs to input labels.")
     if self.save_model:
         if self.epi_uncer_model_path is None or self.ale_uncer_model_path is None:
             raise ValueError(
                 "If save_model is True, the epi_uncer_model_path and "
                 "ale_uncer_model_path should not be None.")
示例#19
0
 def __init__(self, in_channels, var_channels):
     super(InceptionBlockB_2, self).__init__()
     self.branch_1 = Conv2dBlock(in_channels,
                                 out_channels=192,
                                 kernel_size=1)
     self.branch_2 = nn.SequentialCell([
         Conv2dBlock(in_channels, out_channels=var_channels, kernel_size=1),
         Conv2dBlock(in_channels=var_channels,
                     out_channels=var_channels,
                     kernel_size=(1, 7)),
         Conv2dBlock(in_channels=var_channels,
                     out_channels=192,
                     kernel_size=(7, 1))
     ])
     self.branch_3 = nn.SequentialCell([
         Conv2dBlock(in_channels, out_channels=var_channels, kernel_size=1),
         Conv2dBlock(in_channels=var_channels,
                     out_channels=var_channels,
                     kernel_size=(7, 1)),
         Conv2dBlock(in_channels=var_channels,
                     out_channels=var_channels,
                     kernel_size=(1, 7)),
         Conv2dBlock(in_channels=var_channels,
                     out_channels=var_channels,
                     kernel_size=(7, 1)),
         Conv2dBlock(in_channels=var_channels,
                     out_channels=192,
                     kernel_size=(1, 7))
     ])
     self.branch_4 = nn.SequentialCell([
         nn.AvgPool2d(kernel_size=3, stride=1, pad_mode='same'),
         Conv2dBlock(in_channels, out_channels=192, kernel_size=1)
     ])
     self.concat = P.Concat(axis=1)
示例#20
0
    def construct(self, x, x_prev):
        x_left = self.conv_prev_1x1(x_prev)
        x_right = self.conv_1x1(x)

        x_comb_iter_0_left = self.comb_iter_0_left(x_right)
        x_comb_iter_0_right = self.comb_iter_0_right(x_left)
        x_comb_iter_0 = x_comb_iter_0_left + x_comb_iter_0_right

        x_comb_iter_1_left = self.comb_iter_1_left(x_right)
        x_comb_iter_1_right = self.comb_iter_1_right(x_left)
        x_comb_iter_1 = x_comb_iter_1_left + x_comb_iter_1_right

        x_comb_iter_2_left = self.comb_iter_2_left(x_right)
        x_comb_iter_2_right = self.comb_iter_2_right(x_left)
        x_comb_iter_2 = x_comb_iter_2_left + x_comb_iter_2_right

        x_comb_iter_3_right = self.comb_iter_3_right(x_comb_iter_0)
        x_comb_iter_3 = x_comb_iter_3_right + x_comb_iter_1

        x_comb_iter_4_left = self.comb_iter_4_left(x_comb_iter_0)
        x_comb_iter_4_right = self.comb_iter_4_right(x_right)
        x_comb_iter_4 = x_comb_iter_4_left + x_comb_iter_4_right

        x_out = P.Concat(1)(
            (x_comb_iter_1, x_comb_iter_2, x_comb_iter_3, x_comb_iter_4))
        return x_out
示例#21
0
文件: lstm.py 项目: xyg320/mindspore
    def __init__(self, vocab_size, embed_size, num_hiddens, num_layers,
                 bidirectional, num_classes, weight, batch_size):
        super(SentimentNet, self).__init__()
        # Mapp words to vectors
        self.embedding = nn.Embedding(vocab_size,
                                      embed_size,
                                      embedding_table=weight)
        self.embedding.embedding_table.requires_grad = False
        self.trans = P.Transpose()
        self.perm = (1, 0, 2)
        self.encoder = nn.LSTM(input_size=embed_size,
                               hidden_size=num_hiddens,
                               num_layers=num_layers,
                               has_bias=True,
                               bidirectional=bidirectional,
                               dropout=0.0)

        self.h, self.c = lstm_default_state(batch_size, num_hiddens,
                                            num_layers, bidirectional)

        self.concat = P.Concat(1)
        if bidirectional:
            self.decoder = nn.Dense(num_hiddens * 4, num_classes)
        else:
            self.decoder = nn.Dense(num_hiddens * 2, num_classes)
 def __init__(self, model, train_dataset, task_type, num_classes=None, epochs=1,
              epi_uncer_model_path=None, ale_uncer_model_path=None, save_model=False):
     self.epi_model = model
     self.ale_model = deepcopy(model)
     self.epi_train_dataset = train_dataset
     self.ale_train_dataset = deepcopy(train_dataset)
     self.task_type = task_type
     self.epochs = Validator.check_positive_int(epochs)
     self.epi_uncer_model_path = epi_uncer_model_path
     self.ale_uncer_model_path = ale_uncer_model_path
     self.save_model = Validator.check_bool(save_model)
     self.epi_uncer_model = None
     self.ale_uncer_model = None
     self.concat = P.Concat(axis=0)
     self.sum = P.ReduceSum()
     self.pow = P.Pow()
     if not isinstance(model, Cell):
         raise TypeError('The model should be Cell type.')
     if task_type not in ('regression', 'classification'):
         raise ValueError('The task should be regression or classification.')
     if task_type == 'classification':
         self.num_classes = Validator.check_positive_int(num_classes)
     else:
         self.num_classes = num_classes
     if save_model:
         if epi_uncer_model_path is None or ale_uncer_model_path is None:
             raise ValueError("If save_model is True, the epi_uncer_model_path and "
                              "ale_uncer_model_path should not be None.")
示例#23
0
    def __init__(self,
                 num_in,
                 num_out,
                 kernel_size=1,
                 stride=1,
                 padding=0,
                 ratio=2,
                 dw_size=3,
                 use_act=True,
                 act_type='relu'):
        super(GhostModule, self).__init__()
        init_channels = math.ceil(num_out / ratio)
        new_channels = init_channels * (ratio - 1)

        self.primary_conv = ConvBNReLU(num_in,
                                       init_channels,
                                       kernel_size=kernel_size,
                                       stride=stride,
                                       groups=1,
                                       use_act=use_act,
                                       act_type='relu')
        self.cheap_operation = ConvBNReLU(init_channels,
                                          new_channels,
                                          kernel_size=dw_size,
                                          stride=1,
                                          groups=init_channels,
                                          use_act=use_act,
                                          act_type='relu')
        self.concat = P.Concat(axis=1)
示例#24
0
 def __init__(self, encoder, decoder, hidden_size, latent_size,
              num_classes):
     super(ConditionalVAE, self).__init__()
     self.encoder = encoder
     self.decoder = decoder
     if (not isinstance(encoder, Cell)) or (not isinstance(decoder, Cell)):
         raise TypeError('The encoder and decoder should be Cell type.')
     self.hidden_size = check_int_positive(hidden_size)
     self.latent_size = check_int_positive(latent_size)
     if hidden_size < latent_size:
         raise ValueError(
             'The latent_size should be less than or equal to the hidden_size.'
         )
     self.num_classes = check_int_positive(num_classes)
     self.normal = C.normal
     self.exp = P.Exp()
     self.reshape = P.Reshape()
     self.shape = P.Shape()
     self.concat = P.Concat(axis=1)
     self.to_tensor = P.ScalarToArray()
     self.one_hot = OneHot(depth=num_classes)
     self.dense1 = Dense(self.hidden_size, self.latent_size)
     self.dense2 = Dense(self.hidden_size, self.latent_size)
     self.dense3 = Dense(self.latent_size + self.num_classes,
                         self.hidden_size)
示例#25
0
def gaussian_orthogonal_random_matrix(nb_rows,
                                      nb_columns,
                                      scaling=0,
                                      qr_uniform_q=False):
    # print(nb_rows, nb_columns, scaling, qr_uniform_q)
    nb_full_blocks = int(nb_rows / nb_columns)
    block_list = []
    for _ in range(nb_full_blocks):
        q = orthogonal_matrix_chunk(
            nb_columns,
            qr_uniform_q=qr_uniform_q,
        )
        block_list.append(q)
    remaining_rows = nb_rows - nb_full_blocks * nb_columns
    if remaining_rows > 0:
        q = orthogonal_matrix_chunk(
            nb_columns,
            qr_uniform_q=qr_uniform_q,
        )
        block_list.append(q[:remaining_rows])
    final_matrix = P.Concat()(tuple(block_list))

    if scaling == 0:
        multiplier = Tensor(
            np.diag(
                np.linalg.norm(np.random.randn(nb_rows,
                                               nb_columns).astype(np.float32),
                               axis=1)))
    elif scaling == 1:
        multiplier = Tensor(
            np.diag(math.sqrt((float(nb_columns))) * np.ones((nb_rows, ))))
    else:
        raise ValueError(f'Invalid scaling {scaling}')

    return P.MatMul()(multiplier, final_matrix)
示例#26
0
 def __init__(self,
              max_val=1.0,
              power_factors=(0.0448, 0.2856, 0.3001, 0.2363, 0.1333),
              filter_size=11,
              filter_sigma=1.5,
              k1=0.01,
              k2=0.03):
     super(MSSSIM, self).__init__()
     validator.check_value_type('max_val', max_val, [int, float],
                                self.cls_name)
     validator.check_number('max_val', max_val, 0.0, Rel.GT, self.cls_name)
     self.max_val = max_val
     validator.check_value_type('power_factors', power_factors,
                                [tuple, list], self.cls_name)
     self.filter_size = validator.check_integer('filter_size', filter_size,
                                                1, Rel.GE, self.cls_name)
     self.filter_sigma = validator.check_float_positive(
         'filter_sigma', filter_sigma, self.cls_name)
     self.k1 = validator.check_value_type('k1', k1, [float], self.cls_name)
     self.k2 = validator.check_value_type('k2', k2, [float], self.cls_name)
     window = _create_window(filter_size, filter_sigma)
     self.level = len(power_factors)
     self.conv = []
     for i in range(self.level):
         self.conv.append(_conv2d(1, 1, filter_size, Tensor(window)))
         self.conv[i].weight.requires_grad = False
     self.multi_convs_list = CellList(self.conv)
     self.weight_tensor = Tensor(power_factors, mstype.float32)
     self.avg_pool = AvgPool2d(kernel_size=2, stride=2, pad_mode='valid')
     self.relu = ReLU()
     self.reduce_mean = P.ReduceMean()
     self.prod = P.ReduceProd()
     self.pow = P.Pow()
     self.pack = P.Pack(axis=-1)
     self.concat = P.Concat(axis=1)
示例#27
0
 def __init__(self, in_channels, has_bias=False):
     super(Inception_D, self).__init__()
     self.concat = P.Concat(axis=1)
     self.branch0 = nn.SequentialCell([
         BasicConv2d(in_channels, 192, kernel_size=1, has_bias=has_bias),
         BasicConv2d(192,
                     320,
                     kernel_size=3,
                     stride=2,
                     pad_mode='valid',
                     has_bias=has_bias)
     ])
     self.branch1 = nn.SequentialCell([
         BasicConv2d(in_channels, 192, kernel_size=1, has_bias=has_bias),
         BasicConv2d(192, 192, kernel_size=(1, 7),
                     has_bias=has_bias),  # check
         BasicConv2d(192, 192, kernel_size=(7, 1), has_bias=has_bias),
         BasicConv2d(192,
                     192,
                     kernel_size=3,
                     stride=2,
                     pad_mode='valid',
                     has_bias=has_bias)
     ])
     self.branch_pool = nn.MaxPool2d(kernel_size=3, stride=2)
示例#28
0
 def __init__(self, network, optimizer, sens=1.0):
     super(TrainOneStepCellWithGradClip, self).__init__(auto_prefix=False)
     self.network = network
     self.network.set_grad()
     self.network.add_flags(defer_inline=True)
     self.weights = optimizer.parameters
     self.optimizer = optimizer
     self.grad = C.GradOperation(get_by_list=True, sens_param=True)
     self.sens = sens
     self.reducer_flag = False
     self.grad_reducer = None
     self.hyper_map = C.HyperMap()
     self.greater = P.Greater()
     self.select = P.Select()
     self.norm = nn.Norm(keep_dims=True)
     self.dtype = P.DType()
     self.cast = P.Cast()
     self.concat = P.Concat(axis=0)
     self.ten = Tensor(np.array([10.0]).astype(np.float32))
     parallel_mode = _get_parallel_mode()
     if parallel_mode in (ParallelMode.DATA_PARALLEL,
                          ParallelMode.HYBRID_PARALLEL):
         self.reducer_flag = True
     if self.reducer_flag:
         mean = _get_gradients_mean()
         degree = _get_device_num()
         self.grad_reducer = DistributedGradReducer(optimizer.parameters,
                                                    mean, degree)
示例#29
0
文件: loss.py 项目: yrpang/mindspore
 def __init__(self, num_classes, num_boxes, neg_pre_positive, batch_size):
     super(MultiBoxLoss, self).__init__()
     self.num_classes = num_classes
     self.num_boxes = num_boxes
     self.neg_pre_positive = neg_pre_positive
     self.notequal = P.NotEqual()
     self.less = P.Less()
     self.tile = P.Tile()
     self.reduce_sum = P.ReduceSum()
     self.reduce_mean = P.ReduceMean()
     self.expand_dims = P.ExpandDims()
     self.smooth_l1_loss = P.SmoothL1Loss()
     self.cross_entropy = SoftmaxCrossEntropyWithLogits()
     self.maximum = P.Maximum()
     self.minimum = P.Minimum()
     self.sort_descend = P.TopK(True)
     self.sort = P.TopK(True)
     self.gather = P.GatherNd()
     self.max = P.ReduceMax()
     self.log = P.Log()
     self.exp = P.Exp()
     self.concat = P.Concat(axis=1)
     self.reduce_sum2 = P.ReduceSum(keep_dims=True)
     self.idx = Tensor(
         np.reshape(np.arange(batch_size * num_boxes), (-1, 1)), ms.int32)
示例#30
0
 def __init__(self, num_classes):
     super(Encoder, self).__init__()
     self.fc1 = nn.Dense(1024 + num_classes, 400)
     self.relu = nn.ReLU()
     self.flatten = nn.Flatten()
     self.concat = P.Concat(axis=1)
     self.one_hot = nn.OneHot(depth=num_classes)