【YOLOv8多模态融合改进】| CVPR 2024 MFM(Modulation Fusion Module,调制融合模块):动态特征加权融合,突出关键特征抑制冗余

一、本文介绍

本文记录的是利用 DCMPNet中的 MFM 模块改进 YOLOv8 的多模态融合部分 。

MFM 模块通过 动态调制特征融合 过程,实现了 对多尺度、跨层级特征的智能聚合 。将其应用于 YOLOv8 的改进过程中,针对目标检测中 边界特征 与 语义信息 的互补性需求, 缓解网络中浅层细节与深层语义融合不足的问题 。



二、MFM介绍

Depth Information Assisted Collaborative Mutual Promotion Network for Single Image Dehazing

2.1 设计出发点

在图像去雾网络中,不同层级和类型的特征包含着互补的信息(如浅层的纹理细节与深层的语义结构)。

传统的特征融合方法(如简单相加或拼接)难以动态适应不同特征的重要性差异,可能导致关键信息被稀释或次要信息过度增强。 MFM模块的核心目标是通过动态调整特征融合权重,增强网络对关键特征的敏感度,提升特征表示能力 ,从而优化去雾结果的细节恢复和结构一致性。

2.2 模块结构

  1. 输入特征
    接收来自不同路径的特征图(如编码器的输出特征与解码器的中间特征),例如:

    • F ^ l e g m 1 \hat{F}_{legm}^{1} F ^ l e g m 1 ​ :来自编码器的局部-全局特征融合结果
    • F r c 1 F_{rc}^{1} F rc 1 ​ :经过3×3卷积处理的浅层特征。
  2. 权重生成组件

    • 全局平均池化(GAP) :对输入特征进行全局上下文感知,压缩空间维度以提取全局统计信息。
    • 多层感知机(MLP) :通过非线性变换生成初步的权重向量。
    • Softmax归一化 :将权重向量归一化为概率分布,得到系数矩阵 A r , c 1 A_{r,c}^{1} A r , c 1 ​ ,表示各通道/空间位置特征在融合中的重要性。
  3. 特征调制与融合

    • 特征加权 :利用系数矩阵 A r , c 1 A_{r,c}^{1} A r , c 1 ​ 对输入特征进行逐元素相乘( ⊙ \odot ⊙ ),突出关键特征并抑制冗余信息:
      F ~ r c 1 = A r , c 1 ⊙ F ^ l e g m 1 + A r , c 1 ⊙ F r c 1 \tilde{F}_{rc}^{1} = A_{r,c}^{1} \odot \hat{F}_{legm}^{1} + A_{r,c}^{1} \odot F_{rc}^{1} F ~ rc 1 ​ = A r , c 1 ​ ⊙ F ^ l e g m 1 ​ + A r , c 1 ​ ⊙ F rc 1 ​
    • 特征拼接与卷积 :将调制后的特征拼接后,通过卷积层进一步融合跨通道信息,输出最终的融合特征。

在这里插入图片描述

2.3 模块特点

  1. 动态特征加权
    通过自适应学习的权重矩阵,MFM模块能够根据输入内容动态调整不同特征的贡献度。例如,在去雾任务中,针对雾霾残留较多的区域,模块会增强对应的深层语义特征;而对于纹理丰富的细节区域,则强化浅层的局部特征。

  2. 跨层级特征交互
    融合编码器的深层语义特征与解码器的浅层细节特征,缓解传统U型网络中浅层特征在跨层传输时的“稀释”问题,提升图像结构的稳定性和细节的清晰度。

论文: https://openaccess.thecvf.com/content/CVPR2024/papers/Zhang_Depth_Information_Assisted_Collaborative_Mutual_Promotion_Network_for_Single_Image_CVPR_2024_paper.pdf
源码: https://github.com/zhoushen1/DCMPNet

三、MFM的实现代码

MFM模块 的实现代码如下:

import torch
import torch.nn as nn

