コード例 #1
0
def sync_step(step, sync_op):
    step_tensor = ms.Tensor(step, dtype=ms.float32)
    cluster_step = sync_op(step_tensor)
    new_step = int(cluster_step.asnumpy())
    print('sync step %d -> %d' % (step, new_step))
    return new_step
コード例 #2
0
ファイル: test_tensor.py プロジェクト: wenkai128/mindspore
def test_tensor_input_tuple_string():
    with pytest.raises(TypeError):
        input_data = (2, 3, '4', 5)
        ms.Tensor(input_data)
コード例 #3
0
ファイル: test_tensor.py プロジェクト: wenkai128/mindspore
def test_tensor_input_none():
    with pytest.raises(TypeError):
        input_data = None
        ms.Tensor(input_data, np.int64)
コード例 #4
0
ファイル: test_tensor.py プロジェクト: wenkai128/mindspore
def test_set_type():
    t = ms.Tensor(ndarr)
    t.set_dtype(ms.float32)
    assert t.dtype() == ms.float32
コード例 #5
0
ファイル: test_tensor.py プロジェクト: wenkai128/mindspore
def test_sub():
    x = ms.Tensor(ndarr)
    y = ms.Tensor(ndarr)
    z = x - y
    assert isinstance(z, ms.Tensor)
コード例 #6
0
        self.head = head

    def construct(self, x):
        x = self.backbone(x)
        x = self.head(x)
        return x


BACKBONE = effnet(num_classes=1000)
load_checkpoint("efficient_net_b0.ckpt", BACKBONE)

M.context.set_context(mode=M.context.PYNATIVE_MODE,
                      device_target="GPU",
                      save_graphs=False)
BATCH_SIZE = 16
X = M.Tensor(np.ones((BATCH_SIZE, 3, 224, 224)), M.float32)
export(BACKBONE,
       X,
       file_name="transfer_learning_tod_backbone",
       file_format='MINDIR')

label = M.Tensor(np.zeros([BATCH_SIZE, 10]).astype(np.float32))
HEAD = M.nn.Dense(1000, 10)
HEAD.weight.set_data(
    M.Tensor(
        np.random.normal(0, 0.1, HEAD.weight.data.shape).astype("float32")))
HEAD.bias.set_data(M.Tensor(np.zeros(HEAD.bias.data.shape, dtype="float32")))

sgd = M.nn.SGD(HEAD.trainable_params(),
               learning_rate=0.015,
               momentum=0.9,
コード例 #7
0
ファイル: test_tensor.py プロジェクト: wenkai128/mindspore
def test_tensor_type_uint32():
    t_uint32_array = ms.Tensor(
        np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint32))
    assert isinstance(t_uint32_array, ms.Tensor)
    assert t_uint32_array.shape() == (2, 3)
    assert t_uint32_array.dtype() == ms.uint32
