ultralytics / yolov5

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

YOLOv5 custom state dict from Azure automl #7949

Closed RasmusLars closed 2 years ago

RasmusLars commented 2 years ago

Search before asking

Question

Hi,

I have a custom state dict for a yolov5 model with:

The model has been generated by azure automl and has 263 layers. According to Azure, they are using a pre-trained model sourced from: /root/.cache/torch/hub/checkpoints/yolov5.3.0m-a04eea56.pth.

I have extracted the model.pt file and now I am trying to load it using the following code:

model = torch.hub.load('ultralytics/yolov5', 'yolov5m', classes=2, force_reload=True, pretrained=True) state_dict = torch.load('model_yolov_file.pt/model.pt') # custom state dict model.load_state_dict(state_dict['model_state'])

I get the following error: RuntimeError: Error(s) in loading state_dict for AutoShape: Missing key(s) in state_dict: "model.model.0.conv.weight", "model.model.0.bn.weight",.........

if I look in my custom state dict my first layer is called "0.conv.conv.weight" which is != "model.model.0.conv.weight".

So it seems like I don't have "model.model" but I do have an extra "conv".

How can I solve this and what is going on here?

Additional

No response

github-actions[bot] commented 2 years ago

šŸ‘‹ Hello @RasmusLars, 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 screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ā“ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://ultralytics.com or email support@ultralytics.com.

Requirements

Python>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. 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

CI CPU testing

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

glenn-jocher commented 2 years ago

@RasmusLars your example is outdated, use current models and code.

YOLOv5 šŸš€ PyTorch Hub models allow for simple model loading and inference in a pure python environment without using detect.py.

Simple Inference Example

This example loads a pretrained YOLOv5s model from PyTorch Hub as model and passes an image for inference. 'yolov5s' is the YOLOv5 'small' model. For details on all available models please see the README. Custom models can also be loaded, including custom trained PyTorch models and their exported variants, i.e. ONNX, TensorRT, TensorFlow, OpenVINO YOLOv5 models.

import torch

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')  # yolov5n - yolov5x6 official model
#                                            'custom', 'path/to/best.pt')  # custom model

# Images
im = 'https://ultralytics.com/images/zidane.jpg'  # or file, Path, URL, PIL, OpenCV, numpy, list

# Inference
results = model(im)

# Results
results.print()  # or .show(), .save(), .crop(), .pandas(), etc.

results.xyxy[0]  # im predictions (tensor)
results.pandas().xyxy[0]  # im predictions (pandas)
#      xmin    ymin    xmax   ymax  confidence  class    name
# 0  749.50   43.50  1148.0  704.5    0.874023      0  person
# 2  114.75  195.75  1095.0  708.0    0.624512      0  person
# 3  986.00  304.00  1028.0  420.0    0.286865     27     tie

See YOLOv5 PyTorch Hub Tutorial for details.

Good luck šŸ€ and let us know if you have any other questions!

RasmusLars commented 2 years ago

Thanks!

I might need the rephrase my question.

How about loading a custom state dict i.e. I only have the weights for the various layers.

So I trained my model using Azure and now I only have the state dict, nothing else.

Would that mean that in order to load my custom weight I need to:

model = torch.hub.load('ultralytics/yolov5', 'custom', path = 'file.pt', force_reload=True) ?

I tried this and it generated another error:

Exception: 'model'. Cache may be out of date, try force_reload=True

glenn-jocher commented 2 years ago

@RasmusLars that doesn't make any sense, one of my main motivations with YOLO was to abandon the concept of loading state dicts in favor of simply loading a model, i.e. yolov5s.pt is self contained with everything needed for

model = torch.load('yolov5s.pt')
RasmusLars commented 2 years ago

That makes sense, so I imagine that Azure is doing something special in the backend. Because the state dict that I get only contains the weights and not the model itself.

If I would do: model = torch.load("yolov.pt")

I would simply get an error because my file is a dict containing among other things the model state.

glenn-jocher commented 2 years ago

@RasmusLars if Azure if simply a hardware backend (like Colab) you should be able to just train YOLOv5 normally without using their outdated formatting (and outdated 5.x models) for YOLOv5, i.e.:

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

python train.py ...
github-actions[bot] commented 2 years ago

šŸ‘‹ Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 šŸš€ resources:

Access additional Ultralytics āš” resources:

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 YOLOv5 šŸš€ and Vision AI ā­!