YOLOv5+Swin Transformer

参考:(7条消息) 改进YOLOv5系列:3.YOLOv5结合Swin Transformer结构,ICCV 2021最佳论文 使用 Shifted Windows 的分层视觉转换器_芒果汁没有芒果的博客-CSDN博客

本科生工科生cv改代码

本来做的7,但是7报错一直解决不了,我就试试5

1、先是第一个报错

TypeError: __init__() missing 1 required positional argument: 'c2'

解决:在yolo.py里

if m in {
                Conv, GhostConv, Bottleneck, GhostBottleneck, SPP, SPPF, DWConv, MixConv2d, Focus, CrossConv,
                BottleneckCSP, C3, C3TR, C3SPP, C3Ghost, nn.ConvTranspose2d, DWConvTranspose2d, C3x, C3STR}:
                    # add a C3STR at the end of the sentence(aran)
            c1, c2 = ch[f], args[0]
            if c2 != no:  # if not output
                c2 = make_divisible(c2 * gw, 8) # change 8 to 9

            args = [c1, c2, *args[1:]]
            if m in {BottleneckCSP, C3, C3TR, C3Ghost, C3x, C3STR}:# add a C3STR at the end of the sentence(aran)
                args.insert(2, n)  # number of repeats

2、

File "/root/yolov5_master/models/common.py", line 1315, in __init__
super().__init__(c1, c2, c2, n, shortcut, g, e)
TypeError: __init__() takes from 3 to 7 positional arguments but 8 were given

解决:common里删掉一个c2

class C3STR(C3):
    # C3 module with SwinTransformerBlock()
    def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5):
        super().__init__(c1, c2, n, shortcut, g, e) # a c2 was deleted by aran
        c_ = int(c2 * e)
        num_heads = c_ // 32
        self.m = SwinTransformerBlock(c_, c_, num_heads, n)

3、

NameError: name 'window_partition' is not defined

解决:应该加上window_partition 和 window_reverse, 加在common,具体位置是在芒果哥加在common的代码前面

def window_partition(x, window_size):
    B, H, W, C = x.shape
    x = x.view(B, H // window_size, window_size, W // window_size, window_size, C)
    windows = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, window_size, window_size, C)
    return windows


def window_reverse(windows, window_size, H, W):
    B = int(windows.shape[0] / (H * W / window_size / window_size))
    x = windows.view(B, H // window_size, W // window_size, window_size, window_size, -1)
    x = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(B, H, W, -1)
    return x
    

4、

NameError: name 'F' is not defined

解决:在common里面

import torch.nn.functional as F

你可能感兴趣的:(transformer,计算机视觉,人工智能)