Giter VIP home page Giter VIP logo

yolov5-dnn-cpp-python's Introduction

yolov5-dnn-cpp-py

yolov5s,yolov5l,yolov5m,yolov5x的onnx文件在百度云盘下载, 链接:https://pan.baidu.com/s/1d67LUlOoPFQy0MV39gpJiw 提取码:bayj

python版本的主程序是main_yolov5.py,C++版本的主程序是main_yolo.cpp

运行整套程序只需要安装opencv库(4.0以上版本的),彻底摆脱对深度学习框架的依赖

如果你想运行生成onnx文件的程序,那么就cd到convert-onnx文件夹,在百度云盘下载yolov5s,yolov5l,yolov5m,yolov5x的.pth文件放在该目录里, 百度云盘链接: https://pan.baidu.com/s/1oIdwpp6kuasANMInTpHnrw 密码: m3n1

这4个pth文件是从https://github.com/ultralytics/yolov5 的pth文件里抽取出参数,保存到顺序字典OrderedDict里,最后生成新的pth文件 在convert-onnx文件夹里,我把4种yolov5的网络结构全都定义在.py文件里,这样便于读者直观的了解网络结构以及层与层的连接关系。 下载完成pth文件后,运行convert_onnx.py就可以生成.onnx文件,这个程序需要依赖pytorch1.7.0框架,如果pytorch版本低了,程序运行会报错。 因为在yolov5里有新的激活函数,旧版本pytorch可能不支持的

在编写这套程序时,遇到的bug和解决办法,可以阅读我的csdn博客 https://blog.csdn.net/nihate/article/details/112731327

2022年2月26日,看到https://github.com/ultralytics/yolov5 在最近更新的v6.1版本的, 我编写了分别使用OpenCV、ONNXRuntime部署yolov5-v6.1目标检测,包含C++和Python两个版本的程序。 源码地址是: https://github.com/hpc203/yolov5-v6.1-opencv-onnxrun

yolov5-dnn-cpp-python's People

Contributors

hpc203 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

yolov5-dnn-cpp-python's Issues

Error and optimization

yolov5 release tag v5 , main_yolov5.py run error in line 88: “bs, _, ny, nx, _ = outs[i].shape“ ,perhaps need change to “bs, _, ny, nx, _ = outs[i].shape ” ?

Other, detect decoding is too slow, how too ?

请问这个 c++ demo入和运行

您好,我刚刚接触C++,请问这个代码如何测试
我在网上查

g++ -o test main_yolo.cpp

可是报错

In file included from main_yolo.cpp:1:
yolo.h:7:10: fatal error: opencv2/dnn.hpp: 没有那个文件或目录
 #include <opencv2/dnn.hpp>
          ^~~~~~~~~~~~~~~~~
compilation terminated.

但是在我电脑里面终端输入

whereis opencv

会得到

opencv: /usr/include/opencv /usr/share/opencv

请问是哪儿存在了问题

祝好
tongchangD

terminate called after throwing an instance of 'cv::Exception'

/yolov5-dnn-cpp-python/build$ ./opencv_test_project
Net use yolov5s
terminate called after throwing an instance of 'cv::Exception'
what(): OpenCV(4.5.3-pre) /home/code/opencv/modules/dnn/src/onnx/onnx_importer.cpp:78: error: (-5:Bad argument) Can't read ONNX file: yolov5s.onnx in function 'ONNXImporter'

Aborted (core dumped)

undefined symbol: _ZN2cv3dnn14dnn4_v202009083Net7forwardERKNS_12

cmake .. make 都没报错
opencv4.5.0

./opencv_test_project: symbol lookup error: ./opencv_test_project: undefined symbol: _ZN2cv3dnn14dnn4_v202009083Net7forwardERKNS_12_OutputArrayERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISC_EE

error. can you help me plz

Traceback (most recent call last):
File "C:/Users/xtuye/yolov5-dnn-cpp-python/main_yolov5.py", line 114, in
dets = yolonet.detect(srcimg)
File "C:/Users/xtuye/yolov5-dnn-cpp-python/main_yolov5.py", line 90, in detect
outs[i] = outs[i].reshape(bs, self.na, self.no, ny, nx).transpose(0, 1, 3, 4, 2)
TypeError: 'tuple' object does not support item assignment

导出ONNX的问题

看了您的博文,明白了要想将导出的onnx模型供cv2.dnn调用,需要改写官方源码。但作为小白,怎么改写还是不太懂。能将这个详细过程再讲一遍吗?十分感谢。

How to run?

How to run code? arguments?
where is onnx file?

run convert_onnx.py , I get the warning TracerWarning: Converting a tensor to a Python integer might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs! route = F.interpolate(xRt3, size=(int(xRt3.shape[2] * 2), int(xRt3.shape[3] * 2)), mode='nearest')

