improvement(scripts): cross build aarch64 (#1126)

* udpate

* update

* CI(scripts): add auto cross build aarch64

* docs(scripts): add zh_cn doc

* docs(scripts): update

* docs(scripts): update

* fix(tools): update

* docs(zh_cn): update

* fix(scripts): remove gcc-7

* docs(scripts): update result

* udpate

* fix(tools): remove useless option

* docs(en): typo

* Update cross_build_aarch64.md

* Update cross_build_aarch64.md

* fix(tools/scripts): review advices

* fix(tools/scripts): update

* fix(cmake): remove useless option

* Update aarch64-linux-gnu.cmake
pull/1111/head^2
tpoisonooo 2022-10-17 11:15:29 +08:00 committed by GitHub
parent f8551b0316
commit ace44ae9d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 474 additions and 95 deletions

View File

@ -88,6 +88,23 @@ jobs:
ls -lah coverage.info
cp coverage.info ../
cross_build_aarch64:
runs-on: ubuntu-20.04
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
submodules: 'recursive'
- name: update
run: sudo apt update
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: gcc-multilib
run: |
sh -x tools/scripts/ubuntu_cross_build_aarch64.sh
build_cuda102:
runs-on: ubuntu-18.04
container:

View File

@ -87,6 +87,7 @@ Please read [getting_started](docs/en/get_started.md) for the basic usage of MMD
- [Build for Android](docs/en/01-how-to-build/android.md)
- [Build for Jetson](docs/en/01-how-to-build/jetsons.md)
- [Build for SNPE](docs/en/01-how-to-build/snpe.md)
- [Cross Build for aarch64](docs/en/01-how-to-build/cross_build_ncnn_aarch64.md)
- User Guide
- [How to convert model](docs/en/02-how-to-run/convert_model.md)
- [How to write config](docs/en/02-how-to-run/write_config.md)

View File

@ -86,6 +86,7 @@ MMDeploy 是 [OpenMMLab](https://openmmlab.com/) 模型部署工具箱,**为
- [Build for Android](docs/zh_cn/01-how-to-build/android.md)
- [Build for Jetson](docs/zh_cn/01-how-to-build/jetsons.md)
- [Build for SNPE](docs/zh_cn/01-how-to-build/snpe.md)
- [Cross Build for aarch64](docs/zh_cn/01-how-to-build/cross_build_ncnn_aarch64.md)
- 使用
- [把模型转换到推理 Backend](docs/zh_cn/02-how-to-run/convert_model.md)
- [配置转换参数](docs/zh_cn/02-how-to-run/write_config.md)

View File

@ -0,0 +1,17 @@
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR aarch64)
set(CMAKE_C_COMPILER "aarch64-linux-gnu-gcc")
set(CMAKE_CXX_COMPILER "aarch64-linux-gnu-g++")
set(CMAKE_LINKER "aarch64-linux-gnu-ld")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_C_FLAGS "-march=armv8-a")
set(CMAKE_CXX_FLAGS "-march=armv8-a")
# cache flags
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}" CACHE STRING "c flags")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" CACHE STRING "c++ flags")

View File

