コード例 #1
0
ファイル: _utils.py プロジェクト: noticeable/mindspore
def _to_full_tensor(elem, device_num, global_rank, scaling_sens=None):
    """Convert numpy to tensor, expanding batch dimension according to device_num, adapt to feed the data
       from host solution."""
    lst = []
    if not isinstance(elem, (tuple, list)):
        elem = [elem]
    if global_rank >= device_num:
        raise ValueError(
            "The global rank must be smaller than device number, the global rank is {}, "
            "the device num is {}".format(global_rank, device_num))

    for data in elem:
        if isinstance(data, np.ndarray):
            data = Tensor(data)
        if not isinstance(data, Tensor):
            raise ValueError("elements in tensors must be Tensor")
        shape_ = data.shape
        type_ = data.dtype
        new_shape = ()
        batchsize_per_device = 1
        for i, item in enumerate(shape_):
            if i == 0:
                new_shape += (item * device_num, )
                batchsize_per_device = item
            else:
                new_shape += (item, )
        new_tensor_numpy = np.zeros(new_shape, dtype_to_nptype(type_))
        start = global_rank * batchsize_per_device
        new_tensor_numpy[start:start + batchsize_per_device] = data.asnumpy()
        new_tensor = Tensor(new_tensor_numpy)
        lst.append(new_tensor)
    if scaling_sens:
        lst.append(Tensor(scaling_sens, mstype.float32))
    return tuple(lst)
コード例 #2
0
def test_depthwiseconv_relu6_onnx_load_run():
    onnx_file = 'depthwiseconv_relu6.onnx'
    input_channel = 3
    input = Tensor(np.ones([1, input_channel, 32, 32]).astype(np.float32) * 0.01)
    net = DepthwiseConv2dAndReLU6(input_channel, kernel_size=3)
    export(net, input, file_name=onnx_file, file_format='ONNX')

    import onnx
    import onnxruntime as ort

    print('--------------------- onnx load ---------------------')
    # Load the ONNX model
    model = onnx.load(onnx_file)
    # Check that the IR is well formed
    onnx.checker.check_model(model)
    # Print a human readable representation of the graph
    g = onnx.helper.printable_graph(model.graph)
    print(g)

    print('------------------ onnxruntime run ------------------')
    ort_session = ort.InferenceSession(onnx_file)
    input_map = {'x' : input.asnumpy()}
    # provide only input x to run model
    outputs = ort_session.run(None, input_map)
    print(outputs[0])
    # overwrite default weight to run model
    for item in net.trainable_params():
        input_map[item.name] = np.ones(item.default_input.asnumpy().shape, dtype=np.float32)
    outputs = ort_session.run(None, input_map)
    print(outputs[0])
コード例 #3
0
    def _get_loss(self, cb_params):
        """
        Get loss from the network output.

        Args:
            cb_params (_InternalCallbackParam): Callback parameters.

        Returns:
            Union[Tensor, None], if parse loss success, will return a Tensor value(shape is [1]), else return None.
        """
        if not self._is_parse_loss_success:
            # If parsing has failed before, avoid repeating it
            return None

        output = cb_params.net_outputs
        if output is None:
            logger.warning(
                "Can not find any output by this network, so will not collect loss in SummaryCollector."
            )
            self._is_parse_loss_success = False
            return None

        if isinstance(output, (int, float, Tensor)):
            loss = output
        elif isinstance(output, (list, tuple)) and output:
            # If the output is a list, since the default network returns loss first,
            # we assume that the first one is loss.
            loss = output[0]
        else:
            logger.warning(
                "The output type could not be identified, so no loss was recorded in SummaryCollector."
            )
            self._is_parse_loss_success = False
            return None

        if not isinstance(loss, Tensor):
            loss = Tensor(loss)

        precision = 4
        loss = Tensor(round(np.mean(loss.asnumpy()), precision))
        return loss
コード例 #4
0
class Conv2d(_Conv):
    r"""
    2D convolution layer.

    Applies a 2D convolution over an input tensor which is typically of shape :math:`(N, C_{in}, H_{in}, W_{in})`,
    where :math:`N` is batch size, :math:`C_{in}` is channel number, and :math:`H_{in}, W_{in})` are height and width.
    For each batch of shape :math:`(C_{in}, H_{in}, W_{in})`, the formula is defined as:

    .. math::

        out_j = \sum_{i=0}^{C_{in} - 1} ccor(W_{ij}, X_i) + b_j,

    where :math:`ccor` is the cross-correlation operator, :math:`C_{in}` is the input channel number, :math:`j` ranges
    from :math:`0` to :math:`C_{out} - 1`, :math:`W_{ij}` corresponds to the :math:`i`-th channel of the :math:`j`-th
    filter and :math:`out_{j}` corresponds to the :math:`j`-th channel of the output. :math:`W_{ij}` is a slice
    of kernel and it has shape :math:`(\text{ks_h}, \text{ks_w})`, where :math:`\text{ks_h}` and
    :math:`\text{ks_w}` are the height and width of the convolution kernel. The full kernel has shape
    :math:`(C_{out}, C_{in} // \text{group}, \text{ks_h}, \text{ks_w})`, where group is the group number
    to split the input in the channel dimension.

    If the 'pad_mode' is set to be "valid", the output height and width will be
    :math:`\left \lfloor{1 + \frac{H_{in} + 2 \times \text{padding} - \text{ks_h} -
    (\text{ks_h} - 1) \times (\text{dilation} - 1) }{\text{stride}}} \right \rfloor` and
    :math:`\left \lfloor{1 + \frac{W_{in} + 2 \times \text{padding} - \text{ks_w} -
    (\text{ks_w} - 1) \times (\text{dilation} - 1) }{\text{stride}}} \right \rfloor` respectively.

    The first introduction can be found in paper `Gradient Based Learning Applied to Document Recognition
    <http://vision.stanford.edu/cs598_spring07/papers/Lecun98.pdf>`_.

    Args:
        in_channels (int): The number of input channel :math:`C_{in}`.
        out_channels (int): The number of output channel :math:`C_{out}`.
        kernel_size (Union[int, tuple[int]]): The data type is int or a tuple of 2 integers. Specifies the height
            and width of the 2D convolution window. Single int means the value is for both the height and the width of
            the kernel. A tuple of 2 ints means the first value is for the height and the other is for the
            width of the kernel.
        stride (Union[int, tuple[int]]): The distance of kernel moving, an int number that represents
            the height and width of movement are both strides, or a tuple of two int numbers that
            represent height and width of movement respectively. Default: 1.
        pad_mode (str): Specifies padding mode. The optional values are
            "same", "valid", "pad". Default: "same".

            - same: Adopts the way of completion. The height and width of the output will be the same as
              the input. The total number of padding will be calculated in horizontal and vertical
              directions and evenly distributed to top and bottom, left and right if possible. Otherwise, the
              last extra padding will be done from the bottom and the right side. If this mode is set, `padding`
              must be 0.

            - valid: Adopts the way of discarding. The possible largest height and width of output will be returned
              without padding. Extra pixels will be discarded. If this mode is set, `padding`
              must be 0.

            - pad: Implicit paddings on both sides of the input. The number of `padding` will be padded to the input
              Tensor borders. `padding` must be greater than or equal to 0.

        padding (Union[int, tuple[int]]): Implicit paddings on both sides of the input. If `padding` is one integer,
                    the paddings of top, bottom, left and right are the same, equal to padding. If `padding` is a tuple
                    with four integers, the paddings of top, bottom, left and right will be equal to padding[0],
                    padding[1], padding[2], and padding[3] accordingly. Default: 0.
        dilation (Union[int, tuple[int]]): The data type is int or a tuple of 2 integers. Specifies the dilation rate
                                      to use for dilated convolution. If set to be :math:`k > 1`, there will
                                      be :math:`k - 1` pixels skipped for each sampling location. Its value must
                                      be greater or equal to 1 and bounded by the height and width of the
                                      input. Default: 1.
        group (int): Splits filter into groups, `in_ channels` and `out_channels` must be
            divisible by the number of groups. If the group is equal to `in_channels` and `out_channels`,
            this 2D convolution layer also can be called 2D depthwise convolution layer. Default: 1.
        has_bias (bool): Specifies whether the layer uses a bias vector. Default: False.
        weight_init (Union[Tensor, str, Initializer, numbers.Number]): Initializer for the convolution kernel.
            It can be a Tensor, a string, an Initializer or a number. When a string is specified,
            values from 'TruncatedNormal', 'Normal', 'Uniform', 'HeUniform' and 'XavierUniform' distributions as well
            as constant 'One' and 'Zero' distributions are possible. Alias 'xavier_uniform', 'he_uniform', 'ones'
            and 'zeros' are acceptable. Uppercase and lowercase are both acceptable. Refer to the values of
            Initializer for more details. Default: 'normal'.
        bias_init (Union[Tensor, str, Initializer, numbers.Number]): Initializer for the bias vector. Possible
            Initializer and string are the same as 'weight_init'. Refer to the values of
            Initializer for more details. Default: 'zeros'.
        data_format (str): The optional value for data format, is 'NHWC' or 'NCHW'.
            Default: 'NCHW'.

    Inputs:
        - **input** (Tensor) - Tensor of shape :math:`(N, C_{in}, H_{in}, W_{in})` \
            or `(N, H_{in}, W_{in}, C_{in})`.

    Outputs:
        Tensor of shape :math:`(N, C_{out}, H_{out}, W_{out})` or `(N, H_{out}, W_{out}, C_{out})`.

    Supported Platforms:
        ``Ascend`` ``GPU`` ``CPU``

    Examples:
        >>> net = nn.Conv2d(120, 240, 4, has_bias=False, weight_init='normal')
        >>> input = Tensor(np.ones([1, 120, 1024, 640]), mindspore.float32)
        >>> output = net(input).shape
        >>> print(output)
        (1, 240, 1024, 640)
    """
    @cell_attr_register
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 stride=1,
                 pad_mode='same',
                 padding=0,
                 dilation=1,
                 group=1,
                 has_bias=False,
                 weight_init='normal',
                 bias_init='zeros',
                 data_format='NCHW'):
        kernel_size = twice(kernel_size)
        stride = twice(stride)
        self._dilation = dilation
        dilation = twice(dilation)
        super(Conv2d,
              self).__init__(in_channels, out_channels, kernel_size, stride,
                             pad_mode, padding, dilation, group, has_bias,
                             weight_init, bias_init, data_format)
        self.conv2d = P.Conv2D(out_channel=self.out_channels,
                               kernel_size=self.kernel_size,
                               mode=1,
                               pad_mode=self.pad_mode,
                               pad=self.padding,
                               stride=self.stride,
                               dilation=self.dilation,
                               group=self.group,
                               data_format=self.format)
        self._init_depthwise_conv2d()
        self.bias_add = P.BiasAdd()

    def _init_depthwise_conv2d(self):
        """Initialize depthwise conv2d op"""
        if context.get_context("device_target") == "Ascend" and self.group > 1:
            self.dilation = self._dilation
            Validator.check_equal_int(self.group, self.in_channels, 'group')
            Validator.check_equal_int(self.group, self.out_channels, 'group')
            self.conv2d = P.DepthwiseConv2dNative(channel_multiplier=1,
                                                  kernel_size=self.kernel_size,
                                                  pad_mode=self.pad_mode,
                                                  pad=self.padding,
                                                  stride=self.stride,
                                                  dilation=self.dilation)
            weight_shape = [1, self.in_channels, *self.kernel_size]
            if isinstance(self.weight_init, Tensor):
                self.weight_init = Tensor(
                    self.weight_init.asnumpy().swapaxes(0, 1),
                    self.weight_init.dtype)
            if isinstance(self.weight_init, Initializer):
                self.weight_init.shape = weight_shape
            self.weight = Parameter(initializer(self.weight_init,
                                                weight_shape),
                                    name='weight')

    def construct(self, x):
        output = self.conv2d(x, self.weight)
        if self.has_bias:
            output = self.bias_add(output, self.bias)
        return output

    def extend_repr(self):
        s = 'input_channels={}, output_channels={}, kernel_size={},' \
            'stride={},  pad_mode={}, padding={}, dilation={}, ' \
            'group={}, has_bias={}' \
            'weight_init={}, bias_init={}, format={}'.format(
                self.in_channels,
                self.out_channels,
                self.kernel_size,
                self.stride,
                self.pad_mode,
                self.padding,
                self.dilation,
                self.group,
                self.has_bias,
                self.weight_init,
                self.bias_init,
                self.format)
        return s
コード例 #5
0
ファイル: array_ops_vm_impl.py プロジェクト: yrpang/mindspore
 def vm_impl(x, axis):
     if isinstance(x, float):
         x = Tensor(np.array([x]))
     x = x.asnumpy()
     out = vm.expand_dims(x, axis)
     return Tensor(out)
コード例 #6
0
def test_choose_param_with_input():
    choose = ChooseInitParameterWithInput()
    input = Tensor(np.zeros(2), dtype=mstype.int32)
    expect = Tensor(np.ones(2), dtype=mstype.int32)
    out = choose(input)
    assert np.allclose(expect.asnumpy(), out.asnumpy())
コード例 #7
0
def test_choose_init_param():
    choose = ChooseInitParameter()
    expect = Tensor(np.ones(2), dtype=mstype.int32)
    out = choose()
    assert np.allclose(out.asnumpy(), expect.asnumpy())
コード例 #8
0
def test_choose_const_y():
    tensor_x = Tensor(np.zeros(2), dtype=mstype.int32)
    tensor_y = Tensor(np.ones(2), dtype=mstype.int32)
    choose = ChooseOneConst(1, tensor_x, tensor_y)
    out = choose()
    assert np.allclose(tensor_y.asnumpy(), out.asnumpy())
コード例 #9
0
def test_choose_input_y():
    choose = ChooseOneParam(1)
    tensor_x = Tensor(1, dtype=mstype.int32)
    tensor_y = Tensor(2, dtype=mstype.int32)
    out = choose(tensor_x, tensor_y)
    assert np.allclose(tensor_y.asnumpy(), out.asnumpy())
