一、关键步骤
下载源码: ACL_YOLOV3
1.1 安装opencv
安装编译opencv静态链接库。
1.1.1 下载
链接:https://pan.baidu.com/s/1sdCsP3_NU48Nz8PYw-wpkg 提取码:1234
或者
链接: https://pan.baidu.com/s/1Z8sGoSI3eqUfime9aF5wSQ 密码: r80o
1.1.2 解压
解压opencv压缩包到指定目录,如:/home/ma-user/work/opencv-4.5.1
。
1.2 修改CMakeLists.txt
# 修改OpenCV_DIR路径 # OpenCV_DIR目录包含OpenCVConfig.cmake set(OpenCV_DIR /home/ma-user/work/opencv-4.5.1/lib/cmake/opencv4) # Find OpenCV, you may need to set OpenCV_DIR variable # to the absolute path to the directory containing OpenCVConfig.cmake file # via the command line or GUI find_package(OpenCV REQUIRED) # If the package has been found, several variables will # be set, you can find the full list with descriptions # in the OpenCVConfig.cmake file. # Print some message showing some of them if(${OPENCV_FOUND}) message(STATUS "OpenCV version: ${OpenCV_VERSION}") message(STATUS "OpenCV include path: ${OpenCV_INCLUDE_DIRS}") message(STATUS "OpenCV libraries: ${OpenCV_LIBS}") endif() # Add OpenCV headers location to your include paths include_directories(${OpenCV_INCLUDE_DIRS}) # 修改ACL_PATH路径 set(ACL_PATH /home/ma-user/Ascend/ascend-toolkit/latest)
1.3 模型转换
目标检测模型
1.3.1 模型下载
https://pan.baidu.com/s/1yiCrnmsOm0hbweJBiiUScQ
或者
链接:https://pan.baidu.com/s/1xZ2UuQAchtnpVrcCO6zQPA 提取码:1234
1.3.2 修改 prototxt 配置文件
完全按模板修改:YOLOv3网络模型prototxt修改
-
upsample算子upsample_param修改属性参数;
修改两处。
-
新增三个Yolo算子;
-
最后一层增加YoloV3DetectionOutput算子;
-
新增输入;
原输入:
input: "data" input_shape { dim: 1 dim: 3 dim: 416 dim: 416 }
新增输入:
input: "img_info" input_shape { dim: 1 span class="token literal-property property">dim: 4 }
1.3.3 修改 aipp 配置文件
配置文件模板
不修改。
1.3.4 模型转换
cd yolov3
atc --model=./yolov3.prototxt \
--weight=./yolov3.caffemodel \
--framework=0 \
--output=./yolov3_aipp \
--output_type=FP32 \
--soc_version=Ascend910 \
--insert_op_conf=./aipp_yolov3.cfg
1.4 运行成功
[ma-user@notebook-87136e07-6a9a-4138-beec-742972f7b62f acl_yolov3]$ ./ACL_YOLOV3 ./data/model/yolov3_aipp.om ./test.jpg
acl init successfully
Create context successfully
set context successfully
Create stream successfully
ModelProcess:Begin to init instance.
finish init AclProcess
ModelProcess:Begin to inference.
begin postprocess
The number of output buffers of yolov3 model is 2
Object detected number is 1
#Obj0, box(427.5, 424.25, 1513, 851.5) confidence: 1 lable: 2
model run success!
cost time : 18.924ms
ModelProcess:Begin to deinit instance.
ModelProcess:Finished deinit instance.
all tasks in stream done
Destroy Stream successfully
Destroy Context successfully
acl deinit successfully
二、FAQ
Q:缺少-fPIC
/usr/bin/ld: CMakeFiles/ACL_YOLOV3.dir/main.cpp.o: relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `_ZSt4cout@@GLIBCXX_3.4' which may bind externally can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: CMakeFiles/ACL_YOLOV3.dir/main.cpp.o(.text+0x2c): unresolvable R_AARCH64_ADR_PREL_PG_HI21 relocation against symbol `_ZSt4cout@@GLIBCXX_3.4'
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/ACL_YOLOV3.dir/build.make:144: ../ACL_YOLOV3] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/ACL_YOLOV3.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
解决办法:
修改CMakeLists.txt
add_definitions(-fPIC)
Q:缺少 dlsym
/usr/bin/ld: /home/ma-user/work/opencv-4.5.1/lib/libopencv_core.a(filesystem.cpp.obj): in function `cv::plugin::impl::DynamicLib::getSymbol(char const*) const': filesystem.cpp:(.text._ZNK2cv6plugin4impl10DynamicLib9getSymbolEPKc+0x8): undefined reference to `dlsym' /usr/bin/ld: /home/ma-user/work/opencv-4.5.1/lib/libopencv_core.a(filesystem.cpp.obj): in function `cv::plugin::impl::DynamicLib::libraryLoad(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
filesystem.cpp:(.text._ZN2cv6plugin4impl10DynamicLib11libraryLoadERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE+0x3c): undefined reference to `dlopen' /usr/bin/ld: /home/ma-user/work/opencv-4.5.1/lib/libopencv_core.a(filesystem.cpp.obj): in function `cv::plugin::impl::DynamicLib::libraryRelease()': filesystem.cpp:(.text._ZN2cv6plugin4impl10DynamicLib14libraryReleaseEv+0x3b4): undefined reference to `dlclose'
/usr/bin/ld: /home/ma-user/work/opencv-4.5.1/lib/libopencv_core.a(datafile.cpp.obj): in function `cv::utils::getModuleLocation(void const*)': datafile.cpp:(.text._ZN2cv5utilsL17getModuleLocationEPKv+0x2c): undefined reference to `dladdr' /usr/bin/ld: /home/ma-user/work/opencv-4.5.1/lib/libopencv_core.a(datafile.cpp.obj): in function `cv::utils::getBinLocation(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
datafile.cpp:(.text._ZN2cv5utils14getBinLocationERNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE+0x30): undefined reference to `dladdr'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/ACL_YOLOV3.dir/build.make:144: ../ACL_YOLOV3] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/ACL_YOLOV3.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
解决办法:
修改CMakeLists.txt,添加dl
target_link_libraries(${PROJECT_NAME} ascendcl ${OpenCV_LIBS} pthread -Wl,-z,relro,-z,now,-z,noexecstack -pie -s stdc++)
修改为
target_link_libraries(${PROJECT_NAME} ascendcl ${OpenCV_LIBS} pthread -Wl,-z,relro,-z,now,-z,noexecstack -pie -s stdc++ dl)
Q:缺少acl.json文件
[ma-user@notebook-87136e07-6a9a-4138-beec-742972f7b62f acl_yolov3]$ ./ACL_YOLOV3 ./data/model/yolov3_aipp.om ./test.jpg
Failed to init acl, ret = 100003
AclProcess Init faild.
some tasks in stream not done, ret = 107002
all tasks in stream done
Destroy Stream faild, ret = 100000
Destroy Stream successfully
Destroy Context faild, ret = 100000
Destroy Context successfully
acl deinit successfully
错误原因:
缺少acl.json文件,导致aclInit初始化失败
解决办法:
aclError ret = aclprocess.Init(0,"./data/acl.json",argv[1]);
改为
aclError ret = aclprocess.Init(0,"",argv[1]);
Q:保存图片无法显示
错误原因: 由于采用opencv静态链接库,导致jpg
编码器无法使用(功能不完整) 解决办法: 使用png格式保存图片 imwrite("result.jpg",img); 改为 imwrite("result.png",img);