Skip to content

Commit

Permalink
refactor(onnx2ncnn): add test case and simplify code (open-mmlab#436)
Browse files Browse the repository at this point in the history
* refactor(onnx2ncnn.cpp): split it to shape_inference, pass and utils

* refactor(onnx2ncnn.cpp): split it to shape_inference, pass and utils

* refactor(onnx2ncnn.cpp): split code

* refactor(net_module.cpp): fix build error

* ci(test_onnx2ncnn.py): add generate model adn run

* ci(onnx2ncnn): add ncnn backend

* ci(test_onnx2ncnn): add converted onnx model`

* ci(onnx2ncnn): fix ncnn tar

* ci(backed-ncnn): simplify dependency install

* ci(onnx2ncnn): fix apt install

* Update backend-ncnn.yml

* Update backend-ncnn.yml

* Update backend-ncnn.yml

* Update backend-ncnn.yml

* Update backend-ncnn.yml

* Update backend-ncnn.yml

* Update backend-ncnn.yml

* Update backend-ncnn.yml

* Update backend-ncnn.yml

* Update backend-ncnn.yml

* Update backend-ncnn.yml

* fix(ci): add include algorithm

* Update build.yml

* parent aa85760
author q.yao <[email protected]> 1651287879 +0800
committer tpoisonooo <[email protected]> 1652169959 +0800

[Fix] Fix ci (open-mmlab#426)

* fix ci

* add nvidia key

* remote torch

* recover pytorch

refactor(onnx2ncnn.cpp): split it to shape_inference, pass and utils

* fix(onnx2ncnn): review

* fix(onnx2ncnn): build error

Co-authored-by: q.yao <[email protected]>
  • Loading branch information
tpoisonooo and grimoire authored May 16, 2022
1 parent 6eb83a9 commit d04c8dc
Show file tree
Hide file tree
Showing 11 changed files with 3,257 additions and 2,931 deletions.
97 changes: 97 additions & 0 deletions .github/scripts/test_onnx2ncnn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Copyright (c) OpenMMLab. All rights reserved.

import argparse
import os
import subprocess

# list of tuple: config, pretrained model, onnx filename
CONFIGS = [
(
'mmclassification/configs/vision_transformer/vit-base-p32_ft-64xb64_in1k-384.py', # noqa: E501
'https://download.openmmlab.com/mmclassification/v0/vit/finetune/vit-base-p32_in21k-pre-3rdparty_ft-64xb64_in1k-384_20210928-9cea8599.pth', # noqa: E501
'vit.onnx'),
(
'mmclassification/configs/resnet/resnet50_8xb32_in1k.py',
'https://download.openmmlab.com/mmclassification/v0/resnet/resnet50_8xb32_in1k_20210831-ea4938fc.pth', # noqa: E501
'resnet50.onnx',
),
(
'mmclassification/configs/resnet/resnet18_8xb32_in1k.py',
'https://download.openmmlab.com/mmclassification/v0/resnet/resnet18_8xb32_in1k_20210831-fbbb1da6.pth', # noqa: E501
'resnet18.onnx',
'https://media.githubusercontent.com/media/tpoisonooo/mmdeploy-onnx2ncnn-testdata/main/resnet18.onnx', # noqa: E501
),
(
'mmclassification/configs/mobilenet_v2/mobilenet-v2_8xb32_in1k.py',
'https://download.openmmlab.com/mmclassification/v0/mobilenet_v2/mobilenet_v2_batch256_imagenet_20200708-3b2dc3af.pth', # noqa: E501
'mobilenet-v2.onnx',
'https://media.githubusercontent.com/media/tpoisonooo/mmdeploy-onnx2ncnn-testdata/main/mobilenet-v2.onnx', # noqa: E501
)
]


def parse_args():
parser = argparse.ArgumentParser(
description='MMDeploy onnx2ncnn test tool.')
parser.add_argument('--run', type=bool, help='Execute onnx2ncnn bin.')
parser.add_argument(
'--repo-dir', type=str, default='~/', help='mmcls directory.')
parser.add_argument(
'--out',
type=str,
default='onnx_output',
help='onnx model output directory.')
parser.add_argument(
'--generate-onnx', type=bool, help='Generate onnx model.')
args = parser.parse_args()
return args


def generate_onnx(args):
import mmcv
mmcv.mkdir_or_exist(args.out)
for conf in CONFIGS:
config = os.path.join(args.repo_dir, conf[0])
model = conf[1]
convert_cmd = [
'python3', 'tools/deploy.py',
'configs/mmcls/classification_ncnn_static.py', config, model,
'cat-dog.png', '--work-dir', 'work_dir', '--device', 'cpu'
]
print(subprocess.call(convert_cmd))

move_cmd = [
'mv', 'work_dir/end2end.onnx',
os.path.join(args.out, conf[2])
]
print(subprocess.call(move_cmd))


def run(args):
for conf in CONFIGS:
if len(conf) < 4:
continue
download_url = conf[3]
filename = conf[2]
download_cmd = ['wget', download_url]
# show processbar
os.system(' '.join(download_cmd))

convert_cmd = ['./onnx2ncnn', filename, 'onnx.param', 'onnx.bin']
subprocess.run(convert_cmd, capture_output=True, check=True)


def main():
"""test `onnx2ncnn.cpp`
First generate onnx model then convert it with `onnx2ncnn`.
"""
args = parse_args()
if args.generate_onnx:
generate_onnx(args)
if args.run:
run(args)


if __name__ == '__main__':
main()
68 changes: 68 additions & 0 deletions .github/workflows/backend-ncnn.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: backend

on:
push:
paths-ignore:
- "demo/**"
- "tools/**"

pull_request:
paths-ignore:
- "demo/**"
- "tools/**"
- "docs/**"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
test_onnx2ncnn:
runs-on: ubuntu-18.04
strategy:
matrix:
python-version: [3.7]
torch: [1.9.0]
mmcv: [1.4.2]
include:
- torch: 1.9.0
torch_version: torch1.9
torchvision: 0.10.0
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
submodules: 'recursive'
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install unittest dependencies
run: |
pip install cmake onnx
- name: update
run: sudo apt update
- name: gcc-multilib
run: sudo apt install gcc-multilib g++-multilib wget libprotobuf-dev protobuf-compiler
- name: Install ncnn
run: |
wget https://github.com/Tencent/ncnn/archive/refs/tags/20220420.tar.gz
tar xf 20220420.tar.gz
pushd ncnn-20220420
mkdir build && pushd build
cmake -DCMAKE_INSTALL_PREFIX=$(pwd)/../install -DNCNN_BUILD_TESTS=OFF -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF ..
cmake --build . -j2
make install
popd && popd
- name: Install mmdeploy with ncnn backend
run: |
mkdir -p build && pushd build
export LD_LIBRARY_PATH=/home/runner/work/mmdeploy/mmdeploy/ncnn-20220420/install/lib/:$LD_LIBRARY_PATH
cmake -DMMDEPLOY_TARGET_BACKENDS=ncnn -Dncnn_DIR=/home/runner/work/mmdeploy/mmdeploy/ncnn-20220420/install/lib/cmake/ncnn/ ..
make onnx2ncnn -j2
popd
- name: Test onnx2ncnn
run: |
echo $(pwd)
ln -s build/bin/onnx2ncnn ./
python3 .github/scripts/test_onnx2ncnn.py --run 1
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,4 @@ jobs:
env_vars: OS,PYTHON
name: codecov-umbrella
fail_ci_if_error: false
gcov_ignore : [".github/scripts/doc_link_checker.py"]
gcov_ignore : [".github/scripts/*"]
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ endif ()
message(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}")

cmake_minimum_required(VERSION 3.14)
project(MMDeploy VERSION 0.1.0)
project(MMDeploy VERSION 0.5.0)

set(CMAKE_CXX_STANDARD 17)

Expand Down
2 changes: 1 addition & 1 deletion csrc/backend_ops/ncnn/onnx2ncnn/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ find_package(Protobuf)
if (PROTOBUF_FOUND)
protobuf_generate_cpp(ONNX_PROTO_SRCS ONNX_PROTO_HDRS
${CMAKE_CURRENT_SOURCE_DIR}/onnx.proto)
add_executable(onnx2ncnn onnx2ncnn.cpp ${ONNX_PROTO_SRCS} ${ONNX_PROTO_HDRS})
add_executable(onnx2ncnn onnx2ncnn.cpp fuse_pass.cpp shape_inference.cpp ${ONNX_PROTO_SRCS} ${ONNX_PROTO_HDRS})
target_include_directories(onnx2ncnn PRIVATE ${PROTOBUF_INCLUDE_DIR}
${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(onnx2ncnn PRIVATE ${PROTOBUF_LIBRARIES})
Expand Down
Loading

0 comments on commit d04c8dc

Please sign in to comment.