使用Opencv4和YOLOv4(XTDrone)训练模型遇到问题的记录(二)
阅读原文时间:2023年07月08日阅读:3

使用Opencv4和YOLOv4(XTDrone)训练模型遇到问题的记录(二)

Written By PiscesAlpaca(双鱼座羊驼)

目录

1.在cmake时,需要手动标记生成pgk-config文件

命令如下:

cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=YES -D CMAKE_INSTALL_PREFIX=/usr/local/opencv4 ..

其中,OPENCV_GENERATE_PKGCONFIG=YES即为需要生成opencv4.pc文件,这一文件在之后进行YOLO模型训练时很重要,否则可能会出现找不到Opecv2及其头文件的情况,例如:

Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
./src/image_opencv.cpp:16:10: fatal error: opencv2/core/version.hpp: No such file or directory
16 | #include <opencv2/core/version.hpp>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make: *** [Makefile:174: obj/image_opencv.o] Error 1

2.安装完成后需要配置pgk-config环境

使用如下命令查找opencv4.pc文件的位置

sudo find / -iname opencv4.pc

输出结果一般会有多个,例如:

/home/pisces/opencv/build/unix-install/opencv4.pc
find: ‘/run/user/1000/doc’: 权限不够
find: ‘/run/user/1000/gvfs’: 权限不够
/usr/local/opencv4/lib/pkgconfig/opencv4.pc

选择usr/local开头的即为所需文件路径

/usr/local/opencv4/lib/pkgconfig/路径加入PKG_CONFIG_PATH,使用命令(该文件可能为空文件):

sudo gedit /etc/profile.d/pkgconfig.sh

在打开的文件中键入如下语句:

export PKG_CONFIG_PATH=/usr/local/opencv4/lib/pkgconfig:$PKG_CONFIG_PATH

保存退出后,使用如下命令激活:

source /etc/profile

接着,验证是否配置成功,键入命令:

pkg-config --libs opencv4

输出形式为如下所示即为成功(根据个人环境而定,可以不同):

-L/usr/local/opencv4/lib -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_barcode -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cudabgsegm -lopencv_cudafeatures2d -lopencv_cudaobjdetect -lopencv_cudastereo -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_sfm -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_cudacodec -lopencv_surface_matching -lopencv_tracking -lopencv_highgui -lopencv_datasets -lopencv_text -lopencv_plot -lopencv_videostab -lopencv_cudaoptflow -lopencv_optflow -lopencv_cudalegacy -lopencv_videoio -lopencv_cudawarping -lopencv_viz -lopencv_wechat_qrcode -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_dnn -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_cudaimgproc -lopencv_cudafilters -lopencv_imgproc -lopencv_cudaarithm -lopencv_core -lopencv_cudev

继续配置动态苦环境,使得程序运行时可以加载.so动态库文件,使用命令:

sudo gedit /etc/ld.so.conf.d/opencv4.conf

在打开的文件中键入如下语句:

/usr/local/opencv4/lib

保存退出后,执行以下命令使刚才配置的路径生效:

sudo ldconfig

1.Couldn't find activation function mish, going with ReLU

XTDrone提供的darknet仓库和https://github.com/pjreddie/darknet仓库在进行模型训练时都会遇到如下问题Couldn't find activation function mish, going with ReLU:

layer     filters    size              input                output
    0 Couldn't find activation function mish, going with ReLU
conv     32  3 x 3 / 1   416 x 416 x   3   ->   416 x 416 x  32  0.299 BFLOPs
    1 Couldn't find activation function mish, going with ReLU
conv     64  3 x 3 / 2   416 x 416 x  32   ->   208 x 208 x  64  1.595 BFLOPs
    2 Couldn't find activation function mish, going with ReLU
conv     64  1 x 1 / 1   208 x 208 x  64   ->   208 x 208 x  64  0.354 BFLOPs

需要替换为https://github.com/AlexeyAB/darknet仓库的darknet即可解决

2.mosaic=1-compile Darknet with Opencv for using mosaic=1

替换~/catkin_ws/src/darknet_ros/darknet/cfg/xtdrone/obj_yolov4.cfg文件中:

#cutmix=1
mosaic=1

#cutmix=1
mosaic=0

3.CUDA Error: out of memory darknet: ./src/cuda.c:36: check_error: Assertio `0' failed.

替换~/catkin_ws/src/darknet_ros/darknet/cfg/xtdrone/obj_yolov4.cfg文件中:

subdivisions=1

subdivisions=64

subdivision会让每一个batch不是一下子都丢到网络里。而是分成subdivision对应数字的份数,一份一份的跑完后,在一起打包算作完成一次iteration。这样会降低对显存的占用情况。如果设置这个参数为1的话就是一次性把所有batch的图片都丢到网络里,如果为2的话就是一次丢一半。

4.XTDrone提供的darknet库存在一些预先make过的文件,需要在本机重新make一遍,否则有些文件在本机无法找到。darknet也无需cmake,直接make就行。

5.make编译问题

darknet的MakeFile文件需要结合自己的GPU修改ARCH,如:

ARCH= -gencode arch=compute_35,code=sm_35 \
      -gencode arch=compute_50,code=[sm_50,compute_50] \
      -gencode arch=compute_52,code=[sm_52,compute_52] \
      -gencode arch=compute_61,code=[sm_61,compute_61] \
      -gencode arch=compute_75,code=[sm_75,compute_75]

需要结合GPU的运行能力,添加75 80 85等参数,参数30已经过时需要删除,35也即将过时。

1.无法找到opencv2头文件

正如上一节所示,在执行./darknet detector train cfg/xtdrone/obj.data cfg/xtdrone/obj_yolov4.cfg backup/yolov4.conv.137 -map命令时(具体见XTDrone教程:https://www.yuque.com/xtdrone/manual_cn/target_detection_tracking),会出现无法找到opencv2头文件的情况,使用如下方法即可解决:

手动创建软链接命令如下:

sudo ln -s /usr/local/opencv4/lib/pkgconfig/opencv4.pc /usr/local/lib/pkgconfig/opencv4.pc

解释:由于darknet在寻找.pc文件时,是在/usr/local/lib/pkgconfig里寻找的,因此在该目录创建指向opencv4.pc文件的位置即可

此时yolo的训练过程就可以正常运行了

2.关于YOLO不使用GPU的问题

如果经过上述修改还是使用CPU ,则可能是darknet的MakefIle文件中如下字段没有修改,均改为1重新编译即可:

GPU=1
CUDNN=1
OPENCV=1

[1] Ubuntu 18.04安装OpenCV4.0和环境配置(重要): https://blog.csdn.net/new_delete_/article/details/84797041

[2] windows下训练yolo时出现CUDA Error: out of memory问题的解决: https://blog.csdn.net/qq_33485434/article/details/80432054

[3] Couldn't find activation function mish, going with ReLU: https://github.com/pjreddie/darknet/issues/2440

[4] ubuntu搭建darknet框架并使用alexeyAB版本的yolo跑demo: https://blog.csdn.net/weixin_42630613/article/details/107834361

部分参考资料或有遗漏,再次也对提供的帮助表示感谢,如有必要可联系我增加。


转载请注明出处!

本篇发布在以下博客或网站:

双鱼座羊驼 - 知乎 (zhihu.com)

双鱼座羊驼的博客_CSDN博客

双鱼座羊驼 - SegmentFault 思否

双鱼座羊驼 的个人主页 - 动态 - 掘金 (juejin.cn)

双鱼座羊驼 - 博客园 (cnblogs.com)

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章