Implementation of MindSpore-Powered Models in CV (1) — Basic and Advanced MindSpore APIs

Implementation of MindSpore-Powered Models in CV (1) — Basic and Advanced MindSpore APIs

Implementation of MindSpore-Powered Models in CV (1) — Basic and Advanced MindSpore APIs

1. Introduction

1.1 About This Experiment

This experiment introduces the data structures and data types of MindSpore and the basic modules used by MindSpore to set up a neural network, such as dataset loading, neural network setup, and model training and evaluation. It aims to help trainees get familiar with the basic usage of MindSpore and master the basic development process of MindSpore.

1.2 Objective

● Understand the basic development process of MindSpore.

● Understand the functions of the MindSpore basic modules.

● Master the basic operations of MindSpore.

1.3 Background Knowledge

Knowledge of neural networks, perceptrons, multi-layer perceptrons, forward propagation, backward propagation, activation functions, loss functions, optimizers, validation methods.

1.4 Experiment Design

Tensor and data type, dataset loading, fully-connected network setup, model training, and model evaluation.

2 Experiment Process

2.1 Tensor and Data Type

Tensor is a basic data structure in the MindSpore network computing. Tensors of different dimensions represent different data. For example, a 0-dimensional tensor represents a scalar, a 1-dimensional tensor represents a vector, a 2-dimensional tensor represents a matrix, and a 3-dimensional tensor may represent the three channels of RGB images.

MindSpore tensors support 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 the computation process of MindSpore, the int data type in Python is converted into the defined int64 type, and the float data type is converted into the defined float32 type.

Step 1 Specify the MindSpore data type.

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

# Import MindSpore.
import mindspore
from mindspore import dtype 
from mindspore import Tensor
# The cell outputs multiple lines at the same time.
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

# Specify the data type.
a = 1
type(a)
b = Tensor(a, dtype.float64) 
b.dtype 

Step 2 Construct tensors.

During tensor construction, the tensor, float, int, Boolean, tuple, list, and NumPy.array types can be input. The tuple and list can store only data of the float, int, and Boolean types.

The data type can be specified during tensor initialization. However, if the data type is not specified, the initial values int, float, and bool respectively generate 0-dimensional tensors with mindspore.int32, mindspore.float32 and mindspore.bool_ data types. The data types of the 1-dimensional tensors generated by the initial values tuple and list correspond to those of tensors stored in the tuple and list. If multiple types of data are contained, the MindSpore data type corresponding to the type with the highest priority is selected (Boolean < int < float). If the initial value is Tensor, the data type is tensor. If the initial value is NumPy.array, the generated tensor data type corresponds to NumPy.array.

Use an array to create tensors:

import numpy as np
from mindspore import Tensor

# Use an array to create a tensor.
x = Tensor(np.array([[1, 2], [3, 4]]), dtype.int32)
x

Use a number to create tensors:
# Use a number to create tensors.
y = Tensor(1.0, dtype.int32)
z = Tensor(2, dtype.int32)
y
z

Use Boolean to create tensors:
# Use Boolean to create a tensor.
m = Tensor(True, dtype.bool_)
m

Use a tuple to create tensors:
# Use a tuple to create a tensor.
n = Tensor((1, 2, 3), dtype.int16)
n

Use a list to create tensors:
# Use a list to create a tensor.
p = Tensor([4.0, 5.0, 6.0], dtype.float64)
p

Use a constant to create tensors.
# Use a constant to create a tensor.
q = Tensor(1, dtype.float64)
q

Step 3 Specify attributes of a tensor.

Tensor attributes include shape and data type (dtype).

Shape: a tuple

Data type: a data type of 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

x = Tensor(np.array([[1, 2], [3, 4]]), dtype.int32)

x.shape # Shape
x.dtype # Data type
x.ndim  # Dimension
x.size  # Size

Step 4 Convert tensors.

asnumpy(): converts a tensor to an array of NumPy.

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

# Convert the tensor data type to NumPy.
y_array = y.asnumpy()

y
y_array

2.2 Dataset Loading

