ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
49.97k stars 16.16k forks source link

TypeError: tuple indices must be integers or slices, not tuple #9390

Closed yangboz closed 1 year ago

yangboz commented 2 years ago

Search before asking

YOLOv5 Component

Training

Bug

Traceback (most recent call last): File "/home/xibaoda/yolov5_ws/train.py", line 664, in main(opt) File "/home/xibaoda/yolov5_ws/train.py", line 559, in main train(opt.hyp, opt, device, callbacks) File "/home/xibaoda/yolov5_ws/train.py", line 347, in train loss, loss_items = compute_loss(pred, targets.to(device)) # loss scaled by batch_size File "/home/xibaoda/yolov5_ws/utils/loss.py", line 125, in call tcls, tbox, indices, anchors = self.build_targets(p, targets) # targets File "/home/xibaoda/yolov5_ws/utils/loss.py", line 205, in build_targets r = t[..., 4:6] / anchors[:, None] # wh ratio TypeError: tuple indices must be integers or slices, not tuple

Environment

No response

Minimal Reproducible Example

CUDA_VISIBLE_DEVICES=0 python train.py --weight=yolov5s.pt --data=dataset4xyz.yaml --img=480

Additional

loss.py

dataset4xyz.yaml copy from default [dataset.yaml] python 3.9 torch 1.12.1 torchvision 0.13.1

Are you willing to submit a PR?

github-actions[bot] commented 2 years ago

👋 Hello @yangboz, thank you for your interest in YOLOv5 🚀! 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 support@ultralytics.com.

Requirements

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

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

Environments

YOLOv5 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 YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 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 commented 2 years ago

@yangboz there might be something wrong with your model yaml or dataset format. See Train Custom Data tutorial to get started:

Tutorials

Good luck 🍀 and let us know if you have any other questions!

github-actions[bot] commented 1 year ago

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

HaHalr commented 6 months ago

你好,请问解决了这个问题吗

glenn-jocher commented 6 months ago

@HaHalr 你好!看起来你是在寻找有关之前讨论的问题的解决方案。如果你遇到的是特定的错误消息或问题,我建议首先检查你的代码是否最新,并确认你遵循了所有的数据准备和训练步骤。如果问题仍然存在,请提供更具体的错误描述和代码上下文,这样我们可以更准确地帮助你。感谢你对YOLOv5的支持!🌟

HaHalr commented 6 months ago

@glenn-jocher Hello Author, I have followed the data preparation and training steps, but I still find that the problem exists. I am at a loss on how to deal with it and have not found any relevant solutions, so I am seeking help. Below is the error information prompted after I executed train.py:

home/liu/anaconda3/yes/envs/myolov5/lib/python3.7/site-packages/torch/functional.py:504: 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:3190.) return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined] Model Summary: 363 layers, 7760090 parameters, 7760090 gradients, 23.9 GFLOPS

Transferred 354/450 items from ./yolov5s.pt Scaled weight_decay = 0.0005625000000000001 Optimizer groups: 77 .bias, 79 conv.weight, 73 other train: Scanning 'data/citys/detdata/labels/train.cache' images and labels... 2975 found, 0 missing, 1 empty, 118 corrupt val: Scanning 'data/citys/detdata/labels/val.cache' images and labels... 500 found, 0 missing, 3 empty, 11 corrupted: 10 Plotting labels... Found 500 images in the folder ./data/citys/leftImg8bit/val BaseDataset: base_size 1024, crop_size (832, 416) Random scale low: 0.65, high: 3, sample_std: 25 Found 2975 images in the folder ./data/citys/leftImg8bit/train Image sizes 832 train, 832 test Using 8 dataloader workers Logging results to runs/train/exp54 Starting training for 200 epochs... accumulate: 4

 Epoch   gpu_mem       box       obj       cls     total       seg    labels  img_size

