diff --git a/tutorials/source_zh_cn/advanced_use/mobilenetv2_finetune.md b/tutorials/source_zh_cn/advanced_use/mobilenetv2_finetune.md new file mode 100644 index 0000000000000000000000000000000000000000..599992225a0e27975f3748ccec674b0ad9ffecb8 --- /dev/null +++ b/tutorials/source_zh_cn/advanced_use/mobilenetv2_finetune.md @@ -0,0 +1,415 @@ +#
MobileNetV2 增量学习
+ +`CPU` `Ascend` `GPU` `模型开发` `中级` `高级` + + + +- [增量学习](#增量学习) + - [概述](#概述) + - [任务描述及准备](#任务描述及准备) + - [环境配置](#环境配置) + - [克隆代码](#克隆代码) + - [准备预训练模型](#准备预训练模型) + - [准备数据](#准备数据) + - [预训练模型加载代码详解](#预训练模型加载代码详解) + - [参数简介](#参数简介) + - [运行python文件](#运行python文件) + - [运行shell脚本](#运行shell脚本) + - [加载训练](#加载训练) + - [开始增量训练](#开始增量训练) + - [调整节点数量](#调整节点数量) + - [训练结果](#训练结果) + - [验证增量训练模型](#验证增量训练模型) + - [验证结果](#验证结果) + + +   + +## 概述 + +计算机视觉任务中,从头开始训练一个网络耗时巨大,需要大量计算能力。预训练模型选择的常见的OpenImage、ImageNet、VOC、COCO等公开大型数据集,规模达到几十万甚至超过上百万张。大部分任务数据规模较大,训练网络模型时,如果不使用预训练模型,从头开始训练网络,需要消耗大量的时间与计算能力,模型容易陷入局部极小值和过拟合。因此大部分任务都会选择预训练模型,在其上做增量学习。 + +MindSpore是一个多元化的机器学习框架。既可以在手机等端侧和PC等设备上运行,也可以在云上的服务器集群上运行。目前MobileNetV2支持在windows系统中使用单核CPU做增量学习,在Euler系统中使用单个或者多个Ascend AI处理器和GPU中做增量学习,本教程将会介绍如何在不同系统与处理器下的MindSpore框架中做增量学习。 + +## 任务描述及准备 + +### 环境配置 + +若在华为云环境上运行,不需要安装MindSpore框架和配置Ascend AI处理器,可以跳过本小节。若在本地环境运行,需要安装MindSpore框架,配置CPU、GPU或Ascend AI处理器。 + +1. 安装MindSpore框架 + 在Euler、Ubuntu或者Windows等系统上需要根据系统和处理器架构[安装对应版本MindSpore框架](https://www.mindspore.cn/install)。 + +2. 配置CPU环境 + 使用CPU时,在代码中,需要在调用CPU开始训练或测试前,按照如下代码设置: + + ```python + if config.platform == "CPU": + context.set_context(mode=context.GRAPH_MODE, device_target=config.platform, \ + save_graphs=False) + ``` + +3. 配置GPU环境 + 使用GPU时,在代码中,需要在调用GPU开始训练或测试前,按照如下代码设置: + + ```python + elif config.platform == "GPU": + context.set_context(mode=context.GRAPH_MODE, device_target=config.platform, \ + save_graphs=False) + init("nccl") + context.set_auto_parallel_context(device_num=get_group_size(), + parallel_mode=ParallelMode.DATA_PARALLEL, + mirror_mean=True) + ``` + +4. 配置Ascend环境 + 以Ascend 910 AI处理器为例,1个8个处理器环境的json配置文件`hccl_config.json`示例如下。单/多处理器环境可以根据以下示例调整`"server_count"`与`device`: + + ```json + { + "version": "1.0", + "server_count": "1", + "server_list": [ + { + "server_id": "10.155.111.140", + "device": [ba + {"device_id": "0","device_ip": "192.1.27.6","rank_id": "0"}, + {"device_id": "1","device_ip": "192.2.27.6","rank_id": "1"}, + {"device_id": "2","device_ip": "192.3.27.6","rank_id": "2"}, + {"device_id": "3","device_ip": "192.4.27.6","rank_id": "3"}, + {"device_id": "4","device_ip": "192.1.27.7","rank_id": "4"}, + {"device_id": "5","device_ip": "192.2.27.7","rank_id": "5"}, + {"device_id": "6","device_ip": "192.3.27.7","rank_id": "6"}, + {"device_id": "7","device_ip": "192.4.27.7","rank_id": "7"}], + "host_nic_ip": "reserve" + } + ], + "status": "completed" + } + ``` + + 使用Ascend AI处理器时,在代码中,需要在调用Ascend AI处理器开始训练或测试前,按照如下代码设置: + + ```python + elif config.platform == "Ascend": + context.set_context(mode=context.GRAPH_MODE, device_target=config.platform, \ + device_id=config.device_id, save_graphs=False) + if config.run_distribute: + context.set_auto_parallel_context(device_num=config.rank_size, + parallel_mode=ParallelMode.DATA_PARALLEL, + parameter_broadcast=True, mirror_mean=True) + auto_parallel_context().set_all_reduce_fusion_split_indices([140]) + init() + ... + ``` + +### 克隆代码 + +在Gitee中克隆[MindSpore开源项目仓库](https://gitee.com/mindspore/mindspore.git),进入`./model_zoo/official/cv/mobilenetv2/`。 + +```bash +git clone git@gitee.com:/mindspore.git +cd ./mindspore/model_zoo/official/cv/mobilenetv2 +``` + +使用脚本文件`run_train.sh`时,该文件会将运行`launch.py`并且将参数传入`launch.py`, `launch.py`根据分配的CPU、GPU或Ascend AI处理器数量,启动单个/多个进程运行`train.py`,每一个进程分配对应的一个处理器。 + +代码结构如下: + +``` +├─MobileNetV2 + ├─Readme.md # descriptions about MobileNetV2 + ├─scripts + │ run_train.sh # shell script for train with Ascend or GPU + │ run_eval.sh # shell script for evaluation with Ascend or GPU + ├─src + │ config.py # parameter configuration + │ dataset.py # creating dataset + │ launch.py # start python script + │ lr_generator.py # learning rate config + │ mobilenetV2.py # MobileNetV2 architecture + │ models.py # net utils to load ckpt_file, define_net... + │ utils.py # net utils to switch precision, set_context and so on + ├─train.py # training script + └─eval.py # evaluation script +``` + +### 准备预训练模型 + +[下载预训练模型](https://download.mindspore.cn/model_zoo/official/lite/mobilenetv2_openimage_lite/mobilenetV2.ckpt)到以下目录: +`./pretrain_checkpoint/[pretrain_checkpoint_file]` + +```python +mkdir pretrain_checkpoint +wget -P ./pretrain_checkpoint https://download.mindspore.cn/model_zoo/official/lite/mobilenetv2_openimage_lite/mobilenetV2.ckpt +``` + +### 准备数据 + +准备ImageFolder格式管理的数据集,运行`run_train.sh`时加入`[dataset_path]`参数,运行`train.py`时加入`--dataset_path [dataset_path]`参数: + +数据集结构如下: + +``` +└─ImageFolder + ├─train + │ class1Folder + │ class2Folder + │ ...... + └─eval + class1Folder + class2Folder + ...... +``` + +## 预训练模型加载代码详解 + +在增量学习时,需要加载预训练模型。不同数据集和任务中特征提取层(卷积层)分布趋于一致,但是特征向量的组合(全连接层)不相同,分类数量(全连接层output_size)通常也不一致。在增量学习时,只加载与训练特征提取层参数,不加载与训练全连接层参数;在微调与初始训练时,加载与训练特征提取层参数与全连接层参数。 + +在训练与测试之前,首先按照代码第1行,构建MobileNetV2的backbone网络,head网络,并且构建包含这两个子网络的MobileNetV2网络。代码第4-21行展示了如何在`fine_tune`训练模式下,将预训练模型加载入`net`(MobileNetV2);在`incremental_learn`训练模式下,将预训练模型分别加载入backbone与head两个子网络,并且冻结住backbone子网络中的参数,不参与训练。代码第23-31行展示了如何冻结网络参数。 + +```python + 1: backbone_net, head_net, net = define_net(args_opt, config) + 2: ... + 3: def define_net(args, config): + 4: backbone_net = MobileNetV2Backbone(platform=args.platform) + 5: head_net = MobileNetV2Head(input_channel=backbone_net.out_channels, num_classes=config.num_classes) + 6: net = mobilenet_v2(backbone_net, head_net) + 7: if args.pretrain_ckpt: + 8: if args.train_method == "fine_tune": + 9: load_ckpt(net, args.pretrain_ckpt) +10: elif args.train_method == "incremental_learn": +11: load_ckpt(backbone_net, args.pretrain_ckpt, trainable=False) +12: elif args.train_method == "train": +13: pass +14: else: +15: raise ValueError("must input the usage of pretrain_ckpt when the pretrain_ckpt isn't None") +16: return backbone_net, head_net, net +17: ... +18: def load_ckpt(network, pretrain_ckpt_path, trainable=True): +19: """ +20: incremental_learning or not +21: """ +22: param_dict = load_checkpoint(pretrain_ckpt_path) +23: load_param_into_net(network, param_dict) +24: if not trainable: +25: for param in network.get_parameters(): +26: param.requires_grad = False +``` + +## 参数简介 + +在Windows与Linux系统上训练时,运行`train.py`并且传入`dataset_path`、`pretrain_ckpt`与`platform`三个参数。在Linux系统上时,还可以使用shell脚本文件`./scripts/fun_train.sh`与`./scripts/infer.sh`,运行时读取参数。 + +```shell +# windows doesn't support shell +# windows/Linux train with python file +python train.py --dataset_path [dataset_path] --pretrain_ckpt [pretrain_checkpoint_path] --platform [platform] --train_method[("train", "fine_tune", "incremental_learn")] + +# windows/Linux eval with python file +python eval.py --dataset_path [dataset_path] --pretrain_ckpt [pretrain_checkpoint_path] --head_ckpt [head_ckpt_path] --platform [platform] + +# Linux train with shell script +sh run_train.sh [PLATFORM] [DEVICE_NUM] [VISIABLE_DEVICES(0,1,2,3,4,5,6,7)] [RANK_TABLE_FILE] [DATASET_PATH] [CKPT_PATH] [TRAIN_METHOD] + +# Linux eval with shell script for train or fine_tune +sh run_eval.sh [PLATFORM] [DATASET_PATH] [CKPT_PATH] + +# Linux eval with shell script for train or incremental learn +sh run_eval.sh [PLATFORM] [DATASET_PATH] [PRETRAIN_CKPT_PATH] [HEAD_CKPT_PATH] + +``` + +### 运行python文件: + +- `--dataset_path`:训练与验证数据集地址,无默认值,用户训练/验证时必须输入。 +- `--pretrain_ckpt`:增量训练或调优时,需要传入checkpoint文件路径以加载预训练好的模型参数权重。 +- `--head_ckpt`:增量训练模型验证时,需要传入head_net预训练模型路径以加载预训练好的模型参数权重。 +- `--platform`:芯片,默认为“Ascend”,可以设置为“CPU”或"GPU"。 +- `--train_method`:训练方法,必须输入“train"、"fine_tune"和incremental_learn"其中一个。 + +### 运行shell脚本: + +- `[PLATFORM]`:芯片,默认为“Ascend”,可以设置为“GPU”。 +- `[DEVICE_NUM]`:每个节点(一台服务器/PC相当于一个节点)进程数量,建议设置为机器上Ascend 芯片数量或GPU数量。 +- `[VISIABLE_DEVICES(0,1,2,3,4,5,6,7)]`:字符串格式的的设备ID,训练将会根据`[VISIABLE_DEVICES]`将进程绑定到对应ID的设备上,多个设备ID之间使用','分隔,建议ID数量与进程数量相同。 +- `[RANK_TABLE_FILE]`:platform选择Ascend时,需要配置Ascend的配置Json文件,。 +- `[DATASET_PATH]`:训练与验证数据集地址,无默认值,用户训练/验证时必须输入。 +- `[CKPT_PATH]`:增量训练或调优时,需要传入checkpoint文件路径以加载预训练好的模型参数权重。 +- `[TRAIN_METHOD]`:训练方法,必须输入`train`、`fine_tune`和`incremental_learn`其中一个。 +- `[PRETRAIN_CKPT_PATH]`:针对增量学习的模型做验证时,需要输入主干网络层保存模型路径。 +- `[HEAD_CKPT_PATH]`:针对增量学习的模型做验证时,需要输入全连接层保存模型路径。 + + +## 加载训练 + +### 开始增量训练 + +在windows系统上,`MobileNetV2`做增量训练时,只能运行`train.py`。 +在Linux系统时,使用`MobileNetV2`做增量训练时,也可以选择运行`run_train.sh`, 并在在运行shell脚本文件时传入[参数](#训练参数简介)。 + +Windows系统输出信息到交互式命令行,Linux系统环境下运行`run_train.sh`时,命令行结尾使用`&> [log_file_path]`将标准输出与错误输出写入log文件。 增量训练成功开始训练,`./train/device*/log*.log`中会持续写入每一个epoch的训练时间与Loss等信息。若未成功,上述log文件会写入失败报错信息。 + +```shell +# windows +python train.py --platform CPU --dataset_path /store/dataset/OpenImage/train/ --pretrain_ckpt ./pretrain_checkpoint/mobilenetV2.ckpt -- train_method incremental_learn + +# windows or Linux: GPU +sh run_train.sh GPU 8 0,1,2,3,4,5,6,7 /store/dataset/OpenImage/train/ ../pretrain_checkpoint/mobilenetV2.ckpt incremental_learn + +# windows or Linux: Ascemd +sh run_train.sh Ascend 8 0,1,2,3,4,5,6,7 ~/rank_table.json /store/dataset/OpenImage/train/ ../pretrain_checkpoint/mobilenetV2.ckpt incremental_learn +``` + +### 调整节点数量 + +目前运行`train.py`时仅支持单处理器,不需要调整节点数量。运行`run_train.sh`文件时,设置`[nproc_per_node]`为Ascend AI处理器/GPU数量, `[visible_devices]`为可使用的处理器编号,即Ascend AI处理器或GPU的ID,可以选择0-7中一个或多个设备ID。目前Ascend节点进程数量只能设置为1或者8。 + +- 使用样例1:通过python文件调用1个CPU处理器。 + + ```shell + python train.py --platform CPU --dataset_path /store/dataset/OpenImage/train/ --pre_trained ./pretrain_checkpoint/mobilenetV2.ckpt --train_method incremental_learn + ``` + +- 使用样例2:通过python文件调用1个GPU处理器。 + + ```shell + python train.py --platform GPU --dataset_path /store/dataset/OpenImage/train/ --pre_trained ./pretrain_checkpoint/mobilenetV2.ckpt --train_method incremental_learn + ``` + +- 使用样例3:通过python文件调用1个Ascend处理器。 + + ```shell + python train.py --platform Ascend --dataset_path /store/dataset/OpenImage/train/ --pre_trained ./pretrain_checkpoint/mobilenetV2.ckpt --train_method incremental_learn + ``` + +- 使用样例4:通过shell脚本调用1个GPU处理器,设备ID为“0”。 + + ```shell + sh run_train.sh GPU 1 0 /store/dataset/OpenImage/train/ ../pretrain_checkpoint/mobilenetV2.ckpt incremental_learn + ``` + +- 使用样例5:通过shell脚本调用1个GPU处理器,设备ID为“4”。 + + ```shell + sh run_train.sh GPU 1 4 /store/dataset/OpenImage/train/ ../pretrain_checkpoint/mobilenetV2.ckpt incremental_learn + ``` + +- 使用样例6:通过shell脚本调用8个GPU处理器,设备ID为“0,1,2,3,4,5,6,7”。 + + ```shell + sh run_train.sh GPU 8 0,1,2,3,4,5,6,7 /store/dataset/OpenImage/train/ ../pretrain_checkpoint/mobilenetV2.ckpt incremental_learn + ``` + +- 使用样例7:通过shell脚本调用1个Ascend AI处理器,设备ID为“0”。 + + ```shell + sh run_train.sh Ascend 1 0 ~/rank_table.json /store/dataset/OpenImage/train/ ../pretrain_checkpoint/mobilenetV2.ckpt incremental_learn + ``` + +- 使用样例8:通过shell脚本调用1个Ascend AI处理器,设备ID为“4”。 + + ```shell + sh run_train.sh Ascend 1 4 ~/rank_table.json /store/dataset/OpenImage/train/ ../pretrain_checkpoint/mobilenetV2.ckpt incremental_learn + ``` + +- 使用样例9:通过shell脚本调用8个Ascend AI处理器,设备ID为”0,1,2,3,4,5,6,7“。 + + ```shell + sh run_train.sh Ascend 8 0,1,2,3,4,5,6,7 ~/rank_table.json /store/dataset/OpenImage/train/ ../pretrain_checkpoint/mobilenetV2.ckpt incremental_learn + ``` + +### 训练结果 + +- 查看运行结果。 + + - 运行python文件: + 在交互式命令行中查看打印信息 + + 输出结果如下: + + ```shell + train args: Namespace(dataset_path='.\\dataset\\train', platform='CPU', \ + pretrain_ckpt='.\\pretrain_checkpoint\\mobilenetV2.ckpt', train_method='incremental_learn') + cfg: {'num_classes': 26, 'image_height': 224, 'image_width': 224, 'batch_size': 150, \ + 'epoch_size': 15, 'warmup_epochs': 0, 'lr_max': 0.03, 'lr_end': 0.03, 'momentum': 0.9, \ + 'weight_decay': 4e-05, 'label_smooth': 0.1, 'loss_scale': 1024, 'save_checkpoint': True, \ + 'save_checkpoint_epochs': 1, 'keep_checkpoint_max': 20, 'save_checkpoint_path': './checkpoint', \ + 'platform': 'CPU'} + Processing batch: 16: 100%|███████████████████████████████████████████ █████████████████████| 16/16 [00:00