コード例 #10
0
def test_choose_input_x():
    choose = ChooseOneParam(0)
    tensor_x = Tensor(np.zeros(2), dtype=mstype.int32)
    tensor_y = Tensor(np.ones(2), dtype=mstype.int32)
    out = choose(tensor_x, tensor_y)
    assert np.allclose(tensor_x.asnumpy(), out.asnumpy())
コード例 #11
0
    context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")

    print("test lenet predict start")
    seed = 0
    np.random.seed(seed)
    batch = 32
    channel = 1
    input_h = 32
    input_w = 32
    origin_data = np.random.uniform(low=0,
                                    high=255,
                                    size=(batch, channel, input_h,
                                          input_w)).astype(np.float32)
    origin_data.tofile("lenet_input_data.bin")

    input_data = Tensor(origin_data)
    print(input_data.asnumpy())
    net = LeNet()
    ckpt_file_path = "./tests/ut/python/predict/checkpoint_lenet.ckpt"
    predict_args = parser.parse_args()
    model_path_name = predict_args.path

    is_ckpt_exist = os.path.exists(ckpt_file_path)
    if is_ckpt_exist:
        param_dict = load_checkpoint(ckpt_file_name=ckpt_file_path)
        load_param_into_net(net, param_dict)
        export(net, input_data, file_name=model_path_name, file_format='LITE')
        print("test lenet predict success.")
    else:
        print("checkpoint file is not exist.")