Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add class names fallback for legacy checkpoints on export - #24715

Open
venu-banaras wants to merge 14 commits into
ultralytics:mainfrom
venu-banaras:idea/onnx_legacy_names_fallback
Open

Add class names fallback for legacy checkpoints on export#24715
venu-banaras wants to merge 14 commits into
ultralytics:mainfrom
venu-banaras:idea/onnx_legacy_names_fallback

Conversation

@venu-banaras

@venu-banaras venu-banaras commented Jun 5, 2026

Copy link
Copy Markdown

Problem

Older YOLO variant checkpoints sometimes saved without separate YAML files might sometimes have missing, None or malformed names property. Due to this and the current Exporter class logic, we immediately fallback to default_class_names() which assigns 999 generic classes silently, regardless of model's actual class count.

Solution

Add a fallback for export when a checkpoint's names property is missing, None or malformed, resolving it via model.yaml() before falling back to default_class_names().

Changes

1. ultralytics/engine/exporter.py - Try to resolve names via model.yaml before falling back to default_class_names() and give a warning to the user in the process.
  1. tests/test_onnx_legacy_names.py - Testing file to test missing, None type and malformed names property.

Further improvements

  1. Infer number of classes from detection head of any YOLO variant, instead of assigning 999 generic class names to any type of exported model.
  2. Add empty class name string check in check_class_names() and give a warning to the user, so that they know that certain classes might come as empty strings. Just a quality of life improvement.

@UltralyticsAssistant UltralyticsAssistant added bug Something isn't working as intended in the official Ultralytics package. exports Model exports (ONNX, TensorRT, TFLite, etc.) python Pull requests that update python code labels Jun 5, 2026
@UltralyticsAssistant

Copy link
Copy Markdown
Member

👋 Hello @venu-banaras, thank you for submitting a ultralytics/ultralytics 🚀 PR! This is an automated message to help with review readiness—an engineer will assist you shortly. Please review the checklist below to help ensure smooth integration. 😊

  • Define a Purpose: Clearly explain the purpose of your fix or feature in your PR description, and link to any relevant issues. Ensure your commit messages are clear, concise, and adhere to the project's conventions.
  • Synchronize with Source: Confirm your PR is synchronized with the ultralytics/ultralytics main branch. If it's behind, update it by clicking the 'Update branch' button or by running git pull and git merge main locally.
  • Ensure CI Checks Pass: Verify all Ultralytics Continuous Integration (CI) checks are passing. If any checks fail, please address the issues.
  • Update Documentation: Update the relevant documentation for any new or modified features.
  • Add Tests: If applicable, include or update tests to cover your changes, and confirm that all tests are passing.
  • Sign the CLA: Please ensure you have signed our Contributor License Agreement if this is your first Ultralytics PR by writing "I have read the CLA Document and I sign the CLA" in a new message.
  • Minimize Changes: Limit your changes to the minimum necessary for your bug fix or feature addition. "It is not daily increase but daily decrease, hack away the unessential. The closer to the source, the less wastage there is." — Bruce Lee

For more guidance, please refer to our Contributing Guide. Don't hesitate to leave a comment if you have any questions. Thank you for contributing to Ultralytics! 🚀

@UltralyticsAssistant UltralyticsAssistant left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 PR Review

Made with ❤️ by Ultralytics Actions

The legacy names fallback logic and new ONNX regression coverage look solid overall, but there is one functional regression in exporter.py: the TensorRT <8.5.0 end-to-end safeguard was unintentionally removed when scoping the engine workaround to int8. The new tests should also skip cleanly when onnxruntime is unavailable.

💬 Posted 2 inline comments

@venu-banaras

Copy link
Copy Markdown
Author

I have read the CLA Document and I sign the CLA

@codecov

codecov Bot commented Jun 5, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 25.00000% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
ultralytics/nn/tasks.py 25.00% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@UltralyticsAssistant UltralyticsAssistant left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 PR Review 2

Made with ❤️ by Ultralytics Actions

The names fallback change looks directionally good and the new ONNX coverage helps, but there are two issues to address before merging: the TensorRT end-to-end compatibility logic regresses for non-INT8 engine exports, and the added tests still miss the new exception-driven fallback path they are meant to protect.

💬 Posted 2 inline comments

Comment thread ultralytics/engine/exporter.py Outdated
Comment thread tests/test_onnx_legacy_names.py Outdated
@venu-banaras

Copy link
Copy Markdown
Author

@glenn-jocher Kindly review. I was finally able to solve my original idea of legacy checkpoint fallback during ONNX export for names.

@glenn-jocher

Copy link
Copy Markdown
Member

Thanks for the update — using Model.names as the legacy fallback for export looks like the right direction; please keep this PR scoped to that exporter fix plus the regression test, and move the broader check_class_names() or class-count inference ideas to a separate PR.

@venu-banaras

Copy link
Copy Markdown
Author

Yes those ideas will be separate PRs. this one is only checking if names is missing or None type or malformed, and fixes them. These 2 further improvement ideas would be follow-ups to this PR, only when this PR is approved.

@glenn-jocher

Copy link
Copy Markdown
Member

Thanks for confirming — keeping this PR limited to the exporter-side fallback for missing or invalid names plus the regression test is the right scope.

@venu-banaras

Copy link
Copy Markdown
Author

yes. now just waiting for feedback on my code...

@glenn-jocher

Copy link
Copy Markdown
Member

Thanks, noted — a maintainer will review the code when available; for now, please keep the PR unchanged unless you need to address CI or a clearly reproducible edge case.

@glenn-jocher glenn-jocher changed the title Legacy names fallback for older checkpoints Add class names fallback for legacy checkpoints on export Jul 5, 2026
Keep check_class_names() validation errors loud instead of catching them, guard non-dict model.yaml, and drop the default_class_names import.
@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown

👋 Hello there! We wanted to let you know that we've decided to close this pull request due to inactivity. We appreciate the effort you put into contributing to our project, but unfortunately, not all contributions are suitable or aligned with our product roadmap.

We hope you understand our decision, and please don't let it discourage you from contributing to open source projects in the future. We value all of our community members and their contributions, and we encourage you to keep exploring new projects and ways to get involved.

For additional resources and information, please see the links below:

Thank you for your contributions to YOLO 🚀 and Vision AI ⭐

@github-actions github-actions Bot added the Stale Stale and schedule for closing soon label Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working as intended in the official Ultralytics package. exports Model exports (ONNX, TensorRT, TFLite, etc.) python Pull requests that update python code Stale Stale and schedule for closing soon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants