ultralytics / yolov5

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

The advantage of yolov5s #5730

Closed liang-jingyi closed 2 years ago

liang-jingyi commented 2 years ago

Search before asking

Question

When I was learning yolov4Tiny and Yolov5s, I found a big difference in performance between the two, with yolov5S working much better. I want to know what makes this difference. I know that FPN and SPP can greatly improve yOLO network performance. In addition, I wonder if the number of C3 stacks is also an important factor. The higher the number of layers, the better the network performance. I'm confused. Please help me. Thank you!

Additional

No response

github-actions[bot] commented 2 years ago

πŸ‘‹ Hello @liang-jingyi, 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 Glenn Jocher at glenn.jocher@ultralytics.com.

Requirements

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

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

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.

Zengyf-CVer commented 2 years ago

@liang-jingyi You can compare the parameters of the two, the weight file of yolov5s is much smaller than that of yolov4 tiny.

liang-jingyi commented 2 years ago

Yes, I wonder why yolov5s is so much more accurate than Yolov4Tiny.

glenn-jocher commented 2 years ago

@liang-jingyi it's more accurate because of thousands of hours of experiments I ran when developing it and a multitude of improvements such as target assignment, improved augmentation, SiLU activations, EMA, C3 modules, an extra P5 output head (tiny has only P3, P4), and more layers in general.

In general the tiny models are faster but also larger and with much worse accuracy (basically unusable in real-world products).

liang-jingyi commented 2 years ago

@glenn-jocher When I compared the network structure, I found that you stacked three C3s in a row in two places on the backbone network, and you used CON instead of pool. Is that one of the reasons for yolov5S's success? Thank you very much

glenn-jocher commented 2 years ago

@liang-jingyi no. C3() modules are evolved CSPBottleneck() modules with the least productive of of the 4 modules removed, leaving 3 (hence the name), and also achieving a few more efficiencies at fuse time. The benefit is not improved accuracy, it's less layers and parameters and faster speeds.

liang-jingyi commented 2 years ago

@glenn-jocher Thank you very much for your answer! It helps me a lot.

liang-jingyi commented 2 years ago

@Zengyf-CVer @glenn-jocher I constructed the yolov4tiny.cfg file using the Yolov5 framework. Why is the weight file trained by me much smaller than the weight file trained by the yolov4 authors?I trained them to be even smaller than Yolov5s. The yolov4Tiny model I constructed has a parameter of 6 million.I don't understand why. Please help me

Zengyf-CVer commented 2 years ago

@liang-jingyi First of all, I need you to answer a few questions:

  1. Is your yolov4tiny.cfg based on the C++ version or the PyTorch version?
  2. What are the original parameters of yolov4tiny? Compare.
  3. Your build file has not been given now, and we cannot infer whether your build is accurate. If it is convenient, you can show it.
liang-jingyi commented 2 years ago

@Zengyf-CVer Sorry, I misdescribed, the file I built is yaml format, not cfg file 1、I use pytorch.Because yolov5 is easy to use, I added yolov4tiny.yaml directly to yolov5 2、I am comparing Yolov5s to Yolov4Tiny.The weight file trained by Yolov4Tiny should be larger than yolov5s. But the yolov4Tiny weight file I trained is smaller than yolov5s. 3、The contents of yolov4tiny.yaml that I built.

parameters

nc: 80 # number of classes depth_multiple: 1.0 # model depth multiple width_multiple: 1.0 # layer channel multiple

anchors

anchors:

YOLOv4-tiny backbone

backbone:

[from, number, module, args]

[[-1, 1, BasicConv, [32, 3, 2]], # 0 [-1, 1, BasicConv, [64, 3, 2]], # 1-P1/2

[-1, 1, BasicConv, [64, 3, 1]], [-1, 1, BConv, [64, 64]], # 3-P2/4 [[-1, 2], 1, Concat, [1]],

[-1, 1, nn.MaxPool2d, [2, 2, 0]], # 5-P3/8

[-1, 1, BasicConv, [128, 3, 1]], [-1, 1, BConv, [128, 128]], # 7-P4/16 [[-1, 6], 1, Concat, [1]],

[-1, 1, nn.MaxPool2d, [2, 2, 0]], # 9-P4/16

[-1, 1, BasicConv, [256, 3, 1]], [-1, 1, BConv, [256, 256]], # 11 [[-1, 10], 1, Concat, [1]],

[-1, 1, nn.MaxPool2d, [2, 2, 0]], # 13-P5/32

[-1, 1, BasicConv, [512, 3, 1]], ]

YOLOv4-tiny head

head: [[-1, 1, BasicConv, [256, 1, 1]], #15 [-1, 1, BasicConv, [512, 3, 1]],

[-2, 1, BasicConv, [128, 1, 1]], # 17 (P5/32-large) [-1, 1, nn.Upsample, [None, 2, 'nearest']], [[-1, 11], 1, Concat, [1]], # cat backbone P4 [-1, 1, BasicConv, [256, 3, 1]], # 20 (P4/16-medium)

[[20, 16], 1, Detect, [nc, anchors]], # Detect(P4, P5) ]

common.py

I created two class.

class BasicConv(nn.Module): def init(self, c1, c2, k=1, s=1, p=None, g=1): super(BasicConv, self).init()

    self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False)
    self.bn = nn.BatchNorm2d(c2)
    self.activation = nn.LeakyReLU(0.1)

def forward(self, x):
    x = self.conv(x)
    x = self.bn(x)
    x = self.activation(x)
    return x