def autopad(k, p=None, d=1):  # kernel, padding, dilation
    """Pad to 'same' shape outputs."""
    if d > 1:
        k = d * (k - 1) + 1 if isinstance(k, int) else [d * (x - 1) + 1 for x in k]  # actual kernel-size
    if p is None:
        p = k // 2 if isinstance(k, int) else [x // 2 for x in k]  # auto-pad
    return p


class Conv(nn.Module):
    """Standard convolution with args(ch_in, ch_out, kernel, stride, padding, groups, dilation, activation)."""

    default_act = nn.SiLU()  # default activation

    def __init__(self, c1, c2, k=1, s=1, p=None, g=1, d=1, act=True):
        """Initialize Conv layer with given arguments including activation."""
        super().__init__()
        self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p, d), groups=g, dilation=d, bias=False)
        self.bn = nn.BatchNorm2d(c2)
        self.act = self.default_act if act is True else act if isinstance(act, nn.Module) else nn.Identity()

    def forward(self, x):
        """Apply convolution, batch normalization and activation to input tensor."""
        return self.act(self.bn(self.conv(x)))

    def forward_fuse(self, x):
        """Perform transposed convolution of 2D data."""
        return self.act(self.conv(x))

class MFM(nn.Module):
    def __init__(self, inc, dim, reduction=8):
        super(MFM, self).__init__()

        self.height = len(inc)
        d = max(int(dim/reduction), 4)

        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.mlp = nn.Sequential(
            nn.Conv2d(dim, d, 1, bias=False),
            nn.ReLU(),
            nn.Conv2d(d, dim * self.height, 1, bias=False)
        )

        self.softmax = nn.Softmax(dim=1)

        self.conv1x1 = nn.ModuleList([])
        for i in inc:
            if i != dim:
                self.conv1x1.append(Conv(i, dim, 1))
            else:
                self.conv1x1.append(nn.Identity())

    def forward(self, in_feats_):
        in_feats = []
        for idx, layer in enumerate(self.conv1x1):
            in_feats.append(layer(in_feats_[idx]))

        B, C, H, W = in_feats[0].shape

        in_feats = torch.cat(in_feats, dim=1)
        in_feats = in_feats.view(B, self.height, C, H, W)

        feats_sum = torch.sum(in_feats, dim=1)
        attn = self.mlp(self.avg_pool(feats_sum))
        attn = self.softmax(attn.view(B, self.height, C, 1, 1))

        out = torch.sum(in_feats*attn, dim=1)
        return out

四、添加步骤

4.1 修改一

① 在 ultralytics/nn/ 目录下新建 AddModules 文件夹用于存放模块代码

② 在 AddModules 文件夹下新建 MFM.py ,将 第三节 中的代码粘贴到此处

在这里插入图片描述

4.2 修改二

在 AddModules 文件夹下新建 __init__.py (已有则不用新建),在文件内导入模块: from .MFM import *

在这里插入图片描述

4.3 修改三

在 ultralytics/nn/modules/tasks.py 文件中,需要在两处位置添加各模块类名称。

首先:导入模块

在这里插入图片描述

然后,在 parse_model函数 中添加如下代码:

        elif m in {MFM}:
            if args[0] == 'head_channel':
                args[0] = d[args[0]]
            c1 = [ch[x] for x in f]
            c2 = make_divisible(min(args[0], max_channels) * width, 8)
            args = [c1, c2, *args[1:]]

在这里插入图片描述


五、yaml模型文件

5.1 中期融合⭐

📌 此模型的修方法是将MFM模块应用到YOLOv8的中期融合中。

# Ultralytics YOLO 🚀, AGPL-3.0 license
# YOLOv8 object detection model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/detect

