ultralytics / yolov5

YOLOv5 πŸš€ in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
50.89k stars 16.39k forks source link

Difference between Ensemble with one Model and the original Detection Model #11990

Closed Ayame4 closed 1 year ago

Ayame4 commented 1 year ago

Search before asking

Question

Hello,

As I have thus far understood:

In detection (detect.py) with yolov5s.pt as weights, first DetectMultiBackend is called

if pt:  # PyTorch
       model = attempt_load(weights if isinstance(weights, list) else w, device=device, inplace=True, fuse=fuse)

which then creates an Ensemble where the model weights of the Detection Model are loaded.

Now since only one model is used in the Ensemble, I am trying to replace the Ensemble with the DetectionModel directly:

from models.yolo import DetectionModel

if pt:  # PyTorch
    ckpt = torch.load(w, map_location='cpu')
    model = DetectionModel(ckpt['model'].yaml, ch=3).to(device)

however the prediction of the second way yields almost no results.

Could you help me understand what is the difference between the Ensemble with one model and the model itself? Is it possible to use the DetectionModel directly and obtain the same predictions?

I don't have a lot of experience, so I would be grateful for some help.

Additional

Note: in order to use the DetectionModel I moved DetectMultiBackend to a new file to avoid circular dependencies.

github-actions[bot] commented 1 year ago

πŸ‘‹ Hello @Ayame4, thank you for your interest in YOLOv5 πŸš€! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

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.

Requirements

Python>=3.8.0 with all requirements.txt installed including PyTorch>=1.8. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

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

Status

YOLOv5 CI

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training, validation, inference, export and benchmarks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

Introducing YOLOv8 πŸš€

We're excited to announce the launch of our latest state-of-the-art (SOTA) object detection model for 2023 - YOLOv8 πŸš€!

Designed to be fast, accurate, and easy to use, YOLOv8 is an ideal choice for a wide range of object detection, image segmentation and image classification tasks. With YOLOv8, you'll be able to quickly and accurately detect objects in real-time, streamline your workflows, and achieve new levels of accuracy in your projects.

Check out our YOLOv8 Docs for details and get started with:

pip install ultralytics
Ayame4 commented 1 year ago

The problem was that the DetectionModel doesn't load the weights from ckpt as input, rather the weights needed to be loaded like this:

from models.yolo import DetectionModel

if pt:  # PyTorch
    model = torch.load(w, map_location='cpu')['model'].float()
glenn-jocher commented 1 year ago

@Ayame4 hi there,

Thank you for sharing your findings. Indeed, the DetectionModel loads the weights a bit differently from the Ensemble. Your solution to load the weights directly using torch.load is correct, and it should yield the same predictions as using the Ensemble.

Your diligence and adaptability in finding this solution are commendable. If you have further questions or need additional assistance, feel free to ask!