class BConv(nn.Module): def init(self, c1, c2, k=1, s=1, p=None, g=1): super(BConv, self).init()

    self.c2 = c2
    self.conv2 = BasicConv(c2 // 2, c2 // 2, 3)
    self.conv3 = BasicConv(c2 // 2, c2 // 2, 3)
    self.conv4 = BasicConv(c2, c2, 1)

def forward(self, x):
    c = self.c2
    x = torch.split(x, c // 2, dim=1)[1]
    x = self.conv2(x)
    route = x
    x = self.conv3(x)
    x = torch.cat([x, route], dim=1)

    x = self.conv4(x)
    return x

Please forgive me for not being able to upload the file. The network speed is too slow.Thank you very much for your reply.

Zengyf-CVer commented 2 years ago

@liang-jingyi There are still a few questions that you need to add.

  1. Have you used a pre-trained model? If it is used, what is it used? yolov5n?yolov5s?
  2. What are your training instructions? For example, --img-size. Please write your complete training instructions.
liang-jingyi commented 2 years ago

@Zengyf-CVer
1、I didn't use any pre-training models. 2、img-size 640*640 batch-size 8 Other parameters I have not set, use the default value.

Zengyf-CVer commented 2 years ago

@liang-jingyi If you simply look at your yaml file, the parameter amount of the model you design should be smaller than that of yolov5s, because the model complexity is much lower than that of yolov5s, which is easy to see.What do you think? @glenn-jocher

glenn-jocher commented 2 years ago

@liang-jingyi I don't have time to examine the yaml file but typically if you are constructing a yaml and the yaml contents don't match the YOLOv4 publication expectations (i.e. parameter count) then I would suspect user error on the yaml author's part.

liang-jingyi commented 2 years ago

@glenn-jocher @Zengyf-CVer Maybe I could answer the question in a different way. Yolov5s has more parameters than yolov4-tiny. Why is the file size of yolov5s.pt smaller than yolov4-tiny.pt? Is the data type causing this file size difference? Like int, float.....

Zengyf-CVer commented 2 years ago

@liang-jingyi You can use some tools or scripts to parse the pt file, and then explore this problem.

github-actions[bot] commented 2 years 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 ⭐!

ys31jp commented 2 years ago

@glenn-jocher @Zengyf-CVer Maybe I could answer the question in a different way. Yolov5s has more parameters than yolov4-tiny. Why is the file size of yolov5s.pt smaller than yolov4-tiny.pt? Is the data type causing this file size difference? Like int, float.....

@Zengyf-CVer Sorry, I misdescribed, the file I built is yaml format, not cfg file 1、I use pytorch.Because yolov5 is easy to use, I added yolov4tiny.yaml directly to yolov5 2、I am comparing Yolov5s to Yolov4Tiny.The weight file trained by Yolov4Tiny should be larger than yolov5s. But the yolov4Tiny weight file I trained is smaller than yolov5s. 3、The contents of yolov4tiny.yaml that I built.

parameters

nc: 80 # number of classes depth_multiple: 1.0 # model depth multiple width_multiple: 1.0 # layer channel multiple

anchors

anchors:

  • [10,14, 23,27, 37,58] # P4/16
  • [81,82, 135,169, 344,319] # P5/32

YOLOv4-tiny backbone

backbone:

[from, number, module, args]

[[-1, 1, BasicConv, [32, 3, 2]], # 0 [-1, 1, BasicConv, [64, 3, 2]], # 1-P1/2

[-1, 1, BasicConv, [64, 3, 1]], [-1, 1, BConv, [64, 64]], # 3-P2/4 [[-1, 2], 1, Concat, [1]],

[-1, 1, nn.MaxPool2d, [2, 2, 0]], # 5-P3/8

[-1, 1, BasicConv, [128, 3, 1]], [-1, 1, BConv, [128, 128]], # 7-P4/16 [[-1, 6], 1, Concat, [1]],

[-1, 1, nn.MaxPool2d, [2, 2, 0]], # 9-P4/16

[-1, 1, BasicConv, [256, 3, 1]], [-1, 1, BConv, [256, 256]], # 11 [[-1, 10], 1, Concat, [1]],

[-1, 1, nn.MaxPool2d, [2, 2, 0]], # 13-P5/32

[-1, 1, BasicConv, [512, 3, 1]], ]

YOLOv4-tiny head

head: [[-1, 1, BasicConv, [256, 1, 1]], #15 [-1, 1, BasicConv, [512, 3, 1]],

[-2, 1, BasicConv, [128, 1, 1]], # 17 (P5/32-large) [-1, 1, nn.Upsample, [None, 2, 'nearest']], [[-1, 11], 1, Concat, [1]], # cat backbone P4 [-1, 1, BasicConv, [256, 3, 1]], # 20 (P4/16-medium)

[[20, 16], 1, Detect, [nc, anchors]], # Detect(P4, P5) ]

common.py

I created two class.

class BasicConv(nn.Module): def init(self, c1, c2, k=1, s=1, p=None, g=1): super(BasicConv, self).init()

    self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False)
    self.bn = nn.BatchNorm2d(c2)
    self.activation = nn.LeakyReLU(0.1)

def forward(self, x):
    x = self.conv(x)
    x = self.bn(x)
    x = self.activation(x)
    return x

class BConv(nn.Module): def init(self, c1, c2, k=1, s=1, p=None, g=1): super(BConv, self).init()

    self.c2 = c2
    self.conv2 = BasicConv(c2 // 2, c2 // 2, 3)
    self.conv3 = BasicConv(c2 // 2, c2 // 2, 3)
    self.conv4 = BasicConv(c2, c2, 1)

def forward(self, x):
    c = self.c2
    x = torch.split(x, c // 2, dim=1)[1]
    x = self.conv2(x)
    route = x
    x = self.conv3(x)
    x = torch.cat([x, route], dim=1)

    x = self.conv4(x)
    return x

Please forgive me for not being able to upload the file. The network speed is too slow.Thank you very much for your reply.

Hi, @liang-jingyi could I request the yolov4-tiny file you build? and how is your prgoress of testing? thank you in advance.

glenn-jocher commented 11 months ago

Hi @ys31jp, it seems the file size difference you're observing could be due to various factors such as data types, compression, or differences in architecture between yolov5s and yolov4-tiny. Have you considered analyzing the weight files to check for any discrepancies? Also, could you provide more details on your training process and any progress with your testing? Thank you!