0%| | 0/159 [00:12<?, ?it/s] 0%| | 0/159 [00:12<?, ?it/s] Traceback (most recent call last): File "train.py", line 635, in train(hyp, opt, device, tb_writer) File "train.py", line 365, in train loss, loss_items = compute_loss(pred[0], targets.to(device)) # loss scaled by batch_size File "/media/liu/Liu/code/multiyolov5/utils/loss.py", line 118, in call tcls, tbox, indices, anchors = self.build_targets(p, targets) # targets File "/media/liu/Liu/code/multiyolov5/utils/loss.py", line 186, in build_targets r = t[:, :, 4:6] / anchors[:, None] # 元 TypeError: tuple indices must be integers or slices, not tuple

. I hope you can give me some direction or method to solve this problem. I would be very grateful.

glenn-jocher commented 6 months ago

@HaHalr Hi there! It looks like the issue is related to improper indexing or data types when handling the anchors in your loss.py. This type of error usually happens when the expected object structure is different from what is actually provided.

Please ensure that anchors is a tensor. If it's being loaded as a different datatype (e.g., a list or a numpy array), you might need to convert it to a PyTorch tensor before performing operations on it. You can convert anchors to a tensor using:

anchors = torch.tensor(anchors).float().to(device)  # Assuming you have a device defined

Also, double-check the shape and type of t and anchors just before the line causing the error to ensure they are compatible for the operations you're trying to perform.

If the problem persists, it might be helpful to print out the types and shapes of variables involved in the operation causing the error right before it occurs. This can give you more insight into why the type error is happening.

Keep going, debugging issues like this can be tricky but you're on the right path! 🚀

HaHalr commented 6 months ago

@glenn-jocher Dear author, thank you for your valuable suggestions. After repeatedly checking the loss.py file, I found two errors and have corrected them. I am very grateful for your recent responses. I would like to share the joy of solving the problem with you. Thank you once again! OEZ(V9VU@~7OD1091VZXDBC_tmb

glenn-jocher commented 6 months ago

@HaHalr That's fantastic news! 🎉 I'm glad to hear you were able to solve the issues by making those corrections in the loss.py file. Thanks for sharing your success - it's always rewarding to see problems being solved, and your perseverance is commendable. Keep up the great work, and don't hesitate to reach out if you run into further questions or need assistance down the road. Happy coding! 😊

Unlicensed-driver-ljx commented 4 months ago

@HaHalr 麻烦能告知改了那个地方吗

daidairong commented 2 months ago

尊敬的作者,感谢您的宝贵建议。在反复检查 loss.py 文件后,我发现了两个错误并已进行了纠正。我非常感谢你最近的答复。我想与您分享解决问题的喜悦。再次感谢你们! OEZ(V9VU@~7OD1091VZXDBC_tmb

想知道改了哪里求求了

HaHalr commented 2 months ago

错误原因好像出才数据集上,有点忘了,你可以先试试用200张或者更少的数据集试试,因为我后续用了yolo 其他版本,所以可能有点忘了在 2024年7月28日,12:15,daidairong @.***> 写道:

尊敬的作者,感谢您的宝贵建议。在反复检查 loss.py 文件后,我发现了两个错误并已进行了纠正。我非常感谢你最近的答复。我想与您分享解决问题的喜悦。再次感谢你们!

想知道改了哪里求求了

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>

glenn-jocher commented 2 months ago

@HaHalr 你好!很高兴听到你已经解决了问题并且愿意分享你的解决方案。对于@HaHalr 提到的错误,可能确实与数据集有关。为了帮助其他遇到类似问题的用户,能否请你详细描述一下你在 loss.py 文件中所做的具体修改?

如果你不太记得具体的修改内容,可以尝试以下步骤来帮助其他用户排查问题:

  1. 数据集检查:确保你的数据集没有损坏的文件或标签。你可以先用一个较小的数据集(例如200张图片)进行测试,看看问题是否依然存在。
  2. 代码调试:在 loss.py 文件中,添加一些调试信息来打印出变量的类型和形状,以确保它们符合预期。例如:
    print(f"Type of anchors: {type(anchors)}, Shape of anchors: {anchors.shape}")
    print(f"Type of t: {type(t)}, Shape of t: {t.shape}")
  3. 最新版本:确保你使用的是 YOLOv5 的最新版本,因为一些已知问题可能在新版本中已经修复。

希望这些建议对你有所帮助。如果你有更多的细节可以分享,欢迎随时更新!感谢你对 YOLOv5 社区的支持!😊