Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues exporting custom model #1866

Closed
1 task done
HampusAstrom opened this issue Nov 23, 2021 · 6 comments
Closed
1 task done

Issues exporting custom model #1866

HampusAstrom opened this issue Nov 23, 2021 · 6 comments
Labels
question Further information is requested Stale

Comments

@HampusAstrom
Copy link

Search before asking

Question

I have trained a model using the command python train.py --img 400 --epochs 100 --data tless-synthetic.yaml --weights yolov3.pt --batch 8, and this works fine (data was setup correctly and defined in .yaml mentioned above).

But now that I try to export the model so in can import it in another repo (repo contrains own utils folder, had trouble importing using torch.hub). When I run the export command python export.py --data tless-synthetic.yaml --weights runs/train/exp4/weights/best.pt I get the following error:

python export.py --data tless-synthetic.yaml --weights runs/train/exp4/weights/best.pt
export: data=tless-synthetic.yaml, weights=runs/train/exp4/weights/best.pt, imgsz=[640, 640], batch_size=1, device=cpu, half=False, inplace=False, train=False, optimize=False, int8=False, dynamic=False, simplify=False, opset=13, topk_per_class=100, topk_all=100, iou_thres=0.45, conf_thres=0.25, include=['torchscript', 'onnx']
YOLOv3 🚀 v9.6.0-4-g6bf59c9 torch 1.7.1 CPU

Fusing layers... 
Model Summary: 261 layers, 61653595 parameters, 0 gradients, 155.2 GFLOPs

PyTorch: starting from runs/train/exp4/weights/best.pt (123.7 MB)

TorchScript: starting export with torch 1.7.1...
/home/hampus/vision/yolov3/models/yolo.py:58: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if self.onnx_dynamic or self.grid[i].shape[2:4] != x[i].shape[2:4]:
TorchScript: export failure: save(): incompatible function arguments. The following argument types are supported:
    1. (self: torch._C.ScriptModule, filename: str, _extra_files: Dict[str, str] = {}) -> None

Invoked with: <torch._C.ScriptModule object at 0x7f37a822c8b0>, PosixPath('runs/train/exp4/weights/best.torchscript.pt'); kwargs: _extra_files={'config.txt': '{"shape": [1, 3, 640, 640], "stride": 32, "names": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30"]}'}

ONNX: starting export with onnx 1.10.2...
ONNX: export failure: Unsupported ONNX opset version: 13

Do you have any suggestions on how I can get it to export in either TorchScript or ONNX format so I can import it in my other repo without reference issues like with torch.hub?

I am running on Ubuntu 16.

Additional

No response

@HampusAstrom HampusAstrom added the question Further information is requested label Nov 23, 2021
@github-actions
Copy link

github-actions bot commented Nov 23, 2021

👋 Hello @HampusAstrom, thank you for your interest in YOLOv3 🚀! 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 Glenn Jocher at glenn.jocher@ultralytics.com.

Requirements

Python>=3.6.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

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

Environments

YOLOv3 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 YOLOv3 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv3 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
Copy link
Member

@HampusAstrom your torch or onnx or both dependencies are outdated.

@HampusAstrom
Copy link
Author

Thanks for the quick reply.

Onnx was at latest python version, but I managed to get it to export by lowering the opset slightly.

This however made me realize that this might not be the best solution for my usecase anyway.

My situation is this: I need object detection as a first stage in my pipeline, and your repo was great for training such a custom detector. I now want to combine that detector with our pose estimator (https://github.com/shbe-aau/multi-pose-estimation) in a (preferably slim) version. So I only need preprocessing and inference versions of the networks, that I can build together and pass cutouts of the image from each bbox to my code.

What would be the best solution for how to import your code for such a scenario? Do I get the preprocessing that the model expects with onnx?

I tried importing with torch.hub (custom, local) but ran into issues that seem to be due to your costom hub code breaking if not calling it in your repos folder (not finding models.yolo, looking in my utils folder in m repo rather than in yours et.c.). Calling torch.hub when located in your repo works fine with my custom trained model (and weights). How can I import the model I trained with your code in another repo?

Sorry if anything is unclear, I have not used torch.hub before.

@HampusAstrom
Copy link
Author

For instance if I try to follow them method mentioned here ultralytics/yolov5#22 (comment) I get the following error:

Downloading: "https://github.com/ultralytics/yolov3/archive/master.zip" to /home/hampus/.cache/torch/hub/master.zip
Traceback (most recent call last):
  File "inference.py", line 131, in <module>
    inference = Inference()
  File "inference.py", line 53, in __init__
    detector = torch.hub.load('ultralytics/yolov3', "yolov3", classes=30, force_reload=True, pretrained=False)
  File "/home/hampus/.local/lib/python3.8/site-packages/torch/hub.py", line 370, in load
    model = _load_local(repo_or_dir, model, *args, **kwargs)
  File "/home/hampus/.local/lib/python3.8/site-packages/torch/hub.py", line 399, in _load_local
    model = entry(*args, **kwargs)
  File "/home/hampus/.cache/torch/hub/ultralytics_yolov3_master/hubconf.py", line 74, in yolov3
    return _create('yolov3', pretrained, channels, classes, autoshape, verbose, device)
  File "/home/hampus/.cache/torch/hub/ultralytics_yolov3_master/hubconf.py", line 30, in _create
    from models.experimental import attempt_load
  File "/home/hampus/.cache/torch/hub/ultralytics_yolov3_master/models/experimental.py", line 11, in <module>
    from models.common import Conv
  File "/home/hampus/.cache/torch/hub/ultralytics_yolov3_master/models/common.py", line 22, in <module>
    from utils.datasets import exif_transpose, letterbox
ModuleNotFoundError: No module named 'utils.datasets' 

Similar to the issues in ultralytics/yolov5#1181. I do not have the python packages utils installed, but my folder also contains it's own utils package. Maybe I can fix it by renaming my packages but that will of course break my import statements. A solution where torch.hub does not look locally first would be nice.

To be clear, this even happens when calling torch.hub with external source, the command I use is:
detector = torch.hub.load('ultralytics/yolov3', "yolov3", classes=30, force_reload=True, pretrained=False)

@github-actions
Copy link

👋 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 YOLOv3 🚀 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 YOLOv3 🚀 and Vision AI ⭐!

@glenn-jocher
Copy link
Member

@HampusAstrom you're correct, using torch.hub can lead to issues due to conflicting local packages and the repository structure. To address this, you can download the model's source code directly and use it in your own repository. This can be done using the following steps:

  1. Clone the YOLOv3 repository to your local machine.
  2. Place the required YOLOv3 model and its associated files into your repository.
  3. Modify any import statements in the YOLOv3 code to align with the structure of your repository.

By doing this, you can ensure that the YOLOv3 model is correctly situated within your project, addressing the issues you faced with torch.hub. Additionally, you can make any necessary modifications to the YOLOv3 model to fit your specific requirements.

Let me know if you encounter any further issues or need additional assistance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested Stale
Projects
None yet
Development

No branches or pull requests

2 participants