ultralytics / yolov5

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

RuntimeError: The size of tensor a (80) must match the size of tensor b (60) at non-singleton dimension 3 #13302

Open haniraid opened 2 months ago

haniraid commented 2 months ago

Search before asking

YOLOv5 Component

Detection

Bug

i tried to train my yolov5 on this dataset https://www.kaggle.com/datasets/thepbordin/indoor-object-detection/data and after i tried to run the detection using ros2 node with robomaster s1 monocular camera im having this issue and i cant get rid of it ros2_ws/src/YOLOv5-ROS/yolov5_ros/yolov5_ros$ ros2 launch yolov5_ros yolov5s_simple.launch.py [INFO] [launch]: All log files can be found below /home/raidhani/.ros/log/2024-09-07-20-24-29-467496-raidhani-All-Series-149777 [INFO] [launch]: Default logging verbosity is set to INFO [INFO] [v4l2_camera_node-1]: process started with pid [149780] [INFO] [yolov5_ros-2]: process started with pid [149782] [v4l2_camera_node-1] [ERROR] [1725755069.564331567] [v4l2_camera]: Failed opening device /dev/video0: No such file or directory (2) [yolov5_ros-2] YOLOv5 ๐Ÿš€ 2024-9-2 torch 1.11.0+cpu CPU [yolov5_ros-2] [yolov5_ros-2] Loading config/best.torchscript for TorchScript inference... [yolov5_ros-2] Traceback (most recent call last): [yolov5_ros-2] File "/home/raidhani/ros2_ws/src/YOLOv5-ROS/yolov5_ros/install/yolov5_ros/lib/yolov5_ros/yolov5_ros", line 33, in [yolov5_ros-2] sys.exit(load_entry_point('yolov5-ros', 'console_scripts', 'yolov5_ros')()) [yolov5_ros-2] File "/home/raidhani/ros2_ws/src/YOLOv5-ROS/yolov5_ros/build/yolov5_ros/yolov5_ros/main.py", line 283, in ros_main [yolov5_ros-2] rclpy.spin(yolov5_node) [yolov5_ros-2] File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/init.py", line 191, in spin [yolov5_ros-2] executor.spin_once() [yolov5_ros-2] File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/executors.py", line 719, in spin_once [yolov5_ros-2] raise handler.exception() [yolov5_ros-2] File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/task.py", line 239, in call [yolov5_ros-2] self._handler.send(None) [yolov5_ros-2] File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/executors.py", line 429, in handler [yolov5_ros-2] await call_coroutine(entity, arg) [yolov5_ros-2] File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/executors.py", line 354, in _execute_subscription [yolov5_ros-2] await await_or_execute(sub.callback, msg) [yolov5_ros-2] File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/executors.py", line 118, in await_or_execute [yolov5_ros-2] return callback(args) [yolov5_ros-2] File "/home/raidhani/ros2_ws/src/YOLOv5-ROS/yolov5_ros/build/yolov5_ros/yolov5_ros/main.py", line 269, in image_callback [yolov5_ros-2] class_list, confidence_list, x_min_list, y_min_list, x_max_list, y_max_list = self.yolov5.image_callback(image_raw) [yolov5_ros-2] File "/home/raidhani/ros2_ws/src/YOLOv5-ROS/yolov5_ros/build/yolov5_ros/yolov5_ros/main.py", line 128, in image_callback [yolov5_ros-2] pred = self.model(im, augment=False, visualize=False) [yolov5_ros-2] File "/home/raidhani/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl [yolov5_ros-2] return forward_call(input, *kwargs) [yolov5_ros-2] File "/home/raidhani/ros2_ws/src/YOLOv5-ROS/yolov5_ros/build/yolov5_ros/yolov5_ros/models/common.py", line 416, in forward [yolov5_ros-2] y = self.model(im)[0] [yolov5_ros-2] File "/home/raidhani/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl [yolov5_ros-2] return forward_call(input, **kwargs) [yolov5_ros-2] RuntimeError: The following operation failed in the TorchScript interpreter. [yolov5_ros-2] Traceback of TorchScript, serialized code (most recent call last): [yolov5_ros-2] File "code/torch/models/yolo.py", line 78, in forward [yolov5_ros-2] _36 = (_20).forward(act, _35, ) [yolov5_ros-2] _37 = (_22).forward((_21).forward(act, _36, ), _30, ) [yolov5_ros-2] _38 = (_24).forward(_34, _36, (_23).forward(_37, ), ) [yolov5_ros-2] ~~~~ <--- HERE [yolov5_ros-2] return (_38,) [yolov5_ros-2] class Detect(Module): [yolov5_ros-2] File "code/torch/models/yolo.py", line 108, in forward [yolov5_ros-2] _20 = torch.split_with_sizes(torch.sigmoid(_19), [2, 2, 11], 4) [yolov5_ros-2] xy, wh, conf, = _20 [yolov5_ros-2] _21 = torch.add(torch.mul(xy, CONSTANTS.c0), CONSTANTS.c1) [yolov5_ros-2] ~~~~~ <--- HERE [yolov5_ros-2] xy0 = torch.mul(_21, torch.select(CONSTANTS.c2, 0, 0)) [yolov5_ros-2] _22 = torch.pow(torch.mul(wh, CONSTANTS.c0), 2) [yolov5_ros-2] [yolov5_ros-2] Traceback of TorchScript, original code (most recent call last): [yolov5_ros-2] /home/raidhani/ros2_ws/src/yolov5/models/yolo.py(111): forward [yolov5_ros-2] /home/raidhani/.local/lib/python3.8/site-packages/torch/nn/modules/module.py(1098): _slow_forward [yolov5_ros-2] /home/raidhani/.local/lib/python3.8/site-packages/torch/nn/modules/module.py(1110): _call_impl [yolov5_ros-2] /home/raidhani/ros2_ws/src/yolov5/models/yolo.py(169): _forward_once [yolov5_ros-2] /home/raidhani/ros2_ws/src/yolov5/models/yolo.py(270): forward [yolov5_ros-2] /home/raidhani/.local/lib/python3.8/site-packages/torch/nn/modules/module.py(1098): _slow_forward [yolov5_ros-2] /home/raidhani/.local/lib/python3.8/site-packages/torch/nn/modules/module.py(1110): _call_impl [yolov5_ros-2] /home/raidhani/.local/lib/python3.8/site-packages/torch/jit/_trace.py(958): trace_module [yolov5_ros-2] /home/raidhani/.local/lib/python3.8/site-packages/torch/jit/_trace.py(741): trace [yolov5_ros-2] export.py(274): export_torchscript [yolov5_ros-2] export.py(218): outer_func [yolov5_ros-2] export.py(1414): run [yolov5_ros-2] /home/raidhani/.local/lib/python3.8/site-packages/torch/autograd/grad_mode.py(27): decorate_context [yolov5_ros-2] export.py(1536): main [yolov5_ros-2] export.py(1541): [yolov5_ros-2] RuntimeError: The size of tensor a (80) must match the size of tensor b (60) at non-singleton dimension 3 [yolov5_ros-2] [ERROR] [yolov5_ros-2]: process has died [pid 149782, exit code 1, cmd '/home/raidhani/ros2_ws/src/YOLOv5-ROS/yolov5_ros/install/yolov5_ros/lib/yolov5_ros/yolov5_ros --ros-args --params-file /tmp/launch_params_s6m3axfb']. ^C[WARNING] [launch]: user interrupted with ctrl-c (SIGINT)

