Linaom1214 / TensorRT-For-YOLO-Series

tensorrt for yolo series (YOLOv10,YOLOv9,YOLOv8,YOLOv7,YOLOv6,YOLOX,YOLOv5), nms plugin support
877 stars 151 forks source link

"Resize scales must be an initializer" when try to convert yolov7 model. #52

Closed cyrusbehr closed 1 year ago

cyrusbehr commented 1 year ago

Hi there,

I am following the instructions in your google colab notebook (here).

I start by downloading the model file: wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-tiny.pt

Then I convert to onnx: python3 ~/work/repos/yolov7/export.py --weights yolov7-tiny.pt --grid --simplify

Which prints out the following:

Namespace(batch_size=1, conf_thres=0.25, device='cpu', dynamic=False, dynamic_batch=False, end2end=False, fp16=False, grid=True, img_size=[640, 640], include_nms=False, int8=False, iou_thres=0.45, max_wh=None, simplify=True, topk_all=100, weights='yolov7-tiny.pt')
YOLOR 🚀 v0.1-107-g44d8ab4 torch 1.12.1+cu102 CPU

Fusing layers... 
Model Summary: 200 layers, 6219709 parameters, 6219709 gradients
/home/cyrus/.local/lib/python3.8/site-packages/torch/functional.py:478: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at  ../aten/src/ATen/native/TensorShape.cpp:2895.)
  return _VF.meshgrid(tensors, **kwargs)  # type: ignore[attr-defined]

Starting TorchScript export with torch 1.12.1+cu102...
/home/cyrus/work/repos/yolov7/models/yolo.py:52: 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.grid[i].shape[2:4] != x[i].shape[2:4]:
TorchScript export success, saved as yolov7-tiny.torchscript.pt
CoreML export failure: No module named 'coremltools'

Starting TorchScript-Lite export with torch 1.12.1+cu102...
TorchScript-Lite export success, saved as yolov7-tiny.torchscript.ptl

Starting ONNX export with onnx 1.12.0...
/home/cyrus/work/repos/yolov7/models/yolo.py:582: 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 augment:
/home/cyrus/work/repos/yolov7/models/yolo.py:614: 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 profile:
/home/cyrus/work/repos/yolov7/models/yolo.py:629: 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 profile:
Simplifier failure: No module named 'onnxsim'
ONNX export success, saved as yolov7-tiny.onnx

Export complete (4.02s). Visualize with https://github.com/lutzroeder/netron.

Finally, I try converting to tensorrt by running: python export.py -o yolov7-tiny.onnx -e yolov7-tiny.trt --end2end

This produces the following error:

Namespace(calib_batch_size=8, calib_cache='./calibration.cache', calib_input=None, calib_num_images=5000, conf_thres=0.4, end2end=True, engine='yolov7-tiny.trt', iou_thres=0.5, max_det=100, onnx='yolov7-tiny.onnx', precision='fp16', verbose=False, workspace=1)
[09/12/2022-15:41:59] [TRT] [I] [MemUsageChange] Init CUDA: CPU +449, GPU +0, now: CPU 475, GPU 283 (MiB)
[09/12/2022-15:41:59] [TRT] [I] [MemUsageSnapshot] Begin constructing builder kernel library: CPU 475 MiB, GPU 283 MiB
[09/12/2022-15:41:59] [TRT] [I] [MemUsageSnapshot] End constructing builder kernel library: CPU 629 MiB, GPU 327 MiB
[09/12/2022-15:41:59] [TRT] [W] onnx2trt_utils.cpp:366: Your ONNX model has been generated with INT64 weights, while TensorRT does not natively support INT64. Attempting to cast down to INT32.
Failed to load ONNX file: /home/cyrus/work/repos/TensorRT-For-YOLO-Series/yolov7-tiny.onnx
In node 88 (importResize): UNSUPPORTED_NODE: Assertion failed: scales.is_weights() && "Resize scales must be an initializer!"

Please help.

mihuzz commented 1 year ago

Hi. You try to convert default yolov7-tiny model?

mihuzz commented 1 year ago

for the first i see you have installed torch 1.12 but see requierments.txt

cyrusbehr commented 1 year ago

wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-tiny.pt

Yes, here you can see that I'm using their default model:

wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-tiny.pt

cyrusbehr commented 1 year ago

