From c024d4fa63bf53d590e4c6d14707d0a961caa43e Mon Sep 17 00:00:00 2001 From: zhangyi Date: Mon, 18 Apr 2022 18:39:22 +0800 Subject: [PATCH] modify the files --- tutorials/source_en/beginner/autograd.md | 67 +- tutorials/source_en/beginner/dataset.md | 86 +-- tutorials/source_en/beginner/infer.md | 162 ++-- .../source_en/beginner/introduction.ipynb | 142 ---- tutorials/source_en/beginner/introduction.md | 118 +++ tutorials/source_en/beginner/model.md | 75 +- tutorials/source_en/beginner/quick_start.md | 68 +- tutorials/source_en/beginner/save_load.md | 81 +- tutorials/source_en/beginner/tensor.ipynb | 695 ------------------ tutorials/source_en/beginner/tensor.md | 439 +++++++++++ tutorials/source_en/beginner/train.md | 59 +- tutorials/source_zh_cn/beginner/infer.ipynb | 28 +- 12 files changed, 867 insertions(+), 1153 deletions(-) delete mode 100644 tutorials/source_en/beginner/introduction.ipynb create mode 100644 tutorials/source_en/beginner/introduction.md delete mode 100644 tutorials/source_en/beginner/tensor.ipynb create mode 100644 tutorials/source_en/beginner/tensor.md diff --git a/tutorials/source_en/beginner/autograd.md b/tutorials/source_en/beginner/autograd.md index d7cfa94f0e..4c12656907 100644 --- a/tutorials/source_en/beginner/autograd.md +++ b/tutorials/source_en/beginner/autograd.md @@ -1,22 +1,24 @@ # Automatic Differentiation - + -Automatic differentiation is able to calculate the derivative value of a derivative function at a certain point, which is a generalization of backpropagation algorithms. The main problem solved by automatic differentiation is to decompose a complex mathematical operation into a series of simple basic operations, which shields the user from a large number of details and processes of differentiation, which greatly reduces the threshold for the use of the framework. +Automatic differentiation can calculate a derivative value of a derivative function at a certain point, which is a generalization of backpropagation algorithms. The main problem solved by automatic differentiation is to decompose a complex mathematical operation into a series of simple basic operations. This function shields a large number of derivative details and processes from users, greatly reducing the threshold for using the framework. -MindSpore uses `ops.GradOperation` to calculate a first-order derivative, and the attributes of the first-order derivative are as the following: +MindSpore uses `ops.GradOperation` to calculate the first-order derivative. The `ops.GradOperation` attributes are as follows: -- `get_all`:Whether to derive the input parameters, the default value is False. -- `get_by_list`:Whether to derive the weight parameters, the default value is False. -- `sens_param`:Whether to scale the output value of the network to change the final gradient, the default value is False. ++ `get_all`: determines whether to derive the input parameters. The default value is False. ++ `get_by_list`: determines whether to derive the weight parameters. The default value is False. ++ `sens_param`: determines whether to scale the output value of the network to change the final gradient. The default value is False. This chapter uses `ops.GradOperation` in MindSpore to find first-order derivatives of the function $f(x)=wx+b$. ## First-order Derivative of the Input -The formula needs to be defined before the input can be derived:$f(x)=wx+b \tag {1}$ +Define the formula before deriving the input: -The example code below is an expression of Equation (1), and since MindSpore is functionally programmed, all expressions of computational formulas are represented as functions. +$$f(x)=wx+b \tag {1} $$ + +The example code below is an expression of Equation (1). Since MindSpore is functionally programmed, all expressions of computational formulas are represented as functions. ```python import numpy as np @@ -34,7 +36,9 @@ class Net(nn.Cell): return f ``` -Define the derivative class `GradNet`. In the `__init__` function, define the `self.net` and `ops.GradOperation` networks. In the `construct` function, compute the derivative of `self.net`. Its corresponding MindSpore internally produces the following formula (2):$f^{'}(x)=w\tag {2}$ +Define the derivative class `GradNet`. In the `__init__` function, define the `self.net` and `ops.GradOperation` networks. In the `construct` function, compute the derivative of `self.net`. The following formula (2) is generated in MindSpore: + +$$f^{'}(x)=w\tag {2}$$ ```python from mindspore import dtype as mstype @@ -51,14 +55,13 @@ class GradNet(nn.Cell): return gradient_function(x) ``` -At last, define the weight parameter as w and a first-order derivative is found for the input parameter x in the input formula (1). From the running result, the input in formula (1) is 6, that is: -$$ -f(x)=wx+b=6*x+1 \tag {3} -$$ - To derive the above equation, there is: -$$ -f^{'}(x)=w=6 \tag {4} -$$ +Finally, the weight parameter is defined as w, and a first-order derivative is found for the input parameter x in the input formula (1). According to the running result, the input in formula (1) is 6, that is: + +$$f(x)=wx+b=6*x+1 \tag {3}$$ + +Derive the above equation: + +$$f^{'}(x)=w=6 \tag {4}$$ ```python x = Tensor([100], dtype=mstype.float32) @@ -71,7 +74,7 @@ print(output) [6.] ``` -MindSpore calculates the first derivative method `ops.GradOperation (get_all=False, get_by_lsit=False, sens_param=False)`, where when `get_all` is `False`, only the first input is evaluated, and when `True` is set, all inputs are evaluated. +MindSpore calculates the first-order derivative using `ops.GradOperation (get_all=False, get_by_lsit=False, sens_param=False)`. If `get_all` is set to `False`, the derivative of only the first input is calculated. If `get_all` is set to `True`, the derivative of all inputs is calculated. ## First-order Derivative of the Weight @@ -85,7 +88,7 @@ class GradNet(nn.Cell): super(GradNet, self).__init__() self.net = net self.params = ParameterTuple(net.trainable_params()) - self.grad_op = ops.GradOperation(get_by_list=True) # Set the first-order derivative of the weight parameters + self.grad_op = ops.GradOperation(get_by_list=True) # Set the first-order derivative of the weight parameters. def construct(self, x): gradient_function = self.grad_op(self.net, self.params) @@ -95,24 +98,24 @@ class GradNet(nn.Cell): Next, derive the function: ```python -# Perform a derivative calculation on the function +# Perform a derivative calculation on the function. x = Tensor([100], dtype=mstype.float32) fx = GradNet(Net())(x) -# Print the results +# Print the result. print(fx) print(f"wgrad: {fx[0]}\nbgrad: {fx[1]}") ``` ```text -(Tensor(shape=[1], dtype=Float32, value= [ 6.00000000e+00]), Tensor(shape=[1], dtype=Float32, value= [ 1.00000000e+00])) -wgrad: [6.] +(Tensor(shape=[1], dtype=Float64, value= [ 1.00000000e+02]), Tensor(shape=[1], dtype=Float64, value= [ 1.00000000e+00])) +wgrad: [100.] bgrad: [1.] ``` -If computation of certain weight derivatives is not required, set `requirements_grad` to `False` when defining the network requiring derivatives. +If derivation is not required for some weights, set `requires_grad` to `False` when defining the derivation network and declaring the corresponding weight parameters. -```Python +```python class Net(nn.Cell): def __init__(self): super(Net, self).__init__() @@ -134,7 +137,7 @@ class GradNet(nn.Cell): gradient_function = self.grad_op(self.net, self.params) return gradient_function(x) -# Construct a derivative network +# Construct a derivative network. x = Tensor([5], dtype=mstype.float32) fw = GradNet(Net())(x) @@ -154,9 +157,9 @@ class GradNet(nn.Cell): def __init__(self, net): super(GradNet, self).__init__() self.net = net - # Derivative operation + # Derivative operation. self.grad_op = ops.GradOperation(sens_param=True) - # Scale index + # Scale an index. self.grad_wrt_output = Tensor([0.1], dtype=mstype.float32) def construct(self, x): @@ -173,9 +176,9 @@ print(output) [0.6] ``` -## Stopping Gradient +## Stopping Gradient Calculation -We can use `stop_gradient` to disable calculation of gradient for certain operators. For example: +You can use `ops.stop_gradient` to stop calculating gradients. The following is an example: ```python from mindspore.ops import stop_gradient @@ -188,7 +191,7 @@ class Net(nn.Cell): def construct(self, x): out = x * self.w + self.b - # Stops updating the gradient, and out does not contribute to gradient calculations + # Stop updating the gradient. The out does not contribute to gradient calculations. out = stop_gradient(out) return out @@ -212,4 +215,4 @@ print(f"wgrad: {output[0]}\nbgrad: {output[1]}") ```text wgrad: [0.] bgrad: [0.] -``` +``` \ No newline at end of file diff --git a/tutorials/source_en/beginner/dataset.md b/tutorials/source_en/beginner/dataset.md index cc0c0bdef8..5a19f2a312 100644 --- a/tutorials/source_en/beginner/dataset.md +++ b/tutorials/source_en/beginner/dataset.md @@ -2,47 +2,47 @@ -Data is the foundation of deep learning, and inputting the high-quality data plays an active role in the entire deep neural network. +Data is the foundation of deep learning, and high-quality data input is beneficial to the entire deep neural network. -[mindspore.dataset](https://www.mindspore.cn/docs/en/r1.7/api_python/mindspore.dataset.html) provides a loading interface for some commonly used datasets and standard format datasets, enabling users to quickly perform data processing operations. For the image datasets, users can use `mindvision.dataset` to load and process datasets. This chapter first describes how to load and process a CIFAR-10 dataset by using the `mindvision.dataset.Cifar10` interface, and then describes how to use `mindspore.dataset.GeneratorDataset` to implement custom dataset loading. +[mindspore.dataset](https://www.mindspore.cn/docs/en/r1.7/api_python/mindspore.dataset.html) provides a loading interface for some commonly used datasets and datasets in standard formats, enabling users to quickly process data. For an image dataset, you can use `mindvision.dataset` to load and process the dataset. This chapter first describes how to load and process the CIFAR-10 dataset by using the `mindvision.dataset.Cifar10` interface, and then describes how to use `mindspore.dataset.GeneratorDataset` to implement custom dataset loading. -> `mindvision.dataset`is a dataset interface developed on the basis of `mindspore.dataset`. In addition to providing dataset loading capabilities, `mindvision.dataset` further provides dataset download capabilities, data processing, and data enhancement capabilities. +> `mindvision.dataset` is a dataset interface developed based on `mindspore.dataset`. In addition to providing dataset loading capabilities, `mindvision.dataset` provides dataset download, data processing, and data argumentation capabilities. ## Data Process -In the network training and inference process, the raw data is generally stored in a disk or database. The raw data needs to be read into the memory space through the data loading step, converted into a framework-common tensor format, and then mapped to a more easy-to-learn space through the data processing and augmentation steps. While the number of samples and generalization is increased, the data finally enters the network for calculation. +In the network training and inference process, raw data is generally stored in disks or databases. You need to read the data to the memory space through data loading, convert the data into the framework-common tensor format, and then map the data to an easy-to-learn feature space through data processing and argumentation. At the same time, increase the number of samples and generalization, and finally input the data to the network for calculation. -The overall process is shown in the following figure: +The following figure shows the overall process. -![dataset_pipeline](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_zh_cn/beginner/images/dataset_pipeline.png) +![dataset-pipeline](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_zh_cn/beginner/images/dataset_pipeline.png) ### Dataset -A dataset is a collection of samples, and a row of a dataset is a sample that contains one or more features, and may further contain a label. The dataset needs to meet certain specification requirements to make it easier to evaluate the effectiveness of the model. +A dataset is a collection of samples, and a row of a dataset is a sample that contains one or more features, and may further contain a label. Datasets must comply with certain specifications to facilitate model effect evaluation. -Dataset supports multiple format datasets, including MindRecord, a MindSpore self-developed data format, commonly used public image datasets and text datasets, user-defined datasets, etc. +The dataset supports multiple formats, such as MindRecord (a MindSpore-developed data format), commonly used public image datasets and text datasets, and custom datasets. ### Dataset Loading -Dataset loading allows the model to be continuously acquired for training during training. Dataset provides corresponding classes for a variety of commonly used datasets to load datasets. For data files in different storage formats, Dataset also has corresponding classes for data loading. +The dataset loading enables continuous data obtaining for model training. The dataset provides classes to load common datasets. The dataset also provides classes for data files in different storage formats to load data. -Dataset provides multiple uses of the sampler (Sampler), and the sampler is responsible for generating the read index sequence. The Dataset is responsible for reading the corresponding data according to the index, helping users to sample the dataset in different forms to meet the training needs, and solving problems such as the data set is too large or the sample class distribution is uneven. +The dataset provides a sampler for multiple purposes. The sampler generates the index sequence to be read, and the dataset reads data based on the index to help users sample datasets in different forms to meet training requirements, solve problems such as large datasets or uneven distribution of sample classes. -> It should be noted that the sampler is responsible for performing filter and reorder operations on the sample, not performing the Batch operation. +> It should be noted that the sampler is responsible for performing the filter and reorder operations on samples, not performing the batch operation. -### Data processing +### Data Processing -After the Dataset loads the data into the memory, the data is organized in a Tensor form. Tensor is also a basic data structure in data augmentation operations. +After the dataset loads data to the memory, the data is organized as tensors. Tensor is also a basic data structure in data augmentation. -## Loading the Dataset +## Loading a Dataset -In the following example, the CIFAR-10 dataset is loaded through the `mindvision.dataset.Cifar10` interface. The CIFAR-10 dataset has a total of 60,000 32*32 color images, which are divided into 10 categories, each with 6,000 maps, and a total of 50,000 training pictures and 10,000 test pictures in the dataset. `Cifar10` interface provides CIFAR-10 dataset download and load capabilities. +In the following example, the CIFAR-10 dataset is loaded through the `mindvision.dataset.Cifar10` interface. The CIFAR-10 dataset contains a total of 60,000 32 x 32 color images which are evenly divided into 10 classes and classified into 50,000 training images and 10,000 test images. The `Cifar10` interface allows users to download and load the CIFAR-10 dataset. ![cifar10](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_zh_cn/beginner/images/cifar10.jpg) -- `path`: The location of the dataset root directory. -- `split`: Training, testing or inferencing of the dataset, optionally `train`,`test` or `infer`, `train` by default. -- `download`: Whether to download the dataset. When `ture` is set, if the dataset does not exist, you can download and extract the dataset, `False` by default. ++ `path`: indicates the root directory of the dataset. ++ `split`: indicates the training, test, or inference dataset. The value can be `train` (default value), `test`, or `infer`. ++ `download`: determines whether to download the dataset. If this parameter is set to `True` and the dataset does not exist, the dataset can be downloaded and decompressed. The default value is `False`. ```python from mindvision.dataset import Cifar10 @@ -50,32 +50,32 @@ from mindvision.dataset import Cifar10 # Dataset root directory data_dir = "./datasets" -# Download, extract and load the CIFAR-10 training dataset +# Download, extract, and load the CIFAR-10 training dataset. dataset = Cifar10(path=data_dir, split='train', batch_size=6, resize=32, download=True) dataset = dataset.run() ``` -The directory structures of the CIFAR-10 dataset files are as follows: +The directory structure of the CIFAR-10 dataset file is as follows: ```text datasets/ ├── cifar-10-batches-py -│ ├── batches.meta -│ ├── data_batch_1 -│ ├── data_batch_2 -│ ├── data_batch_3 -│ ├── data_batch_4 -│ ├── data_batch_5 -│ ├── readme.html -│ └── test_batch +│   ├── batches.meta +│   ├── data_batch_1 +│   ├── data_batch_2 +│   ├── data_batch_3 +│   ├── data_batch_4 +│   ├── data_batch_5 +│   ├── readme.html +│   └── test_batch └── cifar-10-python.tar.gz ``` -## Iterating Dataset +## Iterating a Dataset -You can use `create_dict_iterator` interface to create a data iterator to iteratively access data. The data type of the access is `Tensor` by default, and if `output_numpy=True` is set, the data type of the access is `Numpy`. +You can use the `create_dict_iterator` interface to create a data iterator to iteratively access data. The default type of data to be accessed is `Tensor`. If `output_numpy=True` is set, the type of data to be accessed is `Numpy`. -The following shows the corresponding access data type, and the image shapes and labels. +The following shows the corresponding access data types, and the image shapes and labels. ```python data = next(dataset.create_dict_iterator()) @@ -96,11 +96,13 @@ Image shape: (6, 3, 32, 32), Label: [8 0 0 2 6 1] ### Data Processing -`mindvision.dataset.Cifar10` interface provides data processing capbilities. The data can be processed by simply setting the corresponding attributes. +The `mindvision.dataset.Cifar10` interface provides data processing capability. The data can be processed by simply setting the corresponding attributes. + ++ `shuffle`: determines whether to shuffle datasets. If this parameter is set to `True`, the sequence of data sets is shuffled. The default value is `False`. -- `shuffle`: Whether to disrupt the order of the datasets, when `True` is set, the order of the datasets is disturbed, `False` by default . -- `batch_size`: The number of data contained in each group. The `batch_size=2` contains 2 data per group, and the default size of the `batch_size` value is 32. -- `repeat_num`: For the number of duplicate datasets. `repeat_num=1` is a dataset, and the default value of the `repeat_num` is 1. ++ `batch_size`: indicates the number of data contained in each batch. `batch_size=2` indicates that each batch contains two data records. The default value of `batch_size` is 32. + ++ `repeat_num`: indicates the number of duplicate datasets. `repeat_num=1` indicates one dataset. The default value of `repeat_num` is 1. ```python import numpy as np @@ -132,10 +134,11 @@ Image shape: (6, 3, 32, 32), Label: [9 3 8 9 6 8] ### Data Augmentation -Problems such as too small amount of data or single sample scene will affect the training effect of the model, and users can expand the diversity of samples through data augmentation operations to improve the generalization ability of the model. The `mindvision.dataset.Cifar10` interface uses the default data augmentation feature, which allows users to perform data augmentation operations by setting attribute `transform` and `target_transform`. +If the data volume is too small or the sample scenario is simple, the model training effect is affected. You can perform the data augmentation operation to expand the sample diversity and improve the generalization capability of the model. +The `mindvision.dataset.Cifar10` interface uses the default data augmentation feature, which allows users to perform data augmentation by setting attributes `transform` and `target_transform`. -- `transform`: augment dataset image data. -- `target_transform`: process the dataset label data. ++ `transform`: performs augmentation on dataset image data. ++ `target_transform`: processes the dataset label data. This section describes data augmentation of the CIFAR-10 dataset by using operators in the `mindspore.dataset.vision .c_transforms` module. @@ -147,9 +150,9 @@ import mindspore.dataset.vision.c_transforms as transforms # Image augmentation trans = [ - transforms.RandomCrop((32, 32), (4, 4, 4, 4)), # Automatic cropping of images - transforms.RandomHorizontalFlip(prob=0.5), # Flip the image randomly and horizontally - transforms.HWC2CHW(), # Convert (h, w, c) to (c, h, w) + transforms.RandomCrop((32, 32), (4, 4, 4, 4)), # Automatically crop the image. + transforms.RandomHorizontalFlip(prob=0.5), # Flip the image horizontally at random. + transforms.HWC2CHW(), # Convert (h, w, c) to (c, h, w). ] dataset = Cifar10(data_dir, batch_size=6, resize=32, transform=trans) @@ -171,4 +174,3 @@ plt.show() ```text Image shape: (6, 3, 32, 32), Label: [7 6 7 4 5 3] ``` - diff --git a/tutorials/source_en/beginner/infer.md b/tutorials/source_en/beginner/infer.md index ed41c7677f..54644570dd 100644 --- a/tutorials/source_en/beginner/infer.md +++ b/tutorials/source_en/beginner/infer.md @@ -2,19 +2,19 @@ -This chapter uses the `mobilenet_v2` network fine-tuning approach in MindSpore Vision to develop an AI application (classification of the dog and the croissants) and deploy the trained network model to the Android phone to perform inference and deployment functions. +This chapter uses the `mobilenet_v2` network fine-tuning approach in MindSpore Vision to develop an AI application to classify dogs and croissants, and deploy the trained network model on the Android phone to perform inference and deployment. ## Data Preparation and Loading -### Downloading the dataset +### Downloading a Dataset -First, you need to download the [dog and croissants classification dataset](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/beginner/DogCroissants.zip) used in this case, which has two categories, dog and croissants, and each class has about 150 training images, 20 verification images, and 1 inference image. +First, you need to download the [dog and croissants classification dataset](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/beginner/DogCroissants.zip) which contains two classes, dog and croissants. Each class contains about 150 training images, 20 verification images, and 1 inference image. -The specific dataset is as follows: +The dataset is as follows: ![datset-dog](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_zh_cn/beginner/images/datset_dog.png) -Use the `DownLoad` interface in MindSpore Vision to download and extract the dataset to the specified path, and the sample code is as follows: +Use the `DownLoad` interface in MindSpore Vision to download and decompress the dataset to the specified path. The sample code is as follows: ```python from mindvision.dataset import DownLoad @@ -23,7 +23,7 @@ dataset_url = "https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebo path = "./datasets" dl = DownLoad() -# Download and extract the dataset +# Download and decompress the dataset. dl.download_and_extract_archive(dataset_url, path) ``` @@ -45,7 +45,7 @@ datasets ### Loading the Dataset -Define the `create_dataset` function to load the dog and croissants dataset, perform image enhancement operations on the dataset, and set the dataset batch_size size. +Define the `create_dataset` function to load the dog and croissant dataset, perform image argumentation on the dataset, and set batch_size of the dataset. ```python import mindspore.dataset as ds @@ -54,7 +54,7 @@ import mindspore.dataset.vision.c_transforms as transforms def create_dataset(path, batch_size=10, train=True, image_size=224): dataset = ds.ImageFolderDataset(path, num_parallel_workers=8, class_indexing={"croissants": 0, "dog": 1}) - # Image augmentation operation + # Image augmentation mean = [0.485 * 255, 0.456 * 255, 0.406 * 255] std = [0.229 * 255, 0.224 * 255, 0.225 * 255] if train: @@ -74,7 +74,7 @@ def create_dataset(path, batch_size=10, train=True, image_size=224): ] dataset = dataset.map(operations=trans, input_columns="image", num_parallel_workers=8) - # Sets the size of the batch_size and discards if the number of samples last fetched is less than batch_size + # 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. dataset = dataset.batch(batch_size, drop_remainder=True) return dataset ``` @@ -82,36 +82,36 @@ def create_dataset(path, batch_size=10, train=True, image_size=224): Load the training dataset and validation dataset for subsequent model training and validation. ```python -# Load the training dataset +# Load the training dataset. train_path = "./datasets/DogCroissants/train" dataset_train = create_dataset(train_path, train=True) -# Load the validation dataset +# Load the validation dataset. val_path = "./datasets/DogCroissants/val" dataset_val = create_dataset(val_path, train=False) ``` ## Model Training -In this case, we use a pre-trained model to fine-tune the model on the classification dataset of the dog and croissants, and convert the trained CKPT model file to the MINDIR format for subsequent deployment on the phone side. +In this case, we use a pre-trained model to fine-tune the model on the dog and croissant classification dataset, and convert the trained CKPT model file to the MINDIR format for subsequent deployment on the mobile phone. -> Model training currently only supports running in the Linux environment. +> Currently, model training supports only the Linux environment. ### Principles of the MobileNet V2 Model -MobileNet network is a lightweight CNN network focused on mobile, embedding or IoT devices proposed by the Google team in 2017. Compared to the traditional convolutional neural network, MobileNet network uses depthwise separable convolution idea in the premise of a small reduction in accuracy, which greatly reduces the model parameters and amount of operation. And the introduction of width coefficient and resolution coefficient makes the model meet the needs of different application scenarios. +MobileNet is a lightweight CNN proposed by the Google team in 2017 to focus on mobile, embedded, or IoT devices. Compared with traditional convolutional neural networks, MobileNet uses depthwise separable convolution to greatly reduce the model parameters and computation amount with a slight decrease in accuracy. In addition, the width coefficient $\alpha$ and resolution coefficient $\beta$ are introduced to meet the requirements of different application scenarios. -Since there is a large amount of loss when the Relu activation function processes low-dimensional feature information in the MobileNet network, the MobileNet V2 network proposes to use the inverted residual block and Linear Bottlenecks to design the network, to improve the accuracy of the model and make the optimized model smaller. +Because a large amount of data is lost when the ReLU activation function in the MobileNet processes low-dimensional feature information, the MobileNetV2 proposes to use an inverted residual block and Linear Bottlenecks to design the network, to improve accuracy of the model and make the optimized model smaller. ![mobilenet](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_zh_cn/beginner/images/mobilenet.png) -The Inverted residual block structure in the figure first uses 1x1 convolution for upswing, uses 3x3 DepthWise convolution, and finally uses 1x1 convolution for dimensionality reduction, which is in contrast to the Residual block structure. The Residual block first uses 1x1 convolution for dimensionality reduction, uses 3x3 convolution, and finally uses 1x1 convolution for upswing. +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 detailed contents, refer to [MobileNet V2 thesis](https://arxiv.org/pdf/1801.04381.pdf). +> For details, see the [MobileNet V2 paper.](https://arxiv.org/pdf/1801.04381.pdf) ### Downloading the Pre-trained Model -Download the [ckpt file of the MobileNetV2 pre-trained model](https://download.mindspore.cn/vision/classification/mobilenet_v2_1.0_224.ckpt) required for the case and the width coefficient of the pre-trained model, and the input image size is (224, 224). The downloaded pre-trained model is saved in the current directory. Use the `DownLoad` in MindSpore Vision to download the pre-trained model file to the current directory, and the sample code is as follows: +Download the [ckpt file of the MobileNetV2 pre-trained model](https://download.mindspore.cn/vision/classification/mobilenet_v2_1.0_224.ckpt) required by the case. The width coefficient of the pre-training model is $\alpha=1.0$, and the input image size is (224, 224). Save the downloaded pre-trained model to the current directory. Use `DownLoad` in MindSpore Vision to download the pre-trained model file to the current directory. The sample code is as follows: ```python from mindvision.dataset import DownLoad @@ -119,13 +119,13 @@ from mindvision.dataset import DownLoad models_url = "https://download.mindspore.cn/vision/classification/mobilenet_v2_1.0_224.ckpt" dl = DownLoad() -# Download the pre-trained model file +# Download the pre-trained model file. dl.download_url(models_url) ``` ### MobileNet V2 Model Fine-tuning -This chapter uses MobileNet V2 pretrained model for fine-tuning, and uses the classification dataset of the dog and croissants to retrain the model by deleting the last parameter of the 1x1 convolution layer for classification in the MobileNet V2 pretrained model, to update the model parameter. +This chapter uses MobileNet V2 pre-trained model for fine-tuning, and uses the dog and croissant classification dataset to retrain the model to update the model parameter by deleting the last parameter of the 1 x 1 convolution layer for classification in the MobileNet V2 pre-trained model. ```python import mindspore.nn as nn @@ -135,16 +135,16 @@ from mindspore import load_checkpoint, load_param_into_net from mindvision.classification.models import mobilenet_v2 from mindvision.engine.loss import CrossEntropySmooth -# Build a model with a target classification number of 2 and an image input size of (224,224) +# Create a model, in which the number of target classifications is 2 and the input image size is (224,224). network = mobilenet_v2(num_classes=2, resize=224) -# Save the model parameter in param_dict +# Save model parameters to param_dict. param_dict = load_checkpoint("./mobilenet_v2_1.0_224.ckpt") -# Obtain the parameter name of the last convolutional layer of the mobilenet_v2 network +# Obtain the parameter name of the last convolutional layer of the mobilenet_v2 network. filter_list = [x.name for x in network.head.classifier.get_parameters()] -# Delete the last convolutional layer of the pre-trained model +# Delete the last convolutional layer of the pre-trained model. def filter_ckpt_parameter(origin_dict, param_filter): for key in list(origin_dict.keys()): for name in param_filter: @@ -155,19 +155,19 @@ def filter_ckpt_parameter(origin_dict, param_filter): filter_ckpt_parameter(param_dict, filter_list) -# Load the pre-trained model parameters as the network initialization weight +# Load the pre-trained model parameters as the network initialization weight. load_param_into_net(network, param_dict) -# Define the optimizer +# Define the optimizer. network_opt = nn.Momentum(params=network.trainable_params(), learning_rate=0.01, momentum=0.9) -# Define the loss function +# Define the loss function. network_loss = CrossEntropySmooth(sparse=True, reduction="mean", smooth_factor=0.1, classes_num=2) -# Define evaluation metrics +# Define evaluation metrics. metrics = {"Accuracy": nn.Accuracy()} -# Initialize the model +# Initialize the model. model = Model(network, loss_fn=network_loss, optimizer=network_opt, metrics=metrics) ``` @@ -175,13 +175,14 @@ model = Model(network, loss_fn=network_loss, optimizer=network_opt, metrics=metr [WARNING] ME(375486:140361546602304,MainProcess): [mindspore/train/serialization.py:644] 2 parameters in the 'net' are not loaded, because they are not in the 'parameter_dict'. [WARNING] ME(375486:140361546602304,MainProcess): [mindspore/train/serialization.py:646] head.classifier.weight is not loaded. [WARNING] ME(375486:140361546602304,MainProcess): [mindspore/train/serialization.py:646] head.classifier.bias is not loaded. + Delete parameter from checkpoint: head.classifier.weight Delete parameter from checkpoint: head.classifier.bias Delete parameter from checkpoint: moments.head.classifier.weight Delete parameter from checkpoint: moments.head.classifier.bias ``` -> Due to the model fine-tuning, the above WARNING needs to remove the parameters of the last convolutional layer of the pre-trained model, so loading the pre-trained model will show that the `head.classifier` parameter is not loaded. The `head.classifier` parameter will use the initialization value when the model was built. +> The preceding warning is generated because the last convolutional layer parameter of the pre-trained model needs to be deleted for model fine-tuning. When the pre-trained model is loaded, the system displays a message indicating that the `head.classifier` parameter is not loaded. The `head.classifier` parameter uses the initial value during model creation. ### Model Training and Evaluation @@ -193,7 +194,7 @@ from mindspore.train.callback import TimeMonitor num_epochs = 10 -# Model training and validation, after the training is completed, save the CKPT file with the highest evaluation accuracy, `best.ckpt`, in the current directory +# Train and verify the model. After the training is completed, save the CKPT file with the highest evaluation accuracy, `best.ckpt`, in the current directory. model.train(num_epochs, dataset_train, callbacks=[ValAccMonitor(model, dataset_val, num_epochs), TimeMonitor()]) @@ -236,7 +237,7 @@ End of validation the best Accuracy is: 1.000, save the best ckpt file in ./bes ### Visualizing Model Predictions -Define the `visualize_model` function, use the model with the highest validation accuracy described above to make predictions about the input images and visualize the predictions. +Define the `visualize_model` function, use the model with the highest validation accuracy described above to predict the input image, and visualize the prediction result. ```python import matplotlib.pyplot as plt @@ -257,19 +258,19 @@ def visualize_model(path): image = (image - mean) / std image = image.astype(np.float32) - # Image channel switches (h, w, c) to (c, h, w) + # Convert the image channel from (h, w, c) to (c, h, w). image = np.transpose(image, (2, 0, 1)) - # Extend the data dimension to (1,c, h, w) + # Extend the data dimension to (1, c, h, w) image = np.expand_dims(image, axis=0) - # Define and load the network + # Define and load the network. net = mobilenet_v2(num_classes=2, resize=224) param_dict = load_checkpoint("./best.ckpt") load_param_into_net(net, param_dict) model = Model(net) - # Model prediction + # Use the model for prediction. pre = model.predict(Tensor(image)) result = np.argmax(pre) @@ -291,118 +292,111 @@ plt.show() ### Model Export -After the model is trained, the network model (i.e. CKPT file) after the training is completed is converted to MindIR format for subsequent inference on the phone side. The `export` interface generates `mobilenet_v2_1.0_224.mindir` files in the current directory. +After model training is complete, the trained network model (CKPT file) is converted into the MindIR format for subsequent inference on the mobile phone. The `mobilenet_v2_1.0_224.mindir` file is generated in the current directory through the `export` interface. ```python from mindspore import export, Tensor -# Define and load the network parameters +# Define and load the network parameters. net = mobilenet_v2(num_classes=2, resize=224) param_dict = load_checkpoint("best.ckpt") load_param_into_net(net, param_dict) -# Export the model from the ckpt format to the MINDIR format +# Export the model from the CKPT format to the MINDIR format. input_np = np.random.uniform(0.0, 1.0, size=[1, 3, 224, 224]).astype(np.float32) export(net, Tensor(input_np), file_name="mobilenet_v2_1.0_224", file_format="MINDIR") ``` -## Inference and Deployment on the Phone Side - -To implement the inference function of the model file on the phone side, the steps are as follows: - -- Convert file format: Convert MindIR file format to the MindSpore Lite recognizable file on the Android phone; +## Inference and Deployment on the Mobile Phone -- Application deployment: Deploy the app APK on the phone side, that is, download a MindSpore Vision suite Android APK; and +To implement the inference function of the model file on the mobile phone, perform the following steps: -- Application experience: After finally importing the ms model file to the phone side, experience the recognition function of the dog and croissants. +- Convert file format: Convert MindIR file format to the MindSpore Lite recognizable file on the Android phone. +- Application deployment: Deploy the app APK on the mobile phone, that is, download a MindSpore Vision suite Android APK. +- Application experience: After importing the MS model file to the mobile phone, experience the function of recognizing dogs and croissants. -### Converting the file format +### Converting the File Format -Use the [conversion tool](https://www.mindspore.cn/lite/docs/zh-CN/r1.7/use/converter_tool.html) applied on the use side, and convert the mobilenet_v2_1.0_224.mindir file generated during the training process into a file format recognizable by the MindSpore Lite end-side inference framework mobilenet_v2_1.0_224.ms file. +Use the [conversion tool](https://www.mindspore.cn/lite/docs/zh-CN/r1.7/use/converter_tool.html) applied on the device side to convert the mobilenet_v2_1.0_224.mindir file generated during the training process into the mobilenet_v2_1.0_224.ms file which can be recognized by the MindSpore Lite on-device inference framework. -The specific model file format conversion method is as follows: +The following describes how to convert the model file format: -1. Use MindSpore Lite Converter to convert file formats in the Linux, in the [Linux-x86_64 tool downloading link](https://www.mindspore.cn/lite/docs/en/r1.7/use/downloads.html). +1. Use MindSpore Lite Converter to convert the file format in Linux. [Linux-x86_64 tool download link](https://www.mindspore.cn/lite/docs/en/r1.7/use/downloads.html) ```shell -# Set the path of the package after downloading and extracting, {converter_path}is the path to the extracted toolkit, PACKAGE_ROOT_PATH is set +# Download and decompress the software package and set the path of the software package. {converter_path} indicates the path of the decompressed tool package, and PACKAGE_ROOT_PATH indicates the environment variable. export PACKAGE_ROOT_PATH={converter_path} -# Include the dynamic-link libraries required by the conversion tool in the environment variables LD_LIBRARY_PATH +# Add the dynamic link library required by the converter to the environment variable LD_LIBRARY_PATH. export LD_LIBRARY_PATH=${PACKAGE_ROOT_PATH}/tools/converter/lib:${LD_LIBRARY_PATH} -# Execute the conversion command in mindspore-lite-linux-x64/tools/converter/converter +# Run the conversion command on the mindspore-lite-linux-x64/tools/converter/converter. ./converter_lite --fmk=MINDIR --modelFile=mobilenet_v2_1.0_224.mindir --outputFile=mobilenet_v2_1.0_224 ``` -2. Use MindSpore Lite Converter under Windows to convert file formats, in the [Windows-x64 tool downloading link](https://www.mindspore.cn/lite/docs/en/r1.7/use/downloads.html) +2. Use MindSpore Lite Converter to convert the file format in Windows. [Windows-x64 tool download link](https://www.mindspore.cn/lite/docs/en/r1.7/use/downloads.html) ```shell -# Set the path of the package after downloading and extracting, {converter_path}is the path to the extracted toolkit, PACKAGE_ROOT_PATH is the environment variable that is set +# Download and decompress the software package and set the path of the software package. {converter_path} indicates the path of the decompressed tool package, and PACKAGE_ROOT_PATH indicates the environment variable. set PACKAGE_ROOT_PATH={converter_path} -# Include the dynamic-link libraries required by the conversion tool in the environment variables PATH +# Add the dynamic link library required by the converter to the environment variable PATH. set PATH=%PACKAGE_ROOT_PATH%\tools\converter\lib;%PATH% -# Execute the conversion command in mindspore-lite-win-x64\tools\converter\converter +# Run the following command in the mindspore-lite-win-x64\tools\converter\converter directory: call converter_lite --fmk=MINDIR --modelFile=mobilenet_v2_1.0_224.mindir --outputFile=mobilenet_v2_1.0_224 ``` -After the conversion is successful, `CONVERTL RESULT SUCCESS:0` is printed, and the `mobilenet_v2_1.0_224.ms` file is generated in the current directory. +After the conversion is successful, `CONVERT RESULT SUCCESS:0` is displayed, and the `mobilenet_v2_1.0_224.ms` file is generated in the current directory. -> For other environments to download MindSpore Lite Converter, see [Download MindSpore Lite](https://www.mindspore.cn/lite/docs/en/r1.7/use/downloads.html). +> For details about how to download MindSpore Lite Converter in other environments, see [Download MindSpore Lite](https://www.mindspore.cn/lite/docs/en/r1.7/use/downloads.html). ### Application Deployment -Download [Android apps APK](https://gitee.com/mindspore/vision/releases/) of the MindSpore Vision Suite and install the APK on your phone, whose app name appears as `MindSpore Vision`. +Download [Android app APK](https://gitee.com/mindspore/vision/releases/) of the MindSpore Vision Suite and install the APK on your phone. The app name is `MindSpore Vision`. -> MindSpore Vision APK is mainly used as an example of a visual development tool, providing basic UI functions such as taking pictures and selecting pictures, and providing AI application DEMO such as classification, detection, and face recognition. +> The MindSpore Vision APK is used as an example of the visual development tool. It provides basic UI functions such as photographing and image selection, and provides AI application demos such as classification, detection, and face recognition. -After opening the APP and clicking on the `classification` module on the home page, you can click the middle button to take a picture and get the picture, or click the image button in the upper sidebar to select the picture album for the image classification function. +Open the app, tap the `classification` module on the home screen, and then tap the middle button to take photos or tap the image album button on the top bar to select an image for classification. ![main](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_zh_cn/beginner/images/app1.png) -By default, the MindSpore Vision `classification` module has a built-in universal AI network model to identify and classify images. +By default, the MindSpore Vision `classification` module has a built-in general AI network model for image identification and classification. ![result](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_zh_cn/beginner/images/app2.png) ### Application Experience -Finally, the custom network model `mobilenet_v2_1.0_224.ms` trained above is deployed to the Android phone side to experience the recognition function of dog and croissants. +Finally, the custom network model `mobilenet_v2_1.0_224.ms` trained above is deployed to the Android mobile phone to experience the recognition function of dogs and croissants. #### Customizing the Model Label Files -Customizing model deployment requires the following format to define the information for the network model, that is, customizing the label files, and creating a json format label file that must be named after `custom.json` on the local computer side. +To deploy a custom model, you need to define the information required by the network model in the following format, that is, customize a label file, and create a label file in JSON format named `custom.json` on the local computer. ```text -"title": 'dog and croissants', -"file": 'mobilenet_v2_1.0_224.ms', -"label": ['croissants', 'dag'] +{ + "title": 'dog and croissants', + "file": 'mobilenet_v2_1.0_224.ms', + "label": ['croissants', 'dog'] +} ``` -The Json label file should contain three Key value fields of `title`, `file`, and `label`, the meaning of which is as follows: +The JSON label file must contain the `title`, `file`, and `label` key fields, which are described as follows: -- title: customize the module titles (dog and croissants); -- file: the name of the model file converted above; and -- label: `array` information for customizing the label. +- title: custom module titles (dog and croissants). +- file: the name of the model file converted above. +- label: `array` information about the custom label. -#### Labels and Model Files Deployed to the Phone +#### Labels and Model Files Deployed to Mobile Phones -By pressing the `classification` button on the home page of the `MindSpore Vision APK`, you can enter the customization classification mode and select the tags and model files that need to be deployed. +On the home page of the `MindSpore Vision APK`, hold down the `classification` button to enter the custom classification mode and select the labels and model files to be deployed. -In order to achieve the recognition function of the mobile phone between dog and croissants, the label file `custom.json` file and the model file `mobilenet_v2_1.0_224.ms` should be placed together in the specified directory on the mobile phone. Here to take the `Android/data/Download/` folder as an example, you need to put the tag file and the model file at the same time in the above mobile phone directory first, as shown in the figure, then click the customize button, and the system file function will pop up. You can click the open file in the upper left corner, and then find the directory address where the Json tag file and the model file are stored, and select the corresponding Json file. +To implement the identification function of the dogs and croissants on the mobile phone, you need to place the label file `custom.json` and model file `mobilenet_v2_1.0_224.ms` to the specified directory on the mobile phone. The `Android/data/Download/` folder is used as an example. Place the label file and model file in the preceding mobile phone address, as shown in the following figure. Click the custom button. The system file function is displayed. Click icon in the upper left corner and find the directory where the JSON label file and model file are stored, and select the corresponding JSON file. ![step](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_zh_cn/beginner/images/app3.png) -After the label and model file are deployed to the mobile phone, you can click the middle button to take a picture to get the picture, or click the image button in the upper sidebar to select the picture album for the image, and you can classify the dog and the croissants. +After the label and model file are deployed on the mobile phone, you can click the middle button to take photos and obtain images, or click the image button on the upper side bar to select an image album for images. In this way, the dogs and croissants can be classified and identified. ![result1](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_zh_cn/beginner/images/app4.png) -> This chapter only covers the simple deployment process on the phone side. For more information about inference, please refer to [MindSpore Lite](https://www.mindspore.cn/lite/docs/en/r1.7/index.html). - - - - - - - +> This chapter only covers the simple deployment process on the mobile phone. For more information about inference, please refer to [MindSpore Lite](https://www.mindspore.cn/lite/docs/en/r1.7/index.html). diff --git a/tutorials/source_en/beginner/introduction.ipynb b/tutorials/source_en/beginner/introduction.ipynb deleted file mode 100644 index 32f0067123..0000000000 --- a/tutorials/source_en/beginner/introduction.ipynb +++ /dev/null @@ -1,142 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "source": [ - "# Overview\n", - "\n", - "[![View-Source](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/resource/_static/logo_source_en.png)](https://gitee.com/mindspore/docs/blob/r1.7/tutorials/source_en/beginner/introduction.ipynb)\n", - "\n", - "The following describes the Huawei AI full-stack solution and introduces the position of MindSpore in the solution. Developers who are interested in MindSpore can visit the [MindSpore community](https://gitee.com/mindspore/mindspore) and click [Watch, Star, and Fork](https://gitee.com/mindspore/mindspore).\n", - "\n", - "## MindSpore Introduction\n", - "\n", - "MindSpore is a deep learning framework in all scenarios, aiming to achieve easy development, efficient execution, and all-scenario coverage.\n", - "\n", - "Easy development features friendly APIs and easy debugging. Efficient execution is reflected in computing, data preprocessing, and distributed training. All-scenario coverage means that the framework supports cloud, edge, and device scenarios.\n", - "\n", - "The following figure shows the overall MindSpore architecture:\n", - "\n", - "![MindSpore-arch](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_en/beginner/images/introduction2.png)\n", - "\n", - "- **ModelZoo**: ModelZoo provides available deep learning algorithm networks, and more developers are welcome to contribute new networks ([ModelZoo](https://gitee.com/mindspore/models)).\n", - "- **Extend**: The expansion package of MindSpore expands the support of new fields, such as GNN/deep probabilistic programming/reinforcement learning, etc. We look forward to more developers to contribute and build together.\n", - "- **Science**:MindScience is a scientific computing kits for various industries based on the converged MindSpore framefork. It contains the industry-leading datasets, basic network structures, high-precision pre-trained models, and pre-and post-processing tools that accelerate application development of the scientific computing ([More Information](https://mindspore.cn/mindscience/docs/en/r1.7/index.html)).\n", - "- **Expression**: Python-based frontend expression and programming interfaces. In the future, more frontends based on C/C++ will be provided. Cangjie, Huawei's self-developed programming language frontend, is now in the pre-research phase. In addition, Huawei is working on interconnection with third-party frontends to introduce more third-party ecosystems.\n", - "- **Data**: Providing functions such as efficient data processing, common data sets loading and programming interfaces, and supporting users to flexibly define processing registration and pipeline parallel optimization.\n", - "- **Compiler**: The core compiler of the layer, which implements three major functions based on the unified device-cloud MindIR, including hardware-independent optimization (type derivation, automatic differentiation, and expression simplification), hardware-related optimization (automatic parallelism, memory optimization, graph kernel fusion, and pipeline execution) and optimization related to deployment and inference (quantification and pruning).\n", - "- **Runtime**: MindSpore runtime system, which covers the cloud-side host-side runtime system, the device-side and the lightweight runtime system of the smaller IoT.\n", - "- **Insight**: Provides MindSpore's visual debugging and tuning tools, and supports users to debug and tune the training network ([More Information](https://mindspore.cn/mindinsight/docs/en/r1.7/index.html)).\n", - "- **Armour**: For enterprise-level applications, security and privacy protection related enhancements, such as anti-robustness, model security testing, differential privacy training, privacy leakage risk assessment, data drift detection, etc. technology ([More Information](https://mindspore.cn/mindarmour/docs/en/r1.7/index.html)).\n", - "\n", - "### Execution Process\n", - "\n", - "With an understanding of the overall architecture of Ascend MindSpore, we can look at the overall coordination relationship between the various modules, as shown in the figure:\n", - "\n", - "![MindSpore](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_en/beginner/images/introduction4.png)\n", - "\n", - "As a full-scenario AI framework, MindSpore supports different series of hardware for end (mobile phone and IOT device), edge (base station and routing device), and cloud (server) scenarios, including Ascend series products, NVIDIA series products, Arm series Qualcomm Snapdragon, Huawei Kirin chips and other series of products.\n", - "\n", - "The blue box on the left is the MindSpore main framework, which mainly provides the basic API functions related to the training and verification of neural networks, and also provides automatic differentiation and automatic parallelism by default.\n", - "\n", - "Below the blue box is the MindSpore Data module, which can be used for data preprocessing, including data sampling, data iteration, data format conversion and other different data operations. In the process of training, many debugging and tuning problems will be encountered, so the MindSpore Insight module visualizes the data related to debugging and tuning such as loss curves, operator execution, and weight parameter variables, so as to facilitate users to debug and optimize during training.\n", - "\n", - "The simplest way to be AI security is from the perspective of attack and defense, for example, the attacker introduces malicious data during the training stage, affecting the inference ability of the AI model, so MindSpore launched the MindSpore Armour module to provide AN security mechanisms for MindSpore.\n", - "\n", - "The content above the blue box is closer to the users related to algorithm development, including storing a large number of AI algorithm model libraries ModelZoo, providing a development tool suite for different fields MindSpore DevKit, and a high-level expansion library MindSpore Extend, which is worth mentioning the scientific computing suite MindSciences in MindSpore Extend. For the first time, MindSpore explores the combination of scientific computing and deep learning, the combination of numerical computing and deep learning, and the support of electromagnetic simulation, drug molecule simulation and so on through deep learning.\n", - "\n", - "After the neural network model is trained, you can export the model or load the model that has been trained in MindSpore Hub. Then MindIR provides a unified IR format for the end cloud, which defines the logical structure of the network and the properties of the operators through the unified IR, and decouples the model file in the MindIR format with the hardware platform to achieve multiple deployments at one time. Therefore, as shown in the figure, the model is exported to different modules through IR to perform inference.\n", - "\n", - "### Design Philosophy\n", - "\n", - "- Support full-scenario collaboration\n", - "\n", - " MindSpore is an industry-wide best practice. It provides data scientists and algorithm engineers with a unified interface for model training, inference, and export. It supports flexible deployment in different scenarios such as end, edge, and cloud, and promotes the prosperity and development in deep learning, scientific computing and other fields.\n", - "\n", - "- Provide Python programming normal form to simplify AI programming\n", - "\n", - " MindSpore provides a Python programming normal form. Users can build complex neural network models using Python's native control logic, making AI programming easy.\n", - "\n", - "- Provide a unified coding method for PyNative graphs and static graphs\n", - "\n", - " At present, there are two execution modes of mainstream deep learning frameworks, they are Graph mode and PyNative mode. Graph mode has high training performance but is difficult to debug. Although the Pynative mode is easier to debug than the static graph mode, it is difficult to execute efficiently. MindSpore provides a unified coding method for PyNative graphs and static graphs, which greatly increases their compatibility. Users do not need to develop multiple sets of codes, and can switch the mode only by changing one line of code. For example, users can set `context.set_context(mode=context.PYNATIVE_MODE)` to switch to the PyNative mode and set `context.set_context(mode=context.GRAPH_MODE)` to switch to the Graph mode, users can have an easier development, debugging and performance experience.\n", - "\n", - "- Use functional differentiable programming architecture and allow users to focus on the mathematical native expression of model algorithms\n", - "\n", - " Neural network models are usually trained based on gradient descent algorithms, but the manual derivation process is complicated and the results are prone to errors. The Automatic Differentiation mechanism based on Source Code Transformation of MindSpore adopts a functional differentiable programming architecture, and provides a Python programming interface at the interface layer, including the expression of control flow. Users can focus on the mathematically native expression of the model algorithm without manual derivation.\n", - "\n", - "- Unify the coding method of single device and distributed training\n", - "\n", - " With the increasing scale of neural network models and datasets, distributed parallel training has become a common practice in neural network training. However, the strategy selection and writing of distributed parallel training are very complicated, which seriously restricts the training efficiency of deep learning models. MindSpore unifies the coding methods of single device and distributed training. Developers do not need to write complex distributed strategies. They can implement distributed training by adding a small amount of code to the single device code. For example, setting `context.set_auto_parallel_context(parallel_mode=ParallelMode.AUTO_PARALLEL)` can automatically establish a cost model, select an optimal parallel mode for users, improve the efficiency of neural network training, greatly reduce the threshold of AI development, and enable users to quickly implement model ideas.\n", - "\n", - "### API Level Structure\n", - "\n", - "To support network building, entire graph execution, subgraph execution, and single-operator execution, MindSpore provides users with three levels of APIs. In ascending order, these are Low-Level Python API, Medium-Level Python API, and High-Level Python API.\n", - "\n", - "![MindSpore API](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_zh_cn/beginner/images/introduction3.png)\n", - "\n", - "- High-Level Python API\n", - "\n", - " High-level APIs are at the first layer. Based on the medium-level API, these advanced APIs include training and inference management, mixed precision training, and debugging and optimization, enabling users to control the execution process of the entire network and implement training, inference, and optimization of the neural network. For example, by utilizing the Model API, users can specify the neural network model to be trained as well as related training settings, train the neural network model, and debug the neural network performance through the Profiler API.\n", - "\n", - "- Medium-Level Python API\n", - "\n", - " Medium-level APIs are at the second layer, which encapsulates low-cost APIs and provides such modules as the network layer, optimizer, and loss function. Users can flexibly build neural networks and control execution processes through the medium-level API to quickly implement model algorithm logic. For example, users can call the Cell API to build neural network models and computing logic, add the loss function and optimization methods to the neural network model by using the loss module and Optimizer API, and use the dataset module to process data for model training and derivation.\n", - "\n", - "- Low-Level Python API\n", - "\n", - " Low-level APIs are at the third layer, including tensor definition, basic operators, and automatic differential modules, enabling users to easily define tensors and perform derivative computation. For example, users can customize tensors by using the Tensor API, and use the GradOperation operator in the ops.composite module to calculate the derivative of the function at a specified position.\n", - "\n", - "## Introduction to Huawei Ascend AI Full-Stack Solution\n", - "\n", - "Ascend computing is a full-stack AI computing infrastructure and application based on the Ascend series processors. It includes the Ascend series chips, Atlas series hardware, CANN chip enablement, MindSpore AI framework, ModelArts, and MindX application enablement.\n", - "\n", - "Huawei Atlas AI computing solution is based on Ascend series AI processors and uses various product forms such as modules, cards, edge stations, servers, and clusters to build an all-scenario AI infrastructure solution oriented to device, edge, and cloud. It covers data center and intelligent edge solutions, as well as the entire inference and training processes in the deep learning field.\n", - "\n", - "- **Atlas series**: provides AI training, inference cards, and training servers ([learn more](https://e.huawei.com/en/products/cloud-computing-dc/atlas/)).\n", - "- **CANN at heterogeneous computing architecture**: a driver layer that enables chips ([learn more](https://www.hiascend.com/en/software/cann)).\n", - "- **MindSpore**: all-scenario AI framework ([learn more](https://www.mindspore.cn/en)).\n", - "- **MindX SDK**: Ascend SDK that provides application solutions ([learn more](https://www.hiascend.com/en/software/mindx-sdk)).\n", - "- **ModelArts**: HUAWEI CLOUD AI development platform ([learn more](https://www.huaweicloud.com/product/modelarts.html)).\n", - "- **MindStudio**: E2E development toolchain that provides one-stop IDE for AI development ([learn more](https://www.hiascend.com/en/software/mindstudio)).\n", - "\n", - "For details, click [Huawei Ascend official website](https://e.huawei.com/en/products/servers/ascend).\n", - "\n", - "## Joining the Community\n", - "\n", - "Welcome every developer to the MindSpore community and contribute to this all-scenario AI framework.\n", - "\n", - "- **MindSpore official website**: provides comprehensive MindSpore information, including installation, tutorials, documents, community, resources, and news ([learn more](https://www.mindspore.cn/en)).\n", - "- **MindSpore code**:\n", - "\n", - " - [MindSpore Gitee](https://gitee.com/mindspore/mindspore): Top 1 Gitee open-source project in 2020, where you can track the latest progress of MindSpore by clicking Watch, Star, and Fork, discuss issues, and commit code.\n", - "\n", - " - [MindSpore Github](https://github.com/mindspore-ai/mindspore): MindSpore code image of Gitee. Developers who are accustomed to using GitHub can learn MindSpore and view the latest code implementation here.\n", - "\n", - "- **MindSpore forum**: We are dedicated to serving every developer. You can find your voice in MindSpore, regardless of whether you are an entry-level developer or a master. Let's learn and grow together. ([Learn more](https://bbs.huaweicloud.com/forum/forum-1076-1.html))" - ], - "metadata": {} - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.0" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} \ No newline at end of file diff --git a/tutorials/source_en/beginner/introduction.md b/tutorials/source_en/beginner/introduction.md new file mode 100644 index 0000000000..a35852a130 --- /dev/null +++ b/tutorials/source_en/beginner/introduction.md @@ -0,0 +1,118 @@ +# Overview + + + +The following describes the Huawei AI full-stack solution and the position of MindSpore in the solution. Developers who are interested in MindSpore can visit the [MindSpore community](https://gitee.com/mindspore/mindspore) and click [Watch, Star, and Fork](https://gitee.com/mindspore/mindspore). + +## Introduction to MindSpore + +MindSpore is a deep learning framework in all scenarios, aiming to achieve easy development, efficient execution, and all-scenario coverage. + +Easy development features user-friendly APIs and low debugging difficulty. Efficient execution is reflected in computing, data preprocessing, and distributed training. All-scenario coverage means that the framework supports cloud, edge, and device scenarios. + +The following figure shows the overall MindSpore architecture: + +![MindSpore-arch](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_en/beginner/images/introduction2.png) + +- **ModelZoo**: provides available deep learning algorithm networks, and more developers are welcome to contribute new networks. ([ModelZoo](https://gitee.com/mindspore/models)) +- **Extend**: The MindSpore expansion package supports new domain scenarios, such as GNN, deep probabilistic programming, and reinforcement learning. More developers are expected to contribute and build the library. +- **Science**: MindScience is a scientific computing kit for various industries based on the converged MindSpore framework. It contains the industry-leading datasets, basic network structures, high-precision pre-trained models, and pre-and post-processing tools that accelerate application development of the scientific computing ([More Information](https://mindspore.cn/mindscience/docs/en/r1.7/index.html)). +- **Expression**: Python-based frontend expression and programming interfaces. In the future, Huawei plans to continue to provide interconnection with third-party front-end systems, such as C/C++ and Huawei-developed programming language front-end (currently in the pre-research phase), to introduce more third-party ecosystems. +- **Data**: provides functions such as efficient data processing, common dataset loading and programming interfaces, and allows users to flexibly define processing registration and pipeline parallel optimization. +- **Compiler**: The core compiler of the layer, which implements three major functions based on the unified device-cloud MindIR, including hardware-independent optimization (type derivation, automatic differentiation, and expression simplification), hardware-related optimization (automatic parallelism, memory optimization, graph kernel fusion, and pipeline execution), and optimization related to deployment and inference (quantification and pruning). +- **Runtime**: MindSpore runtime system, including the runtime system on the cloud host, runtime system on the device, and lightweight runtime system of the IoT platform. +- **Insight**: MindSpore visualized debugging and tuning tool, allowing users to debug and tune the training network ([More Information](https://mindspore.cn/mindinsight/docs/en/r1.7/index.html)). +- **Armour**: For enterprise-level applications, provides enhanced functions related to security and privacy protection, such as anti-robustness, model security testing, differential privacy training, privacy leakage risk assessment, and data drift detection. ([Learn more] ()) + +### Execution Process + +With an understanding of the overall architecture of MindSpore, we can look at the overall coordination relationship between the various modules, as shown in the figure: + +![MindSpore](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_en/beginner/images/introduction4.png) + +As an all-scenario AI framework, MindSpore supports different series of hardware in the device (mobile phone and IoT device), edge (base station and routing device), and cloud (server) scenarios, including Ascend series products and NVIDIA series products, Qualcomm Snapdragon in the ARM series, and Huawei Kirin chips. + +The blue box on the left is the main MindSpore framework, which mainly provides the basic API functions related to the training and verification of neural networks, and also provides automatic differentiation and automatic parallelism by default. + +Below the blue box is the MindSpore Data module, which can be used for data preprocessing, including data sampling, data iteration, data format conversion, and other data operations. Many debugging and tuning problems may occur during training. Therefore, the MindSpore Insight module visualizes debugging and tuning data such as the loss curve, operator execution status, and weight parameter variables, facilitating debugging and optimization during training. + +The simplest way to ensure AI security is from the perspective of attack and defense. For example, attackers inject malicious data in the training phase to affect the inference capability of AI models. Therefore, MindSpore launches the MindSpore Armour module to provide an AI security mechanism for MindSpore. + +The content above the blue box is closer to algorithm development users, including the AI algorithm model library ModelZoo, development toolkit MindSpore DevKit for different fields, and advanced extension library MindSpore Extend. MindSciences, a scientific computing kit in MindSpore Extend, is worth mentioning. MindSpore is the first to combine scientific computing with deep learning, combine numerical computing with deep learning, and support electromagnetic simulation and drug molecular simulation through deep learning. + +After the neural network model is trained, you can export the model or load the model that has been trained in MindSpore Hub. Then MindIR provides a unified IR format for the device and cloud, which defines logical network structures and operator attributes through a unified IR, and decouples model files in MindIR format from hardware platforms to implement one-time training and multiple-time deployment. As shown in the figure, the model is exported to different modules through IR to perform inference. + +### Design Philosophy + +- Supporting full-scenario collaboration + + MindSpore is an industry-wide best practice. It provides unified model training, inference, and export APIs for data scientists and algorithm engineers. It supports flexible deployment in different scenarios such as the device, edge, and cloud, and promotes the prosperity of domains such as deep learning and scientific computing. + +- Provideing the Python programming paradigm to simplify AI programming + + MindSpore provides a Python programming paradigm. Users can build complex neural network models using Python's native control logic, making AI programming easy. + +- Providing a unified coding method for dynamic and static graphs + + Currently, there are two execution modes of a mainstream deep learning framework: a static graph mode (GRAPH_MODE) and a dynamic graph mode (PYNATIVE_MODE). The GRAPH mode has high training performance but is difficult to debug. On the contrary, the PYNATIVE mode is easy to debug, but is difficult to execute efficiently. + MindSpore provides an encoding mode that unifies dynamic and static graphs, which greatly improves the compatibility between static and dynamic graphs. Instead of developing multiple sets of code, users can switch between the dynamic and static graph modes by changing only one line of code. For example, set `context.set_context(mode=context.PYNATIVE_MODE)` to switch to the dynamic graph mode, or set `context.set_context(mode=context.GRAPH_MODE)` to switch to the static graph mode, which facilitates development and debugging, and improves performance experience. + +- Using functional differentiable programming architecture and allowing users to focus on the mathematical native expression of model algorithms + + Neural network models are usually trained based on gradient descent algorithms, but the manual derivation process is complicated and the results are prone to errors. The Automatic Differentiation mechanism based on Source Code Transformation (SCT) of MindSpore adopts a functional differentiable programming architecture, and provides a Python programming interface at the interface layer, including the expression of control flow. Users can focus on the mathematically native expression of the model algorithm without manual derivation. + +- Unifying the coding method of single device and distributed training + + As a scale of neural network models and datasets continuously increases, parallel distributed training becomes a common practice of neural network training. However, the strategy selection and compilation of parallel distributed training are very complex, which severely restricts training efficiency of a deep learning model and hinders development of deep learning. MindSpore unifies the coding methods of single device and distributed training. Developers do not need to write complex distributed strategies. They can implement distributed training by adding a small amount of code to the single device code. For example, they can set `context.set_auto_parallel_context(parallel_mode=ParallelMode.AUTO_PARALLEL)` to automatically establish a cost model, select an optimal parallel mode for users, improve the efficiency of neural network training, greatly reduce the threshold of AI development, and enable users to quickly implement model ideas. + +### API Level Structure + +To support network building, entire graph execution, subgraph execution, and single-operator execution, MindSpore provides users with three levels of APIs. In ascending order, these are Low-Level Python API, Medium-Level Python API, and High-Level Python API. + +![MindSpore API](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_zh_cn/beginner/images/introduction3.png) + +- High-Level Python API + + High-level APIs are at the first layer. Based on the medium-level API, these advanced APIs include training and inference management, mixed precision training, and debugging and optimization, enabling users to control the execution process of the entire network and implement training, inference, and optimization of the neural network. For example, by utilizing the Model API, users can specify the neural network model to be trained as well as related training settings, train the neural network model, and debug the neural network performance through the Profiler API. + +- Medium-Level Python API + + Medium-level APIs are at the second layer, which encapsulates low-cost APIs and provides such modules as the network layer, optimizer, and loss function. Users can flexibly build neural networks and control execution processes through the medium-level API to quickly implement model algorithm logic. For example, users can call the Cell API to build neural network models and computing logic, add the loss function and optimization methods to the neural network model by using the loss module and Optimizer API, and use the dataset module to process data for model training and derivation. + +- Low-Level Python API + + Low-level APIs are at the third layer, including tensor definition, basic operators, and automatic differential modules, enabling users to easily define tensors and perform derivative computation. For example, users can customize tensors by using the Tensor API, and use the GradOperation operator in the ops.composite module to calculate the derivative of the function at a specified position. + +## Introduction to Huawei Ascend AI Full-Stack Solution + +Ascend computing is a full-stack AI computing infrastructure and application based on the Ascend series processors. It includes the Ascend series chips, Atlas series hardware, CANN chip enablement, MindSpore AI framework, ModelArts, and MindX application enablement. + +Huawei Atlas AI computing solution is based on Ascend series AI processors and uses various product forms such as modules, cards, edge stations, servers, and clusters to build an all-scenario AI infrastructure solution oriented to device, edge, and cloud. It covers data center and intelligent edge solutions, as well as the entire inference and training processes in the deep learning field. + +The following figure shows the Ascend AI full-stack architecture: + +![ Ascend Full Stack](https://gitee.com/mindspore/docs/raw/tutorials-develop/tutorials/source_en/beginner/images/introduction1.png) + +The functions of each module are described as follows: + +- **Atlas series**: provides AI training, inference cards, and training servers ([learn more](https://e.huawei.com/en/products/cloud-computing-dc/atlas/)). +- **CANN at heterogeneous computing architecture**: a driver layer that enables chips ([learn more](https://www.hiascend.com/en/software/cann)). +- **MindSpore**: all-scenario AI framework ([learn more](https://www.mindspore.cn/en)). +- **MindX SDK (Ascend SDK)**: provides the application solution ([learn more](https://www.hiascend.com/en/software/mindx-sdk)). +- **ModelArts**: HUAWEI CLOUD AI development platform ([learn more](https://www.huaweicloud.com/product/modelarts.html)). +- **MindStudio**: E2E development toolchain that provides one-stop IDE for AI development ([learn more](https://www.hiascend.com/en/software/mindstudio)). + +For details, click [Huawei Ascend official website](https://e.huawei.com/en/products/servers/ascend). + +## Joining the Community + +Welcome every developer to the MindSpore community and contribute to this all-scenario AI framework. + +- **MindSpore official website**: provides comprehensive MindSpore information, including installation, tutorials, documents, community, resources, and news ([learn more](https://www.mindspore.cn/en)). +- **MindSpore code**: + + - [MindSpore Gitee](https://gitee.com/mindspore/mindspore): Top 1 Gitee open-source project in 2020, where you can track the latest progress of MindSpore by clicking Watch, Star, and Fork, discuss issues, and commit code. + + - [MindSpore Github](https://github.com/mindspore-ai/mindspore): MindSpore code image of Gitee. Developers who are accustomed to using GitHub can learn MindSpore and view the latest code implementation here. + +- **MindSpore forum**: We are dedicated to serving every developer. You can find your voice in MindSpore, regardless of whether you are an entry-level developer or a master. Let's learn and grow together. ([Learn more](https://bbs.huaweicloud.com/forum/forum-1076-1.html)) diff --git a/tutorials/source_en/beginner/model.md b/tutorials/source_en/beginner/model.md index 22e0126bf7..9e1ace0587 100644 --- a/tutorials/source_en/beginner/model.md +++ b/tutorials/source_en/beginner/model.md @@ -2,23 +2,23 @@ -A neural network model consists of multiple data operation layers. `mindspore.nn` provides various basic network modules. The following uses LeNet-5 as an example to first describe how to build a neural network model by using `mindspore.nn` , and then describes how to build a LeNet-5 network model by using `mindvision.classification.models`. +A neural network model consists of multiple data operation layers. `mindspore.nn` provides various basic network modules. The following uses LeNet-5 as an example to first describe how to build a neural network model by using `mindspore.nn`, and then describes how to build a LeNet-5 model by using `mindvision.classification.models`. > `mindvision.classification.models` is a network model interface developed based on `mindspore.nn`, providing some classic and commonly used network models for the convenience of users. -## LeNet-5 model +## LeNet-5 Model -[LeNet-5](https://ieeexplore.ieee.org/document/726791) is a typical convolutional neural network proposed by Professor Yann LeCun in 1998, which achieves 99.4% accuracy on the MNIST dataset and is the first classic in the field of CNN. The model structure is shown in the following figure: +[LeNet-5](https://ieeexplore.ieee.org/document/726791) is a typical convolutional neural network proposed by professor Yann LeCun in 1998, which achieves 99.4% accuracy on the MNIST dataset and is the first classic in the field of CNN. The model structure is shown in the following figure: ![LeNet-5](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_zh_cn/beginner/images/lenet.png) -According to the network structure of LeNet, there are 7 layers of LeNet removal input layer, including 2 convolutional layers, 2 sub-sampling layers, and 3 fully-connected layers. +Except the input layer, LeNet contains seven layers: three convolutional layers, two subsampling layers, and two fully-connected layers. ## Defining a Model Class -In the above figure, C is used to represent the convolutional layer, S to represent the sampling layer, and F to represent the fully-connected layer. +In the preceding figure, C indicates the convolutional layer layer, S indicates the sampling layer, and F indicates the fully-connected layer. -The input size of the picture is fixed at 32∗32. In order to get a good convolution effect, the number is required in the center of the picture, so the size at 32∗32 is actually the result of the picture at 28∗28 after filled. In addition, unlike the input picture of the three channels of the CNN network, the input of the LeNet picture is only a normalized binary image. The output of the network is a prediction probability of ten digits 0\~9, which can be understood as the probability that the input image belongs to 0\~9 digits. +The input size of an image is fixed at $32 x 32$. To achieve a good convolution effect, the number must be in the center of the image. Therefore, the input $32 x 32$ is the result after the image is filled with $28 x 28$. Unlike the three-channel input images of the CNN network, the input images of LeNet are only normalized binary images. The output of the network is the prediction probability of digits 0 to 9, which can be understood as the probability that the input image belongs to digits 0 to 9. The `Cell` class of MindSpore is the base class for building all networks and the basic unit of a network. When a neural network is required, you need to inherit the `Cell` class and overwrite the `__init__` and `construct` methods. @@ -27,25 +27,25 @@ import mindspore.nn as nn class LeNet5(nn.Cell): """ - Lenet network structure + LeNet-5 network structure """ def __init__(self, num_class=10, num_channel=1): super(LeNet5, self).__init__() - # Convolutional layer, the number of input channels is num_channel, the number of output channels is 6, and the convolutional kernel size is 5*5 + # Convolutional layer, where the number of input channels is num_channel, the number of output channels is 6, and the convolutional kernel size is 5 x 5. self.conv1 = nn.Conv2d(num_channel, 6, 5, pad_mode='valid') - # Convolutional layer, the number of input channels is 6, the number of output channels is 16, and the convolutional kernel size is 5 * 5 + # Convolutional layer, where the number of input channels is 6, the number of output channels is 16, and the convolutional kernel size is 5 x 5. self.conv2 = nn.Conv2d(6, 16, 5, pad_mode='valid') - # Fully connected layer, the number of inputs is 16*5*5, and the number of outputs is 120 + # Fully-connected layer, where the number of inputs is 16 x 5 x 5 and the number of outputs is 120. self.fc1 = nn.Dense(16 * 5 * 5, 120) - # Fully-connected layer, the number of inputs is 120, and the number of outputs is 84 + # Fully-connected layer, where the number of inputs is 120 and the number of outputs is 84. self.fc2 = nn.Dense(120, 84) - # Fully connected layer, the number of inputs is 84, and the number of classifications is num_class + # Fully-connected layer, where the number of inputs is 84 and the number of classes is num_class. self.fc3 = nn.Dense(84, num_class) - # ReLU Activation function + # ReLU activation function self.relu = nn.ReLU() # Pooling layer self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2) - # Multidimensional arrays are flattened into one-dimensional arrays + # Multidimensional arrays are flattened into one-dimensional arrays. self.flatten = nn.Flatten() def construct(self, x): @@ -75,20 +75,20 @@ print(model) ```text LeNet5< - (conv1): Conv2d - (conv2): Conv2d - (fc1): Dense - (fc2): Dense - (fc3): Dense - (relu): ReLU<> - (max_pool2d): MaxPool2d - (flatten): Flatten<> - > +(conv1): Conv2d +(conv2): Conv2d +(fc1): Dense +(fc2): Dense +(fc3): Dense +(relu): ReLU<> +(max_pool2d): MaxPool2d +(flatten): Flatten<> +> ``` ## Model Layers -The following describes the key member functions of the `Cell` class used in LeNet, and then describes how to use the `Cell` class to access model parameters through the instantiation network. For more `cell` class contents, refer to [mindspore.nn interface](https://www.mindspore.cn/docs/en/r1.7/api_python/mindspore.nn.html). +The following describes the key member functions of the `Cell` class used in LeNet-5, and then describes how to use the `Cell` class to access model parameters through the instantiation network. For more information about the `Cell` class, see [mindspore.nn interface](https://www.mindspore.cn/docs/en/r1.7/api_python/mindspore.nn.html). ### nn.Conv2d @@ -100,7 +100,7 @@ import numpy as np from mindspore import Tensor from mindspore import dtype as mstype -# The number of channels input is 1, the number of channels of output is 6, the convolutional kernel size is 5*5, and the parameters are initialized using the norm operator, and the pixels are not filled +# The number of channels input is 1, the number of channels of output is 6, the convolutional kernel size is 5 x 5, and the parameters are initialized using the normal operator, and the pixels are not filled. conv2d = nn.Conv2d(1, 6, 5, has_bias=False, weight_init='normal', pad_mode='same') input_x = Tensor(np.ones([1, 1, 32, 32]), mstype.float32) @@ -108,7 +108,7 @@ print(conv2d(input_x).shape) ``` ```text -(8, 6, 32, 32) +(1, 6, 32, 32) ``` ### nn.ReLU @@ -125,7 +125,7 @@ print(output) ``` ```text - [0. 2. 0. 2. 0.] +[0. 2. 0. 2. 0.] ``` ### nn.MaxPool2d @@ -140,23 +140,23 @@ print(max_pool2d(input_x).shape) ``` ```text - (1, 6, 7, 7) +(1, 6, 7, 7) ``` ### nn.Flatten -Initialize the `nn.Flatten` layer and convert the 1x16 x 5 x 5 array into 400 consecutive arrays. +Initialize the `nn.Flatten` layer and convert the 1 x 16 x 5 x 5 array into 400 consecutive arrays. ```python flatten = nn.Flatten() -input_x = Tensor(np.ones([1, 16, 5, 5]), mindspore.float32) +input_x = Tensor(np.ones([1, 16, 5, 5]), mstype.float32) output = flatten(input_x) print(output.shape) ``` ```text - (1, 400) +(1, 400) ``` ### nn.Dense @@ -172,12 +172,12 @@ print(output.shape) ``` ```text - (1, 120) +(1, 120) ``` ## Model Parameters -The convolutional layer and fully-connected layer in the network will have weights and offsets after being instantiated, which has a weight parameter and a bias parameter, and these parameters are optimized in subsequent training. During training, you can use `get_parameters()` to view information such as the name, shape, data type, and whether the network layers are inversely calculated. +After instantiation is performed on the convolutional layer and the fully-connected layer in the network, there are a weight parameter and an offset parameter. These parameters are continuously optimized in a training process. During training, you can use `get_parameters()` to view the name, shape, and data type of each network layer, and whether backward calculation is performed. ```python for m in model.get_parameters(): @@ -195,14 +195,14 @@ layer:backbone.fc3.weight, shape:(10, 84), dtype:Float32, requeires_grad:True layer:backbone.fc3.bias, shape:(10,), dtype:Float32, requeires_grad:True ``` -## Quickly Build a LeNet-5 Network Model +## Quickly Building a LeNet-5 Model -The above describes the use of `mindspore.nn.cell` to build a LeNet-5 network model. The network model interface has been built in `mindvision.classification.models`, and the LeNet-5 network model can be directly built using the `lenet` interface. +The preceding describes how to use `mindspore.nn.cell` to build a LeNet-5 model. The built network model API is available in `mindvision.classification.models`. You can also use the `lenet` API to directly build a LeNet-5 model. ```python from mindvision.classification.models import lenet -# num_classes represents the category of the classification, and pretrained indicates whether to train with the trained model +# `num_classes` indicates the number of classes, and `pretrained` determines whether to train with the trained model. model = lenet(num_classes=10, pretrained=False) for m in model.get_parameters(): @@ -218,5 +218,4 @@ layer:backbone.fc2.weight, shape:(84, 120), dtype:Float32, requeires_grad:True layer:backbone.fc2.bias, shape:(84,), dtype:Float32, requeires_grad:True layer:backbone.fc3.weight, shape:(10, 84), dtype:Float32, requeires_grad:True layer:backbone.fc3.bias, shape:(10,), dtype:Float32, requeires_grad:True -``` - +``` \ No newline at end of file diff --git a/tutorials/source_en/beginner/quick_start.md b/tutorials/source_en/beginner/quick_start.md index 53218b9694..1282ca41ba 100644 --- a/tutorials/source_en/beginner/quick_start.md +++ b/tutorials/source_en/beginner/quick_start.md @@ -6,18 +6,18 @@ This section runs through the basic process of MindSpore deep learning, using th ## Downloading and Processing the Dataset -Datasets are very important for model training, and good datasets can effectively improve training accuracy and efficiency. The MNIST dataset used in the example consists of 28∗28 grayscale images of 10 classes. The training dataset contains 60,000 images, and the test dataset contains 10,000 images. +Datasets are crucial for model training. A good dataset can effectively improve training accuracy and efficiency. The MNIST dataset used in the example consists of 28 x 28 grayscale images of 10 classes. The training dataset contains 60,000 images, and the test dataset contains 10,000 images. ![mnist](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_zh_cn/beginner/images/mnist.png) -> You can download it from the [MNIST dataset download page](http://yann.lecun.com/exdb/mnist/), unzip it and place it in the bottom directory structure. +> You can download the dataset from [MNIST dataset download page](http://yann.lecun.com/exdb/mnist/), decompress it, and save it according to the following directory structure. -The MindSpore Vision suite provides a Mnist module for downloading and processing MNIST datasets, and the following sample code downloads, extracts, and processes datasets to a specified location: +The MindSpore Vision suite provides the Mnist module for downloading and processing MNIST datasets. The following sample code downloads and decompresses the datasets to the specified location for data processing: ```python from mindvision.dataset import Mnist -# Download and process the MNIST dataset +# Download and process the MNIST dataset. download_train = Mnist(path="./mnist", split="train", batch_size=32, repeat_num=1, shuffle=True, resize=32, download=True) download_eval = Mnist(path="./mnist", split="test", batch_size=32, resize=32, download=True) @@ -29,12 +29,12 @@ dataset_eval = download_eval.run() Parameters description: - path: dataset path. -- split: dataset type, supporting train, test, and infer, which defaults to train. -- batch_size: the data size set for each training batch, which defaults to 32. -- repeat_num: the number of times the dataset is traversed during training, which defaults to 1. -- shuffle: whether the dataset needs to be randomly scrambled (optional parameter). -- resize: the image size of the output image, which defaults to 32*32. -- download: whether you needs to download the dataset, which defaults to False. +- split: dataset type. The value can be train, test, or infer. The default value is train. +- batch_size: data size set for each training batch. The default value is 32. +- repeat_num: number of times that the dataset is traversed during training. The default value is 1. +- shuffle: determines whether to randomly shuffle the dataset. This parameter is optional. +- resize: size of the output image. The default value is 32 x 32. +- download: determines whether to download the dataset. The default value is False. The directory structure of the downloaded dataset files is as follows: @@ -50,11 +50,11 @@ The directory structure of the downloaded dataset files is as follows: ## Building the Model -According to the network structure of LeNet, there are 7 layers of LeNet removal input layer, including 2 convolutional layers, 2 sub-sampling layers, and 3 fully connected layers. +Except the input layer, LeNet contains seven layers: three convolutional layers, two subsampling layers, and two fully-connected layers. ![](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_zh_cn/beginner/images/lenet.png) -The MindSpore Vision Suite provides the LeNet network model interface lenet, which defines the network model as follows: +The MindSpore Vision Suite provides the LeNet model interface `lenet`, which defines the network model as follows: ```python from mindvision.classification.models import lenet @@ -62,47 +62,47 @@ from mindvision.classification.models import lenet network = lenet(num_classes=10, pretrained=False) ``` -## Defining the Loss Function and the Optimizer +## Defining a Loss Function and an Optimizer To train a neural network model, you need to define a loss function and an optimizer function. -- The loss function here uses the cross-entropy loss function `SoftmaxCrossEntropyWithLogits`. -- The optimizer here uses `Momentum`. +- The following uses the cross-entropy loss function `SoftmaxCrossEntropyWithLogits`. +- The optimizer is `Momentum`. ```python import mindspore.nn as nn from mindspore.train import Model -# Define the loss function +# Define the loss function. net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean') -# Define the optimizer function +# Define the optimizer function. net_opt = nn.Momentum(network.trainable_params(), learning_rate=0.01, momentum=0.9) ``` ## Training and Saving the Model -Before starting training, MindSpore needs to declare in advance whether the network model needs to save intermediate processes and results during training, so the `ModelCheckpoint` interface is used to save the network model and parameters for subsequent Fine-tuning operations. +Before training, MindSpore needs to declare whether the intermediate process and result of the network model need to be saved during training. Therefore, the `ModelCheckpoint` API is used to save the network model and parameters for subsequent fine-tuning. ```python from mindspore.train.callback import ModelCheckpoint, CheckpointConfig -# Set the model saving parameter +# Set the model saving parameters. config_ck = CheckpointConfig(save_checkpoint_steps=1875, keep_checkpoint_max=10) -# Apply the model saving parameter +# Apply the model saving parameters. ckpoint = ModelCheckpoint(prefix="lenet", directory="./lenet", config=config_ck) ``` -The `model.train` interface provided by MindSpore makes it easy to train the network, and `LossMonitor` can monitor the change of `loss` value during training. +The `model.train` API provided by MindSpore can be used to easily train the network. `LossMonitor` can monitor the changes of the `loss` value during the training process. ```python from mindvision.engine.callback import LossMonitor -# Initialize the model parameter +# Initialize the model parameters. model = Model(network, loss_fn=net_loss, optimizer=net_opt, metrics={'accuracy'}) -# Train the network model +# Train the network model. model.train(10, dataset_train, callbacks=[ckpoint, LossMonitor(0.01, 1875)]) ``` @@ -129,11 +129,11 @@ Epoch:[ 9/ 10], step:[ 1875/ 1875], loss:[0.000/0.000], time:1287.693 ms, lr:0. Epoch time: 1288.413 ms, per step time: 0.687 ms, avg loss: 0.000 ``` -The loss value will be printed during training, and the loss value will fluctuate, but in general, the loss value will gradually decrease and the accuracy will gradually increase. The loss values that each person runs have a certain randomness and are not necessarily exactly the same. +During the training, the loss value is printed and fluctuates. However, the loss value gradually decreases and the precision gradually increases. Loss values displayed each time may be different because of their randomness. -Verify the generalization capability of the model by running the test data set from the results obtained by running the model: +Validate the generalization capability of the model based on the result obtained by running the test dataset. -1. Use the `model.eval` interface to read in the test data set. +1. Read the test dataset using the `model.eval` API. 2. Use the saved model parameters for inference. ```python @@ -146,16 +146,16 @@ print("{}".format(acc)) {'accuracy': 0.9903846153846154} ``` -The model accuracy data can be seen in the printed information. The accuracy data in the example reaches more than 95%, and the model quality is good. As the number of network iterations increases, the model accuracy increases further. +The model accuracy data is displayed in the output content. In the example, the accuracy reaches 95%, indicating a good model quality. As the number of network epochs increases, the model accuracy will be further improved. ## Loading the Model ```python from mindspore import load_checkpoint, load_param_into_net -# Load the model that has been saved for testing +# Load the saved model used for testing. param_dict = load_checkpoint("./lenet/lenet-1_1875.ckpt") -# Load parameters into the network +# Load parameters to the network. load_param_into_net(network, param_dict) ``` @@ -169,7 +169,7 @@ load_param_into_net(network, param_dict) Use the generated model to predict the classification of a single image. The procedure is as follows: -> The predicted images will be generated randomly, and the results may be different each time. +> The predicted image is randomly generated, and the execution result may be different each time. ```python import numpy as np @@ -189,18 +189,16 @@ for i in range(1, 7): plt.imshow(images[i-1][0], interpolation="None", cmap="gray") plt.show() -# Predict the image corresponding classification by using the function model.predict +# Use the model.predict function to predict the classification of the image. output = model.predict(Tensor(data['image'])) predicted = np.argmax(output.asnumpy(), axis=1) -# Output prediction classification versus actual classification +# Output the predicted classification and the actual classification. print(f'Predicted: "{predicted}", Actual: "{labels}"') ``` -![img](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXAAAAD6CAYAAAC4RRw1AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAlq0lEQVR4nO2de7AU1bXGvyWCIKCAICIvQRDEB6CAKD5QyqgYA1FjQGNhQoqUyU1pclORpCpaJpUqzB+JicaKJBpJYhnxqkgiRkBRERU58pCXAj5Q3iIiiE9w3z/OsPhmMnPOcM7MnNnd369qim/6Mb37rO5N79VrrW0hBAghhIiPQ5q6AUIIIRqGOnAhhIgUdeBCCBEp6sCFECJS1IELIUSkqAMXQohIaVQHbmYXm9nrZrbOzCaXqlGiaZFdk4tsmyysoXHgZtYMwBoAFwLYAGARgPEhhFWla56oNLJrcpFtk8ehjdh3GIB1IYQ3AcDM/glgDICCF4OZKWuoSgghWIFVsmvE1GFX4CBtK7tWFdtDCJ1yFzbGhdIVwLv0fUNmWRZmNsnMasysphHHEpVDdk0u9dpWdq1a1udb2Jgn8KIIIUwFMBXQ/+hJIgl2NTvwsNquXbusdb/85S9dP/zww64XLFjg+osvvihf45qIJNg1TTTmCXwjgO70vVtmmYgb2TW5yLYJozEd+CIAfc2sl5m1ADAOwMzSNEs0IbJrcpFtE0aDXSghhL1m9j8AngTQDMC9IYSVJWuZaBLSZNdmzZq5Hjp0aNa64447Lu92+/btK3u7ykWabJsWGuUDDyHMAjCrRG0RVYLsmlxk22ShTEwhhIiUskehCFGtHHrogcv/rLPOylr36aefut6zZ4/rL7/8svwNE6JI9AQuhBCRog5cCCEiRS4UkSrYbXLssce6Pu+887K2mz9/vuv33nuv/A0TogHoCVwIISJFHbgQQkSKXChFwIkcgwcPzlrXtm1b12+++abrzZs3u/7888/L2DpxMLC9zj//fNfdunXL2u65555zvWnTpvI3TDQpXBenRYsWrlu3bu26VatWrj/66CPXfE1t27bNdSXuez2BCyFEpKgDF0KISFEHLoQQkSIfeBG0bNnS9Y9//OOsdaeddprr3//+9665hjT7xUTl4dDB7t0PVFMdP36866VLl2bts3btWteffPJJ+RonSgLbmH3V7du3z9qOfd1MmzZtXB999NGue/bs6bpz586u168/ML9Cr169XD/00EN5twHKUz9eT+BCCBEp6sCFECJS5EIpAi5gtH379qx1nTodmGeUQ9EOP/zw8jdMFEXHjh1dc8bloEGDXN99991Z+3AxK1GdsNuE3R5cmOzyyy/P2qd58+Z5f2vAgAGu+/Tpk3f7Qu4XhvuKf/zjH1nrNm4s/eRHegIXQohIUQcuhBCRIhdKEfCw6N13381ax9lWI0aMcM2ZfG+//Xb5GifqhadHGzVqlOunn37a9V133ZW1T66rTFQf7N7o16+f69/97neu2cUJAIcckv+Zle/xzz77zDVnXPLxOPuSYZcNF0QD5EIRQghBqAMXQohISYQLpdDs4jxcfvzxx10vX77cdTGzjO/du9d17rBo0qRJrk8//XTXJ5xwgmseqpcjmF/8Nx06dHDN0Sb9+/d3fdttt7l+//33s/aPefb5tMDuEE6246Scd955J2ufxYsXu169erXrt956K+8+H3zwgeu+ffu6/s1vfuOak31CCMWfQAnQE7gQQkRKvR24md1rZtvMbAUt62Bmc8xsbebf9nX9hqg+ZNfkItumh2JcKPcBuBPA32jZZABPhRCmmNnkzPebSt+84mAXysCBA11fccUVro8//njXnLSxcOHCen+f31Dn1jfg6bZ69OjhmodbvPyNN96o93gV4j5UuV0bA7uzLr74Ytf892fXVsJqtt+HBNt2P5xsxbVsrr/+etc7d+7M2ofr9LN7hKNN9uzZ45r7li5durjmGiuc4PPMM8+43rJlS32n0GjqfQIPITwHYEfO4jEApmX0NABjS9ssUW5k1+Qi26aHh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ErkJggg==) - ```text Predicted: "[4 6 2 3 5 1]", Actual: "[4 6 2 3 5 1]" ``` -As you can see from the printed results above, the predicted values are exactly the same as the target values. +The preceding information shows that the predicted values are the same as the target values. diff --git a/tutorials/source_en/beginner/save_load.md b/tutorials/source_en/beginner/save_load.md index b2b82f7ea5..7a41725180 100644 --- a/tutorials/source_en/beginner/save_load.md +++ b/tutorials/source_en/beginner/save_load.md @@ -2,11 +2,11 @@ -The content of the previous chapter mainly introduced how to adjust hyperparameters and train network models. During the process of training the network model, we actually want to save the intermediate and final results for fine-tune and subsequent model deployment and inference, and now start learning how to set hyperparameters and optimize model parameters. +The previous section describes how to adjust hyperparameters and train network models. During network model training, we want to save the intermediate and final results for fine-tuning and subsequent model deployment and inference. This section describes how to save and load a model. ## Model Training -The following are the basic steps and code for network model training, with sample code as follows: +The following uses the MNIST dataset as an example to describe how to save and load a network model. First, obtain the MNIST dataset and train the model. The sample code is as follows: ```python import mindspore.nn as nn @@ -16,22 +16,22 @@ from mindvision.classification.dataset import Mnist from mindvision.classification.models import lenet from mindvision.engine.callback import LossMonitor -epochs = 10 # Training batch +epochs = 10 # Training epochs -# 1. Build a dataset -download_train = Mnist(path="./mnist", split="train", batch_size=batch_size, repeat_num=1, shuffle=True, resize=32, download=True) +# 1. Build a dataset. +download_train = Mnist(path="./mnist", split="train", batch_size=32, repeat_num=1, shuffle=True, resize=32, download=True) dataset_train = download_train.run() -# 2. Define a neural network +# 2. Define a neural network. network = lenet(num_classes=10, pretrained=False) -# 3.1 Define a loss function +# 3.1 Define a loss function. net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean') -# 3.2 Defines an optimizer function +# 3.2 Define an optimizer function. net_opt = nn.Momentum(network.trainable_params(), learning_rate=0.01, momentum=0.9) -# 3.3 Initialize model parameters +# 3.3 Initialize model parameters. model = Model(network, loss_fn=net_loss, optimizer=net_opt, metrics={'accuracy'}) -# 4. Perform training on the neural network +# 4. Train the neural network. model.train(epochs, dataset_train, callbacks=[LossMonitor(0.01, 1875)]) ``` @@ -58,47 +58,46 @@ Epoch:[ 9/ 10], step:[ 1875/ 1875], loss:[0.022/0.015], time:2.177 ms, lr:0.010 Epoch time: 4623.760 ms, per step time: 2.466 ms, avg loss: 0.015 ``` -As you can see from the printed results above, the loss values tend to converge as the number of training rounds increases. +The preceding print result shows that the loss values tend to converge as the number of training epochs increases. ## Saving the Model -After training the network, the following will describe how to save and load the model. There are two main ways to save the interface of the model: - -1. One is to simply save the network model, which can be saved before and after training. The advantage is that the interface is simple and easy to use, but only the state of the network model when the command is executed is retained; +After the network training is complete, save the network model as a file. There are two types of APIs for saving models: -2. The other one is to save the interface during network model training. In the process of network model training, MindSpore automatically saves the parameters of the epoch number and step number 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. +1. One is to simply save the network model before and after training. The advantage is that the interface is easy to use, but only the network model status when the command is executed is retained. +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. -### Saving the Model Directly +### Directly Saving the Model -Use the save_checkpoint provided by MindSpore to save the model, and pass it to the network and save the path: +Use the save_checkpoint provided by MindSpore to save the model, pass it to the network, and save the path. ```python import mindspore as ms -# The defined network model is net, which is generally used before or after training -ms.save_checkpoint(net, "./MyNet.ckpt") +# The defined network model is net, which is used before or after training. +ms.save_checkpoint(network, "./MyNet.ckpt") ``` -Here, `network` is the training network, and `"./MyNet.ckpt"` is the saving path of the network model. +In the preceding command, `network` indicates the training network, and `"./MyNet.ckpt"` indicates the path for storing the network model. ### Saving the Model During Training -In the process of model training, use the `callbacks` parameter in `model.train` to pass in the object [ModelCheckpoint](https://mindspore.cn/docs/en/r1.7/api_python/mindspore.train.html#mindspore.train.callback.ModelCheckpoint) that saves the model (Generally used with [CheckpointConfig](https://mindspore.cn/docs/en/r1.7/api_python/mindspore.train.html#mindspore.train.callback.CheckpointConfig)), which can save the model parameters and generate CheckPoint (abbreviated as ckpt) files. +During model training, you can use the `callbacks` parameter in `model.train` to transfer the [ModelCheckpoint](https://mindspore.cn/docs/en/r1.7/api_python/mindspore.train.html#mindspore.train.callback.ModelCheckpoint) object (usually used together with [CheckpointConfig](https://mindspore.cn/docs/en/r1.7/api_python/mindspore.train.html#mindspore.train.callback.CheckpointConfig)) to save model parameters and generate a checkpoint (CKPT) file. -You can configure the checkpoint policies as required. The following describes the usage: +You can set `CheckpointConfig` to configure the Checkpoint policy as required. The following describes the usage: ```python from mindspore.train.callback import ModelCheckpoint, CheckpointConfig -# Set the number of epoch_num +# Set the value of epoch_num. epoch_num = 5 -# Set the model saving parameters +# Set the model saving parameters. config_ck = CheckpointConfig(save_checkpoint_steps=1875, keep_checkpoint_max=10) -# Apply the model saving parameters +# Apply the model saving parameters. ckpoint = ModelCheckpoint(prefix="lenet", directory="./lenet", config=config_ck) -model.train(epoch_num, dataset_train, callbacks=ckpoint) +model.train(epoch_num, dataset_train, callbacks=[ckpoint]) ``` In the preceding code, you need to initialize a `CheckpointConfig` class object to set the saving policy. @@ -114,15 +113,15 @@ The generated checkpoint file is as follows: ```text lenet-graph.meta # Computational graph after compiled. -lenet-1_1875.ckpt # The extension of the checkpoint file is .ckpt. -lenet-2_1875.ckpt # The file name format contains the epoch and step correspond to the saved parameters. Here are the model parameters for the 1875th step of the 2nd epoch. -lenet-3_1875.ckpt # indicates the model parameters saved for the 1875th step of the 3rd epoch. +lenet-1_1875.ckpt # The extension of the checkpoint file is '.ckpt'. +lenet-2_1875.ckpt # The file name indicates the epoch where the parameter is stored and the number of steps. Here are the model parameters for the 1875th step of the second epoch. +lenet-3_1875.ckpt # The file name indicates that the model parameters generated during the 1875th step of the third epoch are saved. ... ``` -If you use the same prefix and run the training script for multiple times, checkpoint files with the same name may be generated. To help users distinguish files generated each time, MindSpore adds underscores "_" and digits to the end of the user-defined prefix. If you want to delete the `.ckpt` file, delete the `.meta` file at the same time. +If you use the same prefix and run the training script for multiple times, checkpoint files with the same name may be generated. To help users distinguish files generated each time, MindSpore adds underscores (_) and digits to the end of the user-defined prefix. If you want to delete the `.ckpt` file, delete the `.meta` file at the same time. -For example, `lenet_3-2_1875.ckpt` indicates the CheckPoint file of the 1875th step of the 2nd epoch generated by running the 3rd script. +For example, `lenet_3-2_1875.ckpt` indicates the checkpoint file generated during the 1875th step of the second epoch after the script is executed for the third time. ## Loading the Model @@ -136,14 +135,14 @@ from mindspore import load_checkpoint, load_param_into_net from mindvision.classification.dataset import Mnist from mindvision.classification.models import lenet -# Store the model parameters in the parameter dictionary, where the model parameters saved during the training process above are loaded -param_dict = load_checkpoint("./lenet/lenet-1_1875.ckpt") +# Save the model parameters to the parameter dictionary. The model parameters saved during the training are loaded. +param_dict = load_checkpoint("./lenet/lenet-5_1875.ckpt") -# Redefine a LeNet neural network +# Redefine a LeNet neural network. net = lenet(num_classes=10, pretrained=False) -# Load parameters to the network -load_param_into_net(network, param_dict) +# Load parameters to the network. +load_param_into_net(net, param_dict) model = Model(network, loss_fn=net_loss, optimizer=net_opt, metrics={"accuracy"}) ``` @@ -152,7 +151,7 @@ model = Model(network, loss_fn=net_loss, optimizer=net_opt, metrics={"accuracy"} ### Validating the Model -After the above module loads the parameters into the network, for the inference scenario, you can call the `eval` function for inference verification. The sample code is as follows: +After the preceding modules load the parameters to the network, you can call the `eval` function to verify the inference in the inference scenario. The sample code is as follows: ```python # Call eval() for inference. @@ -164,12 +163,12 @@ print("{}".format(acc)) ``` ```text -{'accuracy': 0.9857772435897436} +{'accuracy': 0.9866786858974359} ``` ### For Transfer Learning -For task interruption retraining and fine-tuning scenarios, you can call the `train` function for transfer learning. The sample code is as follows: +For task interrupt retraining and fine-tuning scenarios, the `train` function can be called to perform transfer learning. The sample code is as follows: ```python # Define a training dataset. @@ -191,6 +190,4 @@ Epoch:[ 3/ 5], step:[ 1875/ 1875], loss:[0.000/0.008], time:2.001 ms, lr:0.010 Epoch time: 4235.036 ms, per step time: 2.259 ms, avg loss: 0.008 Epoch:[ 4/ 5], step:[ 1875/ 1875], loss:[0.002/0.008], time:2.039 ms, lr:0.01000 Epoch time: 4354.482 ms, per step time: 2.322 ms, avg loss: 0.008 -``` - - +``` \ No newline at end of file diff --git a/tutorials/source_en/beginner/tensor.ipynb b/tutorials/source_en/beginner/tensor.ipynb deleted file mode 100644 index a90d4bddf6..0000000000 --- a/tutorials/source_en/beginner/tensor.ipynb +++ /dev/null @@ -1,695 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "source": [ - "# Tensor\n", - "\n", - "[![View-Source](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/resource/_static/logo_source_en.png)](https://gitee.com/mindspore/docs/blob/r1.7/tutorials/source_en/beginner/tensor.ipynb)\n", - "\n", - "Tensor is a multilinear function that can be used to represent linear relationships between vectors , scalars , and other tensors. The basic examples of these linear relations are the inner product , the outer product , the linear map , and the Cartesian product. In the $n$ dimensional space, its coordinates have $n^{r}$ components, where each component is a function of coordinates, and these components are also linearly transformed according to certain rules when the coordinates are transformed. $r$ is called the rank or order of this tensor (neither has anything to do with the rank and order of the matrix).\n", - "\n", - "Tensor is a special data structure that is very similar to arrays and matrices. [Tensor](https://www.mindspore.cn/docs/en/r1.7/api_python/mindspore/mindspore.Tensor.html) is the basic data structure in MindSpore network operations, and this chapter mainly introduces the attributes and usage of the tensor and the sparse tensor." - ], - "metadata": {} - }, - { - "cell_type": "markdown", - "source": [ - "## Building the Tensor\n", - "\n", - "There are multiple methods for initializing the tensor. When building the tensor, you can pass the Tensor with `float`, `int`, `bool`, `tuple`, `list`, and `NumPy.array` types.\n", - "\n", - "- **Generating a tensor based on data.**\n", - "\n", - "You can create a tensor based on data. The data type can be set or automatically inferred." - ], - "metadata": {} - }, - { - "cell_type": "code", - "execution_count": 2, - "source": [ - "from mindspore import Tensor\n", - "\n", - "x = Tensor(0.1)" - ], - "outputs": [], - "metadata": {} - }, - { - "cell_type": "markdown", - "source": [ - "- **Generating a tensor from the NumPy array.**\n", - "\n", - "You can create a tensor from the NumPy array." - ], - "metadata": {} - }, - { - "cell_type": "code", - "execution_count": 3, - "source": [ - "import numpy as np\n", - "\n", - "arr = np.array([1, 0, 1, 0])\n", - "tensor_arr = Tensor(arr)\n", - "\n", - "print(type(arr))\n", - "print(type(tensor_arr))" - ], - "outputs": [], - "metadata": {} - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "\n", - "\n", - "" - ] - }, - { - "cell_type": "markdown", - "source": [ - "If the initial value is `NumPy.array`, the generated `Tensor` data type corresponds to `NumPy.array`." - ], - "metadata": {} - }, - { - "cell_type": "markdown", - "source": [ - "- **Generating a tensor by using the init**\n", - "\n", - "When using the `init` to initialize a tensor, the parameters that support passing are `init`, `shape` and `dtype`.\n", - "\n", - "- `init`: Support passing the subclass of [initializer](https://www.mindspore.cn/docs/en/r1.7/api_python/mindspore.common.initializer.html).\n", - "- `shape`: Support passing `list`, `tuple`, `int`.\n", - "- `dtype`: Support passing [mindspore.dtype](https://www.mindspore.cn/docs/en/r1.7/api_python/mindspore.html#mindspore.dtype)." - ], - "metadata": {} - }, - { - "cell_type": "code", - "execution_count": 4, - "source": [ - "from mindspore import Tensor\n", - "from mindspore import set_seed\n", - "from mindspore import dtype as mstype\n", - "from mindspore.common.initializer import One, Normal\n", - "\n", - "set_seed(1)\n", - "\n", - "tensor1 = Tensor(shape=(2, 2), dtype=mstype.float32, init=One())\n", - "tensor2 = Tensor(shape=(2, 2), dtype=mstype.float32, init=Normal())\n", - "\n", - "print(\"tensor1:\\n\", tensor1)\n", - "print(\"tensor2:\\n\", tensor2)" - ], - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "tensor1:\n", - " [[1. 1.]\n", - " [1. 1.]]\n", - "tensor2:\n", - " [[-0.00128023 -0.01392901]\n", - " [ 0.0130886 -0.00107818]]\n" - ] - } - ], - "metadata": {} - }, - { - "cell_type": "markdown", - "source": [ - "The `init` is used for delayed initialization in parallel mode. Usually, it is not recommended to use `init` interface to initialize parameters.\n", - "\n", - "- **Inheriting attributes of another tensor to form a new tensor.**" - ], - "metadata": {} - }, - { - "cell_type": "code", - "execution_count": 5, - "source": [ - "from mindspore import ops\n", - "\n", - "oneslike = ops.OnesLike()\n", - "x = Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32))\n", - "output = oneslike(x)\n", - "\n", - "print(output)\n", - "print(\"input shape:\", x.shape)\n", - "print(\"output shape:\", output.shape)" - ], - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "[[1 1]\n", - " [1 1]]\n", - "input shape: (2, 2)\n", - "output shape: (2, 2)\n" - ] - } - ], - "metadata": {} - }, - { - "cell_type": "markdown", - "source": [ - "- **Outputting a constant tensor of a specified size.**\n", - "\n", - "`shape` is the size tuple of a tensor, which determines the dimension of the output tensor." - ], - "metadata": {} - }, - { - "cell_type": "code", - "execution_count": 6, - "source": [ - "shape = (2, 2)\n", - "ones = ops.Ones()\n", - "output = ones(shape, mstype.float32)\n", - "\n", - "zeros = ops.Zeros()\n", - "output = zeros(shape, mstype.float32)\n", - "print(output)" - ], - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "[[1. 1.]\n", - " [1. 1.]]\n", - "[[0. 0.]\n", - " [0. 0.]]\n" - ] - } - ], - "metadata": {} - }, - { - "cell_type": "markdown", - "source": [ - "During `Tensor` initialization, dtype can be specified, for example, `mstype.int32`, `mstype.float32` or `mstype.bool_`.\n", - "\n", - "## Tensor Attributes\n", - "\n", - "Tensor attributes include shape, data type, transposed tensor, item size, number of bytes occupied, dimension, size of elements, and stride per dimension.\n", - "\n", - "- shape: the shape of `Tensor`, a tuple.\n", - "\n", - "- dtype: the dtype of `Tensor`, a data type of MindSpore.\n", - "\n", - "- T: the Transpose of `Tensor`, also a `Tensor`.\n", - "\n", - "- itemsize: the number of bytes occupied by each element in `Tensor` is an integer.\n", - "\n", - "- nbytes: the total number of bytes occupied by `Tensor`, an integer.\n", - "\n", - "- ndim: the rank of a `Tensor`, which is len(tensor.shape), an integer.\n", - "\n", - "- size: the number of all elements in `Tensor`, an integer.\n", - "\n", - "- strides: the number of bytes to traverse in each dimension of `Tensor`." - ], - "metadata": {} - }, - { - "cell_type": "code", - "execution_count": 7, - "source": [ - "x = Tensor(np.array([[1, 2], [3, 4]]), mstype.int32)\n", - "\n", - "print(\"x_shape:\", x.shape)\n", - "print(\"x_dtype:\", x.dtype)\n", - "print(\"x_transposed:\\n\", x.T)\n", - "print(\"x_itemsize:\", x.itemsize)\n", - "print(\"x_nbytes:\", x.nbytes)\n", - "print(\"x_ndim:\", x.ndim)\n", - "print(\"x_size:\", x.size)\n", - "print(\"x_strides:\", x.strides)" - ], - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "x_shape: (2, 2)\n", - "x_dtype: Int32\n", - "x_transposed:\n", - " [[1 3]\n", - " [2 4]]\n", - "x_itemsize: 4\n", - "x_nbytes: 16\n", - "x_ndim: 2\n", - "x_size: 4\n", - "x_strides: (8, 4)\n" - ] - } - ], - "metadata": {} - }, - { - "cell_type": "markdown", - "source": [ - "## Tensor Indexing\n", - "\n", - "Tensor indexing is similar to Numpy indexing, indexing starts from 0, negative indexing means indexing in reverse order, and colons `:` and `...` are used for slicing." - ], - "metadata": {} - }, - { - "cell_type": "code", - "execution_count": 8, - "source": [ - "tensor = Tensor(np.array([[0, 1], [2, 3]]).astype(np.float32))\n", - "\n", - "print(\"First row: {}\".format(tensor[0]))\n", - "print(\"value of top right corner: {}\".format(tensor[1, 1]))\n", - "print(\"Last column: {}\".format(tensor[:, -1]))\n", - "print(\"First column: {}\".format(tensor[..., 0]))" - ], - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "First row: [0. 1.]\n", - "value of top right corner: 3.0\n", - "Last column: [1. 3.]\n", - "First column: [0. 2.]\n" - ] - } - ], - "metadata": {} - }, - { - "cell_type": "markdown", - "source": [ - "## Tensor Operation\n", - "\n", - "There are many operations between tensors, including arithmetic, linear algebra, matrix processing (transposing, indexing, and slicing), and sampling. The following describes several operations. The usage of tensor computation is similar to that of NumPy.\n", - "\n", - ">Common arithmetic operations include: addition (+), subtraction (-), multiplication (\\*), division (/), modulo (%), power (\\*\\*), and exact division (//)." - ], - "metadata": {} - }, - { - "cell_type": "code", - "execution_count": 9, - "source": [ - "x = Tensor(np.array([1, 2, 3]), mstype.int32)\n", - "y = Tensor(np.array([4, 5, 6]), mstype.int32)\n", - "\n", - "output_add = x + y\n", - "output_sub = x - y\n", - "output_mul = x * y\n", - "output_div = y / x\n", - "output_mod = y % x\n", - "output_floordiv = y // x\n", - "\n", - "print(\"add:\", output_add)\n", - "print(\"sub:\", output_sub)\n", - "print(\"mul:\", output_mul)\n", - "print(\"div:\", output_div)\n", - "print(\"mod:\", output_mod)\n", - "print(\"floordiv:\", output_floordiv)" - ], - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "add: [5 7 9]\n", - "sub: [-3 -3 -3]\n", - "mul: [ 4 10 18]\n", - "div: [4 2 2]\n", - "mod: [0 1 0]\n", - "floordiv: [4 2 2]\n" - ] - } - ], - "metadata": {} - }, - { - "cell_type": "markdown", - "source": [ - "`Concat` connects a series of tensors in a given dimension." - ], - "metadata": {} - }, - { - "cell_type": "code", - "execution_count": 10, - "source": [ - "data1 = Tensor(np.array([[0, 1], [2, 3]]).astype(np.float32))\n", - "data2 = Tensor(np.array([[4, 5], [6, 7]]).astype(np.float32))\n", - "op = ops.Concat()\n", - "output = op((data1, data2))\n", - "\n", - "print(output)\n", - "print(\"shape:\\n\", output.shape)" - ], - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "[[0. 1.]\n", - " [2. 3.]\n", - " [4. 5.]\n", - " [6. 7.]]\n", - "shape:\n", - " (4, 2)\n" - ] - } - ], - "metadata": {} - }, - { - "cell_type": "markdown", - "source": [ - "`Stack` combines two tensors from another dimension." - ], - "metadata": {} - }, - { - "cell_type": "code", - "execution_count": 11, - "source": [ - "data1 = Tensor(np.array([[0, 1], [2, 3]]).astype(np.float32))\n", - "data2 = Tensor(np.array([[4, 5], [6, 7]]).astype(np.float32))\n", - "op = ops.Stack()\n", - "output = op([data1, data2])\n", - "\n", - "print(output)\n", - "print(\"shape:\\n\", output.shape)" - ], - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "[[[0. 1.]\n", - " [2. 3.]]\n", - "\n", - " [[4. 5.]\n", - " [6. 7.]]]\n", - "shape:\n", - " (2, 2, 2)\n" - ] - } - ], - "metadata": {} - }, - { - "cell_type": "markdown", - "source": [ - "## Conversion Between Tensor and NumPy\n", - "\n", - "Tensor and NumPy can be converted to each other.\n", - "\n", - "### Tensor to NumPy\n", - "\n", - "Use `asnumpy()` to convert Tensor to NumPy." - ], - "metadata": {} - }, - { - "cell_type": "code", - "execution_count": 12, - "source": [ - "zeros = ops.Zeros()\n", - "\n", - "output = zeros((2, 2), mstype.float32)\n", - "print(\"output: {}\".format(type(output)))\n", - "\n", - "n_output = output.asnumpy()\n", - "print(\"n_output: {}\".format(type(n_output)))" - ], - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "output: \n", - "n_output: \n" - ] - } - ], - "metadata": {} - }, - { - "cell_type": "markdown", - "source": [ - "### NumPy to Tensor\n", - "\n", - "Use `asnumpy()` to convert NumPy to Tensor." - ], - "metadata": {} - }, - { - "cell_type": "code", - "execution_count": 13, - "source": [ - "output = np.array([1, 0, 1, 0])\n", - "print(\"output: {}\".format(type(output)))\n", - "\n", - "t_output = Tensor(output)\n", - "print(\"t_output: {}\".format(type(t_output)))" - ], - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "output: \n", - "t_output: \n" - ] - } - ], - "metadata": {} - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Sparse Tensor\n", - "\n", - "The sparse tensor is a special kind of tensor which most of the elements are zero.\n", - "\n", - "In some scenario (e.g., Recommendation Systems, Molecular Dynamics, Graph Neural Networks), the data is sparse. If we use common dense tensors to represent the data, we may introduce many\n", - "unnecessary calculations, storage and communication costs. In this situation, it is better to use sparse tensor to\n", - "represent the data.\n", - "\n", - "MindSpore now supports the two most commonly used `CSR` and `COO` sparse data formats.\n", - "\n", - "The common structure of the sparse tensor is ``. `indices` means index of\n", - "non-zero elements, `values` means the values of these non-zero elements and shape means the dense shape of\n", - "the sparse tensor. Using this structure, we define data structure `CSRTensor`, `COOTensor` and `RowTensor`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### CSRTensor\n", - "\n", - "`CSR`(Compressed Sparse Row) is efficient in both storage and computation. All the non-zero values are stored in `values`, and their positions are stored in `indptr`(row position) and `indices` (column position).\n", - "\n", - "- `indptr`: 1-D integer tensor, indicating the start and end points of the non-zero element of each row of the sparse data in `values`. Index data type only supports int32 for now.\n", - "\n", - "- `indices`: 1-D integer tensor, indicating the position of the sparse tensor non-zero element in the column and has the same length as `values`. Index data type only supports int32 for now.\n", - "\n", - "- `values`: 1-D tensor, indicating that the value of the non-zero element corresponding to the `CSRTensor` and has the same length as `indices`.\n", - "\n", - "- `shape`: indicating that the shape of a compressed sparse tensor. The data type is `Tuple`, and currently only 2-D `CSRTensor` is supported.\n", - "\n", - ">For more details of the `CSRTensor`, please see [mindspore.CSRTensor](https://www.mindspore.cn/docs/en/r1.7/api_python/mindspore/mindspore.CSRTensor.html).\n", - "\n", - "Here are some examples of how CSRTensor can be used:\n" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Float64\n" - ] - } - ], - "source": [ - "import mindspore as ms\n", - "from mindspore import Tensor, CSRTensor\n", - "\n", - "indptr = Tensor([0, 1, 2])\n", - "indices = Tensor([0, 1])\n", - "values = Tensor([1, 2], dtype=ms.float32)\n", - "shape = (2, 4)\n", - "\n", - "# constructs CSRTensor\n", - "csr_tensor = CSRTensor(indptr, indices, values, shape)\n", - "\n", - "print(csr_tensor.astype(ms.float64).dtype)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### COOTensor\n", - "\n", - "`COOTensor` is used to compress Tensors with irregular distribution of non-zero elements. If the number of non-zero elements\n", - "is `N` and the dense shape of the sparse tensor is `ndims`:\n", - "\n", - "- `indices`: 2-D integer Tensor and each row indicates a non-zero element subscript. Shape: `[N, ndims]`. Index data type only supports int32 for now.\n", - "- `values`: 1-D tensor of any type, indicating the value of non-zero elements. Shape: `[N]`.\n", - "- `shape`: indicating a dense shape of the sparse tensor, currently only 2-D `COOTensor` is supported.\n", - "\n", - ">For more details for `COOTensor`, please see [mindspore.COOTensor](https://www.mindspore.cn/docs/en/r1.7/api_python/mindspore/mindspore.COOTensor.html).\n", - "\n", - "Here are some examples of how COOTensor can be used:" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1. 2.]\n", - "[[0 1]\n", - " [1 2]]\n", - "(3, 4)\n", - "Float64\n" - ] - } - ], - "source": [ - "import mindspore as ms\n", - "import mindspore.nn as nn\n", - "from mindspore import Tensor, COOTensor\n", - "\n", - "indices = Tensor([[0, 1], [1, 2]])\n", - "values = Tensor([1, 2], dtype=ms.float32)\n", - "shape = (3, 4)\n", - "\n", - "# constructs COOTensor\n", - "coo_tensor = COOTensor(indices, values, shape)\n", - "\n", - "print(coo_tensor.values)\n", - "print(coo_tensor.indices)\n", - "print(coo_tensor.shape)\n", - "print(coo_tensor.astype(ms.float64).dtype) # COOTensor cast to another data type" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The codes above produce a `COOTensor` as following:\n", - "\n", - "$$\n", - " \\left[\n", - " \\begin{matrix}\n", - " 0 & 1 & 0 & 0 \\\\\n", - " 0 & 0 & 2 & 0 \\\\\n", - " 0 & 0 & 0 & 0\n", - " \\end{matrix}\n", - " \\right]\n", - "$$" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### RowTensor\n", - "\n", - "`RowTensor` is used to compress tensors that are sparse in the zeroth dimension. If the dimension of `RowTensor` is `[L0, D1, D2, ..., DN ]`. The number of non-zero elements in the zeroth dimension is `D0`, then `L0 >> D0`.\n", - "\n", - "- `indices`: 1-D integer tensor, indicating the position of non-zero elements in the zeroth dimension of the sparse tensor, shape: `[D0]`.\n", - "\n", - "- `values`: indicating the value of the corresponding non-zero element, shape: `[D0, D1, D2, ..., DN]`.\n", - "\n", - "- `dense_shape`: indicating the shape of the compressed sparse tensor.\n", - "\n", - "> `RowTensor` can only be used in the constructor of `Cell`. For the detailed contents, refer to the code example in [mindspore.RowTensor](https://www.mindspore.cn/docs/en/r1.7/api_python/mindspore/mindspore.RowTensor.html)" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "non-zero values: [[1. 2.]]\n", - "non-zero indices: [0]\n", - "shape: (3, 2)\n" - ] - } - ], - "source": [ - "from mindspore import RowTensor\n", - "import mindspore.nn as nn\n", - "\n", - "class Net(nn.Cell):\n", - " def __init__(self, dense_shape):\n", - " super(Net, self).__init__()\n", - " self.dense_shape = dense_shape\n", - "\n", - " def construct(self, indices, values):\n", - " x = RowTensor(indices, values, self.dense_shape)\n", - " return x.values, x.indices, x.dense_shape\n", - "\n", - "indices = Tensor([0])\n", - "values = Tensor([[1, 2]], dtype=mstype.float32)\n", - "out = Net((3, 2))(indices, values)\n", - "\n", - "print(\"non-zero values:\", out[0])\n", - "print(\"non-zero indices:\", out[1])\n", - "print(\"shape:\", out[2])" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python [conda env:mindspore_py39] *", - "language": "python", - "name": "conda-env-mindspore_py39-py" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.0" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} \ No newline at end of file diff --git a/tutorials/source_en/beginner/tensor.md b/tutorials/source_en/beginner/tensor.md new file mode 100644 index 0000000000..4e6efc7fb9 --- /dev/null +++ b/tutorials/source_en/beginner/tensor.md @@ -0,0 +1,439 @@ +# Tensor + + + +Tensor is a multilinear function that can be used to represent linear relationships between vectors, scalars, and other tensors. The basic examples of these linear relations are the inner product, the outer product, the linear map, and the Cartesian product. In the $n$ dimensional space, its coordinates have $n^{r}$ components. Each component is a function of coordinates, and these components are also linearly transformed according to certain rules when the coordinates are transformed. $r$ is called the rank or order of this tensor (not related to the rank or order of the matrix). + +A tensor is a special data structure that is similar to arrays and matrices. [Tensor](https://www.mindspore.cn/docs/en/r1.7/api_python/mindspore/mindspore.Tensor.html) is the basic data structure in MindSpore network operations. This chapter describes the attributes and usage of tensors and sparse tensors. + +## Creating a Tensor + +There are multiple methods for creating tensors. When building a tensor, you can pass the `Tensor`, `float`, `int`, `bool`, `tuple`, `list`, and `NumPy.array` types. + +- **Generating a tensor based on data** + +You can create a tensor based on data. The data type can be set or automatically inferred by the framework. + +```python +from mindspore import Tensor + +x = Tensor(0.1) +``` + +- **Generating a tensor from the NumPy array** + +You can create a tensor from the NumPy array. + +```python +import numpy as np + +arr = np.array([1, 0, 1, 0]) +tensor_arr = Tensor(arr) + +print(type(arr)) +print(type(tensor_arr)) +``` + +```text + + +``` + +If the initial value is `NumPy.array`, the generated `Tensor` data type corresponds to `NumPy.array`. + +- **Generating a tensor by using init** + +When `init` is used to initialize a tensor, the `init`, `shape`, and `dtype` parameters can be transferred. + +- `init`: supports the subclass of [initializer](https://www.mindspore.cn/docs/en/r1.7/api_python/mindspore.common.initializer.html). + +- `shape`: supports `list`, `tuple`, and `int`. + +- `dtype`: supports [mindspore.dtype](https://www.mindspore.cn/docs/en/r1.7/api_python/mindspore.html#mindspore.dtype). + +```python +from mindspore import Tensor +from mindspore import set_seed +from mindspore import dtype as mstype +from mindspore.common.initializer import One, Normal + +set_seed(1) + +tensor1 = Tensor(shape=(2, 2), dtype=mstype.float32, init=One()) +tensor2 = Tensor(shape=(2, 2), dtype=mstype.float32, init=Normal()) + +print("tensor1:\n", tensor1) +print("tensor2:\n", tensor2) +``` + +```text +tensor1: +[[1. 1.] +[1. 1.]] +tensor2: +[[-0.00128023 -0.01392901] +[ 0.0130886 -0.00107818]] +``` + +The `init` is used for delayed initialization in parallel mode. Usually, it is not recommended to use `init` interface to initialize parameters. + +- **Inheriting attributes of another tensor to form a new tensor** + +```python +from mindspore import ops + +oneslike = ops.OnesLike() +x = Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32)) +output = oneslike(x) + +print(output) +print("input shape:", x.shape) +print("output shape:", output.shape) +``` + +```text +[[1 1] +[1 1]] +input shape: (2, 2) +output shape: (2, 2) +``` + +- **Outputting a constant tensor of a specified size** + +`shape` is the size tuple of a tensor, which determines the dimension of the output tensor. + +```python +shape = (2, 2) +ones = ops.Ones() +output = ones(shape, mstype.float32) + +zeros = ops.Zeros() +output = zeros(shape, mstype.float32) +print(output) +``` + +```text +[[0. 0.] +[0. 0.]] +``` + +During `Tensor` initialization, dtype can be specified to, for example, `mstype.int32`, `mstype.float32` or `mstype.bool_`. + +## Tensor Attributes + +Tensor attributes include shape, data type, transposed tensor, item size, number of bytes occupied, dimension, size of elements, and stride per dimension. + +- shape: a tuple. + +- dtype: a data type of MindSpore. + +- T: a transpose of a `Tensor`. + +- itemsize: the number of bytes occupied by each element in `Tensor`, which is an integer. + +- nbytes: the total number of bytes occupied by `Tensor`, which is an integer. + +- ndim: rank of `Tensor`, that is, len(tensor.shape), which is an integer. + +- size: the number of all elements in `Tensor`, which is an integer. + +- strides: the number of bytes to traverse in each dimension of `Tensor`, which is a tuple. + +```python +x = Tensor(np.array([[1, 2], [3, 4]]), mstype.int32) + +print("x_shape:", x.shape) +print("x_dtype:", x.dtype) +print("x_transposed:\n", x.T) +print("x_itemsize:", x.itemsize) +print("x_nbytes:", x.nbytes) +print("x_ndim:", x.ndim) +print("x_size:", x.size) +print("x_strides:", x.strides) +``` + +```text +x_shape: (2, 2) +x_dtype: Int32 +x_transposed: +[[1 3] +[2 4]] +x_itemsize: 4 +x_nbytes: 16 +x_ndim: 2 +x_size: 4 +x_strides: (8, 4) +``` + +## Tensor Indexing + +Tensor indexing is similar to NumPy indexing Indexing starts from 0, negative indexing means indexing in reverse order, and colons `:` and `...` are used for slicing. + +```python +tensor = Tensor(np.array([[0, 1], [2, 3]]).astype(np.float32)) + +print("First row: {}".format(tensor[0])) +print("value of top right corner: {}".format(tensor[1, 1])) +print("Last column: {}".format(tensor[:, -1])) +print("First column: {}".format(tensor[..., 0])) +``` + +```text +First row: [0. 1.] +value of top right corner: 3.0 +Last column: [1. 3.] +First column: [0. 2.] +``` + +## Tensor Operation + +There are many operations between tensors, including arithmetic, linear algebra, matrix processing (transposing, indexing, and slicing), and sampling. The usage of tensor computation is similar to that of NumPy. The following describes several operations. + +> Common arithmetic operations include: addition (+), subtraction (-), multiplication (\*), division (/), modulo (%), and exact division (//). + +```python +x = Tensor(np.array([1, 2, 3]), mstype.float32) +y = Tensor(np.array([4, 5, 6]), mstype.float32) + +output_add = x + y +output_sub = x - y +output_mul = x * y +output_div = y / x +output_mod = y % x +output_floordiv = y // x + +print("add:", output_add) +print("sub:", output_sub) +print("mul:", output_mul) +print("div:", output_div) +print("mod:", output_mod) +print("floordiv:", output_floordiv) +``` + +```text +add: [5. 7. 9.] +sub: [-3. -3. -3.] +mul: [ 4. 10. 18.] +div: [4. 2.5 2. ] +mod: [0. 1. 0.] +floordiv: [4. 2. 2.] +``` + +`Concat` connects a series of tensors in a given dimension. + +```python +data1 = Tensor(np.array([[0, 1], [2, 3]]).astype(np.float32)) +data2 = Tensor(np.array([[4, 5], [6, 7]]).astype(np.float32)) +op = ops.Concat() +output = op((data1, data2)) + +print(output) +print("shape:\n", output.shape) +``` + +```text +[[0. 1.] +[2. 3.] +[4. 5.] +[6. 7.]] +shape: +(4, 2) +``` + +`Stack` combines two tensors from another dimension. + +```python +data1 = Tensor(np.array([[0, 1], [2, 3]]).astype(np.float32)) +data2 = Tensor(np.array([[4, 5], [6, 7]]).astype(np.float32)) +op = ops.Stack() +output = op([data1, data2]) + +print(output) +print("shape:\n", output.shape) +``` + +```text +[[[0. 1.] +[2. 3.]] +[[4. 5.] +[6. 7.]]] +shape: +(2, 2, 2) +``` + +## Conversion Between Tensor and NumPy + +Tensor and NumPy can be converted to each other. + +### Tensor to NumPy + +Use `asnumpy()` to convert Tensor to NumPy. + +```python +zeros = ops.Zeros() + +output = zeros((2, 2), mstype.float32) +print("output: {}".format(type(output))) + +n_output = output.asnumpy() +print("n_output: {}".format(type(n_output))) +``` + +```text +output: +n_output: +``` + +### NumPy to Tensor + +Use `asnumpy()` to convert NumPy to Tensor. + +```python +output = np.array([1, 0, 1, 0]) +print("output: {}".format(type(output))) + +t_output = Tensor(output) +print("t_output: {}".format(type(t_output))) +``` + +```text +output: +t_output: +``` + +## Sparse Tensor + +A sparse tensor is a special tensor in which the value of the most significant element is zero. + +In some scenario s(such as recommendation systems, molecular dynamics, graph neural networks), the data is sparse. If you use common dense tensors to represent the data, you may introduce many unnecessary calculations, storage, and communication costs. In this case, it is better to use sparse tensor to represent the data. + +MindSpore now supports the two most commonly used `CSR` and `COO` sparse data formats. + +The common structure of the sparse tensor is ``. `indices` means the indexes of non-zero elements, `values` means the values of non-zero elements, and `shape` means the dense shape of the sparse tensor. In this structure, we define data structure `CSRTensor`, `COOTensor`, and `RowTensor`. + +### CSRTensor + +The compressed sparse row (`CSR`) is efficient in both storage and computation. All the non-zero values are stored in `values`, and their positions are stored in `indptr`(row) and `indices` (column). + +- `indptr`: 1-D integer tensor, indicating the start and end points of the non-zero elements in each row of the sparse data in `values`. The index data type can only be int32. + +- `indices`: 1-D integer tensor, indicating the position of the sparse tensor non-zero elements in the column and has the same length as `values`. The index data type can only be int32. + +- `values`: 1-D tensor, indicating that the value of the non-zero element corresponding to the `CSRTensor` and has the same length as `indices`. + +- `shape`: indicates the shape of a compressed sparse tensor. The data type is `Tuple`. Currently, only 2-D `CSRTensor` is supported. + +> For details about `CSRTensor`, see [mindspore.CSRTensor](https://www.mindspore.cn/docs/en/r1.7/api_python/mindspore/mindspore.CSRTensor.html). + +The following are some examples of using the CSRTensor: + +```python +import mindspore as ms +from mindspore import Tensor, CSRTensor + +indptr = Tensor([0, 1, 2]) +indices = Tensor([0, 1]) +values = Tensor([1, 2], dtype=ms.float32) +shape = (2, 4) + +# CSRTensor construction +csr_tensor = CSRTensor(indptr, indices, values, shape) + +print(csr_tensor.astype(ms.float64).dtype) +``` + +```text +Float64 +``` + +### COOTensor + +`COOTensor` is used to compress tensors whose non-zero elements are irregularly distributed. If the number of non-zero elements is `N` and the dimension of the compressed tensor is `ndims`, then: + +- `indices`: 2-D integer tensor. Each row indicates a non-zero element subscript. Shape: `[N, ndims]`. The index data type can only be int32. + +- `values`: 1-D tensor of any type, indicating the value of the non-zero element. Shape: `[N]`. + +- `shape`: indicates the shape of a compressed sparse tensor. Currently, only 2-D `COOTensor` is supported. + +> For details about `COOTensor`, see [mindspore.COOTensor](https://www.mindspore.cn/docs/en/r1.7/api_python/mindspore/mindspore.COOTensor.html). + +The following are some examples of using COOTensor: + +```python +import mindspore as ms +import mindspore.nn as nn +from mindspore import Tensor, COOTensor + +indices = Tensor([[0, 1], [1, 2]], dtype=ms.int32) +values = Tensor([1, 2], dtype=ms.float32) +shape = (3, 4) + +# COOTensor construction +coo_tensor = COOTensor(indices, values, shape) + +print(coo_tensor.values) +print(coo_tensor.indices) +print(coo_tensor.shape) +print(coo_tensor.astype(ms.float64).dtype) # COOTensor cast to another data type +``` + +```text +[1. 2.] +[[0 1] +[1 2]] +(3, 4) +Float64 +``` + +The preceding code generates `COOTensor` as follows: + +$$ + \left[ + \begin{matrix} + 0 & 1 & 0 & 0 \\ + 0 & 0 & 2 & 0 \\ + 0 & 0 & 0 & 0 + \end{matrix} + \right] +$$ + +### RowTensor + +`RowTensor` is used to compress tensors that are sparse in the dimension 0. If the dimension of `RowTensor` is `[L0, D1, D2, ..., DN ]` and the number of non-zero elements in the dimension 0 is `D0`, then `L0 >> D0`. + +- `indices`: 1-D integer tensor, indicating the position of non-zero elements in the dimension 0 of the sparse tensor. The shape is `[D0]`. + +- `values`: indicating the value of the corresponding non-zero element. The shape is `[D0, D1, D2, ..., DN]`. + +- `dense_shape`: indicates the shape of a compressed sparse tensor. + +> `RowTensor` can only be used in the constructor of `Cell`. For details, see the code example in [mindspore.RowTensor](https://www.mindspore.cn/docs/en/r1.7/api_python/mindspore/mindspore.RowTensor.html) A code example is as follows: + +```python +from mindspore import RowTensor +import mindspore.nn as nn + +class Net(nn.Cell): + def __init__(self, dense_shape): + super(Net, self).__init__() + self.dense_shape = dense_shape + + def construct(self, indices, values): + x = RowTensor(indices, values, self.dense_shape) + return x.values, x.indices, x.dense_shape + +indices = Tensor([0]) +values = Tensor([[1, 2]], dtype=mstype.float32) +out = Net((3, 2))(indices, values) + +print("non-zero values:", out[0]) +print("non-zero indices:", out[1]) +print("shape:", out[2]) +``` + +```text +non-zero values: [[1. 2.]] +non-zero indices: [0] +shape: (3, 2) +``` \ No newline at end of file diff --git a/tutorials/source_en/beginner/train.md b/tutorials/source_en/beginner/train.md index fa3ae1c76b..e6de7d50ff 100644 --- a/tutorials/source_en/beginner/train.md +++ b/tutorials/source_en/beginner/train.md @@ -1,4 +1,4 @@ -# Training the Model +# Model Training @@ -6,15 +6,17 @@ After learning how to create a model and build a dataset in the preceding tutori ## Hyperparameters -Hyperparameters can be adjusted to control the model training and optimization process. Different hyperparameter values may affect the model training and convergence speed. At present, deep learning models are mostly optimized by a batch stochastic gradient descent algorithm, and the principle of the stochastic gradient descent algorithm is as follows: -$$ -w_{t+1}=w_{t}-\eta \frac{1}{n} \sum_{x \in \mathcal{B}} \nabla l\left(x, w_{t}\right) -$$ -where $n$ is the batch size, and $η$ is a learning rate. In addition, $w_{t}$ is the weight parameter in the training batch t, and $\nabla l$ is the derivative of the loss function. It can be known that in addition to the gradient itself, these two factors directly determine the weight update of the model, and from the optimization itself, they are the most important parameters that affect the performance convergence of the model. Generally, the following hyperparameters are defined for training: +Hyperparameters can be adjusted to control the model training and optimization process. Different hyperparameter values may affect the model training and convergence speed. Currently, deep learning models are optimized using the batch stochastic gradient descent algorithm. The principle of the stochastic gradient descent algorithm is as follows: -- Epoch: specifies number of times that the dataset is traversed during training. -- Batch size: The dataset is trained for batch reading, setting the size of each batch of data. The batch size is too small, takes a lot of time, and the gradient oscillation is serious, which is not conducive to convergence. The batch size is too large, and the gradient direction of different batches does not change at all, which is easy to fall into local minimum values. In this way, an appropriate batch size needs to be chosen, to effectively improve the accuracy of the model and global convergence. -- Learning rate: If the learning rate is low, the convergence speed slows down. If the learning rate is high, unpredictable results such as no training convergence may occur. Gradient descent is a parameter optimization algorithm that is widely used to minimize model errors. Gradient descent estimates the parameters of the model by iterating and minimizing the loss function at each step. The learning rate is that during the iteration process, the learning progress of the model will be controlled. +$$w_{t+1}=w_{t}-\eta \frac{1}{n} \sum_{x \in \mathcal{B}} \nabla l\left(x, w_{t}\right)$$ + +In the formula, $n$ is the batch size, and $η$ is a learning rate. In addition, $w_{t}$ is the weight parameter in the training batch t, and $\nabla l$ is the derivative of the loss function. In addition to the gradient itself, the two factors directly determine the weight update of the model. From the perspective of the optimization itself, the two factors are the most important parameters that affect the convergence of the model performance. Generally, the following hyperparameters are defined for training: + +Epoch: specifies number of times that the dataset is traversed during training. + +Batch size: specifies the size of each batch of data to be read. If the batch size is too small, it takes a long time and the gradient oscillation is serious, which is unfavorable to convergence. If the batch size is too large, the gradient directions of different batches do not change, and the local minimum value is easy to fall into. Therefore, you need to select a proper batch size to effectively improve the model precision and global convergence. + +Learning rate: If the learning rate is low, the convergence speed slows down. If the learning rate is high, unpredictable results such as no training convergence may occur. Gradient descent is a parameter optimization algorithm that is widely used to minimize model errors. The gradient descent estimates the parameters of the model by iterating and minimizing the loss function at each step. The learning rate is used to control the learning progress of a model during iteration. ![learning-rate](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_zh_cn/beginner/images/learning_rate.png) @@ -28,12 +30,12 @@ learning_rate = 1e-2 ## Loss Functions The **loss function** is used to evaluate the difference between **predicted value** and **target value** of a model. Here, the absolute error loss function `L1Loss` is used: -$$ -\text { L1 Loss Function }=\sum_{i=1}^{n}\left|y_{true}-y_{predicted}\right| -$$ - `mindspore.nn.loss` provides many common loss functions, such as `SoftmaxCrossEntropyWithLogits`, `MSELoss`, and `SmoothL1Loss`. -Given the predicted value and the target value, we calculate the error (loss value) between the predicted value and the target value by means of a loss function, which is used as follows: +$$\text { L1 Loss Function }=\sum_{i=1}^{n}\left|y_{true}-y_{predicted}\right|$$ + +`mindspore.nn.loss` provides many common loss functions, such as `SoftmaxCrossEntropyWithLogits`, `MSELoss`, and `SmoothL1Loss`. + +Given the predicted value and the target value, we calculate the error (loss value) between the predicted value and the target value by a loss function. The method is as follows: ```python import numpy as np @@ -43,11 +45,12 @@ from mindspore import Tensor loss = nn.L1Loss() output_data = Tensor(np.array([[1, 2, 3], [2, 3, 4]]).astype(np.float32)) target_data = Tensor(np.array([[0, 2, 5], [3, 1, 1]]).astype(np.float32)) + print(loss(output_data, target_data)) ``` ```text - 1.5 +1.5 ``` ## Optimizer Functions @@ -56,11 +59,9 @@ An optimizer is used to compute and update the gradient. The selection of the mo All optimization logic of MindSpore is encapsulated in the `Optimizer` object. Here, the Momentum optimizer is used. `mindspore.nn` provides many common optimizers, such as `Adam`, `SGD` and `RMSProp`. -You need to build an `Optimizer` object. This object can retain the current parameter status and update parameters based on the computed gradient. To build an `Optimizer`, we need to provide an iterator that contains parameters (must be variable objects) to be optimized. For example, set parameters to `net.trainable_params()` for all `parameter` that can be trained on the network. - -Then, you can set the `Optimizer` parameter options, such as the learning rate and weight attenuation. +You need to build an `Optimizer` object. This object can retain the current parameter status and update parameters based on the computed gradient. To build an `Optimizer`, you need to provide an iterator that contains parameters (must be variable objects) to be optimized. For example, set parameters to `net.trainable_params()` for all `parameter` that can be trained on the network. -A code example is as follows: +Then, you can set the `Optimizer` parameter options, such as the learning rate and weight decay. A code example is as follows: ```python from mindspore import nn @@ -72,10 +73,10 @@ optim = nn.Momentum(net.trainable_params(), learning_rate, momentum) ## Model Training -A model training process is generally divided into four steps. +Model training consists of four steps: -1. Define a neural network. -2. Build a dataset. +1. Build a dataset. +2. Define a neural network. 3. Define hyperparameters, a loss function, and an optimizer. 4. Enter the epoch and dataset for training. @@ -89,20 +90,20 @@ from mindvision.classification.dataset import Mnist from mindvision.classification.models import lenet from mindvision.engine.callback import LossMonitor -# 1. Build a dataset +# 1. Build a dataset. download_train = Mnist(path="./mnist", split="train", batch_size=batch_size, repeat_num=1, shuffle=True, resize=32, download=True) dataset_train = download_train.run() -# 2. Define a neural network +# 2. Define a neural network. network = lenet(num_classes=10, pretrained=False) -# 3.1 Define a loss function +# 3.1 Define a loss function. net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean') -# 3.2 Defines an optimizer function +# 3.2 Define an optimizer function. net_opt = nn.Momentum(network.trainable_params(), learning_rate=learning_rate, momentum=momentum) -# 3.3 Initialize model parameters +# 3.3 Initialize model parameters. model = Model(network, loss_fn=net_loss, optimizer=net_opt, metrics={'acc'}) -# 4. Perform training on the neural network +# 4. Train the neural network. model.train(epochs, dataset_train, callbacks=[LossMonitor(learning_rate, 1875)]) ``` @@ -129,4 +130,4 @@ Epoch:[ 9/ 10], step:[ 1875/ 1875], loss:[0.008/0.017], time:2.135 ms, lr:0.010 Epoch time: 4601.861 ms, per step time: 2.454 ms, avg loss: 0.017 ``` -The loss value is printed during training. The loss value fluctuates, but in general the loss value decreases gradually and the accuracy gradually increases. The loss values run by different persons have a certain randomness and are not necessarily exactly the same. \ No newline at end of file +During the training, the loss value is printed and fluctuates. However, the loss value gradually decreases and the precision gradually increases. Loss values displayed each time may be different because of their randomness. diff --git a/tutorials/source_zh_cn/beginner/infer.ipynb b/tutorials/source_zh_cn/beginner/infer.ipynb index effb5ad2c3..c0636246fe 100644 --- a/tutorials/source_zh_cn/beginner/infer.ipynb +++ b/tutorials/source_zh_cn/beginner/infer.ipynb @@ -6,7 +6,7 @@ "source": [ "# 推理与部署\n", "\n", - "[![下载Notebook](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/resource/_static/logo_notebook.png)](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/r1.7/tutorials/zh_cn/beginner/mindspore_infer.ipynb) [![下载样例代码](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/resource/_static/logo_download_code.png)](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/r1.7/tutorials/zh_cn/beginner/mindspore_infer.py) [![查看源文件](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/resource/_static/logo_source.png)](https://gitee.com/mindspore/docs/blob/r1.7/tutorials/source_zh_cn/beginner/infer.ipynb)\n", + "[![下载Notebook](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_notebook.png)](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/tutorials/zh_cn/beginner/mindspore_infer.ipynb) [![下载样例代码](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_download_code.png)](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/master/tutorials/zh_cn/beginner/mindspore_infer.py) [![查看源文件](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_source.png)](https://gitee.com/mindspore/docs/blob/master/tutorials/source_zh_cn/beginner/infer.ipynb)\n", "\n", "本章使用MindSpore Vision中的`mobilenet_v2`网络微调的方法来开发一个AI应用(狗和牛角包分类),并将训练好的网络模型部署到Android手机上,执行推理和部署功能。\n", "\n", @@ -18,7 +18,7 @@ "\n", "具体数据集如下所示:\n", "\n", - "![datset-dog](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_zh_cn/beginner/images/datset_dog.png)\n", + "![datset-dog](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/tutorials/source_zh_cn/beginner/images/datset_dog.png)\n", "\n", "使用MindSpore Vision中的`DownLoad`接口下载并解压数据集到指定路径下,示例代码如下所示:" ] @@ -144,7 +144,7 @@ "\n", "由于MobileNet网络中Relu激活函数处理低维特征信息时会存在大量的丢失,所以MobileNetV2网络提出使用倒残差结构(Inverted residual block)和Linear Bottlenecks来设计网络,以提高模型的准确率,且优化后的模型更小。\n", "\n", - "![mobilenet](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_zh_cn/beginner/images/mobilenet.png)\n", + "![mobilenet](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/tutorials/source_zh_cn/beginner/images/mobilenet.png)\n", "\n", "图中Inverted residual block结构是先使用1x1卷积进行升维,然后使用3x3的DepthWise卷积,最后使用1x1的卷积进行降维。与Residual block结构相反,Residual block是先使用1x1的卷积进行降维,然后使用3x3的卷积,最后使用1x1的卷积进行升维。\n", "\n", @@ -363,7 +363,7 @@ " # 图像通道由(h, w, c)转换为(c, h, w)\n", " image = np.transpose(image, (2, 0, 1))\n", "\n", - " # 扩展数据维数为(1,c, h, w)\n", + " # 扩展数据维数为(1, c, h, w)\n", " image = np.expand_dims(image, axis=0)\n", "\n", " # 定义并加载网络\n", @@ -433,11 +433,11 @@ "\n", "### 转换文件格式\n", "\n", - "使用端侧应用的转换工具[MindSpore Lite Converter](https://www.mindspore.cn/lite/docs/zh-CN/r1.7/use/converter_tool.html),将训练过程当中生成的mobilenet_v2_1.0_224.mindir文件,转换为MindSpore Lite端侧推理框架可识别的文件格式mobilenet_v2_1.0_224.ms文件。\n", + "使用端侧应用的转换工具[MindSpore Lite Converter](https://www.mindspore.cn/lite/docs/zh-CN/master/use/converter_tool.html),将训练过程当中生成的mobilenet_v2_1.0_224.mindir文件,转换为MindSpore Lite端侧推理框架可识别的文件格式mobilenet_v2_1.0_224.ms文件。\n", "\n", "具体的模型文件格式转换方式如下所示:\n", "\n", - "1. Linux下使用MindSpore Lite Converter转换文件格式,[Linux-x86_64工具下载链接](https://www.mindspore.cn/lite/docs/zh-CN/r1.7/use/downloads.html)。\n", + "1. Linux下使用MindSpore Lite Converter转换文件格式,[Linux-x86_64工具下载链接](https://www.mindspore.cn/lite/docs/zh-CN/master/use/downloads.html)。\n", "\n", "```shell\n", "# 下载解压后设置软件包的路径,{converter_path}为解压后工具包的路径,PACKAGE_ROOT_PATH为设置的环境变量\n", @@ -450,7 +450,7 @@ "./converter_lite --fmk=MINDIR --modelFile=mobilenet_v2_1.0_224.mindir --outputFile=mobilenet_v2_1.0_224\n", "```\n", "\n", - "2. Windows下使用MindSpore Lite Converter转换文件格式,[Windows-x64工具下载链接](https://www.mindspore.cn/lite/docs/zh-CN/r1.7/use/downloads.html)。\n", + "2. Windows下使用MindSpore Lite Converter转换文件格式,[Windows-x64工具下载链接](https://www.mindspore.cn/lite/docs/zh-CN/master/use/downloads.html)。\n", "\n", "```shell\n", "# 下载解压后设置软件包的路径,{converter_path}为解压后工具包的路径,PACKAGE_ROOT_PATH为设置的环境变量\n", @@ -465,7 +465,7 @@ "\n", "转换成功后打印`CONVERT RESULT SUCCESS:0`,且在当前目录下生成`mobilenet_v2_1.0_224.ms`文件。\n", "\n", - "> 其他环境下载MindSpore Lite Converter参见[下载MindSpore Lite](https://www.mindspore.cn/lite/docs/zh-CN/r1.7/use/downloads.html)。" + "> 其他环境下载MindSpore Lite Converter参见[下载MindSpore Lite](https://www.mindspore.cn/lite/docs/zh-CN/master/use/downloads.html)。" ] }, { @@ -480,11 +480,11 @@ "\n", "打开APP后,在首页点击`分类`模块后,即可点击中间按钮进行拍照获取图片,或者点击上侧栏的图像按钮选择进行图片相册用于图像分类功能。\n", "\n", - "![main](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_zh_cn/beginner/images/app1.png)\n", + "![main](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/tutorials/source_zh_cn/beginner/images/app1.png)\n", "\n", "在默认情况下,MindSpore Vision`分类`模块内置了一个通用的AI网络模型对图像进行识别分类。\n", "\n", - "![result](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_zh_cn/beginner/images/app2.png)" + "![result](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/tutorials/source_zh_cn/beginner/images/app2.png)" ] }, { @@ -519,11 +519,11 @@ "\n", "为实现手机端狗与牛角包的识别功能,需将标签文件`custom.json`文件和模型文件`mobilenet_v2_1.0_224.ms`一起放置到手机上指定目录下。这里以`Android/data/Download/` 文件夹为例,首先把标签文件和模型文件同时放在上述手机地址,如图所示,点击自定义按钮,然后会弹出系统文件功能,点击左上角的打开文件,然后找到Json标签文件和模型文件存放的目录地址,并选择对应的Json文件。\n", "\n", - "![step](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_zh_cn/beginner/images/app3.png)\n", + "![step](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/tutorials/source_zh_cn/beginner/images/app3.png)\n", "\n", "标签与模型文件部署到手机后,即可点击中间按钮进行拍照获取图片,或者点击上侧栏的图像按钮选择图片相册用于图像,就可以进行狗与牛角包的分类识别。\n", "\n", - "![result1](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.7/tutorials/source_zh_cn/beginner/images/app4.png)\n", + "![result1](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/tutorials/source_zh_cn/beginner/images/app4.png)\n", "\n" ] }, @@ -531,7 +531,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "> 本章仅包含手机侧简单的部署过程,想要了解推理更多内容请参考[MindSpore Lite](https://www.mindspore.cn/lite/docs/zh-CN/r1.7/index.html)。\n" + "> 本章仅包含手机侧简单的部署过程,想要了解推理更多内容请参考[MindSpore Lite](https://www.mindspore.cn/lite/docs/zh-CN/master/index.html)。\n" ] } ], @@ -556,4 +556,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} +} \ No newline at end of file -- Gitee