Environment

Yolov5 with ros2 wrapper on ubuntu 20.04 using libtorch 1.11.0+cpu

Minimal Reproducible Example

RuntimeError: The size of tensor a (80) must match the size of tensor b (60) at non-singleton dimension 3

Additional

No response

Are you willing to submit a PR?

glenn-jocher commented 2 months ago

@haniraid the error you're encountering suggests a mismatch in tensor dimensions, likely due to a configuration issue with your model or dataset. Please ensure your dataset and model configurations are correctly aligned, particularly the number of classes. Also, verify that you are using the latest version of YOLOv5 and PyTorch. If the issue persists, try re-cloning the repository and re-installing the dependencies. For further guidance, refer to the YOLOv5 documentation.

haniraid commented 2 months ago

Thank you for your tips, i recloned yolov5 to the last version v7.0 and upgraded pytorch as you suggested. also ive checked the classes in my model and also in my yaml file the are the same (10 classes), but the issue still presist. notice that whene i try to rexport the model including torchscript the dimesions in the erro they change.

[yolov5_ros-2] YOLOv5 ๐Ÿš€ 2024-9-7 torch 1.11.0+cpu CPU [yolov5_ros-2] [yolov5_ros-2] Loading YOLOv5-ROS/yolov5_ros/yolov5_ros/config/best416.torchscript for TorchScript inference... [yolov5_ros-2] Traceback (most recent call last): [yolov5_ros-2] File "/home/raidhani/ros2_ws/src/install/yolov5_ros/lib/yolov5_ros/yolov5_ros", line 33, in [yolov5_ros-2] sys.exit(load_entry_point('yolov5-ros', 'console_scripts', 'yolov5_ros')()) [yolov5_ros-2] File "/home/raidhani/ros2_ws/src/build/yolov5_ros/yolov5_ros/main.py", line 283, in ros_main [yolov5_ros-2] rclpy.spin(yolov5_node) [yolov5_ros-2] File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/init.py", line 191, in spin [yolov5_ros-2] executor.spin_once() [yolov5_ros-2] File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/executors.py", line 719, in spin_once [yolov5_ros-2] raise handler.exception() [yolov5_ros-2] File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/task.py", line 239, in call [yolov5_ros-2] self._handler.send(None) [yolov5_ros-2] File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/executors.py", line 429, in handler [yolov5_ros-2] await call_coroutine(entity, arg) [yolov5_ros-2] File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/executors.py", line 354, in _execute_subscription [yolov5_ros-2] await await_or_execute(sub.callback, msg) [yolov5_ros-2] File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/executors.py", line 118, in await_or_execute [yolov5_ros-2] return callback(args) [yolov5_ros-2] File "/home/raidhani/ros2_ws/src/build/yolov5_ros/yolov5_ros/main.py", line 269, in image_callback [yolov5_ros-2] class_list, confidence_list, x_min_list, y_min_list, x_max_list, y_max_list = self.yolov5.image_callback(image_raw) [yolov5_ros-2] File "/home/raidhani/ros2_ws/src/build/yolov5_ros/yolov5_ros/main.py", line 128, in image_callback [yolov5_ros-2] pred = self.model(im, augment=False, visualize=False) [yolov5_ros-2] File "/home/raidhani/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl [yolov5_ros-2] return forward_call(input, *kwargs) [yolov5_ros-2] File "/home/raidhani/ros2_ws/src/build/yolov5_ros/yolov5_ros/models/common.py", line 416, in forward [yolov5_ros-2] y = self.model(im)[0] [yolov5_ros-2] File "/home/raidhani/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl [yolov5_ros-2] return forward_call(input, **kwargs) [yolov5_ros-2] RuntimeError: The following operation failed in the TorchScript interpreter. [yolov5_ros-2] Traceback of TorchScript, serialized code (most recent call last): [yolov5_ros-2] File "code/torch/models/yolo.py", line 78, in forward [yolov5_ros-2] _36 = (_20).forward(act, _35, ) [yolov5_ros-2] _37 = (_22).forward((_21).forward(act, _36, ), _30, ) [yolov5_ros-2] _38 = (_24).forward(_34, _36, (_23).forward(_37, ), ) [yolov5_ros-2] ~~~~ <--- HERE [yolov5_ros-2] return (_38,) [yolov5_ros-2] class Detect(Module): [yolov5_ros-2] File "code/torch/models/yolo.py", line 108, in forward [yolov5_ros-2] _20 = torch.split_with_sizes(torch.sigmoid(_19), [2, 2, 11], 4) [yolov5_ros-2] xy, wh, conf, = _20 [yolov5_ros-2] _21 = torch.add(torch.mul(xy, CONSTANTS.c0), CONSTANTS.c1) [yolov5_ros-2] ~~~~~ <--- HERE [yolov5_ros-2] xy0 = torch.mul(_21, torch.select(CONSTANTS.c2, 0, 0)) [yolov5_ros-2] _22 = torch.pow(torch.mul(wh, CONSTANTS.c0), 2) [yolov5_ros-2] [yolov5_ros-2] Traceback of TorchScript, original code (most recent call last): [yolov5_ros-2] /home/raidhani/ros2_ws/src/yolov5/models/yolo.py(111): forward [yolov5_ros-2] /home/raidhani/.local/lib/python3.8/site-packages/torch/nn/modules/module.py(1098): _slow_forward [yolov5_ros-2] /home/raidhani/.local/lib/python3.8/site-packages/torch/nn/modules/module.py(1110): _call_impl [yolov5_ros-2] /home/raidhani/ros2_ws/src/yolov5/models/yolo.py(169): _forward_once [yolov5_ros-2] /home/raidhani/ros2_ws/src/yolov5/models/yolo.py(270): forward [yolov5_ros-2] /home/raidhani/.local/lib/python3.8/site-packages/torch/nn/modules/module.py(1098): _slow_forward [yolov5_ros-2] /home/raidhani/.local/lib/python3.8/site-packages/torch/nn/modules/module.py(1110): _call_impl [yolov5_ros-2] /home/raidhani/.local/lib/python3.8/site-packages/torch/jit/_trace.py(958): trace_module [yolov5_ros-2] /home/raidhani/.local/lib/python3.8/site-packages/torch/jit/_trace.py(741): trace [yolov5_ros-2] export.py(274): export_torchscript [yolov5_ros-2] export.py(218): outer_func [yolov5_ros-2] export.py(1414): run [yolov5_ros-2] /home/raidhani/.local/lib/python3.8/site-packages/torch/autograd/grad_mode.py(27): decorate_context [yolov5_ros-2] export.py(1536): main [yolov5_ros-2] export.py(1541): [yolov5_ros-2] RuntimeError: The size of tensor a (80) must match the size of tensor b (52) at non-singleton dimension 3

haniraid commented 2 months ago

im sorry. i changed a line in comon.py in the ros2 wrapper and rebuilded the ws after i cloned the last version of yolov5 as you mentionned also i upgraded pytorch. and BANG it WORKED!!! thank you so much for your help.

glenn-jocher commented 2 months ago

Glad to hear it's working now! If you have any more questions, feel free to reach out. Thanks to the YOLO community and the Ultralytics team for their support!