Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output nodes before 5D Reshape #2881

Closed
PiyalGeorge opened this issue Apr 21, 2021 · 12 comments
Closed

Output nodes before 5D Reshape #2881

PiyalGeorge opened this issue Apr 21, 2021 · 12 comments
Labels
question Further information is requested Stale

Comments

@PiyalGeorge
Copy link

Can you specify the output nodes before 5D Reshape?
I'm using a 320X320 yolov5s model from 3.1 release.

Or

In other way:
I converted the yolov5s model to onnx model following this .
Then i simplified the onnx model using this . I tried this in yolov5s - release number :3.1

I wish to know "the output nodes before 5D Reshape". How can i get these output nodes? Kindly help me to know these output nodes.

@PiyalGeorge PiyalGeorge added the question Further information is requested label Apr 21, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Apr 21, 2021

👋 Hello @PiyalGeorge, thank you for your interest in 🚀 YOLOv5! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://www.ultralytics.com or email Glenn Jocher at glenn.jocher@ultralytics.com.

Requirements

Python 3.8 or later with all requirements.txt dependencies installed, including torch>=1.7. To install run:

$ pip install -r requirements.txt

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), testing (test.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

@glenn-jocher
Copy link
Member

glenn-jocher commented Apr 21, 2021

@PiyalGeorge I'm not sure exactly what you mean by the output nodes before 5D reshape, though the 5D reshape is in the Detect layer, so what you are looking for is probably there.

yolov5/models/yolo.py

Lines 24 to 58 in 5f7d39f

class Detect(nn.Module):
stride = None # strides computed during build
export = False # onnx export
def __init__(self, nc=80, anchors=(), ch=()): # detection layer
super(Detect, self).__init__()
self.nc = nc # number of classes
self.no = nc + 5 # number of outputs per anchor
self.nl = len(anchors) # number of detection layers
self.na = len(anchors[0]) // 2 # number of anchors
self.grid = [torch.zeros(1)] * self.nl # init grid
a = torch.tensor(anchors).float().view(self.nl, -1, 2)
self.register_buffer('anchors', a) # shape(nl,na,2)
self.register_buffer('anchor_grid', a.clone().view(self.nl, 1, -1, 1, 1, 2)) # shape(nl,1,na,1,1,2)
self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch) # output conv
def forward(self, x):
# x = x.copy() # for profiling
z = [] # inference output
self.training |= self.export
for i in range(self.nl):
x[i] = self.m[i](x[i]) # conv
bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85)
x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
if not self.training: # inference
if self.grid[i].shape[2:4] != x[i].shape[2:4]:
self.grid[i] = self._make_grid(nx, ny).to(x[i].device)
y = x[i].sigmoid()
y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy
y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh
z.append(y.view(bs, -1, self.no))
return x if self.training else (torch.cat(z, 1), x)

On a side note, onnx-simplifier is now integrated with YOLOv5 export via PR #2815, you can access it like this in the latest code:

python export.py --simplify

@PiyalGeorge
Copy link
Author

PiyalGeorge commented Apr 22, 2021

@glenn-jocher , Thanks for your reply.
By "the output nodes before 5D Reshape" - I'm expecting the three output nodes like 742, 762, 782.
These node number(eg:742, 762, etc) seems different in each release of yolov5s.
What i want is, for the release number - 3.1 , yolov5s, 320X320 (or 416X416). Hope you got what i had in mind.

@glenn-jocher
Copy link
Member

@PiyalGeorge these operations are in the Detect() layer before merging into a single output:

yolov5/models/yolo.py

Lines 24 to 58 in 5f7d39f

class Detect(nn.Module):
stride = None # strides computed during build
export = False # onnx export
def __init__(self, nc=80, anchors=(), ch=()): # detection layer
super(Detect, self).__init__()
self.nc = nc # number of classes
self.no = nc + 5 # number of outputs per anchor
self.nl = len(anchors) # number of detection layers
self.na = len(anchors[0]) // 2 # number of anchors
self.grid = [torch.zeros(1)] * self.nl # init grid
a = torch.tensor(anchors).float().view(self.nl, -1, 2)
self.register_buffer('anchors', a) # shape(nl,na,2)
self.register_buffer('anchor_grid', a.clone().view(self.nl, 1, -1, 1, 1, 2)) # shape(nl,1,na,1,1,2)
self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch) # output conv
def forward(self, x):
# x = x.copy() # for profiling
z = [] # inference output
self.training |= self.export
for i in range(self.nl):
x[i] = self.m[i](x[i]) # conv
bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85)
x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
if not self.training: # inference
if self.grid[i].shape[2:4] != x[i].shape[2:4]:
self.grid[i] = self._make_grid(nx, ny).to(x[i].device)
y = x[i].sigmoid()
y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy
y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh
z.append(y.view(bs, -1, self.no))
return x if self.training else (torch.cat(z, 1), x)

@github-actions
Copy link
Contributor

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the Stale label May 23, 2021
@PiyalGeorge
Copy link
Author

thanks @glenn-jocher ,

The reason i asked the question is because of this - https://developer.qualcomm.com/docs/snpe/revision_history.html
As per the qualcomm, they have added support for yolov5. So i was trying to figure it out. But still no luck.

@glenn-jocher
Copy link
Member

@PiyalGeorge that's awesome that Qualcomm has added support for YOLOv5 models!! I didn't know about that. I wonder if they have more documentation?

@PiyalGeorge
Copy link
Author

PiyalGeorge commented May 27, 2021

My ideas are from these links:-

  1. https://developer.qualcomm.com/comment/reply/68483#comment-form
  2. https://docs.ultralytics.com/yolov5/tutorials/model_export
  3. https://developer.qualcomm.com/docs/snpe/model_conv_tensorflow.html
  4. Pytorch->ONNX->TF-> SNPE Error for batchnorm variance onnx/onnx-tensorflow#749

In the qualcomm's SNPE, the final model format we want is a .dlc file. Since there is no direct method to convert .pt file to .dlc, we need to convert the .pt file to onnx then to .dlc (that's the whole idea i get from first link).
So first need to convert a .pt file to onnx, using 2nd link OR onnx-simplifier mentioned in 1st link.
Once we get onnx we need the out_node to convert it to .dlc like mentioned in link 3, 4. Used netron viewer to view the onnx model and there are three output nodes.

@glenn-jocher
Copy link
Member

@PiyalGeorge you can skip the Detect() layer concatenation and grid op with the --train mode flag:

python models/export.py --train

Make sure you use the latest master, we recently pushed a fix for this.

@github-actions github-actions bot removed the Stale label Jun 4, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Jul 5, 2021

👋 Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 🚀 resources:

Access additional Ultralytics ⚡ resources:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLOv5 🚀 and Vision AI ⭐!

@evdoks
Copy link

evdoks commented Aug 27, 2021

@PiyalGeorge were you able to export an already trained YOLOv5 model to DLC?
My understanding is that export with --train flag results in ONNX representation without the detection layer and thus cannot be used for detection. What would be the purpose of such an export or am I missing something?

@PiyalGeorge
Copy link
Author

Sorry to disappoint, i couldnt figure it out. Its possible, but i was busy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested Stale
Projects
None yet
Development

No branches or pull requests

3 participants