From 833320e50d5fd2c65bc15dcdee39766fa2085bc5 Mon Sep 17 00:00:00 2001 From: xiao-daipeng Date: Fri, 22 Jul 2022 15:02:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0mxbase=EF=BC=8Cmindxsdk?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E8=84=9A=E6=9C=AC=EF=BC=8C=E4=BF=AE=E6=94=B9?= =?UTF-8?q?cmake=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Shufflenetv2_for_PyTorch/8p_main_med.py | 4 +- .../sdk_infer/mxbase_infer/CMakeLists.txt | 38 +++++++----- .../sdk_infer/mxbase_infer/build.sh | 62 +++++++++++++++++++ .../sdk_infer/mxbase_infer/run.sh | 22 +++++++ .../sdk_infer/sdk_run_infer/run.sh | 29 +++++++++ .../sdk_infer/{ => util}/task_metric.py | 0 6 files changed, 137 insertions(+), 18 deletions(-) create mode 100644 PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/sdk_infer/mxbase_infer/build.sh create mode 100644 PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/sdk_infer/mxbase_infer/run.sh create mode 100644 PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/sdk_infer/sdk_run_infer/run.sh rename PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/sdk_infer/{ => util}/task_metric.py (100%) diff --git a/PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/8p_main_med.py b/PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/8p_main_med.py index 7c92061fbe..a1d1f585b7 100644 --- a/PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/8p_main_med.py +++ b/PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/8p_main_med.py @@ -253,8 +253,8 @@ def main_worker(gpu, ngpus_per_node, args): if args.amp: model, optimizer = amp.initialize(model, optimizer, opt_level=args.opt_level, loss_scale=args.loss_scale,combine_grad=True) - - model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.gpu], broadcast_buffers=False) + if args.distributed: + model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.gpu], broadcast_buffers=False) # optionally resume from a checkpoint if args.resume: diff --git a/PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/sdk_infer/mxbase_infer/CMakeLists.txt b/PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/sdk_infer/mxbase_infer/CMakeLists.txt index 993b92a568..b7c1a131a7 100644 --- a/PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/sdk_infer/mxbase_infer/CMakeLists.txt +++ b/PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/sdk_infer/mxbase_infer/CMakeLists.txt @@ -1,9 +1,15 @@ -cmake_minimum_required(VERSION 3.10.0) -project(main) +cmake_minimum_required(VERSION 3.5.2) +SET(CMAKE_BUILD_TYPE "Debug") +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") +SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb") +SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall") +project(shufflenetv2) -set(TARGET main) +set(TARGET shufflenetv2) add_definitions(-DENABLE_DVPP_INTERFACE) +add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0) +add_definitions(-Dgoogle=mindxsdk_private) add_compile_options(-std=c++11 -fPIE -fstack-protector-all -fPIC -Wall) add_link_options(-Wl,-z,relro,-z,now,-z,noexecstack -s -pie) @@ -19,23 +25,21 @@ if(NOT DEFINED ENV{ARCH_PATTERN}) endif() set(ACL_INC_DIR $ENV{ASCEND_HOME}/$ENV{ASCEND_VERSION}/$ENV{ARCH_PATTERN}/acllib/include) set(ACL_LIB_DIR $ENV{ASCEND_HOME}/$ENV{ASCEND_VERSION}/$ENV{ARCH_PATTERN}/acllib/lib64) - -set(MXBASE_ROOT_DIR ${PROJECT_SOURCE_DIR}/../../) -set(MXBASE_INC ${MXBASE_ROOT_DIR}/mxbase/include) -set(MXBASE_LIB_DIR ${MXBASE_ROOT_DIR}/dist/lib) -set(MXBASE_POST_LIB_DIR ${MXBASE_ROOT_DIR}/dist/lib/modelpostprocessors) -set(MXBASE_POST_PROCESS_DIR ${MXBASE_ROOT_DIR}/postprocess/include) -if(DEFINED ENV{MXSDK_OPENSOURCE_DIR}) - set(OPENSOURCE_DIR $ENV{MXSDK_OPENSOURCE_DIR}) -else() - set(OPENSOURCE_DIR ${MXBASE_ROOT_DIR}/opensource/dist) -endif() +set(ACL_LIB_PATH $ENV{ASCEND_HOME}/ascend-toolkit/latest/acllib) +set(MXBASE_ROOT_DIR $ENV{MX_SDK_HOME}) +set(MXBASE_INC ${MXBASE_ROOT_DIR}/include) +set(MXBASE_LIB_DIR ${MXBASE_ROOT_DIR}/lib) +set(MXBASE_POST_LIB_DIR ${MXBASE_ROOT_DIR}/lib/modelpostprocessors) +set(MXBASE_POST_PROCESS_DIR ${MXBASE_ROOT_DIR}/include/MxBase/postprocess/include) +set(OPENSOURCE_DIR ${MXBASE_ROOT_DIR}/opensource) include_directories(${ACL_INC_DIR}) include_directories(${OPENSOURCE_DIR}/include) include_directories(${OPENSOURCE_DIR}/include/opencv4) - include_directories(${MXBASE_INC}) + +include_directories(${MXBASE_POST_LIB_DIR}) + include_directories(${MXBASE_POST_PROCESS_DIR}) link_directories(${ACL_LIB_DIR}) @@ -43,7 +47,9 @@ link_directories(${OPENSOURCE_DIR}/lib) link_directories(${MXBASE_LIB_DIR}) link_directories(${MXBASE_POST_LIB_DIR}) + + add_executable(${TARGET} main.cpp Shufflenetv2Classify.cpp) target_link_libraries(${TARGET} glog cpprest mxbase resnet50postprocess opencv_world stdc++fs) -install(TARGETS ${TARGET} RUNTIME DESTINATION ${PROJECT_SOURCE_DIR}/) \ No newline at end of file +install(TARGETS ${TARGET} RUNTIME DESTINATION ${PROJECT_SOURCE_DIR}/) diff --git a/PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/sdk_infer/mxbase_infer/build.sh b/PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/sdk_infer/mxbase_infer/build.sh new file mode 100644 index 0000000000..d7a957b2a0 --- /dev/null +++ b/PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/sdk_infer/mxbase_infer/build.sh @@ -0,0 +1,62 @@ +#!/bin/bash + +# Copyright 2021 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. + +path_cur=$(dirname $0) + +function check_env() +{ + # set ASCEND_VERSION to ascend-toolkit/latest when it was not specified by user + if [ ! "${ASCEND_HOME}" ]; then + export ASCEND_HOME=/usr/local/Ascend/ + echo "Set ASCEND_HOME to the default value: ${ASCEND_HOME}" + else + echo "ASCEND_HOME is set to ${ASCEND_HOME} by user" + fi + + if [ ! "${ASCEND_VERSION}" ]; then + export ASCEND_VERSION=ascend-toolkit//latest + echo "Set ASCEND_VERSION to the default value: ${ASCEND_VERSION}" + else + echo "ASCEND_VERSION is set to ${ASCEND_VERSION} by user" + fi + + if [ ! "${ARCH_PATTERN}" ]; then + # set ARCH_PATTERN to ./ when it was not specified by user + export ARCH_PATTERN=./ + echo "ARCH_PATTERN is set to the default value: ${ARCH_PATTERN}" + else + echo "ARCH_PATTERN is set to ${ARCH_PATTERN} by user" + fi +} + +function build_east() +{ + cd $path_cur + rm -rf build + mkdir -p build + cd build + cmake .. + make + ret=$? + if [ ${ret} -ne 0 ]; then + echo "Failed to build east." + exit ${ret} + fi + make install +} + +check_env +build_east diff --git a/PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/sdk_infer/mxbase_infer/run.sh b/PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/sdk_infer/mxbase_infer/run.sh new file mode 100644 index 0000000000..7f2b4725d9 --- /dev/null +++ b/PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/sdk_infer/mxbase_infer/run.sh @@ -0,0 +1,22 @@ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the BSD 3-Clause License (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://opensource.org/licenses/BSD-3-Clause +# +# 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. + + +image_path=$1 limitnum=$2 + +export LD_LIBRARY_PATH=${MX_SDK_HOME}/lib:${MX_SDK_HOME}/lib/modelpostprocessors:${MX_SDK_HOME}/opensource/lib:${MX_SDK_HOME}/opensource/lib64:/usr/local/Ascend/ascend-toolkit/latest/acllib/lib64:${LD_LIBRARY_PATH} + +# run +./shufflenetv2 $image_path $limitnum + diff --git a/PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/sdk_infer/sdk_run_infer/run.sh b/PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/sdk_infer/sdk_run_infer/run.sh new file mode 100644 index 0000000000..114479bfee --- /dev/null +++ b/PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/sdk_infer/sdk_run_infer/run.sh @@ -0,0 +1,29 @@ +#!/bin/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. + + +image_path=$1 +result_dir=$2 + +set -e + +#to set PYTHONPATH, import the StreamManagerApi.py +export PYTHONPATH=$PYTHONPATH:${MX_SDK_HOME}/python +export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/sdk_home/mxManufacture/lib/modelpostprocessors + +image_path=$1 +python3 main.py $image_path $result_dir +exit 0 diff --git a/PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/sdk_infer/task_metric.py b/PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/sdk_infer/util/task_metric.py similarity index 100% rename from PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/sdk_infer/task_metric.py rename to PyTorch/built-in/cv/classification/Shufflenetv2_for_PyTorch/sdk_infer/util/task_metric.py -- Gitee