@ -156,7 +156,7 @@ bool SubgraphMatcher::SubgraphMatcherImpl::matchAttributes(const Node* n1, Node*
n1is = n1->is(attr_name);
n2is = n2->is(attr_name);
if (n1is.size() != n2is.size()) return false;
for (int i = 0; i < n1is.size(); ++i) {
for (size_t i = 0; i < n1is.size(); ++i) {
if (n1is[i] != n2is[i]) return false;
}
break;
@ -164,7 +164,7 @@ bool SubgraphMatcher::SubgraphMatcherImpl::matchAttributes(const Node* n1, Node*
n1fs = n1->fs(attr_name);
n2fs = n2->fs(attr_name);
if (n1fs.size() != n2fs.size()) return false;
for (int i = 0; i < n1fs.size(); ++i) {
for (size_t i = 0; i < n1fs.size(); ++i) {
if (n1fs[i] != n2fs[i]) return false;
}
break;

View File

@ -2,7 +2,13 @@
Through user investigation, we know that most users are already familiar with python and torch before using mmdeploy. Therefore we provide scripts to simplify mmdeploy installation.
Assuming you have a python ready (whether `conda` or `pyenv`), run this script to install mmdeploy + ncnn backend, `nproc` is not compulsory.
Assuming you already have
- python3 -m pip (`conda` or `pyenv`)
- nvcc (depends on inference backend)
- torch (not compulsory)
run this script to install mmdeploy + ncnn backend, `nproc` is not compulsory.
```bash
$ cd /path/to/mmdeploy

View File

@ -0,0 +1,108 @@
# Ubuntu Cross Build aarch64
mmdeploy chose ncnn as the inference backend for aarch64 embedded linux devices. There are two parts:
Host
- model conversion
- cross build SDK and demo for embedded devices
Device
- Run converted model
## 1. Model Convert on Host
Refer to the doc to install [mmdeploy](../01-how-to-build/) and [mmcls](https://github.com/open-mmlab/mmclassification), and convert resnet18 for model package
```bash
export MODEL_CONFIG=/path/to/mmclassification/configs/resnet/resnet18_8xb32_in1k.py
export MODEL_PATH=https://download.openmmlab.com/mmclassification/v0/resnet/resnet18_8xb32_in1k_20210831-fbbb1da6.pth
# Convert resnet18
cd /path/to/mmdeploy
python tools/deploy.py \
configs/mmcls/classification_ncnn_static.py \
$MODEL_CONFIG \
$MODEL_PATH \
tests/data/tiger.jpeg \
--work-dir resnet18 \
--device cpu \
--dump-info
```
## 2. Cross Build on Host
It is recommended to compile directly with the script
```bash
sh -x tools/scripts/ubuntu_cross_build_aarch64.sh
```
The following is the manual process corresponding to the script:
a) Install aarch64 build tools
```bash
sudo apt install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
```
b) Cross build opencv and install to /tmp/ocv-aarch64
```bash
git clone https://github.com/opencv/opencv --depth=1 --branch=4.x --recursive
cd opencv/platforms/linux/
mkdir build && cd build
cmake ../../.. \
-DCMAKE_INSTALL_PREFIX=/tmp/ocv-aarch64 \
-DCMAKE_TOOLCHAIN_FILE=../aarch64-gnu.toolchain.cmake
make -j && make install
ls -alh /tmp/ocv-aarch64
..
```
c) Cross build ncnn and install to /tmp/ncnn-aarch64
```bash
git clone https://github.com/tencent/ncnn --branch 20220729 --depth=1
mkdir build && cd build
cmake .. \
-DCMAKE_TOOLCHAIN_FILE=../toolchains/aarch64-linux-gnu.toolchain.cmake \
-DCMAKE_INSTALL_PREFIX=/tmp/ncnn-aarch64
make -j && make install
ls -alh /tmp/ncnn-aarch64
..
```
d) Cross build mmdeploy
```bash
git submodule init
git submodule update
mkdir build && cd build
cmake .. \
-DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/aarch64-linux-gnu.cmake \
-DMMDEPLOY_TARGET_DEVICES="cpu" \
-DMMDEPLOY_TARGET_BACKENDS="ncnn" \
-Dncnn_DIR=/tmp/ncnn-aarch64/lib/cmake/ncnn \
-DOpenCV_DIR=/tmp/ocv-aarch64/lib/cmake/opencv4
make install
ls -lah install/bin/*
..
```
## 3. Execute on Device
Make sure that `--dump-info` is used during model conversion, so that the `resnet18` directory contains the files required by the SDK such as `pipeline.json`.
Copy the model folder(resnet18), executable(image_classification) file, test image(tests/data/tiger.jpeg) and prebuilt OpenCV(/tmp/ocv-aarch64) to the device.
```bash
./image_classification cpu ./resnet18 tiger.jpeg
..
label: 292, score: 0.9261
label: 282, score: 0.0726
label: 290, score: 0.0008
label: 281, score: 0.0002
label: 340, score: 0.0001
```

View File

@ -2,7 +2,13 @@
通过用户调研,我们得知多数使用者在了解 mmdeploy 前,已经熟知 python 和 torch 用法。因此我们提供脚本简化 mmdeploy 安装。
假设您已经准备好 Python3.6 pip 以上环境(无论 conda 或 pyenv运行这个脚本来安装 mmdeploy + ncnn backend`nproc` 可以不指定。
假设您已经准备好
- python3 -m pip必须conda 或 pyenv 皆可)
- nvcc取决于推理后端
- torch非必须可延后安装
运行这个脚本来安装 mmdeploy + ncnn backend`nproc` 可以不指定。
```bash
$ cd /path/to/mmdeploy

View File

@ -0,0 +1,108 @@
# ubuntu 交叉编译 aarch64
mmdeploy 选 ncnn 作为 aarch64 嵌入式 linux 设备的推理后端。 完整的部署分为两部分:
Host
- 模型转换
- 交叉编译嵌入式设备所需 SDK 和 bin
Device
- 运行编译结果
## 1. Host 模型转换
参照文档安装 [mmdeploy](../01-how-to-build/) 和 [mmcls](https://github.com/open-mmlab/mmclassification),转换 resnet18 对应模型包
```bash
export MODEL_CONFIG=/path/to/mmclassification/configs/resnet/resnet18_8xb32_in1k.py
export MODEL_PATH=https://download.openmmlab.com/mmclassification/v0/resnet/resnet18_8xb32_in1k_20210831-fbbb1da6.pth
# 模型转换
cd /path/to/mmdeploy
python tools/deploy.py \
configs/mmcls/classification_ncnn_static.py \
$MODEL_CONFIG \
$MODEL_PATH \
tests/data/tiger.jpeg \
--work-dir resnet18 \
--device cpu \
--dump-info
```
## 2. Host 交叉编译
建议直接用脚本编译
```bash
sh -x tools/scripts/ubuntu_cross_build_aarch64.sh
```
以下是脚本对应的手动过程
a) 安装 aarch64 交叉编译工具
```bash
sudo apt install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
```
b) 交叉编译 opencv 安装到 tmp 目录
```bash
git clone https://github.com/opencv/opencv --depth=1 --branch=4.x --recursive
cd opencv/platforms/linux/
mkdir build && cd build
cmake ../../.. \
-DCMAKE_INSTALL_PREFIX=/tmp/ocv-aarch64 \
-DCMAKE_TOOLCHAIN_FILE=../aarch64-gnu.toolchain.cmake
make -j && make install
ls -alh /tmp/ocv-aarch64
..
```
c) 交叉编译 ncnn 安装到 tmp 目录
```bash
git clone https://github.com/tencent/ncnn --branch 20220729 --depth=1
mkdir build && cd build
cmake .. \
-DCMAKE_TOOLCHAIN_FILE=../toolchains/aarch64-linux-gnu.toolchain.cmake \
-DCMAKE_INSTALL_PREFIX=/tmp/ncnn-aarch64
make -j && make install
ls -alh /tmp/ncnn-aarch64
..
```
d) 交叉编译 mmdeployinstall/bin 目录是可执行文件
```bash
git submodule init
git submodule update
mkdir build && cd build
cmake .. \
-DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/aarch64-linux-gnu.cmake \
-DMMDEPLOY_TARGET_DEVICES="cpu" \
-DMMDEPLOY_TARGET_BACKENDS="ncnn" \
-Dncnn_DIR=/tmp/ncnn-aarch64/lib/cmake/ncnn \
-DOpenCV_DIR=/tmp/ocv-aarch64/lib/cmake/opencv4
make install
ls -lah install/bin/*
..
```
## 3. Device 执行
确认转换模型用了 `--dump-info`,这样 `resnet18` 目录才有 `pipeline.json` 等 SDK 所需文件。
把 dump 好的模型目录(resnet18)、可执行文件(image_classification)、测试图片(tests/data/tiger.jpeg)、交叉编译的 OpenCV(/tmp/ocv-aarch64) 拷贝到设备中
```bash
./image_classification cpu ./resnet18 tiger.jpeg
..
label: 292, score: 0.9261
label: 282, score: 0.0726
label: 290, score: 0.0008
label: 281, score: 0.0002
label: 340, score: 0.0001
```

View File

@ -9,7 +9,8 @@ g_jobs = 2
def install_protobuf(dep_dir) -> int:
"""build and install protobuf.
"""build and install protobuf. protobuf seems not support repeated install,
so clean build first.
Args:
wor_dir (_type_): _description_
@ -29,11 +30,22 @@ def install_protobuf(dep_dir) -> int:
os.chdir(os.path.join(dep_dir, 'protobuf-3.20.0'))
install_dir = os.path.join(dep_dir, 'pbinstall')
if os.path.exists(install_dir):
os.system('rm -rf {}'.format(install_dir))
os.system('make clean')
os.system('./configure --prefix={}'.format(install_dir))
os.system('make -j {} && make install'.format(g_jobs))
protoc = os.path.join(dep_dir, 'pbinstall', 'bin', 'protoc')
protoc = os.path.join(install_dir, 'bin', 'protoc')
print('protoc \t:{}'.format(cmd_result('{} --version'.format(protoc))))
os.system(""" echo 'export PATH={}:$PATH' >> ~/mmdeploy.env """.format(
os.path.join(install_dir, 'bin')))
os.system(
""" echo 'export LD_LIBRARY_PATH={}:$LD_LIBRARY_PATH' >> ~/mmdeploy.env """ # noqa: E501
.format(os.path.join(install_dir, 'lib')))
return 0
@ -60,6 +72,7 @@ def install_pyncnn(dep_dir):
os.system('mkdir build')
os.chdir(os.path.join(ncnn_dir, 'build'))
os.system('rm -rf CMakeCache.txt')
pb_install = os.path.join(dep_dir, 'pbinstall')
pb_bin = os.path.join(pb_install, 'bin', 'protoc')
pb_lib = os.path.join(pb_install, 'lib', 'libprotobuf.so')
@ -101,9 +114,9 @@ def install_mmdeploy(work_dir, dep_dir, ncnn_cmake_dir):
pb_lib = os.path.join(pb_install, 'lib', 'libprotobuf.so')
pb_include = os.path.join(pb_install, 'include')
os.system('rm -rf build/CMakeCache.txt')
cmd = 'cd build && cmake ..'
cmd += ' -DCMAKE_C_COMPILER=gcc-7 '
cmd += ' -DCMAKE_CXX_COMPILER=g++-7 '
cmd += ' -DMMDEPLOY_BUILD_SDK=ON '
cmd += ' -DMMDEPLOY_BUILD_EXAMPLES=ON '
cmd += ' -DMMDEPLOY_BUILD_SDK_PYTHON_API=ON '
@ -117,7 +130,14 @@ def install_mmdeploy(work_dir, dep_dir, ncnn_cmake_dir):
os.system('cd build && make -j {} && make install'.format(g_jobs))
os.system('python3 -m pip install -v -e .')
os.system('python3 tools/check_env.py')
os.system(""" echo 'export PATH={}:$PATH' >> ~/mmdeploy.env """.format(
os.path.join(work_dir, 'mmdeploy', 'backend', 'ncnn')))
try:
import mmcv
print(mmcv.__version__)
os.system('python3 tools/check_env.py')
except Exception:
print('Please install torch & mmcv later.. ╮(╯▽╰)╭')
return 0
@ -143,7 +163,7 @@ def main():
return -1
os.mkdir(dep_dir)
success, envs = ensure_base_env(work_dir, dep_dir)
success = ensure_base_env(work_dir, dep_dir)
if success != 0:
return -1
@ -155,12 +175,9 @@ def main():
if install_mmdeploy(work_dir, dep_dir, ncnn_cmake_dir) != 0:
return -1
if len(envs) > 0:
print(
'We recommend that you set the following environment variables:\n')
for env in envs:
print(env)
print('\n')
if os.path.exists('~/mmdeploy.env'):
print('Please source ~/mmdeploy.env to setup your env !')
os.system('cat ~/mmdeploy.env')
if __name__ == '__main__':

View File

@ -20,7 +20,7 @@ def install_ort(dep_dir):
# git clone
if not os.path.exists('onnxruntime-linux-x64-1.8.1'):
os.system(
'wget https://github.com/microsoft/onnxruntime/releases/download/v1.8.1/onnxruntime-linux-x64-1.8.1.tgz' # noqa: E501
'wget -q --show-progress https://github.com/microsoft/onnxruntime/releases/download/v1.8.1/onnxruntime-linux-x64-1.8.1.tgz' # noqa: E501
)
os.system('tar xvf onnxruntime-linux-x64-1.8.1.tgz')
@ -41,9 +41,9 @@ def install_mmdeploy(work_dir, ort_dir):
if not os.path.exists('build'):
os.system('mkdir build')
os.system('rm -rf build/CMakeCache.txt')
cmd = 'cd build && cmake ..'
cmd += ' -DCMAKE_C_COMPILER=gcc-7 '
cmd += ' -DCMAKE_CXX_COMPILER=g++-7 '
cmd += ' -DMMDEPLOY_BUILD_SDK=ON '
cmd += ' -DMMDEPLOY_BUILD_EXAMPLES=ON '
cmd += ' -DMMDEPLOY_BUILD_SDK_PYTHON_API=ON '
@ -54,7 +54,12 @@ def install_mmdeploy(work_dir, ort_dir):
os.system('cd build && make -j {} && make install'.format(g_jobs))
os.system('python3 -m pip install -e .')
os.system('python3 tools/check_env.py')
try:
import mmcv
print(mmcv.__version__)
os.system('python3 tools/check_env.py')
except Exception:
print('Please install torch & mmcv later.. ⊙▽⊙')
return 0
@ -80,7 +85,7 @@ def main():
return -1
os.mkdir(dep_dir)
success, envs = ensure_base_env(work_dir, dep_dir)
success = ensure_base_env(work_dir, dep_dir)
if success != 0:
return -1
@ -89,12 +94,9 @@ def main():
if install_mmdeploy(work_dir, ort_dir) != 0:
return -1
if len(envs) > 0:
print(
'We recommend that you set the following environment variables:\n')
for env in envs:
print(env)
print('\n')
if os.path.exists('~/mmdeploy.env'):
print('Please source ~/mmdeploy.env to setup your env !')
os.system('cat ~/mmdeploy.env')
if __name__ == '__main__':

View File

@ -85,9 +85,9 @@ def install_mmdeploy(work_dir, pplnn_cmake_dir, pplcv_cmake_dir, build_cuda):
if not os.path.exists('build'):
os.system('mkdir build')
os.system('rm -rf build/CMakeCache.txt')
cmd = 'cd build && cmake ..'
cmd += ' -DCMAKE_C_COMPILER=gcc-7 '
cmd += ' -DCMAKE_CXX_COMPILER=g++-7 '
cmd += ' -DMMDEPLOY_BUILD_SDK=ON '
cmd += ' -DMMDEPLOY_BUILD_EXAMPLES=ON '
cmd += ' -DMMDEPLOY_BUILD_SDK_PYTHON_API=ON '
@ -104,7 +104,12 @@ def install_mmdeploy(work_dir, pplnn_cmake_dir, pplcv_cmake_dir, build_cuda):
os.system('cd build && make -j {} && make install'.format(g_jobs))
os.system('python3 -m pip install -e .')
os.system('python3 tools/check_env.py')
try:
import mmcv
print(mmcv.__version__)
os.system('python3 tools/check_env.py')
except Exception:
print('Please install torch & mmcv later.. ∩▽∩')
return 0
@ -130,23 +135,10 @@ def main():
return -1
os.mkdir(dep_dir)
success, envs = ensure_base_env(work_dir, dep_dir)
success = ensure_base_env(work_dir, dep_dir)
if success != 0:
return -1
# enable g++ and gcc
gplus = cmd_result('which g++')
if gplus is None or len(gplus) < 1:
sudo = 'sudo'
if 'root' in cmd_result('whoami'):
sudo = ''
os.system(
'{} update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 200' # noqa: E501
.format(sudo))
os.system(
'{} update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 200' # noqa: E501
.format(sudo))
# install pplcv and pplnn
nvcc = cmd_result('which nvcc')
build_cuda = False
@ -158,12 +150,9 @@ def main():
build_cuda) != 0:
return -1
if len(envs) > 0:
print(
'We recommend that you set the following environment variables:\n')
for env in envs:
print(env)
print('\n')
if os.path.exists('~/mmdeploy.env'):
print('Please source ~/mmdeploy.env to setup your env !')
os.system('cat ~/mmdeploy.env')
if __name__ == '__main__':

View File

@ -3,7 +3,8 @@ import os
import sys
import time
from ubuntu_utils import cmd_result, cu_version_name, ensure_base_env, get_job
from ubuntu_utils import (cmd_result, cu_version_name, ensure_base_env,
get_job, pytorch_version)
g_jobs = 2
@ -17,15 +18,9 @@ def install_libtorch(dep_dir):
if os.path.exists(unzipped_name):
return os.path.join(dep_dir, unzipped_name)
torch_version = None
try:
import torch
torch_version = torch.__version__
except Exception:
pass
torch_version = pytorch_version()
if torch_version is None:
print('torch version is None, use 1.11.0')
print('torch version is None, try 1.11.0')
torch_version = '1.11.0'
version_name = None
@ -46,7 +41,7 @@ def install_libtorch(dep_dir):
torch_version, version_name)
url = 'https://download.pytorch.org/libtorch/{}/{}'.format(
version_name, filename)
os.system('wget {} -O libtorch.zip'.format(url))
os.system('wget -q --show-progress {} -O libtorch.zip'.format(url))
os.system('unzip libtorch.zip')
if not os.path.exists(unzipped_name):
print(
@ -67,9 +62,9 @@ def install_mmdeploy(work_dir, libtorch_dir):
if not os.path.exists('build'):
os.system('mkdir build')
os.system('rm -rf build/CMakeCache.txt')
cmd = 'cd build && Torch_DIR={} cmake ..'.format(libtorch_dir)
cmd += ' -DCMAKE_C_COMPILER=gcc-7 '
cmd += ' -DCMAKE_CXX_COMPILER=g++-7 '
cmd += ' -DMMDEPLOY_BUILD_SDK=ON '
cmd += ' -DMMDEPLOY_BUILD_EXAMPLES=ON '
cmd += ' -DMMDEPLOY_BUILD_SDK_PYTHON_API=ON '
@ -80,7 +75,12 @@ def install_mmdeploy(work_dir, libtorch_dir):
os.system('cd build && make -j {} && make install'.format(g_jobs))
os.system('python3 -m pip install -e .')
os.system('python3 tools/check_env.py')
try:
import mmcv
print(mmcv.__version__)
os.system('python3 tools/check_env.py')
except Exception:
print('Please install torch & mmcv later.. ≥▽≤')
return 0
@ -106,7 +106,7 @@ def main():
return -1
os.mkdir(dep_dir)
success, envs = ensure_base_env(work_dir, dep_dir)
success = ensure_base_env(work_dir, dep_dir)
if success != 0:
return -1
@ -118,12 +118,9 @@ def main():
if install_mmdeploy(work_dir, libtorch_dir) != 0:
return -1
if len(envs) > 0:
print(
'We recommend that you set the following environment variables:\n')
for env in envs:
print(env)
print('\n')
if os.path.exists('~/mmdeploy.env'):
print('Please source ~/mmdeploy.env to setup your env !')
os.system('cat ~/mmdeploy.env')
if __name__ == '__main__':

View File

@ -0,0 +1,105 @@
#!/bin/bash
# set -ex
# get appropriate proc number: max(1, nproc-3)
good_nproc() {
num=`nproc`
num=`expr $num - 3`
if [ $num -lt 1 ];then
return 1
fi
return ${num}
}
install_tools() {
sudo apt install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
aarch64-linux-gnu-g++ --version
aarch64-linux-gnu-gcc --version
aarch64-linux-gnu-ld --version
sudo apt install wget git git-lfs
python3 -m pip install cmake==3.22.0
echo 'export PATH=~/.local/bin:${PATH}' >> ~/mmdeploy.env
export PATH=~/.local/bin:${PATH}
}
build_ocv() {
if [ ! -e "opencv" ];then
git clone https://github.com/opencv/opencv --depth=1 --branch=4.6.0 --recursive
fi
if [ ! -e "opencv/platforms/linux/cross_build_aarch64" ];then
mkdir opencv/platforms/linux/cross_build_aarch64
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
good_nproc
jobs=$?
make -j${jobs}
make install
cd -
}
build_ncnn() {
if [ ! -e "ncnn" ];then
git clone https://github.com/tencent/ncnn --branch 20220729 --depth=1
fi
if [ ! -e "ncnn/build_aarch64" ];then
mkdir -p ncnn/build_aarch64
fi
cd ncnn/build_aarch64
rm -rf CMakeCache.txt
cmake .. \
-DCMAKE_TOOLCHAIN_FILE=../toolchains/aarch64-linux-gnu.toolchain.cmake \
-DCMAKE_INSTALL_PREFIX=/tmp/ncnn-aarch64
good_nproc
jobs=$?
make -j${jobs}
make install
cd -
}
build_mmdeploy() {
git submodule init
git submodule update
if [ ! -e "build_aarch64" ];then
mkdir build_aarch64
fi
cd build_aarch64
rm -rf CMakeCache.txt
cmake .. \
-DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/aarch64-linux-gnu.cmake \
-DMMDEPLOY_TARGET_DEVICES="cpu" \
-DMMDEPLOY_TARGET_BACKENDS="ncnn" \
-Dncnn_DIR=/tmp/ncnn-aarch64/lib/cmake/ncnn \
-DOpenCV_DIR=/tmp/ocv-aarch64/lib/cmake/opencv4
good_nproc
jobs=$?
make -j${jobs}
make install
ls -lah install/bin/*
}
print_success() {
echo "----------------------------------------------------------------------"
echo "Cross build finished, PLS copy bin/model/test_data to the device.. QVQ"
echo "----------------------------------------------------------------------"
}
if [ ! -e "../mmdeploy-dep" ];then
mkdir ../mmdeploy-dep
fi
cd ../mmdeploy-dep
install_tools
build_ocv
build_ncnn
cd ../mmdeploy
build_mmdeploy
print_success

View File

@ -4,21 +4,33 @@ import re
import time
def pytorch_version():
version = None
try:
import torch
raw = torch.__version__
pattern = re.compile(r'[0-9]+\.[0-9]+\.[0-9]+')
version = pattern.findall(raw)[0]
except Exception:
pass
return version
def cmd_result(txt: str):
cmd = os.popen(txt)
return cmd.read().rstrip().lstrip()
def get_job(argv) -> int:
# get nprocs, if user not specified, use max(2, nproc-1)
# get nprocs, if user not specified, use max(1, nproc-2)
job = 2
if len(argv) <= 1:
print('your can use `python3 {} N` to set make -j [N]'.format(argv[0]))
nproc = cmd_result('nproc')
if nproc is not None and len(nproc) > 0:
job = max(int(nproc) - 1, 2)
job = max(int(nproc) - 2, 1)
else:
job = 2
job = 1
else:
job = int(argv[1])
return job
@ -55,7 +67,7 @@ def ensure_base_env(work_dir, dep_dir):
check python, root, pytorch version, auto install these binary:
* make
* g++-7
* g++
* git
* wget
* unzip
@ -63,7 +75,6 @@ def ensure_base_env(work_dir, dep_dir):
* mmcv (not compulsory)
"""
envs = []
print('-' * 10 + 'ensure base env' + '-' * 10)
print(description)
@ -83,18 +94,18 @@ def ensure_base_env(work_dir, dep_dir):
cmake = cmd_result('which cmake')
if cmake is None or len(cmake) < 1:
print('cmake not found, try install cmake ..', end='')
os.system('python3 -m pip install cmake>=3.14.0')
os.system('python3 -m pip install cmake')
cmake = cmd_result('which cmake')
if cmake is None or len(cmake) < 1:
env = 'export PATH=${PATH}:~/.local/bin'
os.system(env)
envs.append(env)
os.system(""" echo '{}' >> ~/mmdeploy.env """.format(env))
cmake = cmd_result('which cmake')
if cmake is None or len(cmake) < 1:
print('Check cmake failed.')
return -1, envs
return -1
print('success')
# check make
@ -109,14 +120,14 @@ def ensure_base_env(work_dir, dep_dir):
make = cmd_result('which make')
if make is None or len(make) < 1:
print('Check make failed.')
return -1, envs
return -1
print('success')
# check g++ version
gplus = cmd_result('which g++-7')
gplus = cmd_result('which g++')
if gplus is None or len(gplus) < 1:
# install g++
print('g++-7 not found, try install g++-7 ..', end='')
print('g++ not found, try install g++ ..', end='')
os.system(
'{} DEBIAN_FRONTEND="noninteractive" apt install software-properties-common -y' # noqa: E501
.format(sudo)) # noqa: E501
@ -125,18 +136,12 @@ def ensure_base_env(work_dir, dep_dir):
os.system(
'{} add-apt-repository ppa:ubuntu-toolchain-r/test -y'.format(
sudo))
os.system('{} apt install gcc-7 g++-7 -y'.format(sudo))
os.system('{} apt install gcc g++ -y'.format(sudo))
gplus = cmd_result('which g++-7')
gplus = cmd_result('which g++')
if gplus is None or len(gplus) < 1:
print('Check g++-7 failed.')
return -1, envs
os.system(
'{} update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 200' # noqa: E501
.format(sudo))
os.system(
'{} update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 200' # noqa: E501
.format(sudo))
print('Check g++ failed.')
return -1
print('success')
# wget
@ -197,7 +202,7 @@ def ensure_base_env(work_dir, dep_dir):
ocv = cmd_result('which opencv_version')
if ocv is None or len(ocv) < 1:
print('Check ocv failed.')
return -1, envs
return -1
print('success')
# print all
@ -217,11 +222,11 @@ def ensure_base_env(work_dir, dep_dir):
cmd_result(" make --version | head -n 1 | awk '{print $3}' ")))
print('wget bin\t:{}'.format(wget))
print('g++-7 bin\t:{}'.format(gplus))
print('g++ bin\t:{}'.format(gplus))
print('mmcv version\t:{}'.format(mmcv_version))
if mmcv_version is None:
print('\t please install an mm serials algorithm later.')
print('\t please install mmcv later.')
time.sleep(2)
print('torch version\t:{}'.format(torch_version))
@ -241,4 +246,4 @@ def ensure_base_env(work_dir, dep_dir):
print('dep dir \t:{}'.format(dep_dir))
print('\n')
return 0, envs
return 0