Skip to content
Snippets Groups Projects
Commit c3398492 authored by hangangqiang's avatar hangangqiang
Browse files

apply simqat on lenet

parent bd2c1a6f
No related branches found
No related tags found
No related merge requests found
Showing
with 615 additions and 75 deletions
......@@ -17,6 +17,14 @@
- [Export MindIR](#export-mindir)
- [Infer on Ascend310](#infer-on-ascend310)
- [result](#result)
- [Apply algorithm in MindSpore Golden Stick](#apply-algorithm-in-mindspore-golden-stick)
- [Training Process](#Training Process-1)
- [Running on GPU](#running-on-gpu-1)
- [Evaluation Process](#evaluation-process-1)
- [Running on GPU](#running-on-gpu-2)
- [Result](#resutl-3)
- [Inference Process](#inference-process-1)
- [Export MindIR](#export-mindir-1)
- [Model Description](#model-description)
- [Performance](#performance)
- [Evaluation Performance](#evaluation-performance)
......@@ -280,6 +288,64 @@ Inference result is saved in current path, you can find result like this in acc.
'Accuracy': 0.9843
```
# Apply algorithm in MindSpore Golden Stick
MindSpore Golden Stick is a compression algorithm set for MindSpore. We usually apply algorithm in Golden Stick before training for smaller model size, lower power consuming or faster inference process.
MindSpore Golden Stick provides SimQAT algorithm for Lenet5. SimQAT is a quantization-aware training algorithm that trains the quantization parameters of certain layers in the network by introducing fake-quantization nodes, so that the model can perform inference with less power consumption or higher performance during the deployment phase.
## Training Process
### Running on GPU
```text
cd ./golden_stick/scripts/
# PYTHON_PATH represents path to directory of 'train.py'.
bash run_standalone_train_gpu.sh [PYTHON_PATH] [CONFIG_FILE] [DATASET_PATH] [CKPT_TYPE](optional) [CKPT_PATH](optional)
# standalone training example, apply SimQAT and train from beginning
cd ./golden_stick/scripts/
bash run_standalone_train_gpu.sh ../quantization/simqat/ ../quantization/simqat/lenet_mnist_config.yaml /path/to/dataset
# standalone training example, apply SimQAT and train from full precision checkpoint
cd ./golden_stick/scripts/
bash run_standalone_train_gpu.sh ../quantization/simqat/ ../quantization/simqat/lenet_mnist_config.yaml /path/to/dataset FP32 /path/to/fp32_ckpt
# standalone training example, apply SimQAT and train from pretrained checkpoint
cd ./golden_stick/scripts/
bash run_standalone_train_gpu.sh ../quantization/simqat/ ../quantization/simqat/lenet_mnist_config.yaml /path/to/dataset PRETRAINED /path/to/pretrained_ckpt
```
## Evaluation Process
### Running on GPU
```text
# evaluation
cd ./golden_stick/scripts/
# PYTHON_PATH represents path to directory of 'eval.py'.
bash run_eval_gpu.sh [PYTHON_PATH] [CONFIG_FILE] [DATASET_PATH] [CHECKPOINT_PATH]
# evaluation example
cd ./golden_stick/scripts/
bash run_eval_gpu.sh ../quantization/simqat/ ../quantization/simqat/lenet_mnist_config.yaml /path/to/dataset ./checkpoint/lenet-10.ckpt
```
### Result
Evaluation result will be stored in the example path, whose folder name is "eval". Under this, you can find result like the following in log.
- Apply SimQAT on Lenet5, and evaluating with MNIST dataset
```bash
================ {'Accuracy': 0.9907852564102564} ================
```
## Inference Process
### Export MindIR
Not support exporting MindIR now.
## [Model Description](#contents)
### [Performance](#contents)
......
......@@ -19,6 +19,14 @@
- [导出MindIR](#导出mindir)
- [在Ascend310执行推理](#在ascend310执行推理)
- [结果](#结果)
- [应用金箍棒模型压缩算法](#应用金箍棒模型压缩算法)
- [训练过程](#训练过程-1)
- [GPU处理器环境运行](#gpu处理器环境运行)
- [评估过程](#评估过程-1)
- [GPU处理器环境运行](#gpu处理器环境运行-1)
- [结果](#结果-3)
- [推理过程](#推理过程-1)
- [导出MindIR](#导出mindir-1)
- [模型描述](#模型描述)
- [性能](#性能)
- [评估性能](#评估性能)
......@@ -278,6 +286,68 @@ bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [DVPP] [DEVICE_ID]
'Accuracy':0.9843
```
# 应用金箍棒模型压缩算法
金箍棒是MindSpore的模型压缩算法集,我们可以在模型训练前应用金箍棒中的模型压缩算法,从而达到压缩模型大小、降低模型推理功耗,或者加速推理过程的目的。
针对Lenet5,金箍棒提供了SimQAT算法,SimQAT是量化感知训练算法,通过引入伪量化节点来训练网络中的某些层的量化参数,从而在部署阶段,模型得以以更小的功耗或者更高的性能进行推理。
## 训练过程
### GPU处理器环境运行
```text
# 训练
cd ./golden_stick/scripts/
# PYTHON_PATH 表示需要应用的算法的'train.py'脚本所在的目录。
bash run_standalone_train_gpu.sh [PYTHON_PATH] [CONFIG_FILE] [DATASET_PATH] [CKPT_TYPE](optional) [CKPT_PATH](optional)
# 训练示例(应用SimQAT算法并从头开始量化训练)
cd ./golden_stick/scripts/
bash run_standalone_train_gpu.sh ../quantization/simqat/ ../quantization/simqat/lenet_mnist_config.yaml /path/to/dataset
# 训练示例(应用SimQAT算法并加载预训练的全精度checkoutpoint,并进行量化训练)
cd ./golden_stick/scripts/
bash run_standalone_train_gpu.sh ../quantization/simqat/ ../quantization/simqat/lenet_mnist_config.yaml /path/to/dataset FP32 /path/to/fp32_ckpt
# 训练示例(应用SimQAT算法并加载上次量化训练的checkoutpoint,继续进行量化训练)
cd ./golden_stick/scripts/
bash run_standalone_train_gpu.sh ../quantization/simqat/ ../quantization/simqat/lenet_mnist_config.yaml /path/to/dataset PRETRAINED /path/to/pretrained_ckpt
```
## 评估过程
### GPU处理器环境运行
```text
# 评估
cd ./golden_stick/scripts/
# PYTHON_PATH 表示需要应用的算法的'eval.py'脚本所在的目录。
bash run_eval_gpu.sh [PYTHON_PATH] [CONFIG_FILE] [DATASET_PATH] [CHECKPOINT_PATH]
```
```text
# 评估示例
cd ./golden_stick/scripts/
bash run_eval_gpu.sh ../quantization/simqat/ ../quantization/simqat/lenet_mnist_config.yaml /path/to/dataset ./checkpoint/lenet-10.ckpt
```
### 结果
评估结果保存在示例路径中,文件夹名为“eval”。您可在此路径下的日志找到如下结果:
- 使用SimQAT算法量化Lenet5,并使用MNIST数据集评估:
```bash
================ {'Accuracy': 0.9907852564102564} ================
```
## 推理过程
### 导出MindIR
当前暂不支持导出MindIR。
## 模型描述
## 性能
......
# Copyright 2022 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Create SimQAT algorithm instance."""
from mindspore_gs import SimulatedQuantizationAwareTraining as SimQAT
def create_simqat():
algo = SimQAT()
algo.set_act_quant_delay(900)
algo.set_weight_quant_delay(900)
algo.set_act_symmetric(False)
algo.set_weight_symmetric(True)
algo.set_act_per_channel(False)
algo.set_weight_per_channel(True)
algo.set_enable_fusion(True)
algo.set_bn_fold(True)
algo.set_one_conv_fold(False)
return algo
# Copyright 2022 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""
######################## eval lenet example ########################
Apply quantization-aware-training algorithms on LeNet5 model and eval accuracy according to model file:
"""
import os
from src.model_utils.config import config
from src.dataset import create_dataset
from src.lenet import LeNet5
import mindspore.nn as nn
from mindspore import context
from mindspore.train.serialization import load_checkpoint, load_param_into_net
from mindspore.train import Model
from mindspore.nn.metrics import Accuracy
from algorithm import create_simqat
def eval_lenet():
print('eval with config: ', config)
if config.device_target != "GPU":
raise NotImplementedError("SimQAT only support running on GPU now!")
if config.mode_name == "GRAPH":
context.set_context(mode=context.GRAPH_MODE, device_target=config.device_target)
else:
context.set_context(mode=context.PYNATIVE_MODE, device_target=config.device_target)
network = LeNet5(config.num_classes)
# apply golden stick algorithm on LeNet5 model
algo = create_simqat()
network = algo.apply(network)
net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
model = Model(network, net_loss, metrics={"Accuracy": Accuracy()})
print("============== Starting Testing ==============")
param_dict = load_checkpoint(config.checkpoint_file_path)
load_param_into_net(network, param_dict)
ds_eval = create_dataset(os.path.join(config.data_path), config.batch_size)
if ds_eval.get_dataset_size() == 0:
raise ValueError("Please check dataset size > 0 and batch_size <= dataset size")
acc = model.eval(ds_eval)
print("============== {} ==============".format(acc))
if __name__ == "__main__":
eval_lenet()
# Builtin Configurations(DO NOT CHANGE THESE CONFIGURATIONS unless you know exactly what you are doing)
enable_modelarts: False
data_url: ""
train_url: ""
checkpoint_url: ""
data_path: "/cache/data"
output_path: "/cache/train"
device_target: GPU
enable_profiling: False
checkpoint_file_path: ""
# ==============================================================================
# Training options
num_classes: 10
lr: 0.01
momentum: 0.9
epoch_size: 10
batch_size: 32
buffer_size: 1000
image_height: 32
image_width: 32
save_checkpoint_steps: 1875
keep_checkpoint_max: 10
device_id: 0
file_name: "lenet-sim-qat"
file_format: "MINDIR"
fp32_ckpt: ""
model_name: lenet
learning_rate: 0.002
dataset_name: 'mnist'
sink_size: -1
dataset_sink_mode: True
save_checkpoint: True
save_checkpoint_epochs: 2
mode_name: "GRAPH"
# lenet acc calculation
result_path: ''
img_path: ''
---
# Config description for each option
data_url: 'Dataset url for obs'
train_url: 'Training output url for obs'
data_path: 'Dataset path for local'
output_path: 'Training output path for local'
device_target: 'Target device type'
enable_profiling: 'Whether enable profiling while training, default: False'
file_name: 'output file name.'
file_format: 'file format'
result_path: "result files path."
img_path: "image file path."
---
device_target: ['GPU']
file_format: ['AIR', 'MINDIR']
# Copyright 2022 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""
######################## train lenet example ########################
train lenet and get network model files(.ckpt) :
"""
import os
from src.model_utils.config import config
from src.dataset import create_dataset
from src.lenet import LeNet5
import mindspore.nn as nn
from mindspore import context
from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor, TimeMonitor
from mindspore.train import Model
from mindspore.nn.metrics import Accuracy
from mindspore.common import set_seed
from mindspore import load_checkpoint, load_param_into_net
from algorithm import create_simqat
set_seed(1)
def train_lenet():
if config.device_target != "GPU":
raise NotImplementedError("SimQAT only support running on GPU now!")
if config.mode_name == "GRAPH":
context.set_context(mode=context.GRAPH_MODE, device_target=config.device_target)
else:
context.set_context(mode=context.PYNATIVE_MODE, device_target=config.device_target)
ds_train = create_dataset(os.path.join(config.data_path), config.batch_size)
if ds_train.get_dataset_size() == 0:
raise ValueError("Please check dataset size > 0 and batch_size <= dataset size")
network = LeNet5(config.num_classes)
if config.fp32_ckpt:
fp32_ckpt = load_checkpoint(config.fp32_ckpt)
load_param_into_net(network, fp32_ckpt)
# apply golden stick algorithm on LeNet5 model
algo = create_simqat()
network = algo.apply(network)
net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
net_opt = nn.Momentum(network.trainable_params(), config.lr, config.momentum)
time_cb = TimeMonitor(data_size=ds_train.get_dataset_size())
config_ck = CheckpointConfig(save_checkpoint_steps=config.save_checkpoint_steps,
keep_checkpoint_max=config.keep_checkpoint_max)
ckpoint_cb = ModelCheckpoint(prefix="checkpoint_lenet", directory="./ckpt", config=config_ck)
context.set_context(enable_graph_kernel=True)
model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()})
print("============== Starting Training ==============")
model.train(config.epoch_size, ds_train, callbacks=[time_cb, ckpoint_cb, LossMonitor()])
if __name__ == "__main__":
train_lenet()
#!/bin/bash
# Copyright 2022 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
# an simple tutorial as follows, more parameters can be setting
CURPATH="$(dirname "$0")"
if [ $# != 4 ]
then
echo "Usage: bash run_eval_gpu.sh [PYTHON_PATH] [CONFIG_FILE] [DATASET_PATH] [CHECKPOINT_PATH]"
echo "PYTHON_PATH represents path to directory of 'eval.py'."
exit 1
fi
get_real_path(){
if [ "${1:0:1}" == "/" ]; then
echo "$1"
else
echo "$(realpath -m $PWD/$1)"
fi
}
PYTHON_PATH=$(get_real_path $1)
CONFIG_FILE=$(get_real_path $2)
DATASET_PATH=$(get_real_path $3)
CKPT_FILE=$(get_real_path $4)
if [ ! -d $PYTHON_PATH ]
then
echo "error: PYTHON_PATH=$PYTHON_PATH is not a directory"
exit 1
fi
if [ ! -f $CONFIG_FILE ]
then
echo "error: CONFIG_FILE=$CONFIG_FILE is not a file"
exit 1
fi
if [ ! -d $DATASET_PATH ]
then
echo "error: DATASET_PATH=$DATASET_PATH is not a directory"
exit 1
fi
if [ ! -f $CKPT_FILE ]
then
echo "error: CKPT_FILE=$CKPT_FILE is not a file"
exit 1
fi
ulimit -u unlimited
export DEVICE_NUM=1
export DEVICE_ID=0
export RANK_SIZE=$DEVICE_NUM
export RANK_ID=0
if [ -d "eval" ];
then
rm -rf ./eval
fi
mkdir ./eval
cp ${PYTHON_PATH}/*.py ./eval
cp -r ${CURPATH}/../../src ./eval
cd ./eval || exit
env > env.log
echo "start evaluation for device $DEVICE_ID"
python eval.py --config_path=$CONFIG_FILE --data_path=$DATASET_PATH --device_target="GPU" --checkpoint_file_path=$CKPT_FILE > log.txt 2>&1 &
cd ..
#!/bin/bash
# Copyright 2022 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
CURPATH="$(dirname "$0")"
# shellcheck source=/dev/null
if [ $# != 3 ] && [ $# != 5 ]; then
echo "Usage: bash run_standalone_train_gpu.sh [PYTHON_PATH] [CONFIG_FILE] [DATASET_PATH] [CKPT_TYPE](optional) [CKPT_PATH](optional)"
echo "Examples:"
echo " Train from the beginning:"
echo " bash run_standalone_train_gpu.sh /path/to/train.py lenet_config.yaml /path/to/dataset"
echo " Train from full precision checkpoint:"
echo " bash run_standalone_train_gpu.sh /path/to/train.py lenet_config.yaml /path/to/dataset FP32 /path/to/fp32_ckpt"
echo " Train from pretrained checkpoint:"
echo " bash run_standalone_train_gpu.sh /path/to/train.py lenet_config.yaml /path/to/dataset PRETRAINED /path/to/pretrained_ckpt"
exit 1
fi
get_real_path(){
if [ "${1:0:1}" == "/" ]; then
echo "$1"
else
echo "$(realpath -m $PWD/$1)"
fi
}
PYTHON_PATH=$(get_real_path $1)
CONFIG_FILE=$(get_real_path $2)
DATASET_PATH=$(get_real_path $3)
if [ ! -d $PYTHON_PATH ]
then
echo "error: PYTHON_PATH=$PYTHON_PATH is not a directory"
exit 1
fi
if [ ! -f $CONFIG_FILE ]
then
echo "error: CONFIG_FILE=$CONFIG_FILE is not a file"
exit 1
fi
if [ ! -d $DATASET_PATH ]
then
echo "error: DATASET_PATH=$DATASET_PATH is not a directory"
exit 1
fi
if [ $# == 5 ]; then
CKPT_TYPE=$4
CKPT_FILE=$(get_real_path $5)
if [ "x$CKPT_TYPE" != "xFP32" ] && [ "x$CKPT_TYPE" != "xPRETRAINED" ]; then
echo "error: CKPT_TYPE=$CKPT_TYPE is not valid, should be FP32 or PRETRAINED"
exit 1
fi
if [ ! -f $CKPT_FILE ]; then
echo "error: CKPT_FILE=$CKPT_FILE is not a file"
exit 1
fi
fi
ulimit -u unlimited
export DEVICE_NUM=1
export RANK_SIZE=1
if [ -d "train" ];
then
rm -rf ./train
fi
mkdir ./train
cp ${PYTHON_PATH}/*.py ./train
cp -r ${CURPATH}/../../src ./train
cd ./train || exit
if [ "x$CKPT_TYPE" == "xFP32" ]; then
python train.py --config_path=$CONFIG_FILE --device_target="GPU" --data_path=$DATASET_PATH \
--fp32_ckpt=$CKPT_FILE &> log &
elif [ "x$CKPT_TYPE" == "xPRETRAINED" ]; then
python train.py --config_path=$CONFIG_FILE --device_target="GPU" --data_path=$DATASET_PATH \
--pre_trained=$CKPT_FILE &> log &
else
python train.py --config_path=$CONFIG_FILE --device_target="GPU" --data_path=$DATASET_PATH &> log &
fi
......@@ -30,15 +30,13 @@
- [Infer on Ascend310](#infer-on-ascend310)
- [result](#result-2)
- [Apply algorithm in MindSpore Golden Stick](#apply-algorithm-in-mindspore-golden-stick)
- [Apply SimQAT algorithm](#apply-simqat-algorithm)
- [Training Process](#Training Process-1)
- [Running on GPU](#running-on-gpu-2)
- [Resume Process](#resume-process-1)
- [Evaluation Process](#evaluation-process-1)
- [Running on GPU](#running-on-gpu-3)
- [Result](#resutl-3)
- [Inference Process](#inference-process-1)
- [Export MindIR](#export-mindir-1)
- [Training Process](#Training Process-1)
- [Running on GPU](#running-on-gpu-2)
- [Evaluation Process](#evaluation-process-1)
- [Running on GPU](#running-on-gpu-3)
- [Result](#resutl-3)
- [Inference Process](#inference-process-1)
- [Export MindIR](#export-mindir-1)
- [Model Description](#model-description)
- [Performance](#performance)
- [Evaluation Performance](#evaluation-performance)
......@@ -811,41 +809,52 @@ Total data: 50000, top1 accuracy: 0.76844, top5 accuracy: 0.93522.
# Apply algorithm in MindSpore Golden Stick
## Apply SimQAT algorithm
MindSpore Golden Stick is a compression algorithm set for MindSpore. We usually apply algorithm in Golden Stick before training for smaller model size, lower power consuming or faster inference process.
MindSpore Golden Stick provides SimQAT algorithm for ResNet50. SimQAT is a quantization-aware training algorithm that trains the quantization parameters of certain layers in the network by introducing fake-quantization nodes, so that the model can perform inference with less power consumption or higher performance during the deployment phase.
SimQAT is a quantization-aware training algorithm that trains the quantization parameters of certain layers in the network by introducing pseudo-quantization nodes, so that the model can perform inference with less power consumption or higher performance during the deployment phase.
## Training Process
### Training Process
#### Running on GPU
### Running on GPU
```text
# distributed training
cd ./golden_stick/scripts/
# PYTHON_PATH represents path to directory of 'train.py'.
bash run_distribute_train_gpu.sh [PYTHON_PATH] [CONFIG_FILE] [DATASET_PATH] [FP32_CKPT_PATH]
bash run_distribute_train_gpu.sh [PYTHON_PATH] [CONFIG_FILE] [DATASET_PATH] [CKPT_TYPE](optional) [CKPT_PATH](optional)
# distributed training example
# distributed training example, apply SimQAT and train from beginning
cd ./golden_stick/scripts/
bash run_distribute_train_gpu.sh ../quantization/simqat/ ../quantization/simqat/resnet50_cifar10_config.yaml ./cifar10/train/
# distributed training example, apply SimQAT and train from full precision checkpoint
cd ./golden_stick/scripts/
bash run_distribute_train_gpu.sh ../quantization/simqat/ ../quantization/simqat/resnet50_cifar10_config.yaml /path/to/dataset FP32 /path/to/fp32_ckpt
# distributed training example, apply SimQAT and train from pretrained checkpoint
cd ./golden_stick/scripts/
bash run_distribute_train_gpu.sh ../quantization/simqat/ ../quantization/simqat/resnet50_cifar10_config.yaml ./cifar10/train/ ./checkpoint/resnet-90.ckpt
bash run_distribute_train_gpu.sh ../quantization/simqat/ ../quantization/simqat/resnet50_cifar10_config.yaml /path/to/dataset PRETRAINED /path/to/pretrained_ckpt
# standalone training
cd ./golden_stick/scripts/
# PYTHON_PATH represents path to directory of 'train.py'.
bash run_standalone_train_gpu.sh [PYTHON_PATH] [CONFIG_FILE] [DATASET_PATH] [FP32_CKPT_PATH]
bash run_standalone_train_gpu.sh [PYTHON_PATH] [CONFIG_FILE] [DATASET_PATH] [CKPT_TYPE](optional) [CKPT_PATH](optional)
# standalone training example
# standalone training example, apply SimQAT and train from beginning
cd ./golden_stick/scripts/
bash run_standalone_train_gpu.sh ../quantization/simqat/ ../quantization/simqat/resnet50_cifar10_config.yaml ./cifar10/train/ ./checkpoint/resnet-90.ckpt
```
bash run_standalone_train_gpu.sh ../quantization/simqat/ ../quantization/simqat/resnet50_cifar10_config.yaml ./cifar10/train/
### Resume Process
# standalone training example, apply SimQAT and train from full precision checkpoint
cd ./golden_stick/scripts/
bash run_standalone_train_gpu.sh ../quantization/simqat/ ../quantization/simqat/resnet50_cifar10_config.yaml /path/to/dataset FP32 /path/to/fp32_ckpt
SimQAT algorithm is not support to resume training process now.
# standalone training example, apply SimQAT and train from pretrained checkpoint
cd ./golden_stick/scripts/
bash run_standalone_train_gpu.sh ../quantization/simqat/ ../quantization/simqat/resnet50_cifar10_config.yaml /path/to/dataset PRETRAINED /path/to/pretrained_ckpt
```
### Evaluation Process
## Evaluation Process
#### Running on GPU
### Running on GPU
```text
# evaluation
......@@ -858,19 +867,19 @@ cd ./golden_stick/scripts/
bash run_eval_gpu.sh ../quantization/simqat/ ../quantization/simqat/resnet50_cifar10_config.yaml ./cifar10/train/ ./checkpoint/resnet-90.ckpt
```
#### Result
### Result
Evaluation result will be stored in the example path, whose folder name is "eval". Under this, you can find result like the following in log.
- Apply SumQAT on ResNet50, and evaluating with CIFAR-10 dataset
- Apply SimQAT on ResNet50, and evaluating with CIFAR-10 dataset
```bash
result:{'top_1_accuracy': 0.9354967948717948, 'top_5_accuracy': 0.9981971153846154} ckpt=~/resnet50_cifar10/train_parallel0/resnet-180_195.ckpt
```
### Inference Process
## Inference Process
#### Export MindIR
### Export MindIR
Not support exporting MindIR now.
......
......@@ -32,15 +32,14 @@
- [在Ascend310执行推理](#在ascend310执行推理)
- [结果](#结果-2)
- [应用金箍棒模型压缩算法](#应用金箍棒模型压缩算法)
- [应用SimQAT算法](#应用simqat算法)
- [训练过程](#训练过程-1)
- [GPU处理器环境运行](#gpu处理器环境运行-2)
- [续训过程](#续训过程-1)
- [评估过程](#评估过程-1)
- [GPU处理器环境运行](#gpu处理器环境运行-3)
- [结果](#结果-3)
- [推理过程](#推理过程-1)
- [导出MindIR](#导出mindir-1)
- [训练过程](#训练过程-1)
- [GPU处理器环境运行](#gpu处理器环境运行-2)
- [续训过程](#续训过程-1)
- [评估过程](#评估过程-1)
- [GPU处理器环境运行](#gpu处理器环境运行-3)
- [结果](#结果-3)
- [推理过程](#推理过程-1)
- [导出MindIR](#导出mindir-1)
- [模型描述](#模型描述)
- [性能](#性能)
- [评估性能](#评估性能)
......@@ -770,46 +769,58 @@ Total data: 50000, top1 accuracy: 0.76844, top5 accuracy: 0.93522.
# 应用金箍棒模型压缩算法
## 应用SimQAT算法
金箍棒是MindSpore的模型压缩算法集,我们可以在模型训练前应用金箍棒中的模型压缩算法,从而达到压缩模型大小、降低模型推理功耗,或者加速推理过程的目的。
SimQAT是一种量化感知训练算法,通过引入伪量化节点来训练网络中的某些层的量化参数,从而在部署阶段,模型得以以更小的功耗或者更高的性能进行推理。
针对ResNet50,金箍棒提供了SimQAT算法,SimQAT是一种量化感知训练算法,通过引入伪量化节点来训练网络中的某些层的量化参数,从而在部署阶段,模型得以以更小的功耗或者更高的性能进行推理。
### 训练过程
## 训练过程
#### GPU处理器环境运行
### GPU处理器环境运行
```text
# 分布式训练
cd ./golden_stick/scripts/
# PYTHON_PATH 表示'train.py'脚本所在的目录。
bash run_distribute_train_gpu.sh [PYTHON_PATH] [CONFIG_FILE] [DATASET_PATH] [FP32_CKPT_PATH]
# PYTHON_PATH 表示需要应用的算法的'train.py'脚本所在的目录。
bash run_distribute_train_gpu.sh [PYTHON_PATH] [CONFIG_FILE] [DATASET_PATH] [CKPT_TYPE](optional) [CKPT_PATH](optional)
# 分布式训练示例
# 分布式训练示例(应用SimQAT算法并从头开始量化训练)
cd ./golden_stick/scripts/
bash run_distribute_train_gpu.sh ../quantization/simqat/ ../quantization/simqat/resnet50_cifar10_config.yaml ./cifar10/train/
# 分布式训练示例(应用SimQAT算法并加载预训练的全精度checkoutpoint,进行量化训练)
cd ./golden_stick/scripts/
bash run_distribute_train_gpu.sh ../quantization/simqat/ ../quantization/simqat/resnet50_cifar10_config.yaml /path/to/dataset FP32 /path/to/fp32_ckpt
# 分布式训练示例(应用SimQAT算法并加载之前训练的checkoutpoint,继续进行量化训练)
cd ./golden_stick/scripts/
bash run_distribute_train_gpu.sh ../quantization/simqat/ ../quantization/simqat/resnet50_cifar10_config.yaml ./cifar10/train/ ./checkpoint/resnet-90.ckpt
bash run_distribute_train_gpu.sh ../quantization/simqat/ ../quantization/simqat/resnet50_cifar10_config.yaml /path/to/dataset PRETRAINED /path/to/pretrained_ckpt
# 单机训练
cd ./golden_stick/scripts/
# PYTHON_PATH 表示'train.py'脚本所在的目录。
bash run_standalone_train_gpu.sh [PYTHON_PATH] [CONFIG_FILE] [DATASET_PATH] [FP32_CKPT_PATH]
# PYTHON_PATH 表示需要应用的算法的'train.py'脚本所在的目录。
bash run_standalone_train_gpu.sh [PYTHON_PATH] [CONFIG_FILE] [DATASET_PATH] [CKPT_TYPE](optional) [CKPT_PATH](optional)
# 单机训练示例
# 单机训练示例(应用SimQAT算法并从头开始量化训练)
cd ./golden_stick/scripts/
bash run_standalone_train_gpu.sh ../quantization/simqat/ ../quantization/simqat/resnet50_cifar10_config.yaml ./cifar10/train/ ./checkpoint/resnet-90.ckpt
```
bash run_standalone_train_gpu.sh ../quantization/simqat/ ../quantization/simqat/resnet50_cifar10_config.yaml ./cifar10/train/
### 续训过程
# 单机训练示例(应用SimQAT算法并加载预训练的全精度checkoutpoint,并进行量化训练)
cd ./golden_stick/scripts/
bash run_standalone_train_gpu.sh ../quantization/simqat/ ../quantization/simqat/resnet50_cifar10_config.yaml /path/to/dataset FP32 /path/to/fp32_ckpt
SimQAT算法当前暂不支持续训。
# 单机训练示例(应用SimQAT算法并加载上次量化训练的checkoutpoint,继续进行量化训练)
cd ./golden_stick/scripts/
bash run_standalone_train_gpu.sh ../quantization/simqat/ ../quantization/simqat/resnet50_cifar10_config.yaml /path/to/dataset PRETRAINED /path/to/pretrained_ckpt
```
### 评估过程
## 评估过程
#### GPU处理器环境运行
### GPU处理器环境运行
```text
# 评估
cd ./golden_stick/scripts/
# PYTHON_PATH 表示'eval.py'脚本所在的目录。
# PYTHON_PATH 表示需要应用的算法的'eval.py'脚本所在的目录。
bash run_eval_gpu.sh [PYTHON_PATH] [CONFIG_FILE] [DATASET_PATH] [CHECKPOINT_PATH]
```
......@@ -819,7 +830,7 @@ cd ./golden_stick/scripts/
bash run_eval_gpu.sh ../quantization/simqat/ ../quantization/simqat/resnet50_cifar10_config.yaml ./cifar10/train/ ./checkpoint/resnet-90.ckpt
```
#### 结果
### 结果
评估结果保存在示例路径中,文件夹名为“eval”。您可在此路径下的日志找到如下结果:
......@@ -829,9 +840,9 @@ bash run_eval_gpu.sh ../quantization/simqat/ ../quantization/simqat/resnet50_cif
result:{'top_1_accuracy': 0.9354967948717948, 'top_5_accuracy': 0.9981971153846154} ckpt=~/resnet50_cifar10/train_parallel0/resnet-180_195.ckpt
```
### 推理过程
## 推理过程
#### 导出MindIR
### 导出MindIR
当前暂不支持导出MindIR。
......
......@@ -9,7 +9,6 @@ run_distribute: False
enable_profiling: False
data_path: "/cache/data"
output_path: "/cache/train"
load_path: "/cache/checkpoint_path/"
device_target: "Ascend"
checkpoint_path: "./checkpoint/"
checkpoint_file_path: ""
......@@ -17,7 +16,6 @@ checkpoint_file_path: ""
# ==============================================================================
# Training options
optimizer: "Momentum"
infer_label: ""
class_num: 10
batch_size: 32
loss_scale: 1024
......@@ -40,15 +38,8 @@ dataset: "cifar10"
device_num: 1
pre_trained: ""
fp32_ckpt: ""
run_eval: False
eval_dataset_path: ""
parameter_server: False
filter_weight: False
save_best_ckpt: True
eval_start_epoch: 40
eval_interval: 1
enable_cache: False
cache_session_id: ""
mode_name: "GRAPH"
boost_mode: "O0"
conv_init: "XavierUniform"
......@@ -76,20 +67,13 @@ save_graphs_path: "./graphs"
has_trained_epoch: 0
has_trained_step: 0
# postprocess resnet inference
result_path: ''
label_path: ''
---
# Help description for each configuration
enable_modelarts: "Whether training on modelarts, default: False"
data_url: "Dataset url for obs"
checkpoint_url: "The location of checkpoint for obs"
data_path: "Dataset path for local"
output_path: "Training output path for local"
load_path: "The location of checkpoint for obs"
device_target: "Target device type, available: [Ascend, GPU, CPU]"
enable_profiling: "Whether enable profiling while training, default: False"
num_classes: "Class for dataset"
batch_size: "Batch size for training and evaluation"
epoch_size: "Total training epochs."
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment