Skip to content

Tensor sizes error when testing a modified YOLOv5 nano model (.yaml) #6203

@lessleeb

Description

@lessleeb

Search before asking

Question

Hi, I am trying to modify YOLOv5 nano model. In this case I eliminated the head P5, as shown below in the .yaml file. (it's actually commented).
Additionally, since I am using only 2 anchors now, I commented the corresponding one (P5), and modified the detection part as well. Although, in the training part I do not have any problem, the detection part shows an error regarding the tensor sizes. I have been trying to find what could be the problem, but I've not been able to find it.
This only happens when I remove a layer from the model. If I replace a layer (ex: a Conv for a Focus layer) there is not problem in the detection part.
Could you tell me what could be the problem? Do I need to modify anything else in the code?
Here is the modified yaml file.

YOLOv5n.YAML

Parameters

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

anchors:

  • [10,13, 16,30, 33,23] # P3/8
  • [30,61, 62,45, 59,119] # P4/16
    #- [116,90, 156,198, 373,326] # P5/32

YOLOv5 v6.0 backbone

backbone:

[[-1, 1, Conv, [64, 6, 2, 2]], # 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, 6, 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, 3, C3, [1024]],
[-1, 1, SPPF, [1024, 5]], # 9
]

YOLOv5 v6.0 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)

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

And this is the error:
Exception has occurred: RuntimeError
torch.cat(): Sizes of tensors must match except in dimension 1. Got 26 and 25 in dimension 2 (The offending index is 1)
File "C:\Users\Desktop\yolov5-20211118\models\common.py", line 274, in forward
return torch.cat(x, self.d)
File "C:\Users\Desktop\yolov5-20211118\models\yolo.py", line 150, in _forward_once
x = m(x) # run
File "C:\Users\Desktop\yolov5-20211118\models\yolo.py", line 127, in forward
return self._forward_once(x, profile, visualize) # single-scale inference, train
File "C:\Users\Desktop\yolov5-20211118\models\common.py", line 356, in forward
y = self.model(im) if self.jit else self.model(im, augment=augment, visualize=visualize)
File "C:\Users\Desktop\yolov5-20211118\detect.py", line 115, in run
pred = model(im, augment=augment, visualize=visualize)
File "C:\Users\Desktop\yolov5-20211118\detect.py", line 240, in main
run(**vars(opt))
File "C:\Users\Desktop\yolov5-20211118\detect.py", line 245, in
main(opt)

Additional

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions