Giter VIP home page Giter VIP logo

yolov5-dnn-cpp-python-v2's Introduction

yolov5-dnn-cpp-python-v2

这套程序是对上一版本的后处理模块优化,把三个尺度的输出特征图作reshape和permute维度置换后, 输出特征图的形状分别是(3x80x80, 85), (3x40x40, 85), (3x20x20, 85),然后在行方向拼接成一个特征图。 列方向的长度保持为(num_classes+5),这样在求最大分类置信度时,可以使用opencv内置函数minMaxLoc, 这相比于上一个版本,减少了一个for循环

由于对特征图添加了维度变换的操作,那么生成onnx文件也发生了改变,新的onnx在百度云盘下载 链接: https://pan.baidu.com/s/11uF1QeYyu3otrGbMGhw0ZQ 密码: es2w

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-v2'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

yolov5-dnn-cpp-python-v2's Issues

opencv error

I have problem of loading onnx model by opencv. How to fix it? Thx.
pytorch 1.7.1
opencv 4.3.0
Here is the detail:

python main_yolov5.py --net_type yolov5s
Traceback (most recent call last):
File "main_yolov5.py", line 110, in
yolonet = yolov5(args.net_type, confThreshold=args.confThreshold, nmsThreshold=args.nmsThreshold, objThreshold=args.objThreshold)
File "main_yolov5.py", line 21, in init
self.net = cv2.dnn.readNet(yolo_type + '.onnx')

cv2.error: OpenCV(4.3.0) /io/opencv/modules/dnn/src/graph_simplifier.cpp:79: error: (-212:Parsing error) Input node with name 925 not found in function 'getInputNodeId'

Error with custom model yolov5m

I train my model with your changes, y test this using onnx runtime and all is ok , but in OpenCV I get this error:
outs[row_ind:row_ind+length, 0:2] = (outs[row_ind:row_ind+length, 0:2] * 2. - 0.5 + np.tile(self.grid[i],(self.na, 1))) * int(self.stride[i])
ValueError: operands could not be broadcast together with shapes (1,2,8) (19200,2)

Sigmoid放入onnx

试了一下效果挺好的。请问如果把Sigmoid那层放入onnx来做,是不是更好些?

部署硬件

请问一下有推荐的硬件来部署吗 感谢!!

转换时的警告

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')

大佬 请问这个警告影响推理吗?

formulation problem

I cannot understand the formulation:
outs[row_ind:row_ind+length, 0:2] = (outs[row_ind:row_ind+length, 0:2] * 2. - 0.5 + np.tile(self.grid[i],(self.na, 1))) * int(self.stride[i])
outs[row_ind:row_ind+length, 2:4] = (outs[row_ind:row_ind+length, 2:4] * 2) ** 2 * np.repeat(self.anchor_grid[i],h*w, axis=0)
would you please help explain it

转换yolov5n时发生错误

您好,大佬:
在这个 fork 中,我根据运行您另一个仓库https://github.com/hpc203/yolov5-face-landmarks-opencv-v2 里的main_export_onnx.py文件调整了一些参数以及更新了common.py部分代码,成功转换出yolov5s.onnx并读取成功,实际测试时在 1660 Ti max q 上跑纯推理平均10ms,同时运行浏览器看视频的话12ms。

然而,在转换yolov5n的时候出现了如下错误:

RuntimeError: Given groups=1, weight of size [32, 128, 1, 1], expected input[1, 256, 26, 26] to have 128 channels, but got 256 channels instead

部分Trace:
...File "F:\Downloads\yolov5-dnn-cpp-python-v2-main\convert-onnx\common.py", line 41, in forward
return self.act(self.bn(self.conv(x)))
...File "F:\Downloads\yolov5-dnn-cpp-python-v2-main\convert-onnx\convert_onnx.py", line 107, in
torch.onnx.export(onnx_model, inputs, output_onnx, verbose=False, opset_version=12, input_names=['images'], output_names=['out'])
...File "F:\Downloads\yolov5-dnn-cpp-python-v2-main\convert-onnx\yolov5n.py", line 38, in forward
x = self.seq13_C3(x)
...File "F:\Downloads\yolov5-dnn-cpp-python-v2-main\convert-onnx\common.py", line 248, in forward
return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), dim=1))

其他错误都是torch包里的。

以及一些运行时print的:

