add pretrain eg

This commit is contained in:
liuzx 2022-09-14 16:08:32 +08:00
parent 1517f2fa00
commit 175eedddba
3 changed files with 69 additions and 46 deletions

View File

@ -1,10 +1,12 @@
# 如何在启智平台上进行模型训练 - NPU版本
- **启智集群和智算网络集群的单数据集训练,多数据集训练,训练使用方式不同,请按需求选择一种训练方式即可,注意区别(以下环境默认是训练环境)**
- 启智集群单数据集单卡或多卡的训练示例请参考示例中[train.py](https://git.openi.org.cn/OpenIOSSG/MNIST_Example/src/branch/master/train.py)的代码注释
- 启智集群单数据集单卡或多卡**加载模型**的训练示例请参考示例中 [pretrain.py](https://git.openi.org.cn/OpenIOSSG/MNIST_Example/src/branch/master/pretrain.py)的代码注释
- 启智集群单数据集单卡的推理示例请参考示例中[inference.py](https://git.openi.org.cn/OpenIOSSG/MNIST_Example/src/branch/master/inference.py)的代码注释
- 启智集群多数据集单卡或多卡的训练示例请参考示例中[train_for_multidataset.py](https://git.openi.org.cn/OpenIOSSG/MNIST_Example/src/branch/master/train_for_multidataset.py)的代码注释
- 智算网络集群单数据集单卡或多卡训练示例请参考示例中[train_for_c2net.py](https://git.openi.org.cn/OpenIOSSG/MNIST_Example/src/branch/master/train_for_c2net.py)的代码注释
- 智算网络集群单数据集单卡或多卡**加载模型**的训练示例请参考示例中[pretrain_for_c2net.py](https://git.openi.org.cn/OpenIOSSG/MNIST_Example/src/branch/master/pretrain_for_c2net.py)的代码注释
- 更多关于分布式训练的教程可参考mindspore官网教程[mindspore分布式训练教程](https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/advanced_use/distributed_training_ascend.html)
- **NPU启智集群中单数据集和多数据集的区别**
- 超参数不同:
@ -20,6 +22,7 @@
- 若想要使用调试环境的多卡并行训练,可参考示例[调试环境多卡并行示例](https://git.openi.org.cn/OpenIOSSG/MNIST_Example_NPU_Debug)
## 1 概述
- 本项目以LeNet-MNIST为例简要介绍如何在启智AI协同平台上使用MindSpore完成训练任务并提供单数据集的训练多数据集的训练智算网络的训练单数据集推理等训练代码示例旨在为AI开发者提供启智npu训练示例。对于示例代码有任何问题欢迎在本项目中提issue。
- 用户可以直接使用本项目提供的数据集和代码文件创建自己的训练任务。
- 启智平台对接ModelArts和OBS将数据集代码训练资源池等整合在启智AI协同平台上供开发者使用。
@ -27,14 +30,18 @@
- OBS是华为云提供的存储方式。
## 2 准备工作
- 启智平台使用准备,本项目需要用户创建启智平台账户,克隆代码到自己的账户,上传数据集,具体操作方法可以通过访问[OpenI_Learning](https://git.openi.org.cn/zeizei/OpenI_Learning)项目学习小白训练营系列课程进行学习。
### 2.1 数据准备
#### 数据集下载
- 数据集可从本项目的数据集目录中下载,[数据集下载](https://git.openi.org.cn/OpenIOSSG/MNIST_Example/datasets?type=1)
- 数据文件说明
- MNISTData数据集是由10类2828的灰度图片组成训练数据集包含60000张图片测试数据集包含10000张图片。
- 数据集压缩包的目录结构如下:
> MNIST_Data.zip
> ├── test
> │ ├── t10k-images-idx3-ubyte
@ -42,45 +49,45 @@
> └── train
> ├── train-images-idx3-ubyte
> └── train-labels-idx1-ubyte
>
> checkpoint_lenet-1_1875.zip
> ├── checkpoint_lenet-1_1875.ckpt
>
#### 数据集上传
- 由于本示例使用的是Mindspore开发需要在NPU芯片运行所以上传的数据集需要传到NPU界面。\
【注意如果你需要试运行本示例则无需再次上传数据集因为本示例中的数据集MNIST_Example已经设置为公开数据集可以直接引用或点赞收藏后使用】
- 由于本示例使用的是Mindspore开发需要在NPU芯片运行所以上传的数据集需要传到NPU界面。【注意如果你需要试运行本示例则无需再次上传数据集因为本示例中的数据集MNIST_Example已经设置为公开数据集可以直接引用或点赞收藏后使用】
- 如下所示:
- ![avatar](Example_Picture/数据集上传位置.png)
### 2.2 执行脚本准备
#### 示例代码
- 示例代码可从本仓库中下载,[代码下载](https://git.openi.org.cn/OpenIOSSG/MNIST_Example)
- 代码文件说明
- [train.py](https://git.openi.org.cn/OpenIOSSG/MNIST_Example/src/branch/master/train.py)启智集群单数据集训练的脚本文件包括将数据集从obs拷贝到训练镜像中、指定迭代次数、把训练后的模型数据拷贝回obs等。具体说明请参考[train.py](https://git.openi.org.cn/OpenIOSSG/MNIST_Example/src/branch/master/train.py)的代码注释
- [train_for_c2net.py](https://git.openi.org.cn/OpenIOSSG/MNIST_Example/src/branch/master/train_for_c2net.py),智算网络训练的脚本文件,包括指定迭代次数等。具体说明请参考[train_for_c2net.py](https://git.openi.org.cn/OpenIOSSG/MNIST_Example/src/branch/master/train_for_c2net.py)的代码注释
- [train_for_multidataset.py](https://git.openi.org.cn/OpenIOSSG/MNIST_Example/src/branch/master/train_for_multidataset.py)启智集群包括多数据集训练的脚本文件将多数据集从obs拷贝到训练镜像中、指定迭代次数、把训练后的模型数据拷贝回obs等。具体说明请参考[train_for_multidataset.py](https://git.openi.org.cn/OpenIOSSG/MNIST_Example/src/branch/master/train_for_multidataset.py)的代码注释
- [inference.py](https://git.openi.org.cn/OpenIOSSG/MNIST_Example/src/branch/master/inference.py)启智集群用于推理的脚本文件包括将数据集从obs拷贝到训练镜像中、指定迭代次数、把训练后的模型数据拷贝回obs等。具体说明请参考[inference.py](https://git.openi.org.cn/OpenIOSSG/MNIST_Example/src/branch/master/inference.py)的代码注释
- [config.py](https://git.openi.org.cn/OpenIOSSG/MNIST_Example/src/branch/master/config.py),网络配置信息,在单数据集训练,多数据集训练,智算网络训练等训练脚本中会使用到。
- [dataset.py](https://git.openi.org.cn/OpenIOSSG/MNIST_Example/src/branch/master/dataset.py),对原始数据集进行预处理,产生可用于网络训练的数据集,在单数据集的训练,多数据集的训练,智算网络的训练等训练脚本中会使用到。
- [lenet.py](https://git.openi.org.cn/OpenIOSSG/MNIST_Example/src/branch/master/lenet.py),使用的训练网络,在单数据集训练,多数据集训练,智算网络训练等训练脚本中会使用到。
- [dataset_distributes.py](https://git.openi.org.cn/OpenIOSSG/MNIST_Example/src/branch/master/dataset_distributes.py),对原始数据集进行预处理,产生可用于单机多卡训练的数据集。
## 3 创建训练任务
- 准备好数据和执行脚本以后需要创建训练任务将MindSpore脚本真正运行起来。首次使用的用户可参考本示例代码。
### 使用MindSpore作为训练框架创建训练作业界面截图如下图所示。
![avatar](Example_Picture/新建训练任务页面.png)
![avatar](Example_Picture/新建训练任务页面.png)
表1 创建训练作业界面参数说明
| 参数名称 | 说明 |
| ----------------- | ----------- |
| -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 代码分支 | 选择仓库代码中要使用的代码分支默认可选择master分支。 |
| AI引擎 | AI引擎选择[Ascend-Powered-Engine]和所需的MindSpore版本(本示例图片为 [Mindspore-1.3.0-python3.7-aarch64],请注意使用与所选版本对应的脚本)。 |
| 启动文件 | 启动文件选择代码目录下的启动脚本。 |
@ -90,10 +97,15 @@
<!--若要在启智平台上使用CPU需要在启智平台训练界面上加上运行参数device_target=CPU否则默认是Ascend如下图所示
![avatar](Example_Picture/运行参数界面.png) -->
## 4 查看运行结果
### 4.1 在训练作业界面可以查看运行日志
![avatar](Example_Picture/查看日志页面.png)
### 4.2 训练结束后可以下载模型文件
![avatar](Example_Picture/模型下载页面.png)
## 对于示例代码有任何问题欢迎在本项目中提issue。

View File

@ -25,6 +25,9 @@ function DownloadFromQizhi(obs_data_url, data_dir)
(4)Upload the output to Qizhi
function UploadToQizhi(train_dir, obs_train_url)
(5)Copy ckpt file from obs to training image.
function ObsUrlToEnv(obs_ckpt_url, ckpt_url)
33 parameters need to be defined
--data_url is the dataset you selected on the Qizhi platform
@ -38,6 +41,9 @@ A single dataset uses data_url as the input, and data_dir (ie:'/cache/data') as
of the dataset in the image.
For details, please refer to the following sample code.
5How to load the checkpoint file
The checkpoint file is loaded by the ckpt_url parameter
In addition, if you want to get the model file after each training, you can call the UploadOutput.
"""

View File

@ -6,6 +6,11 @@ The training of the intelligent computing network currently supports single data
the obs copy process.It only needs to define two parameters and then call it directly
train_dir = '/cache/output' #The location of the output
data_dir = '/cache/dataset' #The location of the dataset
pretrain_dir = '/cache/pretrainmodel' #The location of the pretrain model
The checkpoint file name is loaded by the pretrainmodelname parameter,and the checkpoint file is called by
'/cache/pretrainmodel/' + args.pretrainmodelname
In addition, if you want to get the model file after each training, you can call the uploader_for_npu tool,
which is written as:
@ -55,7 +60,7 @@ if __name__ == "__main__":
###define two parameters and then call it directly###
data_dir = '/cache/dataset'
train_dir = '/cache/output'
pretrain_dir = '/cache/pretrainmodel/'
pretrain_dir = '/cache/pretrainmodel'
device_num = int(os.getenv('RANK_SIZE'))
if device_num == 1: