添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

最近使用魔导的思路对YOLOv8的损失函数进行更改:

原文链接如下: YOLOV8改进-添加EIoU,SIoU,AlphaIoU,FocalEIoU,Wise-IoU_魔鬼面具的博客-CSDN博客

按照这个思路改进之后会出现bug:

会出现 AttributeError: 'tuple' object has no attribute 'squeeze'的报错

改进方法如下:

在ultralytics/util/metrics/bbox_iou文件中,魔导改进原文如下:

def bbox_iou(box1, box2, xywh=True, GIoU=False, DIoU=False, CIoU=False, SIoU=False, EIoU=False, Focal=False, alpha=1, gamma=0.5, eps=1e-7):
    # Returns Intersection over Union (IoU) of box1(1,4) to box2(n,4)
    # Get the coordinates of bounding boxes
    if xywh:  # transform from xywh to xyxy
        (x1, y1, w1, h1), (x2, y2, w2, h2) = box1.chunk(4, -1), box2.chunk(4, -1)
        w1_, h1_, w2_, h2_ = w1 / 2, h1 / 2, w2 / 2, h2 / 2
        b1_x1, b1_x2, b1_y1, b1_y2 = x1 - w1_, x1 + w1_, y1 - h1_, y1 + h1_
        b2_x1, b2_x2, b2_y1, b2_y2 = x2 - w2_, x2 + w2_, y2 - h2_, y2 + h2_
    else:  # x1, y1, x2, y2 = box1
        b1_x1, b1_y1, b1_x2, b1_y2 = box1.chunk(4, -1)
        b2_x1, b2_y1, b2_x2, b2_y2 = box2.chunk(4, -1)
        w1, h1 = b1_x2 - b1_x1, (b1_y2 - b1_y1).clamp(eps)
        w2, h2 = b2_x2 - b2_x1, (b2_y2 - b2_y1).clamp(eps)
    # Intersection area
    inter = (b1_x2.minimum(b2_x2) - b1_x1.maximum(b2_x1)).clamp(0) * \
            (b1_y2.minimum(b2_y2) - b1_y1.maximum(b2_y1)).clamp(0)
    # Union Area
    union = w1 * h1 + w2 * h2 - inter + eps
    # IoU
    # iou = inter / union # ori iou
    iou = torch.pow(inter/(union + eps), alpha) # alpha iou
    if CIoU or DIoU or GIoU or EIoU or SIoU:
        cw = b1_x2.maximum(b2_x2) - b1_x1.minimum(b2_x1)  # convex (smallest enclosing box) width
        ch = b1_y2.maximum(b2_y2) - b1_y1.minimum(b2_y1)  # convex height
        if CIoU or DIoU or EIoU or SIoU:  # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
            c2 = (cw ** 2 + ch ** 2) ** alpha + eps  # convex diagonal squared
            rho2 = (((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4) ** alpha  # center dist ** 2
            if CIoU:  # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
                v = (4 / math.pi ** 2) * (torch.atan(w2 / h2) - torch.atan(w1 / h1)).pow(2)
                with torch.no_grad():
                    alpha_ciou = v / (v - iou + (1 + eps))
                if Focal:
                    return iou - (rho2 / c2 + torch.pow(v * alpha_ciou + eps, alpha)), torch.pow(inter/(union + eps), gamma)  # Focal_CIoU
                else:
                    return iou - (rho2 / c2 + torch.pow(v * alpha_ciou + eps, alpha))  # CIoU
            elif EIoU:
                rho_w2 = ((b2_x2 - b2_x1) - (b1_x2 - b1_x1)) ** 2
                rho_h2 = ((b2_y2 - b2_y1) - (b1_y2 - b1_y1)) ** 2
                cw2 = torch.pow(cw ** 2 + eps, alpha)
                ch2 = torch.pow(ch ** 2 + eps, alpha)
                if Focal:
                    return iou - (rho2 / c2 + rho_w2 / cw2 + rho_h2 / ch2), torch.pow(inter/(union + eps), gamma) # Focal_EIou
                else:
                    return iou - (rho2 / c2 + rho_w2 / cw2 + rho_h2 / ch2) # EIou
            elif SIoU:
                # SIoU Loss https://arxiv.org/pdf/2205.12740.pdf
                s_cw = (b2_x1 + b2_x2 - b1_x1 - b1_x2) * 0.5 + eps
                s_ch = (b2_y1 + b2_y2 - b1_y1 - b1_y2) * 0.5 + eps
                sigma = torch.pow(s_cw ** 2 + s_ch ** 2, 0.5)
                sin_alpha_1 = torch.abs(s_cw) / sigma
                sin_alpha_2 = torch.abs(s_ch) / sigma
                threshold = pow(2, 0.5) / 2
                sin_alpha = torch.where(sin_alpha_1 > threshold, sin_alpha_2, sin_alpha_1)
                angle_cost = torch.cos(torch.arcsin(sin_alpha) * 2 - math.pi / 2)
                rho_x = (s_cw / cw) ** 2
                rho_y = (s_ch / ch) ** 2
                gamma = angle_cost - 2
                distance_cost = 2 - torch.exp(gamma * rho_x) - torch.exp(gamma * rho_y)
                omiga_w = torch.abs(w1 - w2) / torch.max(w1, w2)
                omiga_h = torch.abs(h1 - h2) / torch.max(h1, h2)
                shape_cost = torch.pow(1 - torch.exp(-1 * omiga_w), 4) + torch.pow(1 - torch.exp(-1 * omiga_h), 4)
                if Focal:
                    return iou - torch.pow(0.5 * (distance_cost + shape_cost) + eps, alpha), torch.pow(inter/(union + eps), gamma) # Focal_SIou
                else:
                    return iou - torch.pow(0.5 * (distance_cost + shape_cost) + eps, alpha) # SIou
            if Focal:
                return iou - rho2 / c2, torch.pow(inter/(union + eps), gamma)  # Focal_DIoU
            else:
                return iou - rho2 / c2  # DIoU
        c_area = cw * ch + eps  # convex area
        if Focal:
            return iou - torch.pow((c_area - union) / c_area + eps, alpha), torch.pow(inter/(union + eps), gamma)  # Focal_GIoU https://arxiv.org/pdf/1902.09630.pdf
        else:
            return iou - torch.pow((c_area - union) / c_area + eps, alpha)  # GIoU https://arxiv.org/pdf/1902.09630.pdf
    if Focal:
        return iou, torch.pow(inter/(union + eps), gamma)  # Focal_IoU
    else:
        return iou  # IoU

解决方法:

对每一个if后面的focal进行修改:

以CIOU为例:

if Focal:
                    return iou - (rho2 / c2 + torch.pow(v * alpha_ciou + eps, alpha)), torch.pow(inter/(union + eps), gamma)  # Focal_CIoU

if Focal:
       return iou - ((rho2 / c2 + torch.pow(v * alpha_ciou + eps, alpha)), torch.pow(inter/(union + eps), gamma))[0]       # Focal_CIoU即可成功运行

同理要实现 FocalEIOU、SIOU或者其他IOU都需要进行更改

【代码】AttributeError: ‘tuple‘ object has no attribute ‘squeeze‘根据魔导YOLov8改进Focal IOU时出现问题解决。 target = target.cuda() 出现错误AttributeError: 'tuple' object has no attribute 'cuda' tuple转成tensor target是tuple类型,但.conda()需要是tensor类型 tuple——np.array——tensor(中间需要np.array中转;且np.array的元素需要是int或float(原本是str),使用.astype(in 这往往发生在我们对一个tuple类型数据,调用成员变量shape所致(a.shape 或 a.shape[])。 所以要查看调用发生处,看看自己的数据类型是不是有错。我们看代码 import numpy as np a = np.zeros([5,5]) #正确使用方式: print(a) print(type(a)) print(ty...
今天来给大家介绍几种在Python编程中,所常见的几种错误类型。1.在用加号进行拼接,必须用字符串。name='小明' age=18 print('我的名字是'+name+',我今年'+age+'岁了')点击运行输出会报错,错误提示为 :TypeError: must be str, not int,翻译为类型错误,必须为字符串str,不能是数字int。解决方案为:name='小明' age=...
Pycharm关于AttributeError: ‘DataFrame’ object has no attribute ‘score’的错误 import pandas data = pandas.read_excel( r"C:\Users\ASUS\Desktop\0012\data7.1.2.xlsx", data.score.describe() # 逐项分析各统计量 data.score.size data.score.max() data.score.min() data.score.sum() data.score.mea
numpy.array可使用 shape。list不能使用shape。 可以使用np.array(list A)进行转换。 (array转list:array B B.tolist()即可) 补充知识:Pandas使用DataFrame出现错误:AttributeError: ‘list’ object has no attribute ‘astype’ 在使用Pandas的DataFrame出现了错误:AttributeError: ‘list’ object has no attribute ‘astype’ 代码入下: import pandas as pd pop = {'Neva
import pymysql #创建连接 con = pymysql.connect(host='localhost',user='root',password='123456',port=3306,database='zhy') #创建游标对象 cur = con.curson() #编写查询的sql语句 sql = 'select * from t_student' cur.execute(sql) print(查询成功) students = cur.fetchall() print(students) except Exception as
多线程爬虫出现报错AttributeError: ‘NoneType’ object has no attribute ‘xpath’一、前言二、问题三、思考和解决问题四、运行效果 mark一下,本技术小白的第一篇CSDN博客! 最近在捣鼓爬虫,看的是机械工业出版社的《从零开始学Python网络爬虫》。这书吧,一言难尽,优点是案例比较多,说的也还算清楚,但是槽点更多:1、较多低级笔误;2、基础知识一笔带过,简单得不能再简单,对Python基础不好的人不友好;3、代码分析部分,相同的代码反复啰嗦解释多次,而一些该解释的新代码却只字不提;4、这是最重要的一点,但也不全是本书的锅。就是书中
Traceback (most recent call last): File "D:\anaconda\lib\site-packages\django\core\handlers\exception.py", line 34, in inner response = get_response(request) File "D:\anaconda\lib\site-packages\django\core\handlers\base.py", line 115, in _ge
RuntimeError: Given groups=1, weight of size [6, 3, 3, 3], expected input[64, 14, 22, 3] to have 3 channels, but got 14 channels instead 卷积权重为输入3通道, 输出6通道, 卷积核大小为3*3 但是认为输入数据为14通道,是不是应该把数据通道值放到shape[1] 现在输入张量维度为torch.Size([64, 3, 22, 14]),依旧报错 RuntimeError
首先要在这补充上一讲没有说的几个地方。有一点python和matlab一样,不需要像c语言预先给变量定义类型才能赋值。python和matlab一样按Ctrl+C停止程序执行。>>> a=[1,2,3] >>> len(a) >>> a.index(2) >>> del a[:] >>> a
这个错误通常发生在你试图将一个元组进行修改的候,因为元组是不可变的,所以没有 append 方法。 你可以考虑使用列表(list)代替元组(tuple)来进行修改。如果你需要在一个元组中添加元素,你可以创建一个新的元组,包含旧的元组中的元素和新添加的元素。例如: ```python old_tuple = (1, 2, 3) new_tuple = old_tuple + (4,) 这将创建一个新的元组 `(1, 2, 3, 4)`,而不是修改原始元组。
AttributeError: ‘tuple‘ object has no attribute ‘squeeze‘根据魔导YOLov8改进Focal IOU时出现问题解决 逢考必不过就怪: AttributeError: ‘tuple‘ object has no attribute ‘squeeze‘根据魔导YOLov8改进Focal IOU时出现问题解决 Anaconda_: 这样不就等于把focal关了么 oracle数据库在idea中配置数据库连接 柯镇恶~: 感谢笔者,让我找到这个小鸟插件