diff --git a/tutorials/source_en/beginner/autograd.md b/tutorials/source_en/beginner/autograd.md
index 4fc41ed0172053675ba3dba3ac76c1058abba86c..4051e76154ad0af43311b5001ad66f419c10191a 100644
--- a/tutorials/source_en/beginner/autograd.md
+++ b/tutorials/source_en/beginner/autograd.md
@@ -14,7 +14,7 @@ This chapter uses `ops.GradOperation` in MindSpore to find first-order derivativ
## First-order Derivative of the Input
-The formula needs to be defined before the input can be derived: $f(x)=wx+b \tag {1}$
+The formula needs to be defined before the input can be derived:$f(x)=wx+b \tag {1}$
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.
@@ -34,7 +34,7 @@ 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`. Its corresponding MindSpore internally produces the following formula (2):$f^{'}(x)=w\tag {2}$
```python
from mindspore import dtype as mstype
diff --git a/tutorials/source_en/beginner/infer.md b/tutorials/source_en/beginner/infer.md
new file mode 100644
index 0000000000000000000000000000000000000000..e0b9e7e962082ac286ffaf0a0b1cf18e42a0f9de
--- /dev/null
+++ b/tutorials/source_en/beginner/infer.md
@@ -0,0 +1,408 @@
+# Inference and Deployment
+
+
+
+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.
+
+## Data Preparation and Loading
+
+### Downloading the 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.
+
+The specific dataset is as follows:
+
+
+
+Use the `DownLoad` interface in MindSpore Vision to download and extract the dataset to the specified path, and the sample code is as follows:
+
+```python
+from mindvision.dataset import DownLoad
+
+dataset_url = "https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/beginner/DogCroissants.zip"
+path = "./datasets"
+
+dl = DownLoad()
+# Download and extract the dataset
+dl.download_and_extract_archive(dataset_url, path)
+```
+
+The directory structure of the dataset is as follows:
+
+```text
+datasets
+└── DogCroissants
+ ├── infer
+ │ ├── croissants.jpg
+ │ └── dog.jpg
+ ├── train
+ │ ├── croissants
+ │ └── dog
+ └── val
+ ├── croissants
+ └── dog
+```
+
+### 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.
+
+```python
+import mindspore.dataset as ds
+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
+ mean = [0.485 * 255, 0.456 * 255, 0.406 * 255]
+ std = [0.229 * 255, 0.224 * 255, 0.225 * 255]
+ if train:
+ trans = [
+ transforms.RandomCropDecodeResize(image_size, scale=(0.08, 1.0), ratio=(0.75, 1.333)),
+ transforms.RandomHorizontalFlip(prob=0.5),
+ transforms.Normalize(mean=mean, std=std),
+ transforms.HWC2CHW()
+ ]
+ else:
+ trans = [
+ transforms.Decode(),
+ transforms.Resize(256),
+ transforms.CenterCrop(image_size),
+ transforms.Normalize(mean=mean, std=std),
+ transforms.HWC2CHW()
+ ]
+
+ 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
+ dataset = dataset.batch(batch_size, drop_remainder=True)
+ return dataset
+```
+
+Load the training dataset and validation dataset for subsequent model training and validation.
+
+```python
+# Load the training dataset
+train_path = "./datasets/DogCroissants/train"
+dataset_train = create_dataset(train_path, train=True)
+
+# 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.
+
+> Model training currently only supports running in 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.
+
+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.
+
+
+
+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.
+
+> For detailed contents, refer to [MobileNet V2 thesis](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:
+
+```python
+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
+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.
+
+```python
+import mindspore.nn as nn
+from mindspore.train import Model
+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)
+network = mobilenet_v2(num_classes=2, resize=224)
+
+# Save the model parameter in 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
+filter_list = [x.name for x in network.head.classifier.get_parameters()]
+
+# 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:
+ if name in key:
+ print("Delete parameter from checkpoint: ", key)
+ del origin_dict[key]
+ break
+
+filter_ckpt_parameter(param_dict, filter_list)
+
+# Load the pre-trained model parameters as the network initialization weight
+load_param_into_net(network, param_dict)
+
+# Define the optimizer
+network_opt = nn.Momentum(params=network.trainable_params(), learning_rate=0.01, momentum=0.9)
+
+# Define the loss function
+network_loss = CrossEntropySmooth(sparse=True, reduction="mean", smooth_factor=0.1, classes_num=2)
+
+# Define evaluation metrics
+metrics = {"Accuracy": nn.Accuracy()}
+
+# Initialize the model
+model = Model(network, loss_fn=network_loss, optimizer=network_opt, metrics=metrics)
+```
+
+```text
+[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.
+
+### Model Training and Evaluation
+
+Train and evaluate the network, and use the `mindvision.engine.callback.ValAccMonitor` interface in MindSpore Vision to print the loss value and the evaluation accuracy of the training. After the training is completed, save the CKPT file with the highest evaluation accuracy, `best.ckpt`, in the current directory.
+
+```python
+from mindvision.engine.callback import ValAccMonitor
+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
+model.train(num_epochs,
+ dataset_train,
+ callbacks=[ValAccMonitor(model, dataset_val, num_epochs), TimeMonitor()])
+```
+
+```text
+--------------------
+Epoch: [ 1 / 10], Train Loss: [0.388], Accuracy: 0.975
+epoch time: 7390.423 ms, per step time: 254.842 ms
+--------------------
+Epoch: [ 2 / 10], Train Loss: [0.378], Accuracy: 0.975
+epoch time: 1876.590 ms, per step time: 64.710 ms
+--------------------
+Epoch: [ 3 / 10], Train Loss: [0.372], Accuracy: 1.000
+epoch time: 2103.431 ms, per step time: 72.532 ms
+--------------------
+Epoch: [ 4 / 10], Train Loss: [0.346], Accuracy: 1.000
+epoch time: 2246.303 ms, per step time: 77.459 ms
+--------------------
+Epoch: [ 5 / 10], Train Loss: [0.376], Accuracy: 1.000
+epoch time: 2164.527 ms, per step time: 74.639 ms
+--------------------
+Epoch: [ 6 / 10], Train Loss: [0.353], Accuracy: 1.000
+epoch time: 2191.490 ms, per step time: 75.569 ms
+--------------------
+Epoch: [ 7 / 10], Train Loss: [0.414], Accuracy: 1.000
+epoch time: 2183.388 ms, per step time: 75.289 ms
+--------------------
+Epoch: [ 8 / 10], Train Loss: [0.362], Accuracy: 1.000
+epoch time: 2219.950 ms, per step time: 76.550 ms
+--------------------
+Epoch: [ 9 / 10], Train Loss: [0.354], Accuracy: 1.000
+epoch time: 2174.555 ms, per step time: 74.985 ms
+--------------------
+Epoch: [ 10 / 10], Train Loss: [0.364], Accuracy: 1.000
+epoch time: 2190.957 ms, per step time: 75.550 ms
+================================================================================
+End of validation the best Accuracy is: 1.000, save the best ckpt file in ./best.ckpt
+```
+
+### 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.
+
+```python
+import matplotlib.pyplot as plt
+import numpy as np
+from PIL import Image
+
+from mindspore import Tensor
+
+def visualize_model(path):
+ image = Image.open(path).convert("RGB")
+ image = image.resize((224, 224))
+ plt.imshow(image)
+
+ # Normalization processing
+ mean = np.array([0.485 * 255, 0.456 * 255, 0.406 * 255])
+ std = np.array([0.229 * 255, 0.224 * 255, 0.225 * 255])
+ image = np.array(image)
+ image = (image - mean) / std
+ image = image.astype(np.float32)
+
+ # Image channel switches (h, w, c) to (c, h, w)
+ image = np.transpose(image, (2, 0, 1))
+
+ # Extend the data dimension to (1,c, h, w)
+ image = np.expand_dims(image, axis=0)
+
+ # 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
+ pre = model.predict(Tensor(image))
+ result = np.argmax(pre)
+
+ class_name = {0: "Croissants", 1: "Dog"}
+ plt.title(f"Predict: {class_name[result]}")
+ return result
+
+image1 = "./datasets/DogCroissants/infer/croissants.jpg"
+plt.figure(figsize=(15, 7))
+plt.subplot(1, 2, 1)
+visualize_model(image1)
+
+image2 = "./datasets/DogCroissants/infer/dog.jpg"
+plt.subplot(1, 2, 2)
+visualize_model(image2)
+
+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.
+
+```python
+from mindspore import export, Tensor
+
+# 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
+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;
+
+- Application deployment: Deploy the app APK on the phone side, that is, download a MindSpore Vision suite Android APK; and
+
+- Application experience: After finally importing the ms model file to the phone side, experience the recognition function of the dog and croissants.
+
+### Converting the file format
+
+Use the [conversion tool](https://www.mindspore.cn/lite/docs/zh-CN/master/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.
+
+The specific model file format conversion method is as follows:
+
+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/master/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
+export PACKAGE_ROOT_PATH={converter_path}
+
+# Include the dynamic-link libraries required by the conversion tool in the environment variables 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
+./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/master/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
+set PACKAGE_ROOT_PATH={converter_path}
+
+# Include the dynamic-link libraries required by the conversion tool in the environment variables PATH
+set PATH=%PACKAGE_ROOT_PATH%\tools\converter\lib;%PATH%
+
+# Execute the conversion command in mindspore-lite-win-x64\tools\converter\converter
+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.
+
+> For other environments to download MindSpore Lite Converter, see [Download MindSpore Lite](https://www.mindspore.cn/lite/docs/en/master/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`.
+
+> 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.
+
+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.
+
+
+
+By default, the MindSpore Vision `classification` module has a built-in universal AI network model to identify and classify images.
+
+
+
+### 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.
+
+#### 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.
+
+```text
+"title": 'dog and croissants',
+"file": 'mobilenet_v2_1.0_224.ms',
+"label": ['croissants', 'dag']
+```
+
+The Json label file should contain three Key value fields of `title`, `file`, and `label`, the meaning of which is 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.
+
+#### Labels and Model Files Deployed to the Phone
+
+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.
+
+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.
+
+
+
+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.
+
+
+
+> 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/master/index.html).
+
+
+
+
+
+
+
diff --git a/tutorials/source_en/beginner/quick_start.md b/tutorials/source_en/beginner/quick_start.md
index c52a6d2312484d51a1f0cb441fa260cf8bb2cd31..c2f8f235a214e375be788a8948bde2173891926c 100644
--- a/tutorials/source_en/beginner/quick_start.md
+++ b/tutorials/source_en/beginner/quick_start.md
@@ -1,295 +1,166 @@
-# Quick Start for Beginners
-
-`Ascend` `GPU` `CPU` `Beginner` `Whole Process`
+# Quickstart: Handwritten Digit Recognition
-The following describes the basic functions of MindSpore to implement common tasks in deep learning. For details, see links in each section.
+This section runs through the basic process of MindSpore deep learning, using the LeNet5 network model as an example to implement common tasks in deep learning.
-## Configuring the Running Information
+## Downloading and Processing the Dataset
-MindSpore uses `context.set_context` to configure the information required for running, such as the running mode, backend information, and hardware information.
+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.
-Import the `context` module and configure the required information.
+
-```python
-import os
-import argparse
-from mindspore import context
+> 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.
-parser = argparse.ArgumentParser(description='MindSpore LeNet Example')
-parser.add_argument('--device_target', type=str, default="CPU", choices=['Ascend', 'GPU', 'CPU'])
+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:
-args = parser.parse_known_args()[0]
-context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target)
-```
+```python
+from mindvision.dataset import Mnist
-This example runs in graph mode. You can configure hardware information as required. For example, if the code runs on the Ascend AI processor, set `--device_target` to `Ascend`. This rule also applies to the code running on the CPU and GPU. For details about the parameters, see [context.set_context](https://www.mindspore.cn/docs/api/en/master/api_python/mindspore.context.html).
+# 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)
-## Downloading the Dataset
+download_eval = Mnist(path="./mnist", split="test", batch_size=32, resize=32, download=True)
-The MNIST dataset used in this example consists of 10 classes of 28 x 28 pixels grayscale images. It has a training set of 60,000 examples, and a test set of 10,000 examples.
+dataset_train = download_train.run()
+dataset_eval = download_eval.run()
+```
-Click [here](http://yann.lecun.com/exdb/mnist/) to download and unzip the MNIST dataset and place the dataset according to the following directory structure. The following example code downloads and unzips the dataset to the specified location.
+Parameters description:
-```python
-import os
-import requests
-
-def download_dataset(dataset_url, path):
- filename = dataset_url.split("/")[-1]
- save_path = os.path.join(path, filename)
- if os.path.exists(save_path):
- return
- if not os.path.exists(path):
- os.makedirs(path)
- res = requests.get(dataset_url, stream=True, verify=False)
- with open(save_path, "wb") as f:
- for chunk in res.iter_content(chunk_size=512):
- if chunk:
- f.write(chunk)
-
-train_path = "datasets/MNIST_Data/train"
-test_path = "datasets/MNIST_Data/test"
-
-download_dataset("https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/train-labels-idx1-ubyte", train_path)
-download_dataset("https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/train-images-idx3-ubyte", train_path)
-download_dataset("https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/t10k-labels-idx1-ubyte", test_path)
-download_dataset("https://mindspore-website.obs.myhuaweicloud.com/notebook/datasets/mnist/t10k-images-idx3-ubyte", test_path)
-```
+- 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.
-The directory structure of the dataset file is as follows:
+The directory structure of the downloaded dataset files is as follows:
```text
- ./datasets/MNIST_Data
- ├── test
- │ ├── t10k-images-idx3-ubyte
- │ └── t10k-labels-idx1-ubyte
- └── train
- ├── train-images-idx3-ubyte
- └── train-labels-idx1-ubyte
-
- 2 directories, 4 files
+./mnist/
+├── test
+│ ├── t10k-images-idx3-ubyte
+│ └── t10k-labels-idx1-ubyte
+└── train
+ ├── train-images-idx3-ubyte
+ └── train-labels-idx1-ubyte
```
-## Data Processing
+## Building the Model
-Datasets are crucial for model training. A good dataset can effectively improve training accuracy and efficiency.
-MindSpore provides the API module `mindspore.dataset` for data processing to store samples and labels. Before loading a dataset, we usually process the dataset. `mindspore.dataset` integrates common data processing methods.
+According to the network structure of LeNet, there are 7 layers of LeNet removal input layer, including 3 convolutional layers, 2 sub-sampling layers, and 3 fully connected layers.
-Import `mindspore.dataset` and other corresponding modules in MindSpore.
+
-```python
-import mindspore.dataset as ds
-import mindspore.dataset.transforms.c_transforms as C
-import mindspore.dataset.vision.c_transforms as CV
-from mindspore.dataset.vision import Inter
-from mindspore import dtype as mstype
-```
-
-Dataset processing consists of the following steps:
-
-1. Define the `create_dataset` function to create a dataset.
-2. Define the data augmentation and processing operations to prepare for subsequent mapping.
-3. Use the map function to apply data operations to the dataset.
-4. Perform shuffle and batch operations on data.
+The MindSpore Vision Suite provides the LeNet network model interface lenet, which defines the network model as follows:
```python
-def create_dataset(data_path, batch_size=32, repeat_size=1,
- num_parallel_workers=1):
- # Define the dataset.
- mnist_ds = ds.MnistDataset(data_path)
- resize_height, resize_width = 32, 32
- rescale = 1.0 / 255.0
- shift = 0.0
- rescale_nml = 1 / 0.3081
- shift_nml = -1 * 0.1307 / 0.3081
-
- # Define the mapping to be operated.
- resize_op = CV.Resize((resize_height, resize_width), interpolation=Inter.LINEAR)
- rescale_nml_op = CV.Rescale(rescale_nml, shift_nml)
- rescale_op = CV.Rescale(rescale, shift)
- hwc2chw_op = CV.HWC2CHW()
- type_cast_op = C.TypeCast(mstype.int32)
-
- # Use the map function to apply data operations to the dataset.
- mnist_ds = mnist_ds.map(operations=type_cast_op, input_columns="label", num_parallel_workers=num_parallel_workers)
- mnist_ds = mnist_ds.map(operations=[resize_op, rescale_op, rescale_nml_op, hwc2chw_op], input_columns="image", num_parallel_workers=num_parallel_workers)
-
-
- # Perform shuffle, batch and repeat operations.
- buffer_size = 10000
- mnist_ds = mnist_ds.shuffle(buffer_size=buffer_size)
- mnist_ds = mnist_ds.batch(batch_size, drop_remainder=True)
- mnist_ds = mnist_ds.repeat(count=repeat_size)
-
- return mnist_ds
-```
-
-In the preceding information, `batch_size` indicates the number of data records in each group. Assume that each group contains 32 data records.
+from mindvision.classification.models import lenet
-> MindSpore supports multiple data processing and argumentation operations. For details, see [Processing Data](https://www.mindspore.cn/docs/programming_guide/en/master/pipeline.html) and [Data Augmentation](https://www.mindspore.cn/docs/programming_guide/en/master/augmentation.html).
+network = lenet(num_classes=10, pretrained=False)
+```
-## Creating a Model
+## Defining the Loss Function and the Optimizer
-To use MindSpore for neural network definition, inherit `mindspore.nn.Cell`. `Cell` is the base class of all neural networks (such as `Conv2d-relu-softmax`).
+To train a neural network model, you need to define a loss function and an optimizer function.
-Define each layer of a neural network in the `__init__` method in advance, and then define the `construct` method to complete the forward construction of the neural network. According to the LeNet structure, define the network layers as follows:
+- The loss function here uses the cross-entropy loss function `SoftmaxCrossEntropyWithLogits`.
+- The optimizer here uses `Momentum`.
```python
import mindspore.nn as nn
-from mindspore.common.initializer import Normal
-
-class LeNet5(nn.Cell):
- """
- Lenet network structure
- """
- def __init__(self, num_class=10, num_channel=1):
- super(LeNet5, self).__init__()
- # Define the required operation.
- self.conv1 = nn.Conv2d(num_channel, 6, 5, pad_mode='valid')
- self.conv2 = nn.Conv2d(6, 16, 5, pad_mode='valid')
- self.fc1 = nn.Dense(16 * 5 * 5, 120, weight_init=Normal(0.02))
- self.fc2 = nn.Dense(120, 84, weight_init=Normal(0.02))
- self.fc3 = nn.Dense(84, num_class, weight_init=Normal(0.02))
- self.relu = nn.ReLU()
- self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
- self.flatten = nn.Flatten()
-
- def construct(self, x):
- # Use the defined operation to construct a forward network.
- x = self.conv1(x)
- x = self.relu(x)
- x = self.max_pool2d(x)
- x = self.conv2(x)
- x = self.relu(x)
- x = self.max_pool2d(x)
- x = self.flatten(x)
- x = self.fc1(x)
- x = self.relu(x)
- x = self.fc2(x)
- x = self.relu(x)
- x = self.fc3(x)
- return x
-
-# Instantiate the network.
-net = LeNet5()
-```
-
-## Optimizing Model Parameters
+from mindspore.train import Model
-To train a neural network model, a loss function and an optimizer need to be defined.
-
-Loss functions supported by MindSpore include `SoftmaxCrossEntropyWithLogits`, `L1Loss`, and `MSELoss`. The following uses the cross-entropy loss function `SoftmaxCrossEntropyWithLogits`.
-
-```python
-# Define the loss function.
+# Define the loss function
net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
-```
-
-> For more information about using loss functions in mindspore, see [Loss Functions](https://www.mindspore.cn/tutorials/en/master/beginner/train.html#loss-functions).
-
-MindSpore supports the `Adam`, `AdamWeightDecay`, and `Momentum` optimizers. The following uses the `Momentum` optimizer as an example.
-```python
-# Define the optimizer.
-net_opt = nn.Momentum(net.trainable_params(), learning_rate=0.01, momentum=0.9)
+# Define the optimizer function
+net_opt = nn.Momentum(network.trainable_params(), learning_rate=0.01, momentum=0.9)
```
-> For more information about using an optimizer in mindspore, see [Optimizer](https://www.mindspore.cn/tutorials/en/master/beginner/train.html#optimizer-functions).
-
## Training and Saving the Model
-MindSpore provides the callback mechanism to execute custom logic during training. The following uses `ModelCheckpoint` provided by the framework as an example.
-`ModelCheckpoint` can save the network model and parameters for subsequent fine-tuning.
+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.
```python
from mindspore.train.callback import ModelCheckpoint, CheckpointConfig
-# Set model saving parameters.
-config_ck = CheckpointConfig(save_checkpoint_steps=1875, keep_checkpoint_max=10)
-# Use model saving parameters.
-ckpoint = ModelCheckpoint(prefix="checkpoint_lenet", config=config_ck)
-```
-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.
+# Set the model saving parameter
+config_ck = CheckpointConfig(save_checkpoint_steps=1875, keep_checkpoint_max=10)
-```python
-# Import the library required for model training.
-from mindspore.nn import Accuracy
-from mindspore.train.callback import LossMonitor
-from mindspore import Model
+# Apply the model saving parameter
+ckpoint = ModelCheckpoint(prefix="lenet", directory="./lenet", config=config_ck)
```
-```python
-def train_net(model, epoch_size, data_path, repeat_size, ckpoint_cb, sink_mode):
- """Define a training method."""
- # Load the training dataset.
- ds_train = create_dataset(os.path.join(data_path, "train"), 32, repeat_size)
- model.train(epoch_size, ds_train, callbacks=[ckpoint_cb, LossMonitor(125)], dataset_sink_mode=sink_mode)
-```
+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.
-`dataset_sink_mode` is used to control whether data is offloaded. Data offloading means that data is directly transmitted to the device through a channel to accelerate the training speed. If `dataset_sink_mode` is True, data is offloaded. Otherwise, data is not offloaded.
+```python
+from mindvision.engine.callback import LossMonitor
-Validate the generalization capability of the model based on the result obtained by running the test dataset.
+# Initialize the model parameter
+model = Model(network, loss_fn=net_loss, optimizer=net_opt, metrics={'accuracy'})
-1. Read the test dataset using the `model.eval` API.
-2. Use the saved model parameters for inference.
-
-```python
-def test_net(model, data_path):
- """Define a validation method."""
- ds_eval = create_dataset(os.path.join(data_path, "test"))
- acc = model.eval(ds_eval, dataset_sink_mode=False)
- print("{}".format(acc))
+# Train the network model
+model.train(10, dataset_train, callbacks=[ckpoint, LossMonitor(0.01, 1875)])
```
-Set `train_epoch` to 1 to train the dataset in one epoch. In the `train_net` and `test_net` methods, the previously downloaded training dataset is loaded. `mnist_path` is the path of the MNIST dataset.
+```text
+Epoch:[ 0/ 10], step:[ 1875/ 1875], loss:[0.314/0.314], time:2237.313 ms, lr:0.01000
+Epoch time: 3577.754 ms, per step time: 1.908 ms, avg loss: 0.314
+Epoch:[ 1/ 10], step:[ 1875/ 1875], loss:[0.031/0.031], time:1306.982 ms, lr:0.01000
+Epoch time: 1307.792 ms, per step time: 0.697 ms, avg loss: 0.031
+Epoch:[ 2/ 10], step:[ 1875/ 1875], loss:[0.007/0.007], time:1324.625 ms, lr:0.01000
+Epoch time: 1325.340 ms, per step time: 0.707 ms, avg loss: 0.007
+Epoch:[ 3/ 10], step:[ 1875/ 1875], loss:[0.021/0.021], time:1396.733 ms, lr:0.01000
+Epoch time: 1397.495 ms, per step time: 0.745 ms, avg loss: 0.021
+Epoch:[ 4/ 10], step:[ 1875/ 1875], loss:[0.028/0.028], time:1594.762 ms, lr:0.01000
+Epoch time: 1595.549 ms, per step time: 0.851 ms, avg loss: 0.028
+Epoch:[ 5/ 10], step:[ 1875/ 1875], loss:[0.007/0.007], time:1242.175 ms, lr:0.01000
+Epoch time: 1242.928 ms, per step time: 0.663 ms, avg loss: 0.007
+Epoch:[ 6/ 10], step:[ 1875/ 1875], loss:[0.033/0.033], time:1199.938 ms, lr:0.01000
+Epoch time: 1200.627 ms, per step time: 0.640 ms, avg loss: 0.033
+Epoch:[ 7/ 10], step:[ 1875/ 1875], loss:[0.175/0.175], time:1228.845 ms, lr:0.01000
+Epoch time: 1229.548 ms, per step time: 0.656 ms, avg loss: 0.175
+Epoch:[ 8/ 10], step:[ 1875/ 1875], loss:[0.009/0.009], time:1237.200 ms, lr:0.01000
+Epoch time: 1237.969 ms, per step time: 0.660 ms, avg loss: 0.009
+Epoch:[ 9/ 10], step:[ 1875/ 1875], loss:[0.000/0.000], time:1287.693 ms, lr:0.01000
+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.
+
+Verify the generalization capability of the model by running the test data set from the results obtained by running the model:
+
+1. Use the `model.eval` interface to read in the test data set.
+2. Use the saved model parameters for inference.
```python
-train_epoch = 1
-mnist_path = "./datasets/MNIST_Data"
-dataset_size = 1
-model = Model(net, net_loss, net_opt, metrics={"Accuracy": Accuracy()})
-train_net(model, train_epoch, mnist_path, dataset_size, ckpoint, False)
-test_net(model, mnist_path)
-```
-
-Run the following command to execute the script:
+acc = model.eval(dataset_eval)
-```bash
-python lenet.py --device_target=CPU
+print("{}".format(acc))
```
-Where,
-
-`lenet.py`: You can paste the preceding code to lenet.py (excluding the code for downloading the dataset). Generally, you can move the import part to the beginning of the code, place the definitions of classes, functions, and methods after the code, and connect the preceding operations in the main method.
-
-`--device_target=CPU`: specifies the running hardware platform. The parameter value can be `CPU`, `GPU`, or `Ascend`, depending on the actual running hardware platform.
-
-Loss values are displayed during training, as shown in the following. Although loss values may fluctuate, they gradually decrease and the accuracy gradually increases in general. Loss values displayed each time may be different because of their randomicity.
-The following is an example of loss values output during training:
-
```text
-epoch: 1 step: 125, loss is 2.3083377
-epoch: 1 step: 250, loss is 2.3019726
-...
-epoch: 1 step: 1500, loss is 0.028385757
-epoch: 1 step: 1625, loss is 0.0857362
-epoch: 1 step: 1750, loss is 0.05639569
-epoch: 1 step: 1875, loss is 0.12366105
-{'Accuracy': 0.9663477564102564}
+{'accuracy': 0.9903846153846154}
```
-The model accuracy data is displayed in the output content. In the example, the accuracy reaches 96.6%, indicating a good model quality. As the number of network epochs (`train_epoch`) increases, the model accuracy will be further improved.
+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.
## Loading the Model
```python
from mindspore import load_checkpoint, load_param_into_net
-# Load the saved model for testing.
-param_dict = load_checkpoint("checkpoint_lenet-1_1875.ckpt")
-# Load parameters to the network.
-load_param_into_net(net, param_dict)
+
+# Load the model that has been saved for testing
+param_dict = load_checkpoint("./lenet/lenet-1_1875.ckpt")
+# Load parameters into the network
+load_param_into_net(network, param_dict)
+```
+
+```text
+[]
```
> For more information about loading a model in mindspore, see [Loading the Model](https://www.mindspore.cn/tutorials/en/master/save_load_model.html#loading-the-model).
@@ -303,23 +174,33 @@ Use the generated model to predict the classification of a single image. The pro
```python
import numpy as np
from mindspore import Tensor
+import matplotlib.pyplot as plt
-# Define a test dataset. If batch_size is set to 1, an image is obtained.
-ds_test = create_dataset(os.path.join(mnist_path, "test"), batch_size=1).create_dict_iterator()
+mnist = Mnist("./mnist", split="train", batch_size=6, resize=32)
+dataset_infer = mnist.run()
+ds_test = dataset_infer.create_dict_iterator()
data = next(ds_test)
-
-# `images` indicates the test image, and `labels` indicates the actual classification of the test image.
images = data["image"].asnumpy()
labels = data["label"].asnumpy()
-# Use the model.predict function to predict the classification of the image.
+plt.figure()
+for i in range(1, 7):
+ plt.subplot(2, 3, i)
+ plt.imshow(images[i-1][0], interpolation="None", cmap="gray")
+plt.show()
+
+# Predict the image corresponding classification by using the function model.predict
output = model.predict(Tensor(data['image']))
predicted = np.argmax(output.asnumpy(), axis=1)
-# Output the predicted classification and the actual classification.
-print(f'Predicted: "{predicted[0]}", Actual: "{labels[0]}"')
+# Output prediction classification versus actual classification
+print(f'Predicted: "{predicted}", Actual: "{labels}"')
```
+
+
```text
- Predicted: "6", Actual: "6"
+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.
\ No newline at end of file