ultralytics / ultralytics

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

a question about trainer.py #15449

Open ddeellttaa opened 3 months ago

ddeellttaa commented 3 months ago

Search before asking

Question

I'm confused about this code in trainer.py. self.loss, self.loss_items = self.model(batch) since model is a nn.Module, I believe the result of running self.model(batch) is tensor instead of loss. I check the /enfgin/model.py and /yolo/model.py and can't find out why.

Additional

No response

github-actions[bot] commented 3 months ago

👋 Hello @ddeellttaa, thank you for your interest in Ultralytics YOLOv8 🚀! We recommend a visit to the 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.

pderrenger commented 3 months ago

@ddeellttaa thank you for your question. The line self.loss, self.loss_items = self.model(batch) in trainer.py is indeed calling the model, which is a subclass of nn.Module. In this context, the model's forward method is overridden to return both the loss and the loss items when it is in training mode. This is why you see the loss being returned instead of just a tensor. If you need further clarification, please refer to the forward method implementation in the model class. If the issue persists, ensure you are using the latest version of the package.

github-actions[bot] commented 1 week ago

👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

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

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 YOLO 🚀 and Vision AI ⭐