Introduction to MindSpore

1.1 about MindSpore

MindSpore is a Huawei-developed AI computing framework for end-to-end cloud on-demand collaboration in all scenarios. It provides a unified API for all scenarios and provides end-to-end capabilities for model development, model operation, and model deployment in all scenarios.

MindSpore uses an edge-cloud on-demand collaborative distributed architecture, a new paradigm of differential Native programming, and a new AI Native execution model to achieve better resource efficiency, security and reliability, while lowering the threshold for AI development in the industry and unlocking centerm’s computing power to power Pratt & Whitney AI.

For more information, please refer to MindSpore website

1.2 MindSpore installation

MindSpore supports Ascend, GPU, and CPU. You can choose a MindSpore package suitable for your platform. Here is an example of how to install MindSpore on Windows platform.

MindSpore is Windows supported, but only in CPU mode (Windows GPU is not currently supported). If you want to try MindSpore GPU support, you need to use Ubuntu series or Huawei EulerOS.

1.3 installation Anaconda

It is highly recommended to use Anaconda for the management of Python virtual environments.

You can download it from Anaconda and go all the way to Next.

1.4 Creating the MindSpore Python Env

Currently MindSpore only supports Python 3.7.5. The Pyhton Env created below is called MindSpore:

Conda create -n mindspore python=3.7.5Copy the code

1.5 installation MindSpore

We installed using PIP mode

PIP install ms-release.obs.cn-north-4.myhuaweicloud.com/1.2.1/MindS… –trusted-host ms-release.obs.cn-north-4.myhuaweicloud.com -i pypi.tuna.tsinghua.edu.cn/simple

1.6 Verifying the installation

python -c “import mindspore; print(mindspore.version)”

If the MindSpore version number is printed, MindSpore was successfully installed. If No module named ‘MindSpore’ is printed, MindSpore was not successfully installed.

2 MindSpore basis

2.1 Tensors and data types

Tensor is the basic data structure in MindSpore network operations. The data types in the tensors refer to dtype. The tensors of different dimensions represent different data respectively, 0-dimensional tensor represents scalar, 1-dimensional tensor represents vector, 2-dimensional tensor represents matrix, 3-dimensional tensor can represent RGB three channels of color images and so on.

The MindSpore tensor supports different data types, including INT8, INT16, INT32, INT64, Uint8, Uint16, uint32, uint64, FLOAT16, FLOAT32, FLOAT64, and BOOL_, which correspond to the data types of NumPy. In MindSpore’s processing, Ints in Python are converted to the defined int64 type and floats to the defined float32 type.

Step 1 Specify the MindSpore data type

Import MindSpore and set the Jupyter Notebook cell to output multiple lines at the same time.

# import MindSpore
import mindspore
from mindspore import dtype 
from mindspore import Tensor

# cell outputs multiple lines simultaneously
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
Copy the code
# specify the data type
a = 1
type(a)
b = Tensor(a, dtype.float64) 
b.dtype 
Copy the code

Step 2 tensor construction

Tensor: Tensor, float, int, bool, tuple, list, numpy. array. Tuple and list can only store float, int, and bool.

When you initialize the Tensor, you specify dtype. Int, float, bool, mindSpore. Int32, mindspore. Float32, Mindspore. The 1-dimensional Tensor data types generated by tuple and list correspond to the values stored in the tuple and list. If you have multiple different kinds of values, then you need to take precedence: Bool < int < float, select the mindspore data type corresponding to the highest relative priority type. If the initial value is Tensor, then the resulting Tensor data type is the same; If the initial value is numpy. array, then the resultant Tensor data type corresponds to that.

Create tensors with arrays

import numpy as np
from mindspore import Tensor

Create tensors from arrays
x = Tensor(np.array([[1.2], [3.4]]), dtype.int32)
x
Copy the code

Create tensors with numbers

# Create tensors with numbers
y = Tensor(1.0, dtype.int32)
z = Tensor(2, dtype.int32)
y
z
Copy the code

Create tensors with Bool

# create tensors with Bool
m = Tensor(True, dtype.bool_)
m
Copy the code

Create tensors with tuples

Create tensors with tuples
n = Tensor((1.2.3), dtype.int16)
n
Copy the code

Create tensors with lists

Create tensors with list
p = Tensor([4.0.5.0.6.0], dtype.float64)
p
Copy the code

Create tensors with constants

# Create tensors with constants
q = Tensor(1, dtype.float64)
q
Copy the code

Step 3 Properties of tensors

The properties of tensors include shapes and data types.

  • Shape: Tensor’s shape, it’s a tuple.
  • Data type: Tensor’s Dtype, which is a data type for MindSpore.
x = Tensor(np.array([[1.2], [3.4]]), dtype.int32)
x_shape = x.shape  # shape
x_dtype = x.dtype  # data type

x_shape
x_dtype
Copy the code

x = Tensor(np.array([[1.2], [3.4]]), dtype.int32)

x.shape # shape
x.dtype # data type
x.ndim  # dimension
x.size  # size
Copy the code

Step 4 Tensor method

Asnumpy () : The array that translates the Tensor into NumPy.

y = Tensor(np.array([[True.True], [False.False]]), dtype.bool_)

Translate the Tensor data type to NumPy
y_array = y.asnumpy()

y
y_array
Copy the code

2.2 Data set loading

The MindSpore. Dataset provides apis to load and process a variety of common datasets, such as MNIST, CIFAR-10, CIFAR-100, VOC, ImageNet, CelebA, etc.

Procedure Step 1 Load the MNIST data set

mindspore.dataset.MnistDataset

import os
import mindspore.dataset as ds
import matplotlib.pyplot as plt

dataset_dir = "./data/train"  Data set path

Read 3 images from mnist dataset
mnist_dataset = ds.MnistDataset(dataset_dir=dataset_dir, num_samples=3)

# Set the image size
plt.figure(figsize=(8.8))
i = 1

Print 3 subgraphs
for dic in mnist_dataset.create_dict_iterator(output_numpy=True):
    plt.subplot(3.3,i)
    plt.imshow(dic['image'] [:, :,0])
    plt.axis('off')
    i +=1

plt.show()
Copy the code

MindSpore also supports loading data sets in a variety of data storage formats. Users can directly load data files from disks using the corresponding classes in MindSpore. Dataset.

Step 2 Load the NumPy data set

mindspore.dataset.NumpySlicesDataset

import mindspore.dataset as ds

data = ds.NumpySlicesDataset([1.2.3], column_names=["col_1"])
for x in data.create_dict_iterator():
    print(x)
Copy the code

3 Reference Materials

  • Gitee.com/mindspore/m…
  • Github.com/mindspore-a…