-
-
Notifications
You must be signed in to change notification settings - Fork 16.3k
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
ONNX export with GPU (--device opt) is not working #4159
Comments
@SamSamhuns yes this has been reported before. ONNX models must be exported on CPU device for now. If you determine the cause of the issue please submit a PR to help other users, thank you! |
Found a way to avoid generating an error while exporting with GPU but not sure whether it's worth a PR @glenn-jocher It seems the error occurs when using the Unfortunately, even after verifying that each model parameter was on the correct cuda device, the error still persists. However, GPU export is possible with the following but disabling torch.onnx.export(model, img, f, verbose=False, opset_version=opset,
training=torch.onnx.TrainingMode.TRAINING if train else torch.onnx.TrainingMode.EVAL,
do_constant_folding=(not train) and (not next(model.parameters()).is_cuda), # Additional check if cuda used
input_names=['images'],
output_names=['output'],
dynamic_axes={'images': {0: 'batch', 2: 'height', 3: 'width'}, # shape(1,3,640,640)
'output': {0: 'batch', 1: 'anchors'} # shape(1,25200,85)
} if dynamic else None) Note: Setting |
@SamSamhuns can you quantify the penalty, i.e. extra layers or difference in parameters between the two export methods, or profiling results when running python detect.py --weights yolov5s.onnx? |
@SamSamhuns there's really no reason to export on GPU other than to produce an FP16 model. FP16 models don't run in PyTorch on CPU as the PyTorch backend instruction sets are not capable of handling this, I don't know about ONNX. |
Makes sense, anyway there is some underlying issue in ONNX or pytorch onnx export that is causing this. |
👋 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 ⭐! |
Hello, I follow your solution by changed the do_constant_folding= not train to do_constant_folding=(not train) and (not next(model.parameters()).is_cuda). Then, I use the --device 0 to export the onnx, which didn't arise the error. But, I used the onnx file in detect.py, I found that the model is still work on CPU not GPU, do you have any idea? Thanks! |
@SamSamhuns @LaserLV52 good news 😃! Your original issue may now be fixed ✅ in PR #5110 by @SamFC10. This PR implements backend-device change improvements to allow for YOLOv5 models to be exportedto ONNX on either GPU or CPU, and to export at FP16 with the To receive this update:
Thank you for spotting this issue and informing us of the problem. Please let us know if this update resolves the issue for you, and feel free to inform us of any other issues you discover or feature requests that come to mind. Happy trainings with YOLOv5 🚀! |
🐛 Bug
I get an
Expected all tensors to be on the same device
error when using$ python export.py --weight yolov5m.pt --include onnx --device 0
To Reproduce (REQUIRED)
Input:
Output:
Expected behavior
Successful onnx export
Environment
If applicable, add screenshots to help explain your problem.
Additional context
I also checked if the
model
and theimg
passed to the onnx export are present in the same cuda device, which they were.The text was updated successfully, but these errors were encountered: