ultralytics / ultralytics

Ultralytics YOLO11 🚀
https://docs.ultralytics.com
GNU Affero General Public License v3.0
32.23k stars 6.19k forks source link

train classify.how different ultralytics/models/yolo/classify/train.py vs from ultralytics import YOLO? #5725

Closed lucy3589 closed 1 year ago

lucy3589 commented 1 year ago

how different ultralytics/models/yolo/classify/train.py vs from ultralytics import YOLO?

github-actions[bot] commented 1 year ago

👋 Hello @lucy3589, thank you for your interest in YOLOv8 🚀! We recommend a visit to the YOLOv8 Docs for new users where you can find many Python and CLI usage examples and where many of the most common questions may already be answered.

If this is a 🐛 Bug Report, please provide a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Join the vibrant Ultralytics Discord 🎧 community for real-time conversations and collaborations. This platform offers a perfect space to inquire, showcase your work, and connect with fellow Ultralytics users.

Install

Pip install the ultralytics package including all requirements in a Python>=3.8 environment with PyTorch>=1.8.

pip install ultralytics

Environments

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

Status

Ultralytics CI

If this badge is green, all Ultralytics CI tests are currently passing. CI tests verify correct operation of all YOLOv8 Modes and Tasks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

zshar7 commented 1 year ago

Please do specify. I am inferring that you are trying to import ultralytics for your model training. Consult Ultralytics Documentation or Google if you want a "model program" to build your python project from.

Good luck, @zshariff6506

lucy3589 commented 1 year ago

Please do specify. I am inferring that you are trying to import ultralytics for your model training. Consult Ultralytics Documentation or Google if you want a "model program" to build your python project from.

Good luck, @zshariff6506

thanks to your reply

lucy3589 commented 1 year ago

2 structs are same? imageimage

glenn-jocher commented 1 year ago

@lucy3589 hello, it's great to see you exploring the YOLOv8 model! From your images, it looks like you're comparing two structures.

From a high level, I'd say that the layout and design of these structures might seem quite similar. But in terms of actual content and their uses, they could be very different. The differences could range from differences in the type and nature of the data they store, to the specific purposes they serve within the code.

Both structures are components of the YOLOv8 model, and each plays its own unique role. Even if they seem structurally similar at first glance, the details could tell a different story.

As a next step, I suggest delving deeper into the YOLOv8 documentation and implementation. This should give you a clearer view of the specific purposes and roles of these structures in the context of the YOLOv8 model.

Happy coding!

lucy3589 commented 1 year ago

///> @lucy3589 hello, it's great to see you exploring the YOLOv8 model! From your images, it looks like you're comparing two structures.

From a high level, I'd say that the layout and design of these structures might seem quite similar. But in terms of actual content and their uses, they could be very different. The differences could range from differences in the type and nature of the data they store, to the specific purposes they serve within the code.

Both structures are components of the YOLOv8 model, and each plays its own unique role. Even if they seem structurally similar at first glance, the details could tell a different story.

As a next step, I suggest delving deeper into the YOLOv8 documentation and implementation. This should give you a clearer view of the specific purposes and roles of these structures in the context of the YOLOv8 model.

Happy coding!

@glenn-jocher thanks for your reply. i use 2 ways transform pt 2 onnx. but get 2 diff onnx. For the same yolov8n-cls.pt model, I used 2 methods to convert to onnx, but got 2 onnx models with different structures. I feel so confused, I don't know why. Looking forward to your reply

from ultralytics import YOLO
import torch
from torch.autograd import Variable

os.environ['CUDA_VISIBLE_DEVICES']='3'
model0 = YOLO('yolov8n-cls.yaml').load('yolov8n-cls.pt')  # build from YAML and transfer weights
model = model0.model
model.eval()

dummy_input = Variable(torch.randn(1, 3, 224, 224)) 
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

model.to(device)
dummy_input = dummy_input.to(device)

onnx_path = "yolov8n-cls.onnx" 
torch.onnx.export(model, dummy_input, onnx_path, verbose=False, input_names=['input'], output_names=['output'])

Another way to transform onnx models

from ultralytics import YOLO
model = YOLO("yolov8n-cls.pt")
success = model.export(format="onnx", simplify=False, imgsz=224)  # export the model to onnx format
assert success

/

glenn-jocher commented 1 year ago

Hello @lucy3589,

It's great to see you trying to transform YOLOv8 model into onnx. Regarding the different structures you are seeing in the ONNX models, there could be a few reasons for this.

First, please note that the export() method in YOLO already includes several preprocessing steps, whereas when you use torch.onnx.export() directly, you might need to handle these steps manually.

Second, these two methods might implement the export process differently internally, leading to variation in the final onnx model structure.

Lastly, there could be differences due to the Pytorch version you are using, as the ONNX export process can behave differently across different Pytorch versions.

While the structure might look different, as long as both ONNX models work as expected and you do not observe a significant performance change, it is typically fine.

I hope this answers your question. Let us know if you have any other questions or need further clarification!

lucy3589 commented 1 year ago

Hello @lucy3589,

It's great to see you trying to transform YOLOv8 model into onnx. Regarding the different structures you are seeing in the ONNX models, there could be a few reasons for this.

First, please note that the export() method in YOLO already includes several preprocessing steps, whereas when you use torch.onnx.export() directly, you might need to handle these steps manually.

Second, these two methods might implement the export process differently internally, leading to variation in the final onnx model structure.

Lastly, there could be differences due to the Pytorch version you are using, as the ONNX export process can behave differently across different Pytorch versions.

While the structure might look different, as long as both ONNX models work as expected and you do not observe a significant performance change, it is typically fine.

I hope this answers your question. Let us know if you have any other questions or need further clarification!

Thanks for your guidance. The two structures seem to be similar, but the first one is more complicated, and for the split operator, it is divided into several smaller operators to execute. The structure of the onnx model derived from the two methods is as follows imageimage

glenn-jocher commented 1 year ago

Hello @lucy3589,

Indeed, you've correctly observed the differences in complexity and execution method in the two ONNX structures. While the structures may look different, the differences are often based on the way they handle internal computation and the preprocessing steps involved during the export process.

Your observation about the split operator being divided into several sub-operators in one method is also quite noteworthy. This is generally done to increase flexibility of the structure and handle the computations more efficiently, even though it might add to the complexity of the overall structure.

As long as both methods fulfill your requirements and there's no significant discrepancy in terms of performance or accuracy, you can choose either based on your specific needs, such as simplicity or flexibility. It's wonderful to see your thorough examination of this and your understanding of the variations in model structure.

lucy3589 commented 1 year ago

Thanks for your help.

glenn-jocher commented 1 year ago

@lucy3589 hello,

Thank you for reaching out with your question.

The two ONNX structures you're comparing indeed seem to have different levels of complexity and execution methods. In the ONNX model, the variability in the layout can often be due to differences in internal handling of computations and the preprocessing steps used during the export process. Specifically, the split operator being divided into several smaller operators is common, and it's primarily done to increase flexibility of the structure and handle computations in a more granular way.

While the structural differences might seem significant, the performance and accuracy of both methods should be comparable as long as the conversion was done correctly. This means, you can choose either method based on your specific needs, such as simplicity or increased flexibility.

It's great to see you examining this matter so thoroughly, and I hope my response provided some useful insight.

Let me know if you have more questions or if there's something else you'd like me to clarify.

Best Regards.

lucy3589 commented 1 year ago

@lucy3589 hello,

Thank you for reaching out with your question.

The two ONNX structures you're comparing indeed seem to have different levels of complexity and execution methods. In the ONNX model, the variability in the layout can often be due to differences in internal handling of computations and the preprocessing steps used during the export process. Specifically, the split operator being divided into several smaller operators is common, and it's primarily done to increase flexibility of the structure and handle computations in a more granular way.

While the structural differences might seem significant, the performance and accuracy of both methods should be comparable as long as the conversion was done correctly. This means, you can choose either method based on your specific needs, such as simplicity or increased flexibility.

It's great to see you examining this matter so thoroughly, and I hope my response provided some useful insight.

Let me know if you have more questions or if there's something else you'd like me to clarify.

Best Regards.

Thank you very much for your reply every time.

Best wishes

glenn-jocher commented 1 year ago

Hello @lucy3589,

I'm glad to hear that our discussions have been helpful for you! We're always here to support you in your exploration of YOLOv8 and deep learning in general. Thank you for your kind words and thoughtful questions that help everyone in this community learn.

Feel free to reach out if you have more questions or need further clarifications. Happy coding!

Best wishes to you too!