コード例 #8
0
ファイル: robustness.py プロジェクト: ysh329/mindspore
    def evaluate(self, explainer, inputs, targets, saliency=None):
        """
        Evaluate robustness on single sample.

        Note:
            Currently only single sample (:math:`N=1`) at each call is supported.

        Args:
            explainer (Explanation): The explainer to be evaluated, see `mindspore.explainer.explanation`.
            inputs (Tensor): A data sample, a 4D tensor of shape :math:`(N, C, H, W)`.
            targets (Tensor, int): The label of interest. It should be a 1D or 0D tensor, or an integer.
                If `targets` is a 1D tensor, its length should be the same as `inputs`.
            saliency (Tensor, optional): The saliency map to be evaluated, a 4D tensor of shape :math:`(N, 1, H, W)`.
                If it is None, the parsed `explainer` will generate the saliency map with `inputs` and `targets` and
                continue the evaluation. Default: None.

        Returns:
            numpy.ndarray, 1D array of shape :math:`(N,)`, result of localization evaluated on `explainer`.

        Raises:
            ValueError: If batch_size is larger than 1.

        Examples:
            >>> import numpy as np
            >>> import mindspore as ms
            >>> from mindspore.explainer.explanation import Gradient
            >>> from mindspore.explainer.benchmark import Robustness
            >>> from mindspore.train.serialization import load_checkpoint, load_param_into_net
            >>> # prepare your network and load the trained checkpoint file, e.g., resnet50.
            >>> network = resnet50(10)
            >>> param_dict = load_checkpoint("resnet50.ckpt")
            >>> load_param_into_net(network, param_dict)
            >>> # prepare your explainer to be evaluated, e.g., Gradient.
            >>> gradient = Gradient(network)
            >>> input_x = ms.Tensor(np.random.rand(1, 3, 224, 224), ms.float32)
            >>> target_label = ms.Tensor([0], ms.int32)
            >>> # robustness is a Robustness instance
            >>> res = robustness.evaluate(gradient, input_x, target_label)
        """

        self._check_evaluate_param(explainer, inputs, targets, saliency)
        if inputs.shape[0] > 1:
            raise ValueError(
                'Robustness only support a sample each time, but receive {}'.
                format(inputs.shape[0]))

        inputs_np = inputs.asnumpy()
        if isinstance(targets, int):
            targets = ms.Tensor([targets], ms.int32)
        if saliency is None:
            saliency = explainer(inputs, targets)
        saliency_np = saliency.asnumpy()

        norm = np.sqrt(
            np.sum(np.square(saliency_np),
                   axis=tuple(range(1, len(saliency_np.shape)))))
        if (norm == 0).any():
            log.warning(
                'Get saliency norm equals 0, robustness return NaN for zero-norm saliency currently.'
            )
            norm[norm == 0] = np.nan

        full_network = nn.SequentialCell(
            [explainer.network, self._activation_fn])
        original_outputs = full_network(inputs).asnumpy()
        sensitivities = []
        for _ in range(self._num_perturbations):
            perturbations = []
            for j, sample in enumerate(inputs_np):
                perturbation_on_single_sample = self._perturb_with_threshold(
                    full_network, np.expand_dims(sample, axis=0),
                    original_outputs[j])
                perturbations.append(perturbation_on_single_sample)
            perturbations = np.vstack(perturbations)
            perturbations_saliency = explainer(
                ms.Tensor(perturbations, ms.float32), targets).asnumpy()
            sensitivity = np.sqrt(
                np.sum((perturbations_saliency - saliency_np)**2,
                       axis=tuple(range(1, len(saliency_np.shape)))))
            sensitivities.append(sensitivity)
        sensitivities = np.stack(sensitivities, axis=-1)
        max_sensitivity = np.max(sensitivities, axis=1) / norm
        robustness_res = 1 / np.exp(max_sensitivity)
        return robustness_res
コード例 #9
0
def test_tensor_init():
    nparray = np.ones([2, 2], np.float32)
    ms.Tensor(nparray)

    ms.Tensor(nparray, dtype=ms.float32)
コード例 #10
0
def test_assign():
    context.set_context(mode=context.GRAPH_MODE)
    net = Assign()
    input_data = ms.Tensor(np.array(1).astype(np.int32))
    net_back = GradNet(net)
    net_back(input_data)
コード例 #11
0
 def construct(self, input_data, input1=ms.Tensor(np.random.randn(2, 3, 4, 5).astype(np.float32))):
     return self.softmax1(input_data)
コード例 #12
0
def test_assign_in_insert_grad():
    context.set_context(mode=context.GRAPH_MODE)
    net = AssignWhenInsertGrad().to_float(ms.float16)
    input_data = np.array([[1.2, 2.1], [2.2, 3.2]]).astype('float32')
    net_back = GradNet(net)
    net_back(ms.Tensor(input_data))
コード例 #13
0
def test_net_with_ndarray():
    """ test_net_with_ndarray """
    net = NetWithNDarray(0)
    input_data = np.array([[1.2, 2.1], [2.2, 3.2]]).astype('float32')

    net(ms.Tensor(input_data))
コード例 #14
0
 def __init__(self, dim):
     super(NetWithNDarray, self).__init__()
     self.softmax = nn.Softmax(dim)
     self.x = ms.Tensor(np.ones(shape=(1)).astype(np.float32))
コード例 #15
0
def test_print():
    a = ms.Tensor(np.ones((2, 3)))
    a.set_dtype(ms.int32)
    print(a)
コード例 #16
0
def test_tensor_add():
    a = ms.Tensor(np.ones([3, 3], np.float32))
    b = ms.Tensor(np.ones([3, 3], np.float32))
    a += b
コード例 #17
0
def test_float():
    a = ms.Tensor(np.ones((2, 3)), ms.float16)
    assert a.dtype == ms.float16
コード例 #18
0
def test_tensor_sub():
    a = ms.Tensor(np.ones([2, 3]))
    b = ms.Tensor(np.ones([2, 3]))
    b -= a
コード例 #19
0
ファイル: test_tensor.py プロジェクト: wenkai128/mindspore
def test_tensor_type_int16():
    t_int16_array = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int16))
    assert isinstance(t_int16_array, ms.Tensor)
    assert t_int16_array.shape() == (2, 3)
    assert t_int16_array.dtype() == ms.int16
コード例 #20
0
def test_tensor_mul():
    a = ms.Tensor(np.ones([3, 3]))
    b = ms.Tensor(np.ones([3, 3]))
    a *= b
コード例 #21
0
ファイル: test_tensor.py プロジェクト: wenkai128/mindspore
def test_tensor_type_uint64():
    t_uint64 = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint64))
    assert isinstance(t_uint64, ms.Tensor)
    assert t_uint64.shape() == (2, 3)
    assert t_uint64.dtype() == ms.uint64
コード例 #22
0
def test_tensor_dim():
    arr = np.ones((1, 6))
    b = ms.Tensor(arr)
    assert b.dim() == 2
コード例 #23
0
ファイル: test_tensor.py プロジェクト: wenkai128/mindspore
def test_add():
    x = ms.Tensor(ndarr)
    y = ms.Tensor(ndarr)
    z = x + y
    assert isinstance(z, ms.Tensor)
コード例 #24
0
def test_tensor_size():
    arr = np.ones((1, 6))
    b = ms.Tensor(arr)
    assert arr.size == b.size()
コード例 #25
0
ファイル: test_tensor.py プロジェクト: wenkai128/mindspore
def test_tensor_input_string():
    with pytest.raises(TypeError):
        input_data = 'ccc'
        ms.Tensor(input_data)
コード例 #26
0
def test_dtype():
    a = ms.Tensor(np.ones((2, 3), dtype=np.int32))
    assert a.dtype == ms.int32
コード例 #27
0
ファイル: test_tensor.py プロジェクト: wenkai128/mindspore
def test_tensor_input_list_string():
    with pytest.raises(TypeError):
        input_data = [[2, 3, '4', 5], [1, 2, 3, 4]]
        ms.Tensor(input_data)
コード例 #28
0
def test_asnumpy():
    npd = np.ones((2, 3))
    a = ms.Tensor(npd)
    a.set_dtype(ms.int32)
    assert a.asnumpy().all() == npd.all()
コード例 #29
0
ファイル: test_tensor.py プロジェクト: wenkai128/mindspore
def test_tensor_input_empty():
    with pytest.raises(TypeError):
        ms.Tensor()
コード例 #30
0
def test_tensor_type_float32_user_define():
    t = ms.Tensor(np.zeros([1, 2, 3]), ms.float32)
    assert isinstance(t, ms.Tensor)
    assert t.shape == (1, 2, 3)
    assert t.dtype == ms.float32