dbsystel / yolov5-coreml-tools

Scripts for exporting YOLOv5 models to CoreML and benchmarking it.
Apache License 2.0
66 stars 9 forks source link

Regarding custom model #5

Closed pytholic closed 2 years ago

pytholic commented 2 years ago

Hi. Thank you for the repo!

I need some help. I am trying to export a yolo model. I basic used yolov5s configuration with one class (nc = 1).

Before running your export script, I modify yolov5s.yaml from 80 classes to 1. Give path to my own model.pt while running export script. Also added setup.py and installed all the dependencies. However, I still get the following issue:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/content/yolov5-coreml-tools/src/coreml_export/main.py", line 318, in main
    model(sampleInput)
  File "/root/.cache/pypoetry/virtualenvs/coreml-tools-Wi__XBdL-py3.7/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/content/yolov5/models/yolo.py", line 126, in forward
    return self._forward_once(x, profile, visualize)  # single-scale inference, train
  File "/content/yolov5/models/yolo.py", line 149, in _forward_once
    x = m(x)  # run
  File "/root/.cache/pypoetry/virtualenvs/coreml-tools-Wi__XBdL-py3.7/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/content/yolov5/models/yolo.py", line 61, in forward
    self.grid[i], self.anchor_grid[i] = self._make_grid(nx, ny, i)
RuntimeError: The expanded size of the tensor (1) must match the existing size (80) at non-singleton dimension 3.  Target sizes: [1, 3, 1, 1, 2].  Tensor sizes: [3, 80, 80, 2]
Leon0402 commented 2 years ago

This doesn't seem to be an issue of the code here I believe, your not able to run your model. You're sure you just changed the number of classes and not the image size for instance? How did you create your model.pt exactly?

Just as a sidenote, I would recommend using pyton 3.8 as noted in the Readme

pytholic commented 2 years ago

Hi. This is the model I used for my task:

`# parameters nc: 1 # number of classes depth_multiple: 0.33 # model depth multiple width_multiple: 0.50 # layer channel multiple

anchors

anchors:

YOLOv5 backbone

backbone:

[from, number, module, args]

[[-1, 1, Focus, [64, 3]], # 0-P1/2 [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 [-1, 3, BottleneckCSP, [128]], [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 [-1, 9, BottleneckCSP, [256]], [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 [-1, 9, BottleneckCSP, [512]], [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 [-1, 1, SPP, [1024, [5, 9, 13]]], [-1, 3, BottleneckCSP, [1024, False]], # 9 ]

YOLOv5 head

head: [[-1, 1, Conv, [512, 1, 1]], [-1, 1, nn.Upsample, [None, 2, 'nearest']], [[-1, 6], 1, Concat, [1]], # cat backbone P4 [-1, 3, BottleneckCSP, [512, False]], # 13

[-1, 1, Conv, [256, 1, 1]], [-1, 1, nn.Upsample, [None, 2, 'nearest']], [[-1, 4], 1, Concat, [1]], # cat backbone P3 [-1, 3, BottleneckCSP, [256, False]], # 17 (P3/8-small)

[-1, 1, Conv, [256, 3, 2]], [[-1, 14], 1, Concat, [1]], # cat head P4 [-1, 3, BottleneckCSP, [512, False]], # 20 (P4/16-medium)

[-1, 1, Conv, [512, 3, 2]], [[-1, 10], 1, Concat, [1]], # cat head P5 [-1, 3, BottleneckCSP, [1024, False]], # 23 (P5/32-large)

[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) ]`

So basically just changed nc from 80 to 1, and I do the same thing in yolo5s.yaml, while running your script. I created my model.pt by official yolov5 code. they already implemented the method to save best.pt and last.pt models.

First I tried on colab and it gave this error. Now I am trying on local environment with python version 3.8.10. However, I still face the same issue.

Leon0402 commented 2 years ago

I would recommend the following steps first:

