diff --git a/docs/en/02-how-to-run/quantize_model.md b/docs/en/02-how-to-run/quantize_model.md index b6022cdf8..a13487af8 100644 --- a/docs/en/02-how-to-run/quantize_model.md +++ b/docs/en/02-how-to-run/quantize_model.md @@ -8,8 +8,8 @@ The fixed-point model has many advantages over the fp32 model: - Benefit from the smaller model, the Cache hit rate is improved and inference would be faster - Chips tend to have corresponding fixed-point acceleration instructions which are faster and less energy consumed (int8 on a common CPU requires only about 10% of energy) -The size of the installation package and the heat generation are the key indicators of the mobile terminal evaluation APP; -On the server side, quantization means that you can maintain the same QPS and improve model precision in exchange for improved accuracy. +APK file size and heat generation are key indicators while evaluating mobile APP; +On server side, quantization means that you can increase model size in exchange for precision and keep the same QPS. ## Post training quantization scheme @@ -21,7 +21,7 @@ Taking ncnn backend as an example, the complete workflow is as follows: mmdeploy generates quantization table based on static graph (onnx) and uses backend tools to convert fp32 model to fixed point. -Currently mmdeploy support ncnn with PTQ. +mmdeploy currently support ncnn with PTQ. ## How to convert model @@ -38,10 +38,15 @@ Back in mmdeploy, enable quantization with the option 'tools/deploy.py --quant'. ```bash cd /path/to/mmdeploy -export MODEL_PATH=/path/to/mmclassification/configs/resnet/resnet18_8xb16_cifar10.py -export MODEL_CONFIG=https://download.openmmlab.com/mmclassification/v0/resnet/resnet18_b16x8_cifar10_20210528-bd6371c8.pth -python3 tools/deploy.py configs/mmcls/classification_ncnn-int8_static.py ${MODEL_CONFIG} ${MODEL_PATH} /path/to/self-test.png --work-dir work_dir --device cpu --quant --quant-image-dir /path/to/images +export MODEL_CONFIG=/home/rg/konghuanjun/mmclassification/configs/resnet/resnet18_8xb32_in1k.py +export MODEL_PATH=https://download.openmmlab.com/mmclassification/v0/resnet/resnet18_8xb32_in1k_20210831-fbbb1da6.pth + +# get some imagenet sample images +git clone https://github.com/nihui/imagenet-sample-images --depth=1 + +# quantize +python3 tools/deploy.py configs/mmcls/classification_ncnn-int8_static.py ${MODEL_CONFIG} ${MODEL_PATH} /path/to/self-test.png --work-dir work_dir --device cpu --quant --quant-image-dir /path/to/imagenet-sample-images ... ``` @@ -56,7 +61,7 @@ Description Calibration set is used to calculate quantization layer parameters. Some DFQ (Data Free Quantization) methods do not even require a dataset. -- Create a new folder, just put in the picture (no directory structure required, no negative example required, no filename format required) +- Create a folder, just put in some images (no directory structure, no negative example, no special filename format) - The image needs to be the data comes from real scenario otherwise the accuracy would be drop - You can not quantize model with test dataset | Type | Train dataset | Validation dataset | Test dataset | Calibration dataset | diff --git a/docs/zh_cn/02-how-to-run/quantize_model.md b/docs/zh_cn/02-how-to-run/quantize_model.md index 1c2d32bb5..499f910a8 100644 --- a/docs/zh_cn/02-how-to-run/quantize_model.md +++ b/docs/zh_cn/02-how-to-run/quantize_model.md @@ -40,7 +40,11 @@ cd /path/to/mmdeploy export MODEL_CONFIG=/path/to/mmclassification/configs/resnet/resnet18_8xb16_cifar10.py export MODEL_PATH=https://download.openmmlab.com/mmclassification/v0/resnet/resnet18_b16x8_cifar10_20210528-bd6371c8.pth -python3 tools/deploy.py configs/mmcls/classification_ncnn-int8_static.py ${MODEL_CONFIG} ${MODEL_PATH} /path/to/self-test.png --work-dir work_dir --device cpu --quant --quant-image-dir /path/to/images +# 找一些 imagenet 样例图 +git clone https://github.com/nihui/imagenet-sample-images --depth=1 + +# 量化模型 +python3 tools/deploy.py configs/mmcls/classification_ncnn-int8_static.py ${MODEL_CONFIG} ${MODEL_PATH} /path/to/self-test.png --work-dir work_dir --device cpu --quant --quant-image-dir /path/to/imagenet-sample-images ... ``` diff --git a/tools/scripts/build_ubuntu_x64_ncnn.py b/tools/scripts/build_ubuntu_x64_ncnn.py index 8fff3dea2..f50e4b90e 100644 --- a/tools/scripts/build_ubuntu_x64_ncnn.py +++ b/tools/scripts/build_ubuntu_x64_ncnn.py @@ -2,6 +2,7 @@ import os import sys import time +from pathlib import Path from ubuntu_utils import cmd_result, ensure_base_env, get_job @@ -175,7 +176,7 @@ def main(): if install_mmdeploy(work_dir, dep_dir, ncnn_cmake_dir) != 0: return -1 - if os.path.exists('~/mmdeploy.env'): + if os.path.exists(Path('~/mmdeploy.env').expanduser()): print('Please source ~/mmdeploy.env to setup your env !') os.system('cat ~/mmdeploy.env') diff --git a/tools/scripts/build_ubuntu_x64_ort.py b/tools/scripts/build_ubuntu_x64_ort.py index f3a9c3b99..d41c22c38 100644 --- a/tools/scripts/build_ubuntu_x64_ort.py +++ b/tools/scripts/build_ubuntu_x64_ort.py @@ -2,6 +2,7 @@ import os import sys import time +from pathlib import Path from ubuntu_utils import ensure_base_env, get_job @@ -94,7 +95,7 @@ def main(): if install_mmdeploy(work_dir, ort_dir) != 0: return -1 - if os.path.exists('~/mmdeploy.env'): + if os.path.exists(Path('~/mmdeploy.env').expanduser()): print('Please source ~/mmdeploy.env to setup your env !') os.system('cat ~/mmdeploy.env') diff --git a/tools/scripts/build_ubuntu_x64_pplnn.py b/tools/scripts/build_ubuntu_x64_pplnn.py index 327b96187..5c46c0965 100644 --- a/tools/scripts/build_ubuntu_x64_pplnn.py +++ b/tools/scripts/build_ubuntu_x64_pplnn.py @@ -2,6 +2,7 @@ import os import sys import time +from pathlib import Path from ubuntu_utils import cmd_result, ensure_base_env, get_job @@ -65,7 +66,7 @@ def install_pplnn(dep_dir, build_cuda): os.system('cd python/package && ./build.sh') os.system( - 'cd /tmp/pyppl-package/dist && python3 -m pip install pyppl*.whl --force-reinstall' # noqa: E501 + 'cd /tmp/pyppl-package/dist && python3 -m pip install pyppl*.whl --force-reinstall --user' # noqa: E501 ) pplnn_cmake_dir = os.path.join(pplnn_dir, @@ -150,7 +151,7 @@ def main(): build_cuda) != 0: return -1 - if os.path.exists('~/mmdeploy.env'): + if os.path.exists(Path('~/mmdeploy.env').expanduser()): print('Please source ~/mmdeploy.env to setup your env !') os.system('cat ~/mmdeploy.env') diff --git a/tools/scripts/build_ubuntu_x64_torchscript.py b/tools/scripts/build_ubuntu_x64_torchscript.py index 5575eec71..7c68a599c 100644 --- a/tools/scripts/build_ubuntu_x64_torchscript.py +++ b/tools/scripts/build_ubuntu_x64_torchscript.py @@ -2,6 +2,7 @@ import os import sys import time +from pathlib import Path from ubuntu_utils import (cmd_result, cu_version_name, ensure_base_env, get_job, pytorch_version) @@ -118,7 +119,7 @@ def main(): if install_mmdeploy(work_dir, libtorch_dir) != 0: return -1 - if os.path.exists('~/mmdeploy.env'): + if os.path.exists(Path('~/mmdeploy.env').expanduser()): print('Please source ~/mmdeploy.env to setup your env !') os.system('cat ~/mmdeploy.env') diff --git a/tools/scripts/ubuntu_cross_build_aarch64.sh b/tools/scripts/ubuntu_cross_build_aarch64.sh index faedb794f..01d08e0c1 100755 --- a/tools/scripts/ubuntu_cross_build_aarch64.sh +++ b/tools/scripts/ubuntu_cross_build_aarch64.sh @@ -33,7 +33,7 @@ build_ocv() { fi cd opencv/platforms/linux/cross_build_aarch64 rm -rf CMakeCache.txt - cmake ../../.. -DCMAKE_INSTALL_PREFIX=/tmp/ocv-aarch64 -DCMAKE_TOOLCHAIN_FILE=../aarch64-gnu.toolchain.cmake + cmake ../../.. -DBUILD_TIFF=ON -DCMAKE_INSTALL_PREFIX=/tmp/ocv-aarch64 -DCMAKE_TOOLCHAIN_FILE=../aarch64-gnu.toolchain.cmake good_nproc jobs=$? make -j${jobs} diff --git a/tools/scripts/ubuntu_cross_build_rknn.sh b/tools/scripts/ubuntu_cross_build_rknn.sh index 59ba252ed..f3f3f5501 100644 --- a/tools/scripts/ubuntu_cross_build_rknn.sh +++ b/tools/scripts/ubuntu_cross_build_rknn.sh @@ -60,7 +60,7 @@ build_ocv_arm_gnueabi() { cd opencv/build_arm_gnueabi rm -rf CMakeCache.txt cmake .. -DCMAKE_INSTALL_PREFIX=install -DCMAKE_TOOLCHAIN_FILE=../platforms/linux/arm-gnueabi.toolchain.cmake \ - -DBUILD_PERF_TESTS=OFF -DBUILD_SHARED_LIBS=OFF -DBUILD_TESTS=OFF -DCMAKE_BUILD_TYPE=Release + -DBUILD_TIFF=ON -DBUILD_PERF_TESTS=OFF -DBUILD_SHARED_LIBS=OFF -DBUILD_TESTS=OFF -DCMAKE_BUILD_TYPE=Release good_nproc jobs=$? make -j${jobs} && make install