ultralytics / yolov3

YOLOv3 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
10.17k stars 3.44k forks source link

Cannot compute loss function from best model #2194

Closed useruser2023 closed 7 months ago

useruser2023 commented 7 months ago

Search before asking

YOLOv3 Component

Validation

Bug

I need to pass the loss function in order to run val.py for validation. After training on two classes, we chose the best automatically produced model for inference. Used this approach

Environment

Minimal Reproducible Example

from utils.loss import ComputeLoss
from ultralytics import YOLO

model = YOLO("best.pt")
comput_loss = ComputeLoss(model.model)

Additional

AttributeError: 'DetectionModel' object has no attribute 'hyp'

Are you willing to submit a PR?

glenn-jocher commented 7 months ago

@useruser2023 hello! Thanks for reaching out and for your efforts in searching for a solution before posting your issue.

The error you're encountering, AttributeError: 'DetectionModel' object has no attribute 'hyp', suggests that the model object you're trying to use does not have the hyperparameters (hyp) attribute that the ComputeLoss function expects.

The ComputeLoss function requires the model's hyperparameters to calculate the loss. When you load the model using YOLO("best.pt"), it does not automatically load the hyperparameters used during training.

To resolve this, you should ensure that the hyperparameters are correctly loaded and associated with your model object. Typically, these hyperparameters are stored in a .yaml file or within the training script. Make sure to load these hyperparameters and pass them to the ComputeLoss function or the model object as required.

For further guidance on how to properly set up the loss computation for validation, please refer to the Ultralytics Docs, which provide comprehensive information on using the YOLOv3 repository.

If you continue to face issues, please provide more details about how you're loading the model and where the hyperparameters are defined, and we'll be happy to help you further. Remember, the community and the Ultralytics team are here to support you! 😊