WongKinYiu / yolov9

Implementation of paper - YOLOv9: Learning What You Want to Learn Using Programmable Gradient Information
GNU General Public License v3.0
8.8k stars 1.36k forks source link

Trying to export a segment model -- "IndexError: index 1 is out of bounds for dimension 0 with size 1" #472

Open leah-at-lovelace opened 3 months ago

leah-at-lovelace commented 3 months ago

I'm trying to convert yolov9-c-seg.pt to ONNX, using export.py, but I get the following error. Has anyone else hit this?

One note is that I had to make a change to get segment/predict.py running. After pred, proto = model(im, augment=augment, visualize=visualize)[:2] I added proto = proto[2] based on another issue I found here. Not sure it's relevant to my current error, but possibly.

YOLO šŸš€ 2024-6-4 Python-3.12.3 torch-2.3.0 CPU

Fusing layers... 
yolov9-c-dseg-hrsid summary: 746 layers, 57468582 parameters, 0 gradients, 368.6 GFLOPs
Traceback (most recent call last):
  File "/Users/<...>/yolov9/export.py", line 979, in <module>
    main(opt)
  File "/Users/<...>/yolov9/export.py", line 974, in main
    run(**vars(opt))
  File "/Users/<...>/miniconda3/envs/yolov9/lib/python3.12/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/<...>/yolov9/export.py", line 754, in run
    y = model(im)  # dry runs
        ^^^^^^^^^
  File "/Users/<...>/miniconda3/envs/yolov9/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/<...>/miniconda3/envs/yolov9/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/<...>/yolov9/models/yolo.py", line 947, in forward
    return self._forward_once(
           ^^^^^^^^^^^^^^^^^^^
  File "/Users/<...>/yolov9/models/yolo.py", line 810, in _forward_once
    x = m(x)  # run
        ^^^^
  File "/Users/<...>/miniconda3/envs/yolov9/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/<...>/miniconda3/envs/yolov9/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/<...>/yolov9/models/yolo.py", line 754, in forward
    return (torch.cat([d[0][1], mc[1]], 1), (d[1][1], mc[1], p[1]))
                       ~~~~^^^
IndexError: index 1 is out of bounds for dimension 0 with size 1
janb14 commented 2 months ago

Yes i encountered the same error with a trained segmentation model ;(

janb14 commented 2 months ago

It seems to have something to do with the preperation of the dual models outputs when in eval mode. Logically i tried to change the line to return (torch.cat([d[0], mc[1]], 1), (d[1], mc[1], p[1])), then the export works. But when using the onnx for inference, it does not produce 2 output tensors as expected but 4 and they are malformed. For now im falling back onto the gelan models that do work, including the export to onnx. :/ maybe someone smarter than me knows how to concat the resulting tensors correctly.

fukingbus commented 2 months ago

failed to export weight trained using train_dual too, any update on this? the dual model are more precise compared to the one trained using gelan šŸ«”

Ultraopxt commented 2 months ago

same error!

jabuarab commented 3 weeks ago

Shouldn't it be (torch.cat([d[0], mc[0]], 1), (d[1], mc[1], p[1])) ?