# Parameters
ch: 6
nc: 1  # number of classes
scales: # model compound scaling constants, i.e. 'model=yolov8n.yaml' will call yolov8.yaml with scale 'n'
  # [depth, width, max_channels]
   n: [0.33, 0.25, 1024]  # YOLOv8n summary: 225 layers,  3157200 parameters,  3157184 gradients,   8.9 GFLOPs
   s: [0.33, 0.50, 1024]  # YOLOv8s summary: 225 layers, 11166560 parameters, 11166544 gradients,  28.8 GFLOPs
   m: [0.67, 0.75, 768]   # YOLOv8m summary: 295 layers, 25902640 parameters, 25902624 gradients,  79.3 GFLOPs
   l: [1.00, 1.00, 512]   # YOLOv8l summary: 365 layers, 43691520 parameters, 43691504 gradients, 165.7 GFLOPs
   x: [1.00, 1.25, 512]   # YOLOv8x summary: 365 layers, 68229648 parameters, 68229632 gradients, 258.5 GFLOPs

# YOLOv8.0n backbone
backbone:
  # [from, repeats, module, args]
  - [-1, 1, IN, []]  # 0
  - [-1, 1, Multiin, [1]]  # 1
  - [-2, 1, Multiin, [2]]  # 2

  - [1, 1, Conv, [64, 3, 2]] # 3-P1/2
  - [-1, 1, Conv, [128, 3, 2]] # 4-P2/4
  - [-1, 3, C2f, [128, True]]
  - [-1, 1, Conv, [256, 3, 2]] # 6-P3/8
  - [-1, 6, C2f, [256, True]]
  - [-1, 1, Conv, [512, 3, 2]] # 8-P4/16
  - [-1, 6, C2f, [512, True]]
  - [-1, 1, Conv, [1024, 3, 2]] # 10-P5/32
  - [-1, 3, C2f, [1024, True]]
  - [-1, 1, SPPF, [1024, 5]] # 12

  - [2, 1, Conv, [64, 3, 2]] # 13-P1/2
  - [-1, 1, Conv, [128, 3, 2]] # 14-P2/4
  - [-1, 3, C2f, [128, True]]
  - [-1, 1, Conv, [256, 3, 2]] # 16-P3/8
  - [-1, 6, C2f, [256, True]]
  - [-1, 1, Conv, [512, 3, 2]] # 18-P4/16
  - [-1, 6, C2f, [512, True]]
  - [-1, 1, Conv, [1024, 3, 2]] # 20-P5/32
  - [-1, 3, C2f, [1024, True]]
  - [-1, 1, SPPF, [1024, 5]] # 22

  - [[7, 17], 1, MFM, [256]]  # 23 cat backbone P3
  - [[9, 19], 1, MFM, [512]]  # 24 cat backbone P4
  - [[12, 22], 1, MFM, [1024]]  # 25 cat backbone P5


 # YOLOv8.0n head
head:
  - [-1, 1, nn.Upsample, [None, 2, 'nearest']]
  - [[-1, 24], 1, Concat, [1]]  # cat backbone P4
  - [-1, 3, C2f, [512]]  # 28

  - [-1, 1, nn.Upsample, [None, 2, 'nearest']]
  - [[-1, 23], 1, Concat, [1]]  # cat backbone P3
  - [-1, 3, C2f, [256]]  # 31 (P3/8-small)

  - [-1, 1, Conv, [256, 3, 2]]
  - [[-1, 28], 1, Concat, [1]]  # cat head P4
  - [-1, 3, C2f, [512]]  # 34 (P4/16-medium)

  - [-1, 1, Conv, [512, 3, 2]]
  - [[-1, 25], 1, Concat, [1]]  # cat head P5
  - [-1, 3, C2f, [1024]]  # 37 (P5/32-large)

  - [[31, 34, 37], 1, Detect, [nc]]  # Detect(P3, P4, P5)



六、成功运行结果

打印网络模型可以看到不同的融合层已经加入到模型中,并可以进行训练了。

YOLOv8-mid-MFM :

