4 Deep Learning and AI Development Framework Lab Guide - MindSpore
4 Deep Learning and AI Development Framework Lab Guide - MindSpore
HCIA-AI
Deep Learning and AI
Development Framework
Lab Guide
ISSUE: 3.5
1
Copyright © Huawei Technologies Co., Ltd. 2023. All rights reserved.
No part of this document may be reproduced or transmitted in any form or by any means without
prior written consent of Huawei Technologies Co., Ltd.
and other Huawei trademarks are trademarks of Huawei Technologies Co., Ltd.
All other trademarks and trade names mentioned in this document are the property of their
respective holders.
Notice
The purchased products, services and features are stipulated by the contract made between
Huawei and the customer. All or part of the products, services and features described in this
document may not be within the purchase scope or the usage scope. Unless otherwise specified
in the contract, all statements, information, and recommendations in this document are provided
"AS IS" without warranties, guarantees or representations of any kind, either express or implied.
The information in this document is subject to change without notice. Every effort has been made
in the preparation of this document to ensure accuracy of the contents, but all statements,
information, and recommendations in this document do not constitute a warranty of any kind,
express or implied.
Overview
This document applies to candidates who are preparing for the HCIA-AI exam and others who want
to learn basic AI knowledge and basic MindSpore programming.
Description
This guide introduces the following five exercises:
Exercise 1: MindSpore basics, which describes the basic syntax and common modules of
MindSpore.
Exercise 2: Handwritten character recognition, in which the MindSpore framework is used to
recognize handwritten characters.
Exercise 3: MobileNetV2 image classification, which mainly introduces the classification of
flower images using the lightweight network MobileNetV2.
Exercise 4: ResNet-50 image classification exercise, which mainly introduces the classification of
flower images using the ResNet-50 model.
Exercise 5: TextCNN sentiment analysis, which mainly introduces sentiment analysis of
statements using the TextCNN model.
Contents
1 MindSpore Basics
1.1 Introduction
1.1.1 About This Lab
This exercise introduces the tensor data structure of MindSpore. By performing a series of
operations on tensors, you can understand the basic syntax of MindSpore.
1.1.2 Objectives
Master the method of creating tensors.
Master the attributes and methods of tensors.
1.2 Procedure
1.2.1 Introduction to Tensors
Tensor is a basic data structure in MindSpore network computing. For details about data types in
tensors, see the dtype description on the MindSpore official website.
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.
1.2.1.1 Creating a Tensor
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 generate 0-dimensional tensors with mindspore.int32,
mindspore.float32 and mindspore.bool_ data types, respectively. The data types of the 1-
dimensional tensors generated by the initial values tuple and list correspond to the data types of
tensors stored in the tuple and list. If multiple types of data are contained, the MindSpore data type
corresponding to the data type with the highest priority is selected (Boolean < int < float). If the
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 7
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.
# Import MindSpore.
import mindspore
# The cell outputs multiple lines at the same time.
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
import numpy as np
from mindspore import Tensor
from mindspore import dtype
# Use an array to create a tensor.
x = Tensor(np.array([[1, 2], [3, 4]]), dtype.int32)
x
Output:
Output:
Output:
Code:
Output
Output:
Output:
shape = (2, 2)
ones = ops.Ones()
output = ones(shape,dtype.float32)
print(output)
zeros = ops.Zeros()
output = zeros(shape, dtype.float32)
print(output)
Output:
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 9
[[1. 1.]
[1. 1.]]
[[0. 0.]
[0. 0.]]
x.shape # Shape
x.dtype # Data type
x.ndim # Dimension
x.size # Size
Output:
(2, 2)
mindspore.int32
2
4
y
y_array
Output:
Output:
Output:
[[[0. 1.]
[2. 3.]]
[[4. 5.]
[6. 7.]]]
zeros = ops.Zeros()
output = zeros((2,2), dtype.float32)
print("output: {}".format(type(output)))
n_output = output.asnumpy()
print("n_output: {}".format(type(n_output)))
Output:
import os
import mindspore.dataset as ds
import matplotlib.pyplot as plt
Output:
import numpy as np
np.random.seed(58)
class DatasetGenerator:
# When a dataset object is instantiated, the __init__ function is called. You can perform operations such as data
initialization.
def __init__(self):
self.data = np.random.sample((5, 2))
self.label = np.random.sample((5, 1))
# Define the __getitem__ function of the dataset class to support random access and obtain and return data in the
dataset based on the specified index value.
def __getitem__(self, index):
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 12
Output:
ds.config.set_seed(58)
# Shuffle the data sequence. buffer_size indicates the size of the shuffled buffer in the dataset.
dataset = dataset.shuffle(buffer_size=10)
# Divide the dataset into batches. batch_size indicates the number of data records contained in each batch. Set
this parameter to 2.
dataset = dataset.batch(batch_size=2)
Output:
Code:
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 13
DATA_DIR = './MNIST/train'
# Obtain six samples.
mnist_dataset = ds.MnistDataset(DATA_DIR, num_samples=6, shuffle=False)
# View the original image data.
mnist_it = mnist_dataset.create_dict_iterator()
data = next(mnist_it)
plt.imshow(data['image'].asnumpy().squeeze(), cmap=plt.cm.gray)
plt.title(data['label'].asnumpy(), fontsize=20)
plt.show()
Output:
Output:
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 14
import mindspore as ms
import mindspore.nn as nn
from mindspore import Tensor
import numpy as np
Output:
[[1. 1. 1.]
[2. 2. 2.]]
[[3. 3. 3.]
[6. 6. 6.]]
print(conv2d(input_x).shape)
Output:
relu = nn.ReLU()
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 15
print(output)
Output:
[0. 2. 0. 2. 0.]
print(max_pool2d(input_x).shape)
Output:
flatten = nn.Flatten()
input_x = Tensor(np.ones([1, 16, 5, 5]), ms.float32)
output = flatten(input_x)
print(output.shape)
Output:
(1, 400)
class LeNet5(nn.Cell):
"""
Lenet network structure
"""
def __init__(self, num_class=10, num_channel=1):
super(LeNet5, self).__init__()
# Define the required operations.
self.conv1 = nn.Conv2d(num_channel, 6, 5, pad_mode='valid')
self.conv2 = nn.Conv2d(6, 16, 5, pad_mode='valid')
self.fc1 = nn.Dense(16 * 4 * 4, 120)
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 16
Output:
import numpy as np
import mindspore.nn as nn
from mindspore import Tensor
import mindspore.dataset as ds
import mindspore as ms
loss = nn.L1Loss()
output_data = Tensor(np.array([[1, 2, 3], [2, 3, 4]]).astype(np.float32))
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 17
Output:
1.5
import mindspore.dataset.transforms.c_transforms as C
import mindspore.dataset.vision.c_transforms as CV
from mindspore.train.callback import LossMonitor
DATA_DIR = './MNIST/train'
mnist_dataset = ds.MnistDataset(DATA_DIR)
resize_op = CV.Resize((28,28))
rescale_op = CV.Rescale(1/255,0)
hwc2chw_op = CV.HWC2CHW()
# dataset is an input parameter, which indicates the training set, and epoch indicates the number of training
epochs of the training set.
model.train(epoch=1, train_dataset=mnist_dataset,callbacks=[loss_cb])
# Test set
DATA_DIR = './forward_mnist/MNIST/test'
dataset = ds.MnistDataset(DATA_DIR)
resize_op = CV.Resize((28,28))
rescale_op = CV.Rescale(1/255,0)
hwc2chw_op = CV.HWC2CHW()
import mindspore as ms
# net indicates a defined network model, which is used before or after training.
ms.save_checkpoint(net, "./MyNet.ckpt") # net indicates the training network, and ./MyNet.ckpt indicates the
path for saving the network model.
Output:
2. The other one is to save the interface during network model training. MindSpore automatically
saves the number of epochs and number of steps set during training. That is, the intermediate
weight parameters generated during the model training process are also saved to facilitate network
fine-tuning and stop training.
Code:
import numpy as np
import mindspore.nn as nn
import mindspore.ops as ops
from mindspore import Tensor
from mindspore import ParameterTuple, Parameter
from mindspore import dtype as mstype
class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()
self.matmul = ops.MatMul()
self.z = Parameter(Tensor(np.array([1.0], np.float32)), name='z')
Output:
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 20
class GradNetWrtX(nn.Cell):
def __init__(self, net):
super(GradNetWrtX, self).__init__()
self.net = net
self.params = ParameterTuple(net.trainable_params())
self.grad_op = ops.GradOperation(get_by_list=True)
Output:
1.3 Question
When the following code is used to create two tensors, t1 and t2, can t1 be created properly? Check
whether the two tensors have the same outputs. If not, what is the difference?
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 21
2.1 Introduction
2.1.1 About This Exercise
This exercise implements the MNIST handwritten character recognition, which is a typical case in the
deep learning field. The whole process is as follows:
Process the required dataset. (The MNIST dataset is used in this example.)
Define a network. (A simple fully-connected network is built in this example.)
Define a loss function and an optimizer.
Load the dataset and perform training. After the training is complete, use the test set for
validation.
2.2 Preparations
Before you start, check whether MindSpore has been correctly installed. You are advised to install
MindSpore on your computer by referring to the MindSpore official website
https://www.mindspore.cn/install/en.
In addition, you should have basic mathematical knowledge, including knowledge of Python coding
basics, probability, and matrices.
Recommended environment:
Version: MindSpore 1.7
Programming language: Python 3.7
└─MNIST
├─ test
│ t10k-images.idx3-ubyte
│ t10k-labels.idx1-ubyte
│
└─ train
train-images.idx3-ubyte
train-labels.idx1-ubyte
2.3.2 Procedure
Step 1 Import the Python library and module and configure running information.
Import the required Python library.
Currently, the os library is required. Other required libraries will not be described here. For details
about the MindSpore modules, see the MindSpore API page. You can use context.set_context to
configure the information required for running, such as the running mode, backend information, and
hardware information.
Import the context module and configure the required information.
Code:
import mindspore as ms
import mindspore.context as context
import mindspore.dataset as ds
import mindspore.dataset.transforms.c_transforms as C
import mindspore.dataset.vision.c_transforms as CV
from mindspore.nn.metrics import Accuracy
context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
The graph mode is used in this exercise. You can configure hardware information as required. For
example, if the code runs on the Ascend AI processor, set device_target to Ascend. This rule also
applies to the code running on the CPU and GPU. For details about parameters, see the
context.set_context API description at
https://www.mindspore.cn/docs/en/r1.7/api_python/mindspore.context.html.
Code:
In the preceding information, batch_size indicates the number of data records in each batch.
Assume that each batch contains 32 data records. Modify the image size, normalization, and image
channel, and then modify the data type of the label. Perform the shuffle operation, set batch_size,
and set drop_remainder to True. In this case, data that cannot form a batch in the dataset will be
discarded.
MindSpore supports multiple data processing and augmentation operations, which are usually used
together. For details, see Data Processing and Augmentation on the MindSpore official website.
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 24
# Display the first 10 images and the labels, and check whether the images are correctly labeled.
ds = create_dataset(training=False)
data = ds.create_dict_iterator().__next__()
images = data['image'].asnumpy()
labels = data['label'].asnumpy()
plt.figure(figsize=(15,5))
for i in range(1,11):
plt.subplot(2, 5, i)
plt.imshow(np.squeeze(images[i]))
plt.title('Number: %s' % labels[i])
plt.xticks([])
plt.show()
Output:
# Create a model. The model consists of three fully connected layers. The final output layer uses softmax for
classification (10 classes consisting of numbers 0 to 9.)
class ForwardNN(nn.Cell):
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 25
def __init__(self):
super(ForwardNN, self).__init__()
self.flatten = nn.Flatten()
self.fc1 = nn.Dense(784, 512, activation='relu')
self.fc2 = nn.Dense(512, 128, activation='relu')
self.fc3 = nn.Dense(128, 10, activation=None)
# Create a network, loss function, validation metric, and optimizer, and set related hyperparameters.
lr = 0.001
num_epoch = 10
momentum = 0.9
net = ForwardNN()
loss = nn.loss.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
metrics={"Accuracy": Accuracy()}
opt = nn.Adam(net.trainable_params(), lr)
# Build a model.
model = Model(net, loss, opt, metrics)
config_ck = CheckpointConfig(save_checkpoint_steps=1875, keep_checkpoint_max=10)
ckpoint_cb = ModelCheckpoint(prefix="checkpoint_net",directory = "./ckpt" ,config=config_ck)
# Generate a dataset.
ds_eval = create_dataset(False, batch_size=32)
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 26
ds_train = create_dataset(batch_size=32)
# Train the model.
loss_cb = LossMonitor(per_print_times=1875)
time_cb = TimeMonitor(data_size=ds_train.get_dataset_size())
print("============== Starting Training ==============")
model.train(num_epoch, ds_train,callbacks=[ckpoint_cb,loss_cb,time_cb ],dataset_sink_mode=False)
Although loss values may fluctuate, they gradually decrease and the accuracy gradually increases in
general. Loss values displayed each time may be different because of their randomicity. The
following is an example of loss values output during training:
# Use the test set to validate the model and print the overall accuracy.
metrics=model.eval(ds_eval)
print(metrics)
Output:
{'Accuracy': 0.9740584935897436}
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 27
3.1 Introduction
In this exercise, the lightweight network MobileNetV2 is used to classify flower image datasets.
3.2 Preparations
Before you start, check whether MindSpore has been correctly installed. You are advised to install
MindSpore on your computer by referring to the MindSpore official website
https://www.mindspore.cn/install/en.
In addition, you should have basic mathematical knowledge, including knowledge of Python coding
basics, probability, and matrix.
Recommended environment:
Version: MindSpore 1.7
Programming language: Python 3.7
flower_photos_train
├── daisy
├── dandelion
├── roses
├── sunflowers
├── tulips
├── LICENSE.txt
flower_photos_test
├── daisy
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 28
├── dandelion
├── roses
├── sunflowers
├── tulips
├── LICENSE.txt
Obtain the datasets from the following links:
https://ascend-professional-construction-dataset.obs.myhuaweicloud.com/deep-
learning/flower_photos_train.zip
https://ascend-professional-construction-dataset.obs.myhuaweicloud.com/deep-learning/flower_photos_test.zip
3.3.2 Procedure
Step 1 Load the dataset.
Define the create_dataset function, use the ImageFolderDataset API to load the flower image
classification dataset, and perform image enhancement on the dataset. Code:
import mindspore.dataset as ds
import mindspore.dataset.vision.c_transforms as CV
from mindspore import dtype as mstype
train_data_path = 'flower_photos_train'
val_data_path = 'flower_photos_test'
# Set the value of the batch_size. Discard the samples if the number of samples last fetched is less than the
value of batch_size.
data_set = data_set.batch(batch_size, drop_remainder=True)
return data_set
dataset_train = create_dataset(train_data_path)
dataset_val = create_dataset(val_data_path)
data = next(dataset_train.create_dict_iterator())
images = data["image"]
labels = data["label"]
# class_name corresponds to label. Labels are marked in ascending order of the folder character string.
class_name = {0:'daisy',1:'dandelion',2:'roses',3:'sunflowers',4:'tulips'}
plt.figure(figsize=(15, 7))
for i in range(len(labels)):
# Obtain an image and its label.
data_image = images[i].asnumpy()
data_label = labels[i]
# Process images for display.
data_image = np.transpose(data_image, (1, 2, 0))
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
data_image = std * data_image + mean
data_image = np.clip(data_image, 0, 1)
# Display the image.
plt.subplot(3, 6, i + 1)
plt.imshow(data_image)
plt.title(class_name[int(labels[i].asnumpy())])
plt.axis("off")
plt.show()
Output:
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 30
In the inverted residual block structure, the 1 x 1 convolution is used for dimension increase, the 3 x
3 depthwise convolution is used, and the 1 x 1 convolution is used for dimension reduction. This
structure is opposite to the residual block structure. For the residual block, the 1 x 1 convolution is
first used for dimension reduction, then the 3 x 3 convolution is used, and finally the 1 x 1
convolution is used for dimension increase.
For details, see the MobileNetV2 paper at https://arxiv.org/pdf/1801.04381.pdf.
Code:
import numpy as np
import mindspore as ms
import mindspore.nn as nn
import mindspore.ops as ops
# Make sure that round down does not go down by more than 10%.
if new_v < 0.9 * v:
new_v += divisor
return new_v
class GlobalAvgPooling(nn.Cell):
def __init__(self):
super(GlobalAvgPooling, self).__init__()
self.mean = ops.ReduceMean(keep_dims=False)
class ConvBNReLU(nn.Cell):
class InvertedResidual(nn.Cell):
layers = []
if expand_ratio != 1:
layers.append(ConvBNReLU(inp, hidden_dim, kernel_size=1))
layers.extend([
# dw
ConvBNReLU(hidden_dim, hidden_dim,
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 32
stride=stride, groups=hidden_dim),
# pw-linear
nn.Conv2d(hidden_dim, oup, kernel_size=1,
stride=1, has_bias=False),
nn.BatchNorm2d(oup),
])
self.conv = nn.SequentialCell(layers)
self.add = ops.Add()
self.cast = ops.Cast()
class MobileNetV2Backbone(nn.Cell):
def _initialize_weights(self):
self.init_parameters_data()
for _, m in self.cells_and_names():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.set_data(ms.Tensor(np.random.normal(0, np.sqrt(2. / n),
m.weight.data.shape).astype("float32")))
@property
def get_features(self):
return self.features
class MobileNetV2Head(nn.Cell):
def _initialize_weights(self):
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 34
self.init_parameters_data()
for _, m in self.cells_and_names():
if isinstance(m, nn.Dense):
m.weight.set_data(ms.Tensor(np.random.normal(
0, 0.01, m.weight.data.shape).astype("float32")))
if m.bias is not None:
m.bias.set_data(
ms.numpy.zeros(m.bias.data.shape, dtype="float32"))
class MobileNetV2Combine(nn.Cell):
def mobilenet_v2(num_classes):
backbone_net = MobileNetV2Backbone()
head_net = MobileNetV2Head(backbone_net.out_channels,num_classes)
return MobileNetV2Combine(backbone_net, head_net)
import mindspore
import mindspore.nn as nn
from mindspore.train import Model
from mindspore import Tensor, save_checkpoint
from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor
from mindspore.train.serialization import load_checkpoint, load_param_into_net
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 35
train_step_size = dataset_train.get_dataset_size()
epoch_size = 20
lr = nn.cosine_decay_lr(min_lr=0.0, max_lr=0.1,total_step=epoch_size *
train_step_size,step_per_epoch=train_step_size,decay_epoch=200)
# Define the optimizer.
network_opt = nn.Momentum(params=network.trainable_params(), learning_rate=0.01, momentum=0.9)
# Set the number of steps for saving a model and the maximum number of models that can be saved.
ckpt_config = CheckpointConfig(save_checkpoint_steps=100, keep_checkpoint_max=10)
# Save the model. Set the name, path, and parameters for saving the model.
ckpoint_cb = ModelCheckpoint(prefix="mobilenet_v2", directory='./ckpt', config=ckpt_config)
Output:
plt.show()
visualize_model('ckpt/mobilenet_v2-5_201.ckpt', dataset_val)
Output:
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 37
3.4 Question
What API is used to read pre-trained models?
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 38
4.1 Introduction
This exercise implements the ResNet-50 image classification, a classic case in the deep learning field.
The entire process is as follows:
Process the required dataset. (The flower image dataset is used in this example.)
Define a network. You need to set up a ResNet-50 model structure.
Define a loss function and an optimizer.
Load the dataset and perform training. After the training is complete, use the test set for
validation.
4.2 Preparations
Before you start, check whether MindSpore has been correctly installed. You are advised to install
MindSpore on your computer by referring to the MindSpore official website
https://www.mindspore.cn/install/en.
In addition, you should have basic mathematical knowledge, including knowledge of Python coding
basics, probability, and matrices.
Recommended environment:
Version: MindSpore 1.7
Programming language: Python 3.7
flower_photos_train
├── daisy
├── dandelion
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 39
├── roses
├── sunflowers
├── tulips
├── LICENSE.txt
flower_photos_test
├── daisy
├── dandelion
├── roses
├── sunflowers
├── tulips
├── LICENSE.txt
Obtain the datasets from the following links:
https://ascend-professional-construction-dataset.obs.myhuaweicloud.com/deep-
learning/flower_photos_train.zip
https://ascend-professional-construction-dataset.obs.myhuaweicloud.com/deep-learning/flower_photos_test.zip
4.3.2 Procedure
Step 1 Import the Python library and module and configure running information.
Import the required Python library.
For details about the MindSpore modules, see the MindSpore API page.
You can use context.set_context to configure the information required for running, such as the
running mode, backend information, and hardware information.
Import the context module and configure the required information.
Code:
import mindspore
# MindSpore library
import mindspore.dataset as ds
# Dataset processing module
from mindspore.dataset.vision import c_transforms as vision
# Image enhancement module
from mindspore import context
#Environment setting module
import mindspore.nn as nn
# Neural network module
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 40
cfg = edict({
'data_path': 'flowers/flower_photos_train', # Path of the training dataset
'test_path':'flowers/flower_photos_train', # Path of the test dataset
'data_size': 3616,
'HEIGHT': 224, # Image height
'WIDTH': 224, # Image width
'_R_MEAN': 123.68, # Average value of CIFAR-10
'_G_MEAN': 116.78,
'_B_MEAN': 103.94,
'_R_STD': 1, # Customized standard deviation
'_G_STD': 1,
'_B_STD':1,
'_RESIZE_SIDE_MIN': 256, # Minimum resize value for image enhancement
'_RESIZE_SIDE_MAX': 512,
Datasets are crucial for training. A good dataset can effectively improve training accuracy and
efficiency. Generally, before loading a dataset, you need to perform some operations on the dataset.
Define a dataset and data operations.
We define the create_dataset function to create a dataset. In this function, we define the data
augmentation and processing operations to be performed:
Read the dataset.
Define parameters required for data augmentation and processing.
Generate corresponding data augmentation operations according to the parameters.
Use the map function to apply data operations to the dataset.
Process the generated dataset.
Display the processed data as an example.
Code:
# Data processing
def read_data(path,config,usage="train"):
# Read the source dataset of an image from a directory.
dataset = ds.ImageFolderDataset(path,
class_indexing={'daisy':0,'dandelion':1,'roses':2,'sunflowers':3,'tulips':4})
# define map operations
# Operator for image decoding
decode_op = vision.Decode()
# Operator for image normalization
normalize_op = vision.Normalize(mean=[cfg._R_MEAN, cfg._G_MEAN, cfg._B_MEAN], std=[cfg._R_STD,
cfg._G_STD, cfg._B_STD])
# Operator for image resizing
resize_op = vision.Resize(cfg._RESIZE_SIDE_MIN)
# Operator for image cropping
center_crop_op = vision.CenterCrop((cfg.HEIGHT, cfg.WIDTH))
# Operator for image random horizontal flipping
horizontal_flip_op = vision.RandomHorizontalFlip()
# Operator for image channel quantity conversion
channelswap_op = vision.HWC2CHW()
# Operator for random image cropping, decoding, encoding, and resizing
random_crop_decode_resize_op = vision.RandomCropDecodeResize((cfg.HEIGHT, cfg.WIDTH), (0.5, 1.0), (1.0,
1.0), max_attempts=100)
if usage == 'train':
dataset = dataset.shuffle(buffer_size=10000) # 10000 as in imageNet train script
dataset = dataset.batch(cfg.batch_size, drop_remainder=True)
# Batch the test set.
else:
dataset = dataset.batch(1, drop_remainder=True)
# Data augmentation
dataset = dataset.repeat(1)
dataset.map_model = 4
return dataset
plt.figure()
plt.imshow(data_next['image'][0,0,...])
plt.colorbar()
plt.grid(False)
plt.show()
Output:
(https://arxiv.org/pdf/1512.03385.pdf)
If the dimensions are the same:
y = F(x, 𝑊𝑊𝑖𝑖 ) + 𝑥𝑥
F = 𝑊𝑊2 𝜎𝜎(𝑊𝑊, 𝑥𝑥)
If the dimensions are different:
y = F(x, 𝑊𝑊𝑖𝑖 ) + 𝑊𝑊𝑠𝑠 𝑥𝑥
Bottleneck module
The bottleneck module uses the 1x1 convolutional layer to reduce or expand the feature map
dimension so that the number of filters at the 3x3 convolutional layer is not affected by the input of
the upper layer, and the output of the 3x3 convolutional layer does not affect the lower-layer
module.
(https://arxiv.org/pdf/1512.03385.pdf)
ResNet-50 model
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 44
ResNet-50 has two basic blocks: convolutional block and identity block. The input and output
dimensions of the convolutional block are different and cannot be connected in series. The
convolutional block is used to change the network dimensions. The identity block has the same input
and output dimensions, which can be connected in series to deepen the network.
(https://arxiv.org/pdf/1512.03385.pdf)
Code:
"""ResNet."""
Args:
in_channel (int): Input channel.
out_channel (int): Output channel.
stride (int): Stride size for the first convolutional layer. Default: 1.
Returns:
Tensor, output tensor.
Examples:
>>> ResidualBlock(3, 256, stride=2)
"""
expansion = 4 # In conv2_x--conv5_x, the number of convolution kernels at the first two layers is one fourth
of the number of convolution kernels at the third layer (an output channel).
# The number of convolution kernels at the first two layers is equal to a quarter of the number of
convolution kernels at the output channels.
channel = out_channel // self.expansion
# Layer 1 convolution
self.conv1 = _conv1x1(in_channel, channel, stride=1)
self.bn1 = _bn(channel)
# Layer 2 convolution
self.conv2 = _conv3x3(channel, channel, stride=stride)
self.bn2 = _bn(channel)
# Layer 3 convolution. The number of convolution kernels is equal to that of output channels.
self.conv3 = _conv1x1(channel, out_channel, stride=1)
self.bn3 = _bn_last(out_channel)
self.down_sample = False
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 46
# When the step is not 1 or the number of output channels is not equal to that of input channels,
downsampling is performed to adjust the number of channels.
if stride != 1 or in_channel != out_channel:
self.down_sample = True
self.down_sample_layer = None
# Adjust the number of channels using the 1x1 convolution.
if self.down_sample:
self.down_sample_layer = nn.SequentialCell([_conv1x1(in_channel, out_channel, stride), # 1x1
convolution
_bn(out_channel)]) # Batch Norm
# Addition operator
self.add = ops.Add()
return out
Args:
block (Cell): Block for network.
layer_nums (list): Numbers of block in different layers.
in_channels (list): Input channel in each layer.
out_channels (list): Output channel in each layer.
strides (list): Stride size in each layer.
num_classes (int): The number of classes that the training images belong to.
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 47
Returns:
Tensor, output tensor.
Examples:
>>> ResNet(ResidualBlock,
>>> [3, 4, 6, 3],
>>> [64, 256, 512, 1024],
>>> [256, 512, 1024, 2048],
>>> [1, 2, 2, 2],
>>> 10)
"""
# Input parameters: residual block, number of repeated residual blocks, input channel, output channel, stride,
and number of image classes
def __init__(self, block, layer_nums, in_channels, out_channels, strides, num_classes):
super(ResNet, self).__init__()
if not len(layer_nums) == len(in_channels) == len(out_channels) == 4:
raise ValueError("the length of layer_num, in_channels, out_channels list must be 4!")
# Layer 1 convolution; convolution kernels: 7x7, input channels: 3; output channels: 64; step: 2
self.conv1 = _conv7x7(3, 64, stride=2)
self.bn1 = _bn(64)
self.relu = ops.ReLU()
# Output layer
self.end_point = _fc(out_channels[3], num_classes)
# Input parameters: residual block, number of repeated residual blocks, input channel, output channel, and
stride
def _make_layer(self, block, layer_num, in_channel, out_channel, stride):
"""
Make stage network of ResNet.
Args:
block (Cell): Resnet block.
layer_num (int): Layer number.
in_channel (int): Input channel.
out_channel (int): Output channel.
stride (int): Stride size for the first convolutional layer.
Returns:
SequentialCell, the output layer.
Examples:
>>> _make_layer(ResidualBlock, 3, 128, 256, 2)
"""
# Build the residual block of convn_x.
layers = []
return nn.SequentialCell(layers)
return out
"""
Get ResNet50 neural network.
Args:
class_num (int): Class number.
Returns:
Cell, cell instance of ResNet50 neural network.
Examples:
>>> net = resnet50(10)
"""
return ResNet(ResidualBlock, # Residual block
[3, 4, 6, 3], # Number of residual blocks
[64, 256, 512, 1024], # Input channel
[256, 512, 1024, 2048], # Output channel
[1, 2, 2, 2], # Step
class_num) # Number of output classes
Start training.
After preprocessing data and defining the network, loss function, and optimizer, start model
training. Model training involves two iterations: multi-epoch iteration of datasets and single-step
iteration based on the batch size. The single-step iteration refers to extracting data from a dataset
by batch, inputting the data to a network to calculate a loss function, and then calculating and
updating a gradient of training parameters by using an optimizer.
1. Download a pre-trained model.
Create the model_resnet directory, download the model file pre-trained on the ImageNet
dataset, and save the file to the model_resnet directory. Download link:
https://download.mindspore.cn/models/r1.7/resnet50_ascend_v170_imagenet2012_official_c
v_top1acc76.97_top5acc93.44.ckpt.
2. Load the pre-trained model.
Read the pre-trained model file through the load_checkpoint() API to obtain the parameter file
in dictionary format.
3. Modify pre-trained model parameters.
Modify the connection parameters of the last layer of the pre-trained model parameters. (The model
is pre-trained on the ImageNet dataset to classify 1001 types. However, the current exercise is to
classify the five types of flowers.) Therefore, you need to modify the parameters of the last fully-
connected layer.
Code:
# Construct a ResNet-50 network. The number of output classes is 5, corresponding to five flower classes.
net=resnet50(class_num=cfg.num_class)
# Smooth the loss value to solve the problem of the gradient being too small during training.
loss_scale = FixedLossScaleManager(cfg.loss_scale_num, False)
# Build the model. Input the network structure, loss function, optimizer, loss value smoothing, and model
evaluation metrics.
model = Model(net, loss_fn=loss, optimizer=opt, loss_scale_manager=loss_scale, metrics={'acc'})
# Model saving parameters. Set the number of steps for saving a model and the maximum number of models that
can be saved.
ckpt_config = CheckpointConfig(save_checkpoint_steps=cfg.save_checkpoint_steps, keep_checkpoint_max=1)
# Save the model. Set the name, path, and parameters for saving the model.
ckpoint_cb = ModelCheckpoint(prefix=cfg.prefix, directory=cfg.directory, config=ckpt_config)
# Use the test set to validate the model and output the accuracy of the test set.
metric = model.eval(de_test)
print(metric)
Output:
# Model prediction. Select 10 samples from the test set for testing and output the prediction result and actual
result.
class_names = {0:'daisy',1:'dandelion',2:'roses',3:'sunflowers',4:'tulips'}
for i in range(10):
test_ = de_test.create_dict_iterator().__next__()
test = Tensor(test_['image'], mindspore.float32)
# Use the model for prediction.
predictions = model.predict(test)
predictions = predictions.asnumpy()
true_label = test_['label'].asnumpy()
# Show the prediction result.
p_np = predictions[0, :]
pre_label = np.argmax(p_np)
print('Prediction result of the ' + str(i) + '-th sample: ', class_names[pre_label], ' Actual result: ',
class_names[true_label[0]])
Output:
4.4 Question
In this exercise, how many convolutional and fully-connected layers does the ResNet-50 have? How
many epochs are defined for model training? How many classes are output for the network?
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 52
5.1 Introduction
This exercise implements the TextCNN sentiment analysis, a classic case in the deep learning field.
The entire process is as follows:
Download the required dataset. (The rt-polarity dataset is used to define the data preprocessing
function.)
Generate data for training and validation.
Define the TextCNN model structure build, training, validation, offline model loading, and online
inference functions.
Define various parameters required for training, such as optimizer, loss function, checkpoint,
and time monitor.
Load the dataset and perform training. After the training is complete, use the test set for
validation.
5.2 Preparations
Before you start, check whether MindSpore has been correctly installed. You are advised to install
MindSpore on your computer by referring to the MindSpore official website
https://www.mindspore.cn/install/en.
In addition, you should have basic mathematical knowledge, including basic knowledge of Python
coding basics, probability, and matrices.
Recommended environment:
Version: MindSpore 1.7
Programming language: Python 3.7
├─ rt-polarity.pos
└─ rt-polarity.neg
5.3.2 Procedure
Step 1 Import the Python library and module and configure running information.
Import the required Python library.
Currently, the os library is required. Other required libraries will not be described here. For details
about the MindSpore modules, see the MindSpore API page. You can use context.set_context to
configure the information required for running, such as the running mode, backend information, and
hardware information.
Import the context module and configure the required information.
Code:
import math
import numpy as np
import pandas as pd
import os
import math
import random
import codecs
from pathlib import Path
import mindspore
import mindspore.dataset as ds
import mindspore.nn as nn
from mindspore import Tensor
from mindspore import context
from mindspore.train.model import Model
from mindspore.nn.metrics import Accuracy
from mindspore.train.serialization import load_checkpoint, load_param_into_net
from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor, TimeMonitor
from mindspore.ops import operations as ops
cfg = edict({
'name': 'movie review',
'pre_trained': False,
'num_classes': 2,
'batch_size': 64,
'epoch_size': 4,
'weight_decay': 3e-5,
'data_path': './data/',
'device_target': 'CPU',
'device_id': 0,
'keep_checkpoint_max': 1,
'checkpoint_path': './ckpt/train_textcnn-4_149.ckpt',
'word_len': 51,
'vec_length': 40
})
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 54
The graph mode is used in this exercise. You can configure hardware information as required. For
example, if the code runs on the Ascend AI processor, set device_target to Ascend. This rule also
applies to the code running on the CPU and GPU. For details about the parameters, see the
context.set_context API description at
https://www.mindspore.cn/docs/en/r1.7/api_python/mindspore.context.html.
class MovieReview:
'''
Movie review dataset
'''
def __init__(self, root_dir, maxlen, split):
'''
input:
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 55
self.doConvert = False
mypath = Path(self.path)
if not mypath.exists() or not mypath.is_dir():
print("please check the root_dir!")
raise ValueError
# Read data.
self.word_num = 0
self.maxlen = 0
self.minlen = float("inf")
self.maxlen = float("-inf")
self.Pos = []
self.Neg = []
for filename in self.files:
self.read_data(filename)
self.text2vec(maxlen=maxlen)
self.split_dataset(split=split)
.replace(')','')\
.replace(':','')\
.replace('--','')\
.replace('-',' ')\
.replace('\\','')\
.replace('0','')\
.replace('1','')\
.replace('2','')\
.replace('3','')\
.replace('4','')\
.replace('5','')\
.replace('6','')\
.replace('7','')\
.replace('8','')\
.replace('9','')\
.replace('`','')\
.replace('=','')\
.replace('$','')\
.replace('/','')\
.replace('*','')\
.replace(';','')\
.replace('<b>','')\
.replace('%','')
# self.Vocab['None']
for SentenceLabel in self.Pos+self.Neg:
vector = [0]*maxlen
for index, word in enumerate(SentenceLabel[0]):
if index >= maxlen:
break
if word not in self.Vocab.keys():
self.Vocab[word] = len(self.Vocab)
vector[index] = len(self.Vocab) - 1
else:
vector[index] = self.Vocab[word]
SentenceLabel[0] = vector
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 57
self.doConvert = True
random.shuffle(self.train)
def get_dict_len(self):
'''
Obtain the length of a dictionary consisting of characters in a dataset.
'''
if self.doConvert:
return len(self.Vocab)
else:
print("Haven't finished Text2Vec")
return -1
Use the customized read_data function to load the original dataset and use the text2vec function to
vectorize the text of the data. Use split_dataset to split data into training data and test data, and
then create_test_dataset and create_train_dataset call mindspore.dataset. GeneratorDataset
generates data for training and validation.
Configuration code:
dataset = instance.create_train_dataset(batch_size=cfg.batch_size,epoch_size=cfg.epoch_size)
batch_num = dataset.get_dataset_size()
vocab_size=instance.get_dict_len()
print("vocab_size:{0}".format(vocab_size))
item =dataset.create_dict_iterator()
for i,data in enumerate(item):
if i<1:
print(data)
print(data['data'][1])
else:
break
learning_rate = []
warm_up = [1e-3 / math.floor(cfg.epoch_size / 5) * (i + 1) for _ in range(batch_num)
for i in range(math.floor(cfg.epoch_size / 5))]
shrink = [1e-3 / (16 * (i + 1)) for _ in range(batch_num)
for i in range(math.floor(cfg.epoch_size * 3 / 5))]
normal_run = [1e-3 for _ in range(batch_num) for i in
range(cfg.epoch_size - math.floor(cfg.epoch_size / 5)
- math.floor(cfg.epoch_size * 2 / 5))]
learning_rate = learning_rate + warm_up + normal_run + shrink
def make_conv_layer(kernel_size):
weight_shape = (96, 1, *kernel_size)
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 59
weight = _weight_variable(weight_shape)
return nn.Conv2d(in_channels=1, out_channels=96, kernel_size=kernel_size, padding=1,
pad_mode="pad", weight_init=weight, has_bias=True)
class TextCNN(nn.Cell):
def __init__(self, vocab_len, word_len, num_classes, vec_length):
super(TextCNN, self).__init__()
self.vec_length = vec_length
self.word_len = word_len
self.num_classes = num_classes
self.unsqueeze = ops.ExpandDims()
self.embedding = nn.Embedding(vocab_len, self.vec_length, embedding_table='normal')
self.slice = ops.Slice()
self.layer1 = self.make_layer(kernel_height=3)
self.layer2 = self.make_layer(kernel_height=4)
self.layer3 = self.make_layer(kernel_height=5)
self.concat = ops.Concat(1)
def construct(self,x):
x = self.unsqueeze(x, 1)
x = self.embedding(x)
x1 = self.layer1(x)
x2 = self.layer2(x)
x3 = self.layer3(x)
Output:
def preprocess(sentence):
sentence = sentence.lower().strip()
sentence = sentence.replace('\n','')\
.replace('"','')\
.replace('\'','')\
.replace('.','')\
.replace(',','')\
.replace('[','')\
.replace(']','')\
.replace('(','')\
.replace(')','')\
.replace(':','')\
.replace('--','')\
.replace('-',' ')\
.replace('\\','')\
.replace('0','')\
.replace('1','')\
.replace('2','')\
.replace('3','')\
.replace('4','')\
.replace('5','')\
.replace('6','')\
.replace('7','')\
.replace('8','')\
.replace('9','')\
.replace('`','')\
.replace('=','')\
.replace('$','')\
.replace('/','')\
.replace('*','')\
.replace(';','')\
.replace('<b>','')\
.replace('%','')\
.replace(" "," ")
sentence = sentence.split(' ')
maxlen = cfg.word_len
vector = [0]*maxlen
HCIA-AI Deep Learning and AI Development Framework Lab Guide Page 62
return sentence
def inference(review_en):
review_en = preprocess(review_en)
input_en = Tensor(np.array([review_en]).astype(np.int32))
output = net(input_en)
if np.argmax(np.array(output[0])) == 1:
print("Positive comments")
else:
print("Negative comments")
Code:
Output:
5.4 Question
If you want to perform the preceding exercise on a GPU, which configuration item do you need to
change to GPU?