Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 of 2 tasks
yangboz opened this issue Sep 13, 2022 · 13 comments
Closed
1 of 2 tasks

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

yangboz opened this issue Sep 13, 2022 · 13 comments
Labels
bug Something isn't working Stale Stale and schedule for closing soon

Comments

@yangboz
Copy link

yangboz commented Sep 13, 2022

Search before asking

  • I have searched the YOLOv5 issues and found no similar bug report.

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?

  • Yes I'd like to help by submitting a PR!
@yangboz yangboz added the bug Something isn't working label Sep 13, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Sep 13, 2022

👋 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
Copy link
Member

glenn-jocher commented Sep 15, 2022

@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
Copy link
Contributor

github-actions bot commented Oct 16, 2022

👋 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 ⭐!

@github-actions github-actions bot added the Stale Stale and schedule for closing soon label Oct 16, 2022
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 27, 2022
@HaHalr
Copy link

HaHalr commented Mar 19, 2024

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

@glenn-jocher
Copy link
Member

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

@HaHalr
Copy link

HaHalr commented Mar 21, 2024

@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
Copy link
Member

@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
Copy link

HaHalr commented Mar 21, 2024

@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
Copy link
Member

@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
Copy link

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

@daidairong
Copy link

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

想知道改了哪里求求了

@HaHalr
Copy link

HaHalr commented Jul 28, 2024 via email

@glenn-jocher
Copy link
Member

@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 社区的支持!😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Stale Stale and schedule for closing soon
Projects
None yet
Development

No branches or pull requests

5 participants