MindSpore.dataset provides APIs to load and process datasets, such as MNIST, CIFAR-10, CIFAR-100, VOC, ImageNet, and CelebA.

Step 1 Load the MNIST dataset

mindspore.dataset.MnistDataset

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

dataset_dir = "./data/train"  # Dataset path

# Read three images from the 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 three 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()

MindSpore can load datasets in different data storage formats. You can directly use the corresponding classes in mindspore.dataset to load data files in disks.

Step 2 Load the NumPy dataset.

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)

2.3 Fully-Connected Network Setup

Step 1 Set up a fully-connected neural network.

Fully-connected layer: mindspore.nn.Dense

in_channels: input channel

out_channels: output channel

weight_init: weight initialization. Default value: 'normal'.

import mindspore.nn as nn
from mindspore import Tensor

# Construct the input tensor.
input = Tensor(np.array([[1, 1, 1], [2, 2, 2]]), mindspore.float32)
print(input)

# Construct a fully-connected network. Set both in_channels and out_channels to 3.
net = nn.Dense(in_channels=3, out_channels=3, weight_init=1)
output = net(input)
print(output)

Step 2 Construct an activation function.

Rectified linear unit (ReLU) activation function: mindspore.nn.ReLU

input_x = Tensor(np.array([-1, 2, -3, 2, -1]), mindspore.float16)

relu = nn.ReLU()
output = relu(input_x)
print(output)

Step 3 Build a model.

Base class of all neural networks: mindspore.nn.Cell

import mindspore.nn as nn

class MyCell(nn.Cell):
    
    # Define operators.
    def __init__(self, ):
        super(MyCell, self).__init__()
        
         # Fully-connected layer
        self.fc = nn.Dense()

         # Activation function
        self.relu = nn.ReLU()

    # Build the network.
    def construct(self, x):
        x = self.fc(x)
        x = self.relu(x)
        return x

2.4 Model Training and Evaluation

Step 1 Define a loss function.

This cross entropy loss function is used to classify models. If the label data is not encoded in one-hot mode, set sparse to True.

mindspore.nn.SoftmaxCrossEntropyWithLogits

import mindspore.nn as nn

# Cross entropy loss function
loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True)

np.random.seed(0)
logits = Tensor(np.random.randint(0, 9, [1, 10]), mindspore.float32)
print(logits)

labels_np = np.ones([1,]).astype(np.int32)
labels = Tensor(labels_np)
print(labels)

output = loss(logits, labels)
print(output)

Step 2 Define an optimizer.

Common deep learning optimization algorithms include SGD, Adam, Ftrl, lazyadam, Momentum, RMSprop, Lars, Proximal_ada_grad, and lamb.

Momentum optimizer: mindspore.nn.Momentum

# optim = nn.Momentum(params, learning_rate=0.1, momentum=0.9, weight_decay=0.0) # params is the passed parameter.

Step 3 Build the model.

mindspore.Model

network: neural network

loss_fn: loss function

optimizer: optimizer

metrics: evaluation metrics

from mindspore import Model

# Define the neural network.
net = nn.Dense(in_channels=3, out_channels=3, weight_init=1)
# Define the loss function.
loss = nn.SoftmaxCrossEntropyWithLogits()
# Define the optimizer.
optim = nn.Momentum(params=net.trainable_params(), learning_rate=0.1, momentum=0.9)
# Build the model.
model = Model(network = net, loss_fn=loss, optimizer=optim, metrics=None)

Step 4 Train the model.

model.train

epoch: number of training epochs

train_dataset: training dataset

# model.train(epoch=10, train_dataset=train_dataset) # train_dataset is the passed parameter.

Step 5 Evalue the model.

model.eval

valid_dataset: validation dataset

# model.eval(valid_dataset=test_dataset) # test_dataset is the passed parameter.

3 Experiment Summary

This experiment introduces the data structures and types of MindSpore and the basic modules it uses to set up a neural network. It aims to help trainees learn how to load data sets, set up neural networks, train and evaluate models, become familiar with the basic usage of MindSpore, and master its basic development process.