If you need your custom model to be based on release 5 or 6, then support should be first added for the pretrained ones

pytholic commented 2 years ago

@Leon0402 I used official Yolov5 code for training, and now I am cloning same official repository before using your export script. Doesn't that mean that original source code is same in both cases i.e. code from Yolov5 repository?

pytholic commented 2 years ago

@Leon0402 my custom model is based on release 6.0 I think. Should I retrain with previous release like 3 or 4 maybe in order to make this work?

Leon0402 commented 2 years ago

As I said, first try a pretrained model for version 4. This is tested to work, so use this to make sure that your setup works

pytholic commented 2 years ago

@Leon0402 All right, I will check it out and inform here afterwards. Thank you!

pytholic commented 2 years ago

@Leon0402 Hi again. So I tried pre-trained model and also my model conversion with release 4.0. Conversion works fine. While v6.0 gives the same issue. I converted my model using v4.0 now, will test it soon. Thank you for all the help and repo. If I still run into a problem, I might bother you again :,)

pytholic commented 2 years ago

@Leon0402 I have tested the model predictions and it works fine with 4.0 release. Thank you for your help. I will close the issue now!

Leon0402 commented 2 years ago

You're welcome!

NaeemKhan333 commented 2 years ago

@Leon0402 @pytholic I am trying to convert yolov5s (v4) (pre-trained model on coco dataset) into CoreML format. But when I am doing following step , I am getting following error . Can you help me out

poetry install

  ValueError

  Directory /home/user/Downloads/yolov5-4.0/yolov5-coreml-tools/yolov5 does not seem to be a Python package

  at ~/.poetry/lib/poetry/_vendor/py3.8/poetry/core/packages/directory_dependency.py:53 in __init__
       49│             self._full_path / "pyproject.toml"
       50│         ).is_poetry_project()
       51│ 
       52│         if not setup.exists() and not self._supports_poetry:
    →  53│             raise ValueError(
       54│                 "Directory {} does not seem to be a Python package".format(
       55│                     self._full_path
       56│                 )
       57│             )

I am using Ubuntu system for conversion of model.

user@user:~/Downloads/yolov5-4.0/yolov5-coreml-tools$ poetry --version
Poetry version 1.1.12
pytholic commented 2 years ago

@Leon0402 @pytholic I am trying to convert yolov5s (v4) (pre-trained model on coco dataset) into CoreML format. But when I am doing following step , I am getting following error . Can you help me out

poetry install

  ValueError

  Directory /home/user/Downloads/yolov5-4.0/yolov5-coreml-tools/yolov5 does not seem to be a Python package

  at ~/.poetry/lib/poetry/_vendor/py3.8/poetry/core/packages/directory_dependency.py:53 in __init__
       49│             self._full_path / "pyproject.toml"
       50│         ).is_poetry_project()
       51│ 
       52│         if not setup.exists() and not self._supports_poetry:
    →  53│             raise ValueError(
       54│                 "Directory {} does not seem to be a Python package".format(
       55│                     self._full_path
       56│                 )
       57│             )

I am using Ubuntu system for conversion of model.

user@user:~/Downloads/yolov5-4.0/yolov5-coreml-tools$ poetry --version
Poetry version 1.1.12

@NaeemKhan333 Hi. Can you show me your pyproject.toml? Also did you clone and put the official Yolov5 repository in the same directory as yolov5-coreml-tools?

Leon0402 commented 2 years ago

Please see the note in the README:

Note: It assumes that you've cloned the yolov5 repo to ../yolov5 relative to the project and that you have added a setup.py file, so poetry installs the dependencies of YOLOv5. See Issue #2525.

It seems you have done the first step, but not added a setup.py to the repo, so it's regocnized as a python package.

It seems there exists a fork that could be used instead: https://github.com/fcakyon/yolov5-pip I haven't tried it though

Please open up a new issue if you have any further questions. This one is already closed.