YOLOv8-mid-MFM summary: 379 layers, 3,995,315 parameters, 3,995,299 gradients, 10.2 GFLOPs

                   from  n    params  module                                       arguments
  0                  -1  1         0  ultralytics.nn.AddModules.multimodal.IN      []
  1                  -1  1         0  ultralytics.nn.AddModules.multimodal.Multiin [1]
  2                  -2  1         0  ultralytics.nn.AddModules.multimodal.Multiin [2]
  3                   1  1       464  ultralytics.nn.modules.conv.Conv             [3, 16, 3, 2]
  4                  -1  1      4672  ultralytics.nn.modules.conv.Conv             [16, 32, 3, 2]
  5                  -1  1      7360  ultralytics.nn.modules.block.C2f             [32, 32, 1, True]
  6                  -1  1     18560  ultralytics.nn.modules.conv.Conv             [32, 64, 3, 2]
  7                  -1  2     49664  ultralytics.nn.modules.block.C2f             [64, 64, 2, True]
  8                  -1  1     73984  ultralytics.nn.modules.conv.Conv             [64, 128, 3, 2]
  9                  -1  2    197632  ultralytics.nn.modules.block.C2f             [128, 128, 2, True]
 10                  -1  1    295424  ultralytics.nn.modules.conv.Conv             [128, 256, 3, 2]
 11                  -1  1    460288  ultralytics.nn.modules.block.C2f             [256, 256, 1, True]
 12                  -1  1    164608  ultralytics.nn.modules.block.SPPF            [256, 256, 5]
 13                   2  1       464  ultralytics.nn.modules.conv.Conv             [3, 16, 3, 2]
 14                  -1  1      4672  ultralytics.nn.modules.conv.Conv             [16, 32, 3, 2]
 15                  -1  1      7360  ultralytics.nn.modules.block.C2f             [32, 32, 1, True]
 16                  -1  1     18560  ultralytics.nn.modules.conv.Conv             [32, 64, 3, 2]
 17                  -1  2     49664  ultralytics.nn.modules.block.C2f             [64, 64, 2, True]
 18                  -1  1     73984  ultralytics.nn.modules.conv.Conv             [64, 128, 3, 2]
 19                  -1  2    197632  ultralytics.nn.modules.block.C2f             [128, 128, 2, True]
 20                  -1  1    295424  ultralytics.nn.modules.conv.Conv             [128, 256, 3, 2]
 21                  -1  1    460288  ultralytics.nn.modules.block.C2f             [256, 256, 1, True]
 22                  -1  1    164608  ultralytics.nn.modules.block.SPPF            [256, 256, 5]
 23             [7, 17]  1      1536  ultralytics.nn.AddModules.MFM.MFM            [[64, 64], 64]
 24             [9, 19]  1      6144  ultralytics.nn.AddModules.MFM.MFM            [[128, 128], 128]
 25            [12, 22]  1     24576  ultralytics.nn.AddModules.MFM.MFM            [[256, 256], 256]
 26                  -1  1         0  torch.nn.modules.upsampling.Upsample         [None, 2, 'nearest']
 27            [-1, 24]  1         0  ultralytics.nn.modules.conv.Concat           [1]
 28                  -1  1    148224  ultralytics.nn.modules.block.C2f             [384, 128, 1]
 29                  -1  1         0  torch.nn.modules.upsampling.Upsample         [None, 2, 'nearest']
 30            [-1, 23]  1         0  ultralytics.nn.modules.conv.Concat           [1]
 31                  -1  1     37248  ultralytics.nn.modules.block.C2f             [192, 64, 1]
 32                  -1  1     36992  ultralytics.nn.modules.conv.Conv             [64, 64, 3, 2]
 33            [-1, 28]  1         0  ultralytics.nn.modules.conv.Concat           [1]
 34                  -1  1    123648  ultralytics.nn.modules.block.C2f             [192, 128, 1]
 35                  -1  1    147712  ultralytics.nn.modules.conv.Conv             [128, 128, 3, 2]
 36            [-1, 25]  1         0  ultralytics.nn.modules.conv.Concat           [1]
 37                  -1  1    493056  ultralytics.nn.modules.block.C2f             [384, 256, 1]
 38        [31, 34, 37]  1    430867  ultralytics.nn.modules.head.Detect           [1, [64, 128, 256]]
YOLOv8-mid-MFM summary: 379 layers, 3,995,315 parameters, 3,995,299 gradients, 10.2 GFLOPs