在线网站建设课程,2023年税收优惠政策,网站建设调研文档,wordpress edu v2.0文章目录 本文介绍代码迁移步骤一#xff1a;迁移代码步骤二#xff1a;创建模块并导入步骤三#xff1a;修改tasks.py文件步骤四#xff1a;修改配置文件 本文介绍
为提升 YOLOv8 多尺度特征融合能力#xff0c;本文借鉴 TPAMI2025 Hyper-YOLO 所提出的尺度融合方式Hype… 文章目录 本文介绍代码迁移步骤一迁移代码步骤二创建模块并导入步骤三修改tasks.py文件步骤四修改配置文件 本文介绍
为提升 YOLOv8 多尺度特征融合能力本文借鉴 TPAMI2025 Hyper-YOLO 所提出的尺度融合方式HyperC2Net改进YOLOv8的Neck部分。 HyperC2Net有助于在语义层和位置上传递高阶消息从而提高Neck提取高阶特征的能力。 HyperC2Net融合五阶段特征图以构建超图结构并将超图结构分别融合进 B 3 B_3 B3、 B 4 B_4 B4和 B 5 B_5 B5最后通过Bottom-Up结构进行高阶信息的传递。 实验结果如下本文通过VOC数据验证算法性能epoch为100batchsize为32imagesize为640*640
ModelmAP50-95mAP50run time (h)params (M)interence time (ms)YOLOv80.5490.7601.0513.010.20.3(postprocess)YOLO110.5530.7571.1422.590.20.3(postprocess)yolov8_HyperC2Net0.5610.7701.2293.290.40.3(postprocess) 重要声明本文改进后代码可能只是并不适用于我所使用的数据集对于其他数据集可能存在有效性。 本文改进是为了降低最新研究进展至YOLO的代码迁移难度从而为对最新研究感兴趣的同学提供参考。 代码迁移 重点内容 步骤一迁移代码
ultralytics框架的模块代码主要放在ultralytics/nn文件夹下此处为了与官方代码进行区分可以新增一个extra_modules文件夹然后将我们的代码添加进入。
具体代码如下
import torch
import torch.nn as nn__all___ [HyperComputeModule]class MessageAgg(nn.Module):def __init__(self, agg_methodmean):super().__init__()self.agg_method agg_methoddef forward(self, X, path):X: [n_node, dim]path: col(source) - row(target)X torch.matmul(path, X)if self.agg_method mean:norm_out 1 / torch.sum(path, dim2, keepdimTrue)norm_out[torch.isinf(norm_out)] 0X norm_out * Xreturn Xelif self.agg_method sum:passreturn Xclass HyPConv(nn.Module):def __init__(self, c1, c2):super().__init__()self.fc nn.Linear(c1, c2)self.v2e MessageAgg(agg_methodmean)self.e2v MessageAgg(agg_methodmean)def forward(self, x, H):x self.fc(x)# v - eE self.v2e(x, H.transpose(1, 2).contiguous())# e - vx self.e2v(E, H)return xclass HyperComputeModule(nn.Module):def __init__(self, c1, c2, threshold):super().__init__()self.threshold thresholdself.hgconv HyPConv(c1, c2)self.bn nn.BatchNorm2d(c2)self.act nn.SiLU()def forward(self, x):b, c, h, w x.shape[0], x.shape[1], x.shape[2], x.shape[3]x x.view(b, c, -1).transpose(1, 2).contiguous()feature x.clone()distance torch.cdist(feature, feature)hg distance self.thresholdhg hg.float().to(x.device).to(x.dtype)x self.hgconv(x, hg).to(x.device).to(x.dtype) xx x.transpose(1, 2).contiguous().view(b, c, h, w)x self.act(self.bn(x))return x
步骤二创建模块并导入
此时需要在当前目录新增一个__init__.py文件将添加的模块导入到__init__.py文件中这样在调用的时候就可以直接使用from extra_modules import *。__init__.py文件需要撰写以下内容
from .hyper_yolo import HyperComputeModule具体目录结构如下图所示
nn/
└── extra_modules/├── __init__.py└── hyper_yolo .py步骤三修改tasks.py文件
首先在tasks.py文件中添加以下内容
from ultralytics.nn.extra_modules import *然后找到parse_model()函数在函数查找如下内容 if m in base_modules:c1, c2 ch[f], args[0]if c2 ! nc: # if c2 not equal to number of classes (i.e. for Classify() output)c2 make_divisible(min(c2, max_channels) * width, 8)使用较老ultralytics版本的同学此处可能不是base_modules而是相关的模块的字典集合此时直接添加到集合即可若不是就找到base_modules所指向的集合进行添加添加方式如下 base_modules frozenset({Classify, Conv, ConvTranspose, GhostConv, Bottleneck, GhostBottleneck,SPP, SPPF, C2fPSA, C2PSA, DWConv, Focus, BottleneckCSP, C1, C2, C2f, C3k2,RepNCSPELAN4, ELAN1, ADown, AConv, SPPELAN, C2fAttn, C3, C3TR, C3Ghost,torch.nn.ConvTranspose2d, DWConvTranspose2d, C3x, RepC3, PSA, SCDown, C2fCIB,A2C2f,# 自定义模块HyperComputeModule,})步骤四修改配置文件
在相应位置添加如下代码即可。
# Parameters
nc: 80 # number of classes
scales: # model compound scaling constants, i.e. modelyolov8n.yaml will call yolov8.yaml with scale n# [depth, width, max_channels]n: [0.33, 0.25, 1024] # YOLOv8n summary: 129 layers, 3157200 parameters, 3157184 gradients, 8.9 GFLOPSs: [0.33, 0.50, 1024] # YOLOv8s summary: 129 layers, 11166560 parameters, 11166544 gradients, 28.8 GFLOPSm: [0.67, 0.75, 768] # YOLOv8m summary: 169 layers, 25902640 parameters, 25902624 gradients, 79.3 GFLOPSl: [1.00, 1.00, 512] # YOLOv8l summary: 209 layers, 43691520 parameters, 43691504 gradients, 165.7 GFLOPSx: [1.00, 1.25, 512] # YOLOv8x summary: 209 layers, 68229648 parameters, 68229632 gradients, 258.5 GFLOPS# YOLOv8.0n backbone
backbone:# [from, repeats, module, args]- [-1, 1, Conv, [64, 3, 2]] # 0-B1/2- [-1, 1, Conv, [128, 3, 2]] # 1- [-1, 3, C2f, [128, True]] # 2-B2/4- [-1, 1, Conv, [256, 3, 2]] # 3- [-1, 6, C2f, [256, True]] # 4-B3/8- [-1, 1, Conv, [512, 3, 2]] # 5- [-1, 6, C2f, [512, True]] # 6-B4/16- [-1, 1, Conv, [1024, 3, 2]] # 7- [-1, 3, C2f, [1024, True]] # 8- [-1, 1, SPPF, [1024, 5]] # 9-B5/32# YOLOv8.0n head
head:# Semantic Collecting- [0, 1, nn.AvgPool2d, [8, 8, 0]] # 10- [2, 1, nn.AvgPool2d, [4, 4, 0]] # 11- [4, 1, nn.AvgPool2d, [2, 2, 0]] # 12- [9, 1, nn.Upsample, [None, 2, nearest]] # 13- [[10, 11, 12, 6, 13], 1, Concat, [1]] # cat 14# Hypergraph Compution- [-1, 1, Conv, [512, 1, 1]] # 15- [-1, 1, HyperComputeModule, [512, 6]] # 16- [-1, 3, C2f, [512, True]] # 17# Semantic Collecting- [-1, 1, nn.AvgPool2d, [2, 2, 0]] # 18- [[-1, 9], 1, Concat, [1]] # cat 19- [-1, 1, Conv, [1024, 1, 1]] # 20 P5- [[17, 6], 1, Concat, [1]] # cat 21- [-1, 3, C2f, [512]] # 22 P4- [17, 1, nn.Upsample, [None, 2, nearest]] # 23- [[-1, 4], 1, Concat, [1]] # cat 24- [-1, 3, C2f, [256]] # 25 P3/N3- [-1, 1, Conv, [256, 3, 2]] # 26- [[-1, 22], 1, Concat, [1]] # 27 cat - [-1, 3, C2f, [512]] # 28 N4- [-1, 1, Conv, [512, 3, 2]] # 29- [[-1, 20], 1, Concat, [1]] # 30 cat- [-1, 3, C2f, [1024]] # 31 N5- [[25, 28, 31], 1, Detect, [nc]] # Detect(N3, N4, N5)