Exemplo n.º 1
0
    def __init__(self, optimizer, epsilon=1e-05, coefficient=0.001, use_clip=False,
                 lars_filter=lambda x: 'LayerNorm' not in x.name and 'bias' not in x.name):
        super(LARS, self).__init__(0.0, [Parameter(Tensor(0.0), name="fake_param")])
        _check_param_value(optimizer, epsilon, coefficient, use_clip, self.cls_name)
        self.opt = optimizer
        self.parameters = optimizer.parameters
        self.use_clip = use_clip
        self.lars_flag = tuple(lars_filter(x) for x in self.parameters)
        self.is_group = optimizer.is_group
        self.learning_rate = Parameter(Tensor(0.0, dtype=mstype.float32), name="fake_lr")
        self.decay_flags = optimizer.decay_flags
        self.reciprocal_scale = optimizer.reciprocal_scale
        self.hyper_map = C.HyperMap()
        self.lars = P.LARSUpdate(epsilon, coefficient, use_clip)
        self.cast = P.Cast()

        if use_clip:
            self.is_group_lr = optimizer.is_group_lr
            self.dynamic_lr = optimizer.dynamic_lr
            self.origin_learning_rate = optimizer.learning_rate
            self.global_step = optimizer.global_step
            if self.is_group_lr and self.dynamic_lr:
                raise ValueError('Grouped dynamic learning rate is currently not supported for the inputs optimizer ' \
                                 'of lars.')

        if self.is_group:
            self.weight_decay = tuple(map(lambda x: x / optimizer.loss_scale, optimizer.weight_decay))
            optimizer.weight_decay = tuple(map(lambda x: 0.0, optimizer.weight_decay))
        else:
            self.weight_decay = optimizer.weight_decay / optimizer.loss_scale
            optimizer.weight_decay = 0.0

        optimizer.decay_flags = tuple(map(lambda x: False, self.decay_flags))
        optimizer.reciprocal_scale = 1.0
        optimizer.exec_weight_decay = False
Exemplo n.º 2
0
 def __init__(self, optimizer, epsilon=1e-05, coefficient=0.001, use_clip=False,
              lars_filter=lambda x: 'LayerNorm' not in x.name and 'bias' not in x.name):
     super(LARS, self).__init__(0.0, [Parameter(Tensor(0.0), name="fake_param")])
     _check_param_value(optimizer, epsilon, coefficient, use_clip, self.cls_name)
     self.opt = optimizer
     self.lars = P.LARSUpdate(epsilon, coefficient, use_clip)
     self.cast = P.Cast()
     self.parameters = optimizer.parameters
     if use_clip is True:
         self.learning_rate = optimizer.learning_rate
         self.dynamic_lr = optimizer.dynamic_lr
         self.gather = optimizer.gather
         self.assignadd = optimizer.assignadd
         self.global_step = optimizer.global_step
     else:
         self.learning_rate = Parameter(Tensor(0.0, dtype=mstype.float32), name="fake_lr")
     self.reciprocal_scale = optimizer.reciprocal_scale
     optimizer.reciprocal_scale = 1.0
     self.is_group = optimizer.is_group
     if self.is_group:
         self.weight_decay = tuple(map(lambda x: x / optimizer.loss_scale, optimizer.weight_decay))
     else:
         self.weight_decay = optimizer.weight_decay / optimizer.loss_scale
     optimizer.exec_weight_decay = False
     optimizer.weight_decay = 0.0
     self.decay_flags = optimizer.decay_flags
     self.lars_flag = tuple(lars_filter(x) for x in self.parameters)
     self.hyper_map = C.HyperMap()
 def __init__(self,
              in_channels,
              out_channels,
              weight_init='normal',
              bias_init='zeros',
              has_bias=True):
     super(CommonHeadLastFN, self).__init__()
     weight_shape = [out_channels, in_channels]
     self.weight = Parameter(initializer(weight_init, weight_shape),
                             requires_grad=True,
                             name='weight')
     self.x_norm = P.L2Normalize(axis=1)
     self.w_norm = P.L2Normalize(axis=1)
     self.fc = P.MatMul(transpose_a=False, transpose_b=True)
     self.multiplier = Parameter(Tensor(np.ones([1]), mstype.float32),
                                 requires_grad=True,
                                 name='multiplier')
     self.has_bias = has_bias
     if self.has_bias:
         bias_shape = [out_channels]
         self.bias_add = P.BiasAdd()
         self.bias = Parameter(initializer(bias_init, bias_shape),
                               requires_grad=True,
                               name='bias')
Exemplo n.º 4
0
 def __init__(self,
              rgb_range,
              rgb_mean=(0.4488, 0.4371, 0.4040),
              rgb_std=(1.0, 1.0, 1.0),
              sign=-1):
     super(MeanShift, self).__init__(3, 3, kernel_size=1)
     self.reshape = P.Reshape()
     self.eye = P.Eye()
     std = Tensor(rgb_std, mstype.float32)
     self.weight.set_data(
         self.reshape(self.eye(3, 3, mstype.float32), (3, 3, 1, 1)) / self.reshape(std, (3, 1, 1, 1)))
     self.weight.requires_grad = False
     self.bias = Parameter(
         sign * rgb_range * Tensor(rgb_mean, mstype.float32) / std, name='bias', requires_grad=False)
     self.has_bias = True