Then when I infer the *.onnx by above, I get :
cannot reshape array of size 1632000 into shape (1,3,6,80,80)
cannot reshape array of size 408000 into shape (1,3,6,40,40)
cannot reshape array of size 102000 into shape (1,3,6,20,20)
Use YOLOV5 v3.1 yolov5s.pt file.

Encoding Outputs to Bounding boxes

First of all, I really appreciate your work!
While I was trying to work with your code, I found it hard to understand how you enccoded the raw outputs of the onnx model -
(tx,ty,tw,th) to (bx,by,bh,bw). Can you explain how you did it?

Thanks

Does it work for own pretrained weights?

Greetings! First off this is great work and I'm happy to maybe finally find a solution to my problem.

Does your code work for own weights as well, which I've gained by training on a custom dataset with YOLOv5? I need to export the .pt to .onnx files, so I can use my own wiehgts with OpenCV DNN.

how to get _param.pth?

you mean convert yolov5s.pt to yolov5_param.pth?

is there a difference beteween .pt and .pth?
wow, i hope to recieve your answer。

标注的confidences全都太高,接近于1

找了一些原因,可能是由于在原版yolov5中:
# Compute conf
x[:, 5:] *= x[:, 4:5] # conf = obj_conf * cls_conf
但是我发现我这边得到的obj_conf(detection[5])比原版的要低很多,乘下来就只有0.2左右了。
我不是太清楚,这个是怎么计算的,想请教一下您😂
在您的代码中也有类似的描述:
###其实只需要对x,y,w,h做sigmoid变换的, 不过全做sigmoid变换对结果影响不大,因为sigmoid是单调递增函数,那么就不影响类别置信度的排序关系,因此不影响后面的NMS
###不过设断点查看类别置信度,都是负数,看来有必要做sigmoid变换把概率值强行拉回到0到1的区间内

谢谢!

TracerWarning: Converting a tensor to a Python integer might cause the trace to be incorrect.

运行convert_onnx.py时出现下列警告:
Namespace(net_type='yolov5s')
360 360
360 360
H:\yuanbaoxi\ybx_gitee\yolov5-dnn-cpp-py\yolov5s.py:36: TracerWarning: Converting a tensor to a Python integer might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
route = F.interpolate(xRt2, size=(int(xRt2.shape[2] * 2), int(xRt2.shape[3] * 2)), mode='nearest')
H:\yuanbaoxi\ybx_gitee\yolov5-dnn-cpp-py\yolov5s.py:40: TracerWarning: Converting a tensor to a Python integer might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
route = F.interpolate(xRt3, size=(int(xRt3.shape[2] * 2), int(xRt3.shape[3] * 2)), mode='nearest')
convert yolov5s.onnx to onnx finish!!!
然后出现下面错误:
read failed

OpenCV开了cuda加速,但是没有提升,会是什么原因呢?

您好,opencv的cuda加速在yolact上很明显,大概有5倍的提升,但是在yolov5s上,几乎没有变化,想请教您可能是什么原因呢?非常感谢!

string modelFile = this->netname;
modelFile += ".onnx";
cout << "load modelFile : " << modelFile << endl; 
this->net = readNet(modelFile);
// 增加这两行用来加速 
this->net.setPreferableBackend(DNN_BACKEND_CUDA);
this->net.setPreferableTarget(DNN_TARGET_CUDA);

run c++ main_yolo.cpp error

OpenCV(4.0.0) Error: Requested object was not found (Blob 363 not found in const blobs) in cv::dnn::dnn4_v20180917::ONNXImporter::getBlob, file c:\build\master_winpack-build-win64-vc15\opencv\modules\dnn\src\onnx\onnx_importer.cpp, line 272

cv2.error:

pytorch1.7.1 cv2 4.3.0 win10
I download the yolov5s.pth then I run the convert_onnx.py,the onnx file ran normally,but in the end of convert_onnx.py dnnnet = cv2.dnn.readNet(output_onnx) failed:error: (-212:Parsing error) Input node with name 873 not found in function 'cv::dnn::Subgraph::getInputNodeId'

python 如何用opencv部署?

1.修改,因为提示参数不多,返回的只有三个参数
#bs, _, ny, nx = outs[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85)
bs, ny, nx = outs[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85)
修改参数之后,包这个错误:
ValueError: cannot reshape array of size 151200 into shape (1,3,6,25200,6)

[INFO]blob shape: (1, 3, 640, 640)
0 (1, 25200, 6)
Traceback (most recent call last):
File "main_yolov5.py", line 124, in
dets = yolonet.detect(srcimg)
File "main_yolov5.py", line 99, in detect
outs[i] = outs[i].reshape(bs, self.na, self.no, ny, nx).transpose(0, 1, 3, 4, 2)
ValueError: cannot reshape array of size 151200 into shape (1,3,6,25200,6)
image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.