ultralytics / yolov5

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

a questions when improve YOLOv5 #12514

Closed haoaZ closed 7 months ago

haoaZ commented 9 months ago

Search before asking

YOLOv5 Component

Training, Detection

Bug

I want to improve ECA-attention, but there have same bug, which i cant not solve, i want your help@glenn-jocher . When i run yolo.py it work, but run train.py, there have been some issues. `class EfficientChannelAttention(nn.Module): # Efficient Channel Attention module def init(self, c, b=1, gamma=2): super(EfficientChannelAttention, self).init() t = int(abs((math.log(c, 2) + b) / gamma)) k = t if t % 2 else t + 1

    self.avg_pool = nn.AdaptiveAvgPool2d(1)
    self.conv1 = nn.Conv1d(1, 1, kernel_size=k, padding=int(k/2), bias=False)
    self.sigmoid = nn.Sigmoid()

def forward(self, x):
    # print('x是:{}'.format(x.size))
    out = self.avg_pool(x)
    # print('out是:{}'.format(out))
    out_flat = out.view(-1)
    orig_shape = out.size()
    print('out_flat:{}'.format(out_flat))
    sorted_indices = torch.argsort(out_flat,descending=True)
    print('sorted_indices为:{}'.format(sorted_indices))
    reshape_indices = sorted_indices.view(*orig_shape)
    # print('reshape_indices:{}'.format(reshape_indices.shape))
    soted_out = out.flatten()[sorted_indices].reshape(*orig_shape)
    # print('soted_out为:{}'.format(soted_out))
    # sorted_x = x.view(x.size()[0],-1,x.size()[-2],x.size()[-1])[reshape_indices]
    sorted_x = torch.index_select(x, dim = 1, index =sorted_indices)
    # print('sorted_x的形状:{}'.format(sorted_x.shape))
    # print('排序后的x:{}'.format(sorted_x))
    out2 = self.avg_pool(sorted_x)
    # print('avgpool验证排序:{}'.format(out2))
    soted_out = self.conv1(soted_out.squeeze(-1).transpose(-1, -2)).transpose(-1, -2).unsqueeze(-1)
    soted_out = self.sigmoid(soted_out)
    # print('out的形状:{}'.format(out.shape))
    # print(out * sorted_x)
    return soted_out * sorted_x`

`# parameters nc: 80 # number of classes depth_multiple: 0.33 # model depth multiple width_multiple: 0.50 # layer channel multiple

anchors

anchors:

YOLOv5 backbone

backbone:

[from, number, module, args]

[[-1, 1, Focus, [64, 3]], # 0-P1/2 [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 [-1, 3, C3, [128]], [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 [-1, 9, C3, [256]], [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 [-1, 9, C3, [512]], [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 [-1, 1, SPP, [1024, [5, 9, 13]]], [-1, 3, C3, [1024, False]], # 9 ]

YOLOv5 head

head: [[-1, 1, Conv, [512, 1, 1]], [-1, 1, nn.Upsample, [None, 2, 'nearest']], [[-1, 6], 1, Concat, [1]], # cat backbone P4 [-1, 3, C3, [512, False]], # 13

[-1, 1, Conv, [256, 1, 1]], [-1, 1, nn.Upsample, [None, 2, 'nearest']], [[-1, 4], 1, Concat, [1]], # cat backbone P3 [-1, 3, C3, [256, False]], # 17 (P3/8-small)

[-1, 1, Conv, [256, 3, 2]], [[-1, 14], 1, Concat, [1]], # cat head P4 [-1, 3, C3, [512, False]], # 20 (P4/16-medium)

[-1, 1, Conv, [512, 3, 2]], [-1, 1, EfficientChannelAttention, [512]], [[-1, 10], 1, Concat, [1]], # cat head P5 [-1, 3, C3, [1024, False]], # 23 (P5/32-large)

[[17, 20, 24], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) ] When i use cpu the follow problem appear: Epoch gpu_mem box obj cls total labels img_size 0%| | 0/11049 [00:00<?, ?it/s] out_flat:tensor([ 0.13989, 0.01097, 0.67497, ..., 0.14956, 0.13888, -0.00238], grad_fn=) sorted_indices为:tensor([ 27, 84, 107, ..., 539, 596, 706]) Traceback (most recent call last): File "/home/wjh/learning/1/yolov5-5.0/train.py", line 543, in train(hyp, opt, device, tb_writer) File "/home/wjh/learning/1/yolov5-5.0/train.py", line 303, in train pred = model(imgs) # forward File "/home/wjh/.conda/envs/Yolov5/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1194, in _call_impl return forward_call(*input, *kwargs) File "/home/wjh/learning/1/yolov5-5.0/models/yolo.py", line 123, in forward return self.forward_once(x, profile) # single-scale inference, train File "/home/wjh/learning/1/yolov5-5.0/models/yolo.py", line 139, in forward_once x = m(x) # run File "/home/wjh/.conda/envs/Yolov5/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1194, in _call_impl return forward_call(input, **kwargs) File "/home/wjh/learning/1/yolov5-5.0/models/common.py", line 411, in forward sorted_x = torch.index_select(x, dim = 1, index =sorted_indices) RuntimeError: INDICES element is out of DATA bounds, id=918 axis_dim=256

进程已结束,退出代码` The display exceeds the index, but I have checked the index during yolo. py runtime and everything is fine,

Environment

No response

Minimal Reproducible Example

No response

Additional

No response

Are you willing to submit a PR?

github-actions[bot] commented 9 months ago

👋 Hello @haoaZ, 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 a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Requirements

Python>=3.8.0 with all requirements.txt installed including PyTorch>=1.8. 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

YOLOv5 CI

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training, validation, inference, export and benchmarks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

Introducing YOLOv8 🚀

We're excited to announce the launch of our latest state-of-the-art (SOTA) object detection model for 2023 - YOLOv8 🚀!

Designed to be fast, accurate, and easy to use, YOLOv8 is an ideal choice for a wide range of object detection, image segmentation and image classification tasks. With YOLOv8, you'll be able to quickly and accurately detect objects in real-time, streamline your workflows, and achieve new levels of accuracy in your projects.

Check out our YOLOv8 Docs for details and get started with:

pip install ultralytics
glenn-jocher commented 9 months ago

@haoaZ hi there! Thanks for reaching out. It looks like you're encountering an issue related to ECA-attention during training. The error message suggests an out-of-bounds index issue. Although the issue appears complex, it might be arising from the environment or the way the ECA-attention module is integrated into YOLOv5.

To troubleshoot, I recommend checking if the ECA-attention module is properly supported for training and if there are any compatibility issues with your specific environment. Additionally, reviewing the index manipulation and ensuring that it aligns with YOLOv5's requirements might also be helpful.

I appreciate your willingness to improve YOLOv5 and encourage you to keep at it - the YOLO community and the Ultralytics team are grateful for contributions of all kinds. If you're able to identify a resolution, feel free to submit a PR – it would be much appreciated!

haoaZ commented 9 months ago

Thanks for your reply, i have found the problem, the eca attention code in common.py is based on bath size=1, but the training is not 1. I will try to adjust the code to fit all bath sizes. If you have any suggestions for adjustment, I hope to receive your reply.

glenn-jocher commented 9 months ago

@haoaZ great to hear that you've identified the issue! Adapting the ECA attention code to accommodate varying batch sizes is a good approach. You may want to consider using dynamic tensor manipulations that can handle different batch sizes. Additionally, reviewing PyTorch's documentation on tensor operations for batched inputs could provide valuable insights.

Your dedication to improving the YOLOv5 framework is commendable. If you encounter specific challenges while making the adjustments, feel free to ask for help or guidance. Keep up the excellent work!

github-actions[bot] commented 8 months ago

👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

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 YOLO 🚀 and Vision AI ⭐