Exemplo n.º 5
0
def FasterRcnn_eval(dataset_path, ckpt_path, ann_file):
    """FasterRcnn evaluation."""
    ds = create_fasterrcnn_dataset(dataset_path, batch_size=config.test_batch_size, is_training=False)
    net = Faster_Rcnn_Resnet50(config)
    param_dict = load_checkpoint(ckpt_path)
    if args_opt.device_target == "GPU":
        for key, value in param_dict.items():
            tensor = value.asnumpy().astype(np.float32)
            param_dict[key] = Parameter(tensor, key)
    load_param_into_net(net, param_dict)
    net.set_train(False)

    eval_iter = 0
    total = ds.get_dataset_size()
    outputs = []
    dataset_coco = COCO(ann_file)

    print("\n========================================\n")
    print("total images num: ", total)
    print("Processing, please wait a moment.")
    max_num = 128
    for data in ds.create_dict_iterator(num_epochs=1):
        eval_iter = eval_iter + 1

        img_data = data['image']
        img_metas = data['image_shape']
        gt_bboxes = data['box']
        gt_labels = data['label']
        gt_num = data['valid_num']

        start = time.time()
        # run net
        output = net(img_data, img_metas, gt_bboxes, gt_labels, gt_num)
        end = time.time()
        print("Iter {} cost time {}".format(eval_iter, end - start))

        # output
        all_bbox = output[0]
        all_label = output[1]
        all_mask = output[2]

        for j in range(config.test_batch_size):
            all_bbox_squee = np.squeeze(all_bbox.asnumpy()[j, :, :])
            all_label_squee = np.squeeze(all_label.asnumpy()[j, :, :])
            all_mask_squee = np.squeeze(all_mask.asnumpy()[j, :, :])

            all_bboxes_tmp_mask = all_bbox_squee[all_mask_squee, :]
            all_labels_tmp_mask = all_label_squee[all_mask_squee]

            if all_bboxes_tmp_mask.shape[0] > max_num:
                inds = np.argsort(-all_bboxes_tmp_mask[:, -1])
                inds = inds[:max_num]
                all_bboxes_tmp_mask = all_bboxes_tmp_mask[inds]
                all_labels_tmp_mask = all_labels_tmp_mask[inds]

            outputs_tmp = bbox2result_1image(all_bboxes_tmp_mask, all_labels_tmp_mask, config.num_classes)

            outputs.append(outputs_tmp)

    eval_types = ["bbox"]
    result_files = results2json(dataset_coco, outputs, "./results.pkl")

    coco_eval(result_files, eval_types, dataset_coco, single_result=True)
Exemplo n.º 6
0
    def __init__(self,
                 q_tensor_width,
                 k_tensor_width,
                 v_tensor_width,
                 hidden_width,
                 out_tensor_width,
                 num_attention_heads=1,
                 query_act=None,
                 key_act=None,
                 value_act=None,
                 out_act=None,
                 has_attention_mask=True,
                 attention_probs_dropout_prob=0.0,
                 use_one_hot_embeddings=False,
                 initializer_range=0.02,
                 do_return_2d_tensor=False,
                 compute_type=mstype.float16,
                 same_dim=True):
        super(MultiheadAttention, self).__init__()
        self.num_attention_heads = num_attention_heads
        self.size_per_head = int(hidden_width / num_attention_heads)
        self.has_attention_mask = has_attention_mask
        self.use_one_hot_embeddings = use_one_hot_embeddings
        self.initializer_range = initializer_range
        self.do_return_2d_tensor = do_return_2d_tensor
        self.same_dim = same_dim

        self.scores_mul = Tensor(
            [1.0 / math.sqrt(float(self.size_per_head))], dtype=compute_type)
        self.reshape = P.Reshape()
        self.shape_q_2d = (-1, q_tensor_width)
        self.shape_k_2d = (-1, k_tensor_width)
        self.shape_v_2d = (-1, v_tensor_width)
        self.hidden_width = int(hidden_width)
        if self.same_dim:
            self.in_proj_layer = Parameter(Tensor(np.random.rand(hidden_width * 3,
                                                                 q_tensor_width), dtype=mstype.float32), name="weight")
        else:
            self.query_layer = nn.Dense(q_tensor_width,
                                        hidden_width,
                                        activation=query_act,
                                        has_bias=False).to_float(compute_type)
            self.key_layer = nn.Dense(k_tensor_width,
                                      hidden_width,
                                      activation=key_act,
                                      has_bias=False).to_float(compute_type)
            self.value_layer = nn.Dense(q_tensor_width,
                                        hidden_width,
                                        activation=value_act,
                                        has_bias=False).to_float(compute_type)
        self.out_proj = nn.Dense(hidden_width,
                                 out_tensor_width,
                                 activation=out_act,
                                 has_bias=False).to_float(compute_type)

        self.matmul_trans_b = P.BatchMatMul(transpose_b=True)
        self.multiply = P.Mul()
        self.transpose = P.Transpose()
        self.trans_shape = (0, 2, 1, 3)
        self.trans_shape_relative = (2, 0, 1, 3)
        self.trans_shape_position = (1, 2, 0, 3)
        self.multiply_data = Tensor([-10000.0,], dtype=compute_type)
        self.matmul = P.BatchMatMul()

        self.softmax = nn.Softmax()
        self.dropout = nn.Dropout(1. - attention_probs_dropout_prob)
        self.use_dropout = attention_probs_dropout_prob > 0

        if self.has_attention_mask:
            self.expand_dims = P.ExpandDims()
            self.sub = P.Sub()
            self.add = P.TensorAdd()
            self.cast = P.Cast()
            self.get_dtype = P.DType()

        self.softmax_cast = P.Cast()
        self.matmul_dense = P.MatMul(transpose_b=True)
        self.split = P.Split(0, 3)
        self.equal = P.Equal()
        self.shape = P.Shape()
Exemplo n.º 7
0
 def __init__(self):
     super(NetE, self).__init__()
     self.w = Parameter(Tensor(np.ones([16, 16, 3, 3]).astype(np.float32)), name='w')