I had previously installed all the requirements.

Running pip3 install -r requirements.txt outputs the following. As you can see @mihuzz , all requirements are met:

Looking in indexes: https://pypi.org/simple, https://pypi.ngc.nvidia.com
Requirement already satisfied: matplotlib>=3.2.2 in /home/cyrus/.local/lib/python3.8/site-packages (from -r requirements.txt (line 4)) (3.5.3)
Requirement already satisfied: numpy>=1.18.5 in /home/cyrus/.local/lib/python3.8/site-packages (from -r requirements.txt (line 5)) (1.23.1)
Requirement already satisfied: opencv-python>=4.1.1 in /home/cyrus/.local/lib/python3.8/site-packages (from -r requirements.txt (line 6)) (4.6.0.66)
Requirement already satisfied: Pillow>=7.1.2 in /home/cyrus/.local/lib/python3.8/site-packages (from -r requirements.txt (line 7)) (9.2.0)
Requirement already satisfied: PyYAML>=5.3.1 in /usr/lib/python3/dist-packages (from -r requirements.txt (line 8)) (5.3.1)
Requirement already satisfied: requests>=2.23.0 in /home/cyrus/.local/lib/python3.8/site-packages (from -r requirements.txt (line 9)) (2.28.1)
Requirement already satisfied: scipy>=1.4.1 in /home/cyrus/.local/lib/python3.8/site-packages (from -r requirements.txt (line 10)) (1.9.0)
Requirement already satisfied: torch!=1.12.0,>=1.7.0 in /home/cyrus/.local/lib/python3.8/site-packages (from -r requirements.txt (line 11)) (1.12.1)
Requirement already satisfied: torchvision!=0.13.0,>=0.8.1 in /home/cyrus/.local/lib/python3.8/site-packages (from -r requirements.txt (line 12)) (0.13.1)
Requirement already satisfied: tqdm>=4.41.0 in /home/cyrus/.local/lib/python3.8/site-packages (from -r requirements.txt (line 13)) (4.64.1)
Requirement already satisfied: protobuf<4.21.3 in /home/cyrus/.local/lib/python3.8/site-packages (from -r requirements.txt (line 14)) (3.19.4)
Requirement already satisfied: tensorboard>=2.4.1 in /home/cyrus/.local/lib/python3.8/site-packages (from -r requirements.txt (line 17)) (2.9.1)
Requirement already satisfied: pandas>=1.1.4 in /home/cyrus/.local/lib/python3.8/site-packages (from -r requirements.txt (line 21)) (1.4.3)
Requirement already satisfied: seaborn>=0.11.0 in /home/cyrus/.local/lib/python3.8/site-packages (from -r requirements.txt (line 22)) (0.11.2)
Requirement already satisfied: ipython in /home/cyrus/.local/lib/python3.8/site-packages (from -r requirements.txt (line 34)) (8.3.0)
Requirement already satisfied: psutil in /home/cyrus/.local/lib/python3.8/site-packages (from -r requirements.txt (line 35)) (5.9.0)
Requirement already satisfied: thop in /home/cyrus/.local/lib/python3.8/site-packages (from -r requirements.txt (line 36)) (0.1.1.post2209072238)
Requirement already satisfied: kiwisolver>=1.0.1 in /home/cyrus/.local/lib/python3.8/site-packages (from matplotlib>=3.2.2->-r requirements.txt (line 4)) (1.4.4)
Requirement already satisfied: cycler>=0.10 in /home/cyrus/.local/lib/python3.8/site-packages (from matplotlib>=3.2.2->-r requirements.txt (line 4)) (0.11.0)
Requirement already satisfied: packaging>=20.0 in /home/cyrus/.local/lib/python3.8/site-packages (from matplotlib>=3.2.2->-r requirements.txt (line 4)) (21.3)
Requirement already satisfied: fonttools>=4.22.0 in /home/cyrus/.local/lib/python3.8/site-packages (from matplotlib>=3.2.2->-r requirements.txt (line 4)) (4.36.0)
Requirement already satisfied: python-dateutil>=2.7 in /home/cyrus/.local/lib/python3.8/site-packages (from matplotlib>=3.2.2->-r requirements.txt (line 4)) (2.8.2)
Requirement already satisfied: pyparsing>=2.2.1 in /home/cyrus/.local/lib/python3.8/site-packages (from matplotlib>=3.2.2->-r requirements.txt (line 4)) (3.0.7)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/lib/python3/dist-packages (from requests>=2.23.0->-r requirements.txt (line 9)) (1.25.8)
Requirement already satisfied: idna<4,>=2.5 in /usr/lib/python3/dist-packages (from requests>=2.23.0->-r requirements.txt (line 9)) (2.8)
Requirement already satisfied: charset-normalizer<3,>=2 in /home/cyrus/.local/lib/python3.8/site-packages (from requests>=2.23.0->-r requirements.txt (line 9)) (2.1.1)
Requirement already satisfied: certifi>=2017.4.17 in /usr/lib/python3/dist-packages (from requests>=2.23.0->-r requirements.txt (line 9)) (2019.11.28)
Requirement already satisfied: typing-extensions in /home/cyrus/.local/lib/python3.8/site-packages (from torch!=1.12.0,>=1.7.0->-r requirements.txt (line 11)) (4.1.1)
Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /home/cyrus/.local/lib/python3.8/site-packages (from tensorboard>=2.4.1->-r requirements.txt (line 17)) (1.8.1)
Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /home/cyrus/.local/lib/python3.8/site-packages (from tensorboard>=2.4.1->-r requirements.txt (line 17)) (0.4.6)
Requirement already satisfied: grpcio>=1.24.3 in /home/cyrus/.local/lib/python3.8/site-packages (from tensorboard>=2.4.1->-r requirements.txt (line 17)) (1.47.0)
Requirement already satisfied: setuptools>=41.0.0 in /usr/lib/python3/dist-packages (from tensorboard>=2.4.1->-r requirements.txt (line 17)) (45.2.0)
Requirement already satisfied: google-auth<3,>=1.6.3 in /home/cyrus/.local/lib/python3.8/site-packages (from tensorboard>=2.4.1->-r requirements.txt (line 17)) (2.9.0)
Requirement already satisfied: werkzeug>=1.0.1 in /home/cyrus/.local/lib/python3.8/site-packages (from tensorboard>=2.4.1->-r requirements.txt (line 17)) (2.0.3)
Requirement already satisfied: wheel>=0.26 in /usr/lib/python3/dist-packages (from tensorboard>=2.4.1->-r requirements.txt (line 17)) (0.34.2)
Requirement already satisfied: markdown>=2.6.8 in /home/cyrus/.local/lib/python3.8/site-packages (from tensorboard>=2.4.1->-r requirements.txt (line 17)) (3.3.7)
Requirement already satisfied: tensorboard-data-server<0.7.0,>=0.6.0 in /home/cyrus/.local/lib/python3.8/site-packages (from tensorboard>=2.4.1->-r requirements.txt (line 17)) (0.6.1)
Requirement already satisfied: absl-py>=0.4 in /home/cyrus/.local/lib/python3.8/site-packages (from tensorboard>=2.4.1->-r requirements.txt (line 17)) (1.1.0)
Requirement already satisfied: pytz>=2020.1 in /home/cyrus/.local/lib/python3.8/site-packages (from pandas>=1.1.4->-r requirements.txt (line 21)) (2022.2.1)
Requirement already satisfied: matplotlib-inline in /home/cyrus/.local/lib/python3.8/site-packages (from ipython->-r requirements.txt (line 34)) (0.1.3)
Requirement already satisfied: pygments>=2.4.0 in /home/cyrus/.local/lib/python3.8/site-packages (from ipython->-r requirements.txt (line 34)) (2.11.2)
Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /home/cyrus/.local/lib/python3.8/site-packages (from ipython->-r requirements.txt (line 34)) (3.0.29)
Requirement already satisfied: traitlets>=5 in /home/cyrus/.local/lib/python3.8/site-packages (from ipython->-r requirements.txt (line 34)) (5.2.0)
Requirement already satisfied: pexpect>4.3; sys_platform != "win32" in /usr/lib/python3/dist-packages (from ipython->-r requirements.txt (line 34)) (4.6.0)
Requirement already satisfied: stack-data in /home/cyrus/.local/lib/python3.8/site-packages (from ipython->-r requirements.txt (line 34)) (0.2.0)
Requirement already satisfied: backcall in /home/cyrus/.local/lib/python3.8/site-packages (from ipython->-r requirements.txt (line 34)) (0.2.0)
Requirement already satisfied: pickleshare in /home/cyrus/.local/lib/python3.8/site-packages (from ipython->-r requirements.txt (line 34)) (0.7.5)
Requirement already satisfied: decorator in /home/cyrus/.local/lib/python3.8/site-packages (from ipython->-r requirements.txt (line 34)) (5.1.1)
Requirement already satisfied: jedi>=0.16 in /home/cyrus/.local/lib/python3.8/site-packages (from ipython->-r requirements.txt (line 34)) (0.18.1)
Requirement already satisfied: six>=1.5 in /usr/lib/python3/dist-packages (from python-dateutil>=2.7->matplotlib>=3.2.2->-r requirements.txt (line 4)) (1.14.0)
Requirement already satisfied: requests-oauthlib>=0.7.0 in /home/cyrus/.local/lib/python3.8/site-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard>=2.4.1->-r requirements.txt (line 17)) (1.3.1)
Requirement already satisfied: rsa<5,>=3.1.4; python_version >= "3.6" in /home/cyrus/.local/lib/python3.8/site-packages (from google-auth<3,>=1.6.3->tensorboard>=2.4.1->-r requirements.txt (line 17)) (4.8)
Requirement already satisfied: pyasn1-modules>=0.2.1 in /home/cyrus/.local/lib/python3.8/site-packages (from google-auth<3,>=1.6.3->tensorboard>=2.4.1->-r requirements.txt (line 17)) (0.2.8)
Requirement already satisfied: cachetools<6.0,>=2.0.0 in /home/cyrus/.local/lib/python3.8/site-packages (from google-auth<3,>=1.6.3->tensorboard>=2.4.1->-r requirements.txt (line 17)) (5.2.0)
Requirement already satisfied: importlib-metadata>=4.4; python_version < "3.10" in /home/cyrus/.local/lib/python3.8/site-packages (from markdown>=2.6.8->tensorboard>=2.4.1->-r requirements.txt (line 17)) (4.11.3)
Requirement already satisfied: wcwidth in /home/cyrus/.local/lib/python3.8/site-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython->-r requirements.txt (line 34)) (0.2.5)
Requirement already satisfied: pure-eval in /home/cyrus/.local/lib/python3.8/site-packages (from stack-data->ipython->-r requirements.txt (line 34)) (0.2.2)
Requirement already satisfied: asttokens in /home/cyrus/.local/lib/python3.8/site-packages (from stack-data->ipython->-r requirements.txt (line 34)) (2.0.5)
Requirement already satisfied: executing in /home/cyrus/.local/lib/python3.8/site-packages (from stack-data->ipython->-r requirements.txt (line 34)) (0.8.3)
Requirement already satisfied: parso<0.9.0,>=0.8.0 in /home/cyrus/.local/lib/python3.8/site-packages (from jedi>=0.16->ipython->-r requirements.txt (line 34)) (0.8.3)
Requirement already satisfied: oauthlib>=3.0.0 in /usr/lib/python3/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard>=2.4.1->-r requirements.txt (line 17)) (3.1.0)
Requirement already satisfied: pyasn1>=0.1.3 in /home/cyrus/.local/lib/python3.8/site-packages (from rsa<5,>=3.1.4; python_version >= "3.6"->google-auth<3,>=1.6.3->tensorboard>=2.4.1->-r requirements.txt (line 17)) (0.4.8)
Requirement already satisfied: zipp>=0.5 in /home/cyrus/.local/lib/python3.8/site-packages (from importlib-metadata>=4.4; python_version < "3.10"->markdown>=2.6.8->tensorboard>=2.4.1->-r requirements.txt (line 17)) (3.8.0)
Linaom1214 commented 1 year ago

@cyrusbehr @mihuzz update your torch version, you can use the newest version.

cyrusbehr commented 1 year ago

@Linaom1214 I am apparently already using the newest version

image

cyrusbehr commented 1 year ago

@Linaom1214 is there a different version I should be using?

cyrusbehr commented 1 year ago

I followed the instructions in the colab notebook exactly. I'm not sure why it's not working...

cyrusbehr commented 1 year ago

Managed to get the conversion working in a nvidia/cuda:11.2.0-cudnn8-runtime-ubuntu20.04 container, indicated that there was indeed something wrong with my local env.