| @@ -43,9 +43,7 @@ Dataset use: ImageNet | |||
| ```python | |||
| ├── mobilenetv2_quant | |||
| ├── Readme.md | |||
| ├── scripts | |||
| │ ├──run_train.sh | |||
| │ ├──run_infer.sh | |||
| ├── scripts | |||
| │ ├──run_train_quant.sh | |||
| │ ├──run_infer_quant.sh | |||
| ├── src | |||
| @@ -56,49 +54,7 @@ Dataset use: ImageNet | |||
| │ ├──mobilenetV2.py | |||
| ├── train.py | |||
| ├── eval.py | |||
| ``` | |||
| ## Training process | |||
| ### Train MobileNetV2 model | |||
| Train a MindSpore fusion MobileNetV2 model for ImageNet, like: | |||
| - Ascend: sh run_train.sh Ascend [DEVICE_NUM] [SERVER_IP(x.x.x.x)] [VISIABLE_DEVICES(0,1,2,3,4,5,6,7)] [DATASET_PATH] [CKPT_PATH] | |||
| - GPU: sh run_trian.sh GPU [DEVICE_NUM] [VISIABLE_DEVICES(0,1,2,3,4,5,6,7)] [DATASET_PATH] | |||
| You can just run this command instead. | |||
| ``` bash | |||
| >>> Ascend: sh run_train.sh Ascend 4 192.168.0.1 0,1,2,3 ~/imagenet/train/ ~/mobilenet.ckpt | |||
| >>> GPU: sh run_train.sh GPU 8 0,1,2,3,4,5,6,7 ~/imagenet/train/ | |||
| ``` | |||
| Training result will be stored in the example path. Checkpoints will be stored at `. /checkpoint` by default, and training log will be redirected to `./train/train.log` like followings. | |||
| ``` | |||
| >>> epoch: [ 0/200], step:[ 624/ 625], loss:[5.258/5.258], time:[140412.236], lr:[0.100] | |||
| >>> epoch time: 140522.500, per step time: 224.836, avg loss: 5.258 | |||
| >>> epoch: [ 1/200], step:[ 624/ 625], loss:[3.917/3.917], time:[138221.250], lr:[0.200] | |||
| >>> epoch time: 138331.250, per step time: 221.330, avg loss: 3.917 | |||
| ``` | |||
| ### Evaluate MobileNetV2 model | |||
| Evaluate a MindSpore fusion MobileNetV2 model for ImageNet, like: | |||
| - sh run_infer.sh Ascend [DATASET_PATH] [CHECKPOINT_PATH] | |||
| You can just run this command instead. | |||
| ``` bash | |||
| >>> sh run_infer.sh Ascend ~/imagenet/val/ ~/train/mobilenet-200_625.ckpt | |||
| ``` | |||
| Inference result will be stored in the example path, you can find result like the followings in `val.log`. | |||
| ``` | |||
| >>> result: {'acc': 0.71976314102564111} ckpt=/path/to/checkpoint/mobilenet-200_625.ckpt | |||
| ``` | |||
| ### Fine-tune for quantization aware training | |||
| @@ -1,53 +0,0 @@ | |||
| #!/usr/bin/env bash | |||
| # Copyright 2020 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. | |||
| # ============================================================================ | |||
| if [ $# != 3 ] | |||
| then | |||
| echo "Ascend: sh run_infer.sh [PLATFORM] [DATASET_PATH] [CHECKPOINT_PATH]" | |||
| exit 1 | |||
| fi | |||
| # check dataset path | |||
| if [ ! -d $2 ] && [ ! -f $2 ] | |||
| then | |||
| echo "error: DATASET_PATH=$2 is not a directory or file" | |||
| exit 1 | |||
| fi | |||
| # check checkpoint file | |||
| if [ ! -f $3 ] | |||
| then | |||
| echo "error: CHECKPOINT_PATH=$3 is not a file" | |||
| exit 1 | |||
| fi | |||
| # set environment | |||
| BASEPATH=$(cd "`dirname $0`" || exit; pwd) | |||
| export DEVICE_ID=0 | |||
| export RANK_ID=0 | |||
| export RANK_SIZE=1 | |||
| if [ -d "../eval" ]; | |||
| then | |||
| rm -rf ../eval | |||
| fi | |||
| mkdir ../eval | |||
| cd ../eval || exit | |||
| # launch | |||
| python ${BASEPATH}/../eval.py \ | |||
| --device_target=$1 \ | |||
| --dataset_path=$2 \ | |||
| --checkpoint_path=$3 \ | |||
| &> infer.log & # dataset val folder path | |||
| @@ -1,96 +0,0 @@ | |||
| #!/usr/bin/env bash | |||
| # Copyright 2020 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. | |||
| # ============================================================================ | |||
| run_ascend() | |||
| { | |||
| if [ $2 -lt 1 ] && [ $2 -gt 8 ] | |||
| then | |||
| echo "error: DEVICE_NUM=$2 is not in (1-9)" | |||
| exit 1 | |||
| fi | |||
| if [ ! -d $5 ] && [ ! -f $5 ] | |||
| then | |||
| echo "error: DATASET_PATH=$5 is not a directory or file" | |||
| exit 1 | |||
| fi | |||
| BASEPATH=$(cd "`dirname $0`" || exit; pwd) | |||
| export PYTHONPATH=${BASEPATH}:$PYTHONPATH | |||
| if [ -d "../train" ]; | |||
| then | |||
| rm -rf ../train | |||
| fi | |||
| mkdir ../train | |||
| cd ../train || exit | |||
| python ${BASEPATH}/../src/launch.py \ | |||
| --nproc_per_node=$2 \ | |||
| --visible_devices=$4 \ | |||
| --server_id=$3 \ | |||
| --training_script=${BASEPATH}/../train.py \ | |||
| --dataset_path=$5 \ | |||
| --pre_trained=$6 \ | |||
| --device_target=$1 &> train.log & # dataset train folder | |||
| } | |||
| run_gpu() | |||
| { | |||
| if [ $2 -lt 1 ] && [ $2 -gt 8 ] | |||
| then | |||
| echo "error: DEVICE_NUM=$2 is not in (1-8)" | |||
| exit 1 | |||
| fi | |||
| if [ ! -d $4 ] | |||
| then | |||
| echo "error: DATASET_PATH=$4 is not a directory" | |||
| exit 1 | |||
| fi | |||
| BASEPATH=$(cd "`dirname $0`" || exit; pwd) | |||
| export PYTHONPATH=${BASEPATH}:$PYTHONPATH | |||
| if [ -d "../train" ]; | |||
| then | |||
| rm -rf ../train | |||
| fi | |||
| mkdir ../train | |||
| cd ../train || exit | |||
| export CUDA_VISIBLE_DEVICES="$3" | |||
| mpirun -n $2 --allow-run-as-root \ | |||
| python ${BASEPATH}/../train.py \ | |||
| --dataset_path=$4 \ | |||
| --device_target=$1 \ | |||
| &> ../train.log & # dataset train folder | |||
| } | |||
| if [ $# -gt 6 ] || [ $# -lt 4 ] | |||
| then | |||
| echo "Usage:\n \ | |||
| Ascend: sh run_train.sh Ascend [DEVICE_NUM] [SERVER_IP(x.x.x.x)] [VISIABLE_DEVICES(0,1,2,3,4,5,6,7)] [DATASET_PATH] [CKPT_PATH]\n \ | |||
| GPU: sh run_train.sh GPU [DEVICE_NUM] [VISIABLE_DEVICES(0,1,2,3,4,5,6,7)] [DATASET_PATH]\n \ | |||
| " | |||
| exit 1 | |||
| fi | |||
| if [ $1 = "Ascend" ] ; then | |||
| run_ascend "$@" | |||
| elif [ $1 = "GPU" ] ; then | |||
| run_gpu "$@" | |||
| else | |||
| echo "Unsupported device target." | |||
| fi; | |||
| @@ -169,7 +169,7 @@ def train_on_gpu(): | |||
| network = quant.convert_quant_network(network, | |||
| bn_fold=True, | |||
| per_channel=[True, False], | |||
| symmetric=[True, True], | |||
| symmetric=[True, False], | |||
| freeze_bn=1000000, | |||
| quant_delay=step_size * 2) | |||