💡💡💡本专栏所有程序均经过测试,可成功执行💡💡💡


本文给大家带来的教程是将YOLO26的主干网络替换为FaNet 来提取特征。文章在介绍主要的原理后,将手把手教学如何进行模块的代码添加和修改,并将修改后的完整代码放在文章的最后,方便大家一键运行,小白也可轻松上手实践。以帮助您更好地学习深度学习目标检测YOLO系列的挑战。 

专栏地址:YOLO26改进-论文涨点——点击跳转看所有内容,关注不迷路!

目录

1.论文

2. FaNet 代码实现

2.1 将FaNet 添加到YOLO26中

2.2 更改init.py文件

2.3 添加yaml文件

2.4 在task.py中进行注册

2.5 执行程序

3. 完整代码分享

4. GFLOPs

5. 进阶

6.总结


1.论文

​​论文地址:FANET: FEATUREAMPLIFICATIONNETWORKFORSEMANTICSEGMENTATIONIN CLUTTEREDBACKGROUND

官方代码:官方代码仓库点击即可跳转

​​​​​​​2. FaNet 代码实现

2.1 将FaNet 添加到YOLO26中

关键步骤一:在ultralytics\ultralytics\nn\modules下面新建文件夹models,在文件夹下新建FaNet .py,粘贴下面代码

import torch
from torch import nn
import torch.nn.functional as F
from timm.models.layers import trunc_normal_
from timm.models.layers import DropPath
from typing import List, Tuple, Dict, Any, Union # Added for type hinting

# --- Configuration for FANet Variants ---
FANET_SPECS: Dict[str, Dict[str, Any]] = {
    'fanet_tiny': {
        'depths': [2, 2, 8, 2],
        'dims': [96, 192, 384, 768], # [96, 192, 384, 768]
        'drop_path_rate': 0.1,
        'expan_ratio': 4,
        'kernel_sizes': [5, 5, 3, 3],
    },
    'fanet_small': { # Example for another variant
        'depths': [3, 3, 12, 3],
        'dims': [96, 192, 384, 768],
        'drop_path_rate': 0.2,
        'expan_ratio': 4,
        'kernel_sizes': [5, 5, 3, 3],
    },
    # Add more variants as needed
}

class LayerNorm(nn.Module):
    def __init__(self, normalized_shape, eps=1e-6, data_format="channels_last"):
        super().__init__()
        self.weight = nn.Parameter(torch.ones(normalized_shape))
        self.bias = nn.Parameter(torch.zeros(normalized_shape))
        self.eps = eps
        self.data_format = data_format
        if self.data_format not in ["channels_last", "channels_first"]:
            raise NotImplementedError
        self.normalized_shape = (normalized_shape,)

    def forward(self, x):
        if self.data_format == "channels_last":
            return F.layer_norm(x, self.normalized_shape, self.weight, self.bias, self.eps)
        elif self.data_format == "channels_first":
            u = x.mean(1, keepdim=True)
            s = (x - u).pow(2).mean(1, keepdim=True)
            x = (x - u) / torch.sqrt(s + self.eps)
            x = self.weight[:, None, None] * x + self.bias[:, None, None]
            return x


class FeatureRefinementModule(nn.Module):
    def __init__(self, in_dim=128, out_dim=128, down_kernel=5, down_stride=4):
        super().__init__()

        self.lconv = nn.Conv2d(in_dim, in_dim, kernel_size=3, stride=1, padding=1, groups=in_dim)
        self.hconv = nn.Conv2d(in_dim, in_dim, kernel_size=3, stride=1, padding=1, groups=in_dim)
        self.norm1 = LayerNorm(in_dim, eps=1e-6, data_format="channels_first")
        self.norm2 = LayerNorm(in_dim, eps=1e-6, data_format="channels_first")
        self.act = nn.GELU()
        self.down = nn.Conv2d(in_dim, in_dim, kernel_size=down_kernel, stride=down_stride, padding=down_kernel//2, groups=in_dim)
        self.proj = nn.Conv2d(in_dim*2, out_dim, kernel_size=1, stride=1, padding=0)

        self.apply(self._init_weights)
    
    def _init_weights(self, m):
        if isinstance(m, (nn.Conv2d, nn.Linear)):
            trunc_normal_(m.weight, std=.02)
            if m.bias is not None:
                nn.init.constant_(m.bias, 0)
        elif isinstance(m, (LayerNorm, nn.LayerNorm)):
            nn.init.constant_(m.bias, 0)
            nn.init.constant_(m.weight, 1.0)

    def forward(self, x):
        B,C,H,W = x.shape

        dx = self.down(x)
        udx = F.interpolate(dx, size=(H,W), mode='bilinear', align_corners=False)
        lx = self.norm1(self.lconv(self.act(x * udx)))
        hx = self.norm2(self.hconv(self.act(x - udx)))

        out = self.act(self.proj(torch.cat([lx, hx], dim=1))) 
        return out


class AFE(nn.Module): # Attentive Feature Enhancement
    def __init__(self, dim, kernel_size=3):
        super().__init__()
        
        self.dwconv = nn.Conv2d(dim, dim, kernel_size=kernel_size, padding=kernel_size//2, groups=dim)
        self.proj1 = nn.Conv2d(dim, dim//2, 1, padding=0)
        self.proj2 = nn.Conv2d(dim, dim, 1, padding=0) # Takes concat of ctx and enh_x, so input is (dim//2 + dim//2) = dim

        self.ctx_conv = nn.Conv2d(dim//2, dim//2, kernel_size=7, padding=3, groups=dim//2 if dim//2 % 4 ==0 and dim//2 > 0 else 1) # Ensure groups <= in_channels

        self.norm1 = LayerNorm(dim, eps=1e-6, data_format="channels_first")
        self.norm2 = LayerNorm(dim//2, eps=1e-6, data_format="channels_first")
        self.norm3 = LayerNorm(dim//2, eps=1e-6, data_format="channels_first")

        self.enhance = FeatureRefinementModule(in_dim=dim//2, out_dim=dim//2, down_kernel=3, down_stride=2)        
        self.act = nn.GELU()

    def forward(self, x):
        B, C, H, W = x.shape
        
        x_res = x
        x = self.act(self.norm1(self.dwconv(x))) # Original: x = x + self.norm1(self.act(self.dwconv(x)))
        x = x + x_res # Apply residual connection after norm and act

        x = self.norm2(self.act(self.proj1(x))) # Now x has dim//2 channels
        
        ctx = self.norm3(self.act(self.ctx_conv(x)))

        enh_x = self.enhance(x) # enhance takes dim//2 and outputs dim//2
        
        # Concatenate ctx (dim//2) and enh_x (dim//2)
        x_cat = torch.cat([ctx, enh_x], dim=1) # Shape: B, dim, H, W
        x = self.act(self.proj2(x_cat)) # proj2 takes dim and outputs dim

        return x


class Block(nn.Module):
    def __init__(self, dim, drop_path=0.1, expan_ratio=4,
                 kernel_size=3, use_dilated_mlp=False): # Added use_dilated_mlp
        super().__init__()
        
        self.layer_norm1 = LayerNorm(dim, eps=1e-6, data_format="channels_first")
        self.layer_norm2 = LayerNorm(dim, eps=1e-6, data_format="channels_first")

        if use_dilated_mlp: # Not used by default in FANET_SPECS for tiny
            self.mlp = AtrousMLP(dim=dim, mlp_ratio=expan_ratio)
        else:
            self.mlp = MLP(dim=dim, mlp_ratio=expan_ratio)
        self.attn = AFE(dim, kernel_size=kernel_size)

        self.drop_path_1 = DropPath(drop_path) if drop_path > 0. else nn.Identity()
        self.drop_path_2 = DropPath(drop_path) if drop_path > 0. else nn.Identity()
        
    def forward(self, x):
        # B, C, H, W = x.shape # Not needed here

        inp_copy = x
        x = self.layer_norm1(inp_copy)
        x = self.drop_path_1(self.attn(x))
        x_attn_out = x + inp_copy # First residual

        x = self.layer_norm2(x_attn_out)
        x = self.drop_path_2(self.mlp(x))
        out = x_attn_out + x # Second residual

        return out


class MLP(nn.Module):
    def __init__(self, dim, mlp_ratio=4): # Removed use_dcn as it's not in original FANet
        super().__init__()
        
        self.fc1 = nn.Conv2d(dim, dim * mlp_ratio, 1)
        self.pos = nn.Conv2d(dim * mlp_ratio, dim * mlp_ratio, 3, padding=1, groups=dim * mlp_ratio)
        self.fc2 = nn.Conv2d(dim * mlp_ratio, dim, 1)
        self.act = nn.GELU()

    def forward(self, x):
        # B, C, H, W = x.shape # Not needed here

        x = self.fc1(x)
        x = self.act(x)
        x = x + self.act(self.pos(x)) # Element-wise sum
        x = self.fc2(x)

        return x


class AtrousMLP(nn.Module):
    def __init__(self, dim, mlp_ratio=4):
        super().__init__()
        
        hidden_dim = dim * mlp_ratio
        self.fc1 = nn.Conv2d(dim, hidden_dim, 1)
        # For AtrousMLP, the concatenated features (x1, x2) should sum up to hidden_dim
        # So, pos1 and pos2 should output hidden_dim / 2 each
        # And their groups should also be hidden_dim / 2
        # Original code has dim*2, which might be a typo if hidden_dim = dim * mlp_ratio (typically mlp_ratio=4)
        # Let's assume the original intent was for pos1 and pos2 to operate on half of the hidden_dim channels each effectively
        # Or, if hidden_dim is the target for concatenation, then pos1 and pos2 output hidden_dim/2
        
        # Correcting AtrousMLP based on typical patterns:
        # fc1 expands to hidden_dim. Then this hidden_dim is split or processed.
        # The original implementation splits hidden_dim into two paths (dim*2 each)
        # which means mlp_ratio must be 4 for this to make sense (hidden_dim = dim*4, each path gets dim*2).

        if hidden_dim % 2 != 0:
            raise ValueError("hidden_dim must be divisible by 2 for AtrousMLP's split")
        
        self.pos1_out_channels = hidden_dim // 2
        self.pos2_out_channels = hidden_dim // 2
        
        self.pos1 = nn.Conv2d(hidden_dim, self.pos1_out_channels, 3, padding=1, groups=self.pos1_out_channels)
        self.pos2 = nn.Conv2d(hidden_dim, self.pos2_out_channels, 3, padding=2, dilation=2, groups=self.pos2_out_channels)
        
        self.fc2 = nn.Conv2d(self.pos1_out_channels + self.pos2_out_channels, dim, 1) # input is concatenation
        self.act = nn.GELU()

    def forward(self, x):
        # B, C, H, W = x.shape # Not needed here
        
        x = self.act(self.fc1(x)) # x is now [B, hidden_dim, H, W]
        
        # The original AtrousMLP implementation implies pos1 and pos2 take the *same* input 'x' (expanded by fc1)
        # and their outputs are concatenated.
        x1 = self.act(self.pos1(x)) # Output: B, hidden_dim/2, H, W
        x2 = self.act(self.pos2(x)) # Output: B, hidden_dim/2, H, W
        
        x_a = torch.cat([x1,x2], dim=1) # Concatenated: B, hidden_dim, H, W
        x = self.fc2(x_a)

        return x


class FANet(nn.Module):
    def __init__(self, 
                 model_name: str,
                 in_chans: int = 3, 
                 input_size: Union[int, Tuple[int, int]] = 224, # H or (H,W)
                 **kwargs): # Allow other kwargs like num_classes if a head is re-added
        super().__init__()

        if model_name not in FANET_SPECS:
            raise ValueError(f"Unknown model_name: {model_name}. Available: {list(FANET_SPECS.keys())}")
        
        spec = FANET_SPECS[model_name]
        depths = spec['depths']
        dims = spec['dims']
        drop_path_rate = spec['drop_path_rate']
        expan_ratio = spec['expan_ratio']
        kernel_sizes = spec['kernel_sizes']
        # use_dilated_mlp is not in tiny spec, default to False for Block
        # if you add it to specs, you can retrieve it:
        # use_dilated_mlp_stages = spec.get('use_dilated_mlp_stages', [False]*4) 

        self.model_name = model_name
        self.in_chans = in_chans
        if isinstance(input_size, int):
            self.input_h_w = (input_size, input_size)
        elif isinstance(input_size, tuple) and len(input_size) == 2:
            self.input_h_w = input_size
        else:
            raise ValueError(f"input_size must be int or tuple of 2 ints, got {input_size}")

        self.downsample_layers = nn.ModuleList()
        stem = nn.Conv2d(in_chans, dims[0], kernel_size=5, stride=4, padding=2) # padding = (kernel_size-1)//2 if stride=1
                                                                               # For stride 4, output H/4. E.g. 224 -> 56
                                                                               # (224 - 5 + 2*2)/4 + 1 = (223)/4 + 1 = 55 + 1 = 56. Padding is correct.
        self.downsample_layers.append(stem)

        for i in range(3): # 3 more downsampling layers
            downsample_layer = nn.Conv2d(dims[i], dims[i+1], kernel_size=3, stride=2, padding=1)
                                                                                # (H - 3 + 2*1)/2 + 1 = (H-1)/2 + 1. Output H/2. Correct.
            self.downsample_layers.append(downsample_layer)

        self.stages = nn.ModuleList()
        dp_rates = [x.item() for x in torch.linspace(0, drop_path_rate, sum(depths))]
        cur = 0
        for i in range(4): # 4 stages
            stage_blocks = []
            for j in range(depths[i]):
                stage_blocks.append(Block(dim=dims[i], drop_path=dp_rates[cur + j], 
                                          expan_ratio=expan_ratio, kernel_size=kernel_sizes[i],
                                          use_dilated_mlp=False) # Defaulting to False, can be made configurable per stage
                                    )
            self.stages.append(nn.Sequential(*stage_blocks))
            cur += depths[i]

        # Classification head (commented out as per original, for backbone usage)
        # self.norm = nn.LayerNorm(dims[-1], eps=1e-6)
        # self.head = nn.Linear(dims[-1], num_classes) # num_classes would need to be an arg

        self.apply(self._init_weights) # Initialize weights

        # Calculate width_list
        self.width_list: List[int] = []
        self.eval() # Set to eval mode for dummy pass; affects dropout, batchnorm
        try:
            dummy_input = torch.randn(1, self.in_chans, *self.input_h_w)
            features = self.forward_features(dummy_input)
            self.width_list = [f.size(1) for f in features]
        except Exception as e:
            print(f"Warning: Error during dummy forward pass for FANet width_list: {e}.")
            # Fallback: use dims directly, assuming forward_features returns one feature per stage
            self.width_list = list(dims) 
            print(f"Falling back to width_list: {self.width_list}")
        self.train() # Set back to train mode

    def _init_weights(self, m):
        if isinstance(m, (nn.Conv2d, nn.Linear)):
            trunc_normal_(m.weight, std=.02)
            if m.bias is not None:
                nn.init.constant_(m.bias, 0)
        elif isinstance(m, (LayerNorm, nn.LayerNorm)): # Handles custom LayerNorm and nn.LayerNorm
            if hasattr(m, 'bias') and m.bias is not None:
                nn.init.constant_(m.bias, 0)
            if hasattr(m, 'weight') and m.weight is not None:
                nn.init.constant_(m.weight, 1.0)
        elif isinstance(m, nn.BatchNorm2d): # Added for completeness if BatchNorm were used elsewhere
            nn.init.constant_(m.weight, 1.0)
            nn.init.constant_(m.bias, 0)


    def init_weights(self, pretrained: str = None):
        if pretrained is not None:
            try:
                checkpoint = torch.load(pretrained, map_location="cpu")
                state_dict_key = "state_dict" if "state_dict" in checkpoint else "model" # Common keys
                if state_dict_key in checkpoint:
                    state_dict = checkpoint[state_dict_key]
                else: # Try if checkpoint is the state_dict itself
                    state_dict = checkpoint 
                
                # Filter out unnecessary keys (e.g. classifier head if not present)
                # and adapt keys if necessary (e.g. remove 'module.' prefix from DataParallel)
                new_state_dict = {}
                for k, v in state_dict.items():
                    name = k[7:] if k.startswith('module.') else k
                    new_state_dict[name] = v
                
                msg = self.load_state_dict(new_state_dict, strict=False)
                print(f"Pretrained weights loaded from {pretrained}. Load message: {msg}")
            except Exception as e:
                print(f"Error loading pretrained weights from {pretrained}: {e}")
        else:
            # Weights are already initialized by self.apply(self._init_weights) in __init__
            print("No pretrained weights provided, using random initialization.")


    def forward_features(self, x: torch.Tensor) -> List[torch.Tensor]:
        feats = []
        x = self.downsample_layers[0](x) # Stem
        x = self.stages[0](x)
        feats.append(x) # Feature from stage 0

        for i in range(1, 4): # For stages 1, 2, 3
            x = self.downsample_layers[i](x) # Downsample
            x = self.stages[i](x)           # Pass through blocks
            feats.append(x)
        return feats # Returns a list of 4 feature maps

    def forward(self, x: torch.Tensor) -> List[torch.Tensor]:
        # The forward_features method already returns a list of tensors.
        # The original code unpacks and repacks, which is fine but redundant.
        # Directly returning the list from forward_features is cleaner.
        return self.forward_features(x)


# --- Factory Functions ---
def fanet_tiny(input_size: Tuple[int, int, int] = (3, 224, 224), pretrained: str = None, **kwargs) -> FANet:
    """
    Args:
        input_size (Tuple[int, int, int]): Input image size (channels, height, width).
        pretrained (str, optional): Path to pre-trained weights.
    """
    in_chans = input_size[0]
    img_h_w = (input_size[1], input_size[2])
    model = FANet(model_name='fanet_tiny', in_chans=in_chans, input_size=img_h_w, **kwargs)
    if pretrained:
        model.init_weights(pretrained)
    return model

def fanet_small(input_size: Tuple[int, int, int] = (3, 224, 224), pretrained: str = None, **kwargs) -> FANet:
    in_chans = input_size[0]
    img_h_w = (input_size[1], input_size[2])
    model = FANet(model_name='fanet_small', in_chans=in_chans, input_size=img_h_w, **kwargs)
    if pretrained:
        model.init_weights(pretrained)
    return model

# --- Example Usage (for testing the module itself) ---
if __name__ == "__main__":
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    print(f"Using device: {device}")

    test_input_size_config = (3, 224, 224) # C, H, W
    dummy_image = torch.randn(2, *test_input_size_config).to(device) # Batch size 2

    print(f"\n--- Testing FANet Tiny ---")
    # Pass input_size tuple directly to the factory function
    model_tiny = fanet_tiny(input_size=test_input_size_config).to(device)
    model_tiny.eval()

    print(f"FANet Tiny width_list (from __init__): {model_tiny.width_list}")

    with torch.no_grad():
        feature_maps = model_tiny(dummy_image) # Calls FANet.forward()

    print(f"FANet Tiny forward() produced {len(feature_maps)} feature maps (output type: {type(feature_maps)}):")
    for i, fm in enumerate(feature_maps):
        print(f"  Feature map {i} shape: {fm.shape}, Channels: {fm.size(1)}")

    assert isinstance(feature_maps, list), "Output is not a list!"
    print("Output is a list: True")

    assert len(model_tiny.width_list) == len(feature_maps), \
        f"Mismatch: width_list len {len(model_tiny.width_list)} vs num feature maps {len(feature_maps)}"
    
    all_channels_match = True
    for i in range(len(feature_maps)):
        if model_tiny.width_list[i] != feature_maps[i].size(1):
            print(f"Mismatch in channel count for feature map {i}: width_list says {model_tiny.width_list[i]}, actual is {feature_maps[i].size(1)}")
            all_channels_match = False
    
    if all_channels_match:
        print("Width_list channels match actual feature map channels: True")
    else:
        print("ERROR: Width_list channels DO NOT match actual feature map channels.")


    print(f"\n--- Testing FANet Small (example with different size) ---")
    test_input_size_large_config = (3, 384, 384)
    dummy_image_large = torch.randn(1, *test_input_size_large_config).to(device)
    model_small = fanet_small(input_size=test_input_size_large_config).to(device)
    model_small.eval()
    print(f"FANet Small width_list (from __init__): {model_small.width_list}")
    with torch.no_grad():
        feature_maps_large = model_small(dummy_image_large)
    print(f"FANet Small (large input) produced {len(feature_maps_large)} feature maps:")
    for i, fm in enumerate(feature_maps_large):
        print(f"  Feature map {i} shape: {fm.shape}")
    assert isinstance(feature_maps_large, list), "Output for large FANet is not a list!"


    # Test AFE groups parameter more robustly
    print("\n--- Testing AFE with various dimensions ---")
    try:
        afe_test1 = AFE(dim=10) # dim//2 = 5, 5%4 != 0
        print("AFE(dim=10) created with groups=1 for ctx_conv.")
        test_tensor_afe1 = torch.randn(1, 10, 32, 32)
        out_afe1 = afe_test1(test_tensor_afe1)
        print(f"AFE(dim=10) output shape: {out_afe1.shape}")

        afe_test2 = AFE(dim=16) # dim//2 = 8, 8%4 == 0
        print("AFE(dim=16) created with groups=dim//2 for ctx_conv.")
        test_tensor_afe2 = torch.randn(1, 16, 32, 32)
        out_afe2 = afe_test2(test_tensor_afe2)
        print(f"AFE(dim=16) output shape: {out_afe2.shape}")

    except Exception as e:
        print(f"Error during AFE test: {e}")


    print("\nAll FANet tests completed.")

2.2 更改init.py文件

关键步骤二:在文件ultralytics\ultralytics\nn\modules\models文件夹下新建__init__.py文件,先导入函数

然后在下面的__all__中声明函数

2.3 添加yaml文件

关键步骤三:在/ultralytics/ultralytics/cfg/models/26下面新建文件yolo26_FaNet .yaml文件,粘贴下面的内容

  • 目标检测
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license

# Ultralytics YOLO26 object detection model with P3/8 - P5/32 outputs
# Model docs: https://docs.ultralytics.com/models/yolo26
# Task docs: https://docs.ultralytics.com/tasks/detect

# Parameters
nc: 80 # number of classes
end2end: True # whether to use end-to-end mode
reg_max: 1 # DFL bins
scales: # model compound scaling constants, i.e. 'model=yolo26n.yaml' will call yolo26.yaml with scale 'n'
  # [depth, width, max_channels]
  n: [0.50, 0.25, 1024] # summary: 260 layers, 2,572,280 parameters, 2,572,280 gradients, 6.1 GFLOPs
  s: [0.50, 0.50, 1024] # summary: 260 layers, 10,009,784 parameters, 10,009,784 gradients, 22.8 GFLOPs
  m: [0.50, 1.00, 512] # summary: 280 layers, 21,896,248 parameters, 21,896,248 gradients, 75.4 GFLOPs
  l: [1.00, 1.00, 512] # summary: 392 layers, 26,299,704 parameters, 26,299,704 gradients, 93.8 GFLOPs
  x: [1.00, 1.50, 512] # summary: 392 layers, 58,993,368 parameters, 58,993,368 gradients, 209.5 GFLOPs

# YOLO26n backbone
backbone:
  # [from, repeats, module, args]
  - [-1, 1, fanet_tiny, []] # 0-4 P1/2
  - [-1, 1, SPPF, [1024, 5]] # 5
  - [-1, 2, C2PSA, [1024]] # 6
 
# YOLO11n head
head:
  - [-1, 1, nn.Upsample, [None, 2, "nearest"]]
  - [[-1, 3], 1, Concat, [1]] # cat backbone P4
  - [-1, 2, C3k2, [512, False]] # 9
 
  - [-1, 1, nn.Upsample, [None, 2, "nearest"]]
  - [[-1, 2], 1, Concat, [1]] # cat backbone P3
  - [-1, 2, C3k2, [256, False]] # 12 (P3/8-small)
 
  - [-1, 1, Conv, [256, 3, 2]]
  - [[-1, 9], 1, Concat, [1]] # cat head P4
  - [-1, 2, C3k2, [512, False]] # 15 (P4/16-medium)
 
  - [-1, 1, Conv, [512, 3, 2]]
  - [[-1, 6], 1, Concat, [1]] # cat head P5
  - [-1, 2, C3k2, [1024, True]] # 18 (P5/32-large)
 
  - [[12, 15, 18], 1, Detect, [nc]] # Detect(P3, P4, P5)
  • 语义分割
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license

# Ultralytics YOLO26 object detection model with P3/8 - P5/32 outputs
# Model docs: https://docs.ultralytics.com/models/yolo26
# Task docs: https://docs.ultralytics.com/tasks/detect

# Parameters
nc: 80 # number of classes
end2end: True # whether to use end-to-end mode
reg_max: 1 # DFL bins
scales: # model compound scaling constants, i.e. 'model=yolo26n.yaml' will call yolo26.yaml with scale 'n'
  # [depth, width, max_channels]
  n: [0.50, 0.25, 1024] # summary: 260 layers, 2,572,280 parameters, 2,572,280 gradients, 6.1 GFLOPs
  s: [0.50, 0.50, 1024] # summary: 260 layers, 10,009,784 parameters, 10,009,784 gradients, 22.8 GFLOPs
  m: [0.50, 1.00, 512] # summary: 280 layers, 21,896,248 parameters, 21,896,248 gradients, 75.4 GFLOPs
  l: [1.00, 1.00, 512] # summary: 392 layers, 26,299,704 parameters, 26,299,704 gradients, 93.8 GFLOPs
  x: [1.00, 1.50, 512] # summary: 392 layers, 58,993,368 parameters, 58,993,368 gradients, 209.5 GFLOPs

# YOLO26n backbone
backbone:
  # [from, repeats, module, args]
  - [-1, 1, fanet_tiny, []] # 0-4 P1/2
  - [-1, 1, SPPF, [1024, 5]] # 5
  - [-1, 2, C2PSA, [1024]] # 6
 
# YOLO11n head
head:
  - [-1, 1, nn.Upsample, [None, 2, "nearest"]]
  - [[-1, 3], 1, Concat, [1]] # cat backbone P4
  - [-1, 2, C3k2, [512, False]] # 9
 
  - [-1, 1, nn.Upsample, [None, 2, "nearest"]]
  - [[-1, 2], 1, Concat, [1]] # cat backbone P3
  - [-1, 2, C3k2, [256, False]] # 12 (P3/8-small)
 
  - [-1, 1, Conv, [256, 3, 2]]
  - [[-1, 9], 1, Concat, [1]] # cat head P4
  - [-1, 2, C3k2, [512, False]] # 15 (P4/16-medium)
 
  - [-1, 1, Conv, [512, 3, 2]]
  - [[-1, 6], 1, Concat, [1]] # cat head P5
  - [-1, 2, C3k2, [1024, True]] # 18 (P5/32-large)
 
  - [[12, 15, 18], 1, Segment, [nc, 32, 256]]
  • 旋转目标检测
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license

# Ultralytics YOLO26 object detection model with P3/8 - P5/32 outputs
# Model docs: https://docs.ultralytics.com/models/yolo26
# Task docs: https://docs.ultralytics.com/tasks/detect

# Parameters
nc: 80 # number of classes
end2end: True # whether to use end-to-end mode
reg_max: 1 # DFL bins
scales: # model compound scaling constants, i.e. 'model=yolo26n.yaml' will call yolo26.yaml with scale 'n'
  # [depth, width, max_channels]
  n: [0.50, 0.25, 1024] # summary: 260 layers, 2,572,280 parameters, 2,572,280 gradients, 6.1 GFLOPs
  s: [0.50, 0.50, 1024] # summary: 260 layers, 10,009,784 parameters, 10,009,784 gradients, 22.8 GFLOPs
  m: [0.50, 1.00, 512] # summary: 280 layers, 21,896,248 parameters, 21,896,248 gradients, 75.4 GFLOPs
  l: [1.00, 1.00, 512] # summary: 392 layers, 26,299,704 parameters, 26,299,704 gradients, 93.8 GFLOPs
  x: [1.00, 1.50, 512] # summary: 392 layers, 58,993,368 parameters, 58,993,368 gradients, 209.5 GFLOPs

# YOLO26n backbone
backbone:
  # [from, repeats, module, args]
  - [-1, 1, fanet_tiny, []] # 0-4 P1/2
  - [-1, 1, SPPF, [1024, 5]] # 5
  - [-1, 2, C2PSA, [1024]] # 6
 
# YOLO11n head
head:
  - [-1, 1, nn.Upsample, [None, 2, "nearest"]]
  - [[-1, 3], 1, Concat, [1]] # cat backbone P4
  - [-1, 2, C3k2, [512, False]] # 9
 
  - [-1, 1, nn.Upsample, [None, 2, "nearest"]]
  - [[-1, 2], 1, Concat, [1]] # cat backbone P3
  - [-1, 2, C3k2, [256, False]] # 12 (P3/8-small)
 
  - [-1, 1, Conv, [256, 3, 2]]
  - [[-1, 9], 1, Concat, [1]] # cat head P4
  - [-1, 2, C3k2, [512, False]] # 15 (P4/16-medium)
 
  - [-1, 1, Conv, [512, 3, 2]]
  - [[-1, 6], 1, Concat, [1]] # cat head P5
  - [-1, 2, C3k2, [1024, True]] # 18 (P5/32-large)
 
  - [[12, 15, 18], 1, OBB, [nc, 1]]

温馨提示:本文只是对yolo26基础上添加模块,如果要对yolo26 n/l/m/x进行添加则只需要指定对应的depth_multiple 和 width_multiple


end2end: True # whether to use end-to-end mode
reg_max: 1 # DFL bins
scales: # model compound scaling constants, i.e. 'model=yolo26n.yaml' will call yolo26.yaml with scale 'n'
  # [depth, width, max_channels]
  n: [0.50, 0.25, 1024] # summary: 260 layers, 2,572,280 parameters, 2,572,280 gradients, 6.1 GFLOPs
  s: [0.50, 0.50, 1024] # summary: 260 layers, 10,009,784 parameters, 10,009,784 gradients, 22.8 GFLOPs
  m: [0.50, 1.00, 512] # summary: 280 layers, 21,896,248 parameters, 21,896,248 gradients, 75.4 GFLOPs
  l: [1.00, 1.00, 512] # summary: 392 layers, 26,299,704 parameters, 26,299,704 gradients, 93.8 GFLOPs
  x: [1.00, 1.50, 512] # summary: 392 layers, 58,993,368 parameters, 58,993,368 gradients, 209.5 GFLOPs

2.4 在task.py中进行注册

关键步骤四:在parse_model函数中进行注册,添加FaNet

先在task.py导入函数

然后在task.py文件下找到parse_model这个函数,如下图,添加FaNet

        elif m in {debi_tiny, debi_small, debi_base, fanet_tiny, fanet_small}:
            m = m(*args)
            c2 = m.width_list 
            backbone = True

2.5 执行程序

关键步骤五: 在ultralytics文件中新建train.py,将model的参数路径设置为yolo26_FaNet .yaml的路径即可 【注意是在外边的Ultralytics下新建train.py】

from ultralytics import YOLO
import warnings
warnings.filterwarnings('ignore')
from pathlib import Path
 
if __name__ == '__main__':
 
 
    # 加载模型
    model = YOLO("ultralytics/cfg/26/yolo26.yaml")  # 你要选择的模型yaml文件地址
    # Use the model
    results = model.train(data=r"你的数据集的yaml文件地址",
                          epochs=100, batch=16, imgsz=640, workers=4, name=Path(model.cfg).stem)  # 训练模型

   🚀运行程序,如果出现下面的内容则说明添加成功🚀  

                   from  n    params  module                                       arguments
  0                  -1  1  28570752  fanet_tiny                                   []
  1                  -1  1    689408  ultralytics.nn.modules.block.SPPF            [768, 256, 5]
  2                  -1  1    249728  ultralytics.nn.modules.block.C2PSA           [256, 256, 1]
  3                  -1  1         0  torch.nn.modules.upsampling.Upsample         [None, 2, 'nearest']
  4             [-1, 3]  1         0  ultralytics.nn.modules.conv.Concat           [1]
  5                  -1  1    144064  ultralytics.nn.modules.block.C3k2            [640, 128, 1, False]
  6                  -1  1         0  torch.nn.modules.upsampling.Upsample         [None, 2, 'nearest']
  7             [-1, 2]  1         0  ultralytics.nn.modules.conv.Concat           [1]
  8                  -1  1     36192  ultralytics.nn.modules.block.C3k2            [320, 64, 1, False]
  9                  -1  1     36992  ultralytics.nn.modules.conv.Conv             [64, 64, 3, 2]
 10             [-1, 9]  1         0  ultralytics.nn.modules.conv.Concat           [1]
 11                  -1  1     86720  ultralytics.nn.modules.block.C3k2            [192, 128, 1, False]
 12                  -1  1    147712  ultralytics.nn.modules.conv.Conv             [128, 128, 3, 2]
 13             [-1, 6]  1         0  ultralytics.nn.modules.conv.Concat           [1]
 14                  -1  1    378880  ultralytics.nn.modules.block.C3k2            [384, 256, 1, True]
 15        [12, 15, 18]  1    309656  ultralytics.nn.modules.head.Detect           [80, 1, True, [64, 128, 256]] 
YOLO26_FaNet summary: 490 layers, 30,650,104 parameters, 30,650,104 gradients, 78.9 GFLOPs

3. 完整代码分享

主页侧边

4. GFLOPs

关于GFLOPs的计算方式可以查看百面算法工程师 | 卷积基础知识——Convolution

未改进的YOLO26n GFLOPs

​改进后的GFLOPs

5. 进阶

可以与其他的注意力机制或者损失函数等结合,进一步提升检测效果

6.总结

通过以上的改进方法,我们成功提升了模型的表现。这只是一个开始,未来还有更多优化和技术深挖的空间。在这里,我想隆重向大家推荐我的专栏——<专栏地址:YOLO26改进-论文涨点——点击跳转看所有内容,关注不迷路!>。这个专栏专注于前沿的深度学习技术,特别是目标检测领域的最新进展,不仅包含对YOLO26的深入解析和改进策略,还会定期更新来自各大顶会(如CVPR、NeurIPS等)的论文复现和实战分享。

为什么订阅我的专栏? ——专栏地址:YOLO26改进-论文涨点——点击跳转看所有内容,关注不迷路!

  1. 前沿技术解读:专栏不仅限于YOLO系列的改进,还会涵盖各类主流与新兴网络的最新研究成果,帮助你紧跟技术潮流。

  2. 详尽的实践分享:所有内容实践性也极强。每次更新都会附带代码和具体的改进步骤,保证每位读者都能迅速上手。

  3. 问题互动与答疑:订阅我的专栏后,你将可以随时向我提问,获取及时的答疑

  4. 实时更新,紧跟行业动态:不定期发布来自全球顶会的最新研究方向和复现实验报告,让你时刻走在技术前沿。

专栏适合人群:

  • 对目标检测、YOLO系列网络有深厚兴趣的同学

  • 希望在用YOLO算法写论文的同学

  • 对YOLO算法感兴趣的同学等

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