Namespace(net_type='yolov5n', num_classes=2)
349 348
13.cv1.conv.weight backbone_head.seq13_C3.cv1.conv.weight torch.Size([64, 256, 1, 1]) torch.Size([32, 128, 1, 1])
13.cv1.bn.weight backbone_head.seq13_C3.cv1.bn.weight torch.Size([64]) torch.Size([32])
13.cv1.bn.bias backbone_head.seq13_C3.cv1.bn.bias torch.Size([64]) torch.Size([32])
13.cv1.bn.running_mean backbone_head.seq13_C3.cv1.bn.running_mean torch.Size([64]) torch.Size([32])
13.cv1.bn.running_var backbone_head.seq13_C3.cv1.bn.running_var torch.Size([64]) torch.Size([32])
13.cv2.conv.weight backbone_head.seq13_C3.cv2.conv.weight torch.Size([64, 256, 1, 1]) torch.Size([32, 128, 1, 1])
13.cv2.bn.weight backbone_head.seq13_C3.cv2.bn.weight torch.Size([64]) torch.Size([32])
13.cv2.bn.bias backbone_head.seq13_C3.cv2.bn.bias torch.Size([64]) torch.Size([32])
13.cv2.bn.running_mean backbone_head.seq13_C3.cv2.bn.running_mean torch.Size([64]) torch.Size([32])
13.cv2.bn.running_var backbone_head.seq13_C3.cv2.bn.running_var torch.Size([64]) torch.Size([32])
13.cv3.conv.weight backbone_head.seq13_C3.cv3.conv.weight torch.Size([128, 128, 1, 1]) torch.Size([64, 64, 1, 1])
13.cv3.bn.weight backbone_head.seq13_C3.cv3.bn.weight torch.Size([128]) torch.Size([64])
13.cv3.bn.bias backbone_head.seq13_C3.cv3.bn.bias torch.Size([128]) torch.Size([64])
13.cv3.bn.running_mean backbone_head.seq13_C3.cv3.bn.running_mean torch.Size([128]) torch.Size([64])
13.cv3.bn.running_var backbone_head.seq13_C3.cv3.bn.running_var torch.Size([128]) torch.Size([64])
13.m.0.cv1.conv.weight backbone_head.seq13_C3.m.0.cv1.conv.weight torch.Size([64, 64, 1, 1]) torch.Size([32, 32, 1, 1])
13.m.0.cv1.bn.weight backbone_head.seq13_C3.m.0.cv1.bn.weight torch.Size([64]) torch.Size([32])
13.m.0.cv1.bn.bias backbone_head.seq13_C3.m.0.cv1.bn.bias torch.Size([64]) torch.Size([32])
13.m.0.cv1.bn.running_mean backbone_head.seq13_C3.m.0.cv1.bn.running_mean torch.Size([64]) torch.Size([32])
13.m.0.cv1.bn.running_var backbone_head.seq13_C3.m.0.cv1.bn.running_var torch.Size([64]) torch.Size([32])
13.m.0.cv2.conv.weight backbone_head.seq13_C3.m.0.cv2.conv.weight torch.Size([64, 64, 3, 3]) torch.Size([32, 32, 3, 3])
13.m.0.cv2.bn.weight backbone_head.seq13_C3.m.0.cv2.bn.weight torch.Size([64]) torch.Size([32])
13.m.0.cv2.bn.bias backbone_head.seq13_C3.m.0.cv2.bn.bias torch.Size([64]) torch.Size([32])
13.m.0.cv2.bn.running_mean backbone_head.seq13_C3.m.0.cv2.bn.running_mean torch.Size([64]) torch.Size([32])
13.m.0.cv2.bn.running_var backbone_head.seq13_C3.m.0.cv2.bn.running_var torch.Size([64]) torch.Size([32])
anchor
24.m.0.weight yolo_layers.m.0.bias torch.Size([255, 64, 1, 1]) torch.Size([21])
24.m.0.bias yolo_layers.m.1.weight torch.Size([255]) torch.Size([21, 128, 1, 1])
24.m.1.weight yolo_layers.m.1.bias torch.Size([255, 128, 1, 1]) torch.Size([21])
24.m.1.bias yolo_layers.m.2.weight torch.Size([255]) torch.Size([21, 256, 1, 1])
24.m.2.weight yolo_layers.m.2.bias torch.Size([255, 256, 1, 1]) torch.Size([21])
349 348
13.cv1.conv.weight backbone.seq13_C3.cv1.conv.weight torch.Size([64, 256, 1, 1]) torch.Size([32, 128, 1, 1])
13.cv1.bn.weight backbone.seq13_C3.cv1.bn.weight torch.Size([64]) torch.Size([32])
13.cv1.bn.bias backbone.seq13_C3.cv1.bn.bias torch.Size([64]) torch.Size([32])
13.cv1.bn.running_mean backbone.seq13_C3.cv1.bn.running_mean torch.Size([64]) torch.Size([32])
13.cv1.bn.running_var backbone.seq13_C3.cv1.bn.running_var torch.Size([64]) torch.Size([32])
13.cv2.conv.weight backbone.seq13_C3.cv2.conv.weight torch.Size([64, 256, 1, 1]) torch.Size([32, 128, 1, 1])
13.cv2.bn.weight backbone.seq13_C3.cv2.bn.weight torch.Size([64]) torch.Size([32])
13.cv2.bn.bias backbone.seq13_C3.cv2.bn.bias torch.Size([64]) torch.Size([32])
13.cv2.bn.running_mean backbone.seq13_C3.cv2.bn.running_mean torch.Size([64]) torch.Size([32])
13.cv2.bn.running_var backbone.seq13_C3.cv2.bn.running_var torch.Size([64]) torch.Size([32])
13.cv3.conv.weight backbone.seq13_C3.cv3.conv.weight torch.Size([128, 128, 1, 1]) torch.Size([64, 64, 1, 1])
13.cv3.bn.weight backbone.seq13_C3.cv3.bn.weight torch.Size([128]) torch.Size([64])
13.cv3.bn.bias backbone.seq13_C3.cv3.bn.bias torch.Size([128]) torch.Size([64])
13.cv3.bn.running_mean backbone.seq13_C3.cv3.bn.running_mean torch.Size([128]) torch.Size([64])
13.cv3.bn.running_var backbone.seq13_C3.cv3.bn.running_var torch.Size([128]) torch.Size([64])
13.m.0.cv1.conv.weight backbone.seq13_C3.m.0.cv1.conv.weight torch.Size([64, 64, 1, 1]) torch.Size([32, 32, 1, 1])
13.m.0.cv1.bn.weight backbone.seq13_C3.m.0.cv1.bn.weight torch.Size([64]) torch.Size([32])
13.m.0.cv1.bn.bias backbone.seq13_C3.m.0.cv1.bn.bias torch.Size([64]) torch.Size([32])
13.m.0.cv1.bn.running_mean backbone.seq13_C3.m.0.cv1.bn.running_mean torch.Size([64]) torch.Size([32])
13.m.0.cv1.bn.running_var backbone.seq13_C3.m.0.cv1.bn.running_var torch.Size([64]) torch.Size([32])
13.m.0.cv2.conv.weight backbone.seq13_C3.m.0.cv2.conv.weight torch.Size([64, 64, 3, 3]) torch.Size([32, 32, 3, 3])
13.m.0.cv2.bn.weight backbone.seq13_C3.m.0.cv2.bn.weight torch.Size([64]) torch.Size([32])
13.m.0.cv2.bn.bias backbone.seq13_C3.m.0.cv2.bn.bias torch.Size([64]) torch.Size([32])
13.m.0.cv2.bn.running_mean backbone.seq13_C3.m.0.cv2.bn.running_mean torch.Size([64]) torch.Size([32])
13.m.0.cv2.bn.running_var backbone.seq13_C3.m.0.cv2.bn.running_var torch.Size([64]) torch.Size([32])
anchor
24.m.0.weight m0.bias torch.Size([255, 64, 1, 1]) torch.Size([21])
24.m.0.bias m1.weight torch.Size([255]) torch.Size([21, 128, 1, 1])
24.m.1.weight m1.bias torch.Size([255, 128, 1, 1]) torch.Size([21])
24.m.1.bias m2.weight torch.Size([255]) torch.Size([21, 256, 1, 1])
24.m.2.weight m2.bias torch.Size([255, 256, 1, 1]) torch.Size([21])

请问我应该如何调整代码或网络参数来避免这个错误呢?

ValueError: operands could not be broadcast together with shapes (1,2,85) (19200,2)

Hi!

I'm getting the following error:

$ python3 main_yolov5.py --net_type yolov5n
Traceback (most recent call last):
  File "main_yolov5.py", line 111, in <module>
    dets = yolonet.detect(srcimg)
  File "main_yolov5.py", line 95, in detect
    outs[row_ind:row_ind+length, 0:2] = (outs[row_ind:row_ind+length, 0:2] * 2. - 0.5 + np.tile(self.grid[i],(self.na, 1))) * int(self.stride[i])
ValueError: operands could not be broadcast together with shapes (1,2,85) (19200,2) 

I'm using OpenCV 4.5.3 and Python 3.8.10

My yolov5s.onnx file was generated using:

git clone https://github.com/ultralytics/yolov5
cd yolov5
pip install -r requirements.txt
python3 export.py --weights yolov5s.pt --img 640 --include onnx

Any ideas what could be wrong?

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.