前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >YOLO11 全新发布!(原理介绍+代码详见+结构框图)

YOLO11 全新发布!(原理介绍+代码详见+结构框图)

原创
作者头像
AI小怪兽
发布于 2024-10-08 08:00:51
发布于 2024-10-08 08:00:51
11.2K01
代码可运行
举报
文章被收录于专栏:毕业设计毕业设计YOLO大作战
运行总次数:1
代码可运行

1.YOLO11介绍

Ultralytics YOLO11是一款尖端的、最先进的模型,它在之前YOLO版本成功的基础上进行了构建,并引入了新功能和改进,以进一步提升性能和灵活性。YOLO11设计快速、准确且易于使用,使其成为各种物体检测和跟踪、实例分割、图像分类以及姿态估计任务的绝佳选择。

目标检测性能

语义分割性能

Pose关键点检测性能

结构图如下:

1.1 C3k2

C3k2,结构图如下

C3k2,继承自类C2f,其中通过c3k设置False或者Ture来决定选择使用C3k还是Bottleneck

实现代码ultralytics/nn/modules/block.py

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class C3k2(C2f):
    """Faster Implementation of CSP Bottleneck with 2 convolutions."""

    def __init__(self, c1, c2, n=1, c3k=False, e=0.5, g=1, shortcut=True):
        """Initializes the C3k2 module, a faster CSP Bottleneck with 2 convolutions and optional C3k blocks."""
        super().__init__(c1, c2, n, shortcut, g, e)
        self.m = nn.ModuleList(
            C3k(self.c, self.c, 2, shortcut, g) if c3k else Bottleneck(self.c, self.c, shortcut, g) for _ in range(n)
        )


class C3k(C3):
    """C3k is a CSP bottleneck module with customizable kernel sizes for feature extraction in neural networks."""

    def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5, k=3):
        """Initializes the C3k module with specified channels, number of layers, and configurations."""
        super().__init__(c1, c2, n, shortcut, g, e)
        c_ = int(c2 * e)  # hidden channels
        # self.m = nn.Sequential(*(RepBottleneck(c_, c_, shortcut, g, k=(k, k), e=1.0) for _ in range(n)))
        self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, k=(k, k), e=1.0) for _ in range(n)))

1.2 C2PSA介绍

借鉴V10 PSA结构,实现了C2PSA和C2fPSA,最终选择了基于C2的C2PSA(可能涨点更好?)

实现代码ultralytics/nn/modules/block.py

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class PSABlock(nn.Module):
    """
    PSABlock class implementing a Position-Sensitive Attention block for neural networks.

    This class encapsulates the functionality for applying multi-head attention and feed-forward neural network layers
    with optional shortcut connections.

    Attributes:
        attn (Attention): Multi-head attention module.
        ffn (nn.Sequential): Feed-forward neural network module.
        add (bool): Flag indicating whether to add shortcut connections.

    Methods:
        forward: Performs a forward pass through the PSABlock, applying attention and feed-forward layers.

    Examples:
        Create a PSABlock and perform a forward pass
        >>> psablock = PSABlock(c=128, attn_ratio=0.5, num_heads=4, shortcut=True)
        >>> input_tensor = torch.randn(1, 128, 32, 32)
        >>> output_tensor = psablock(input_tensor)
    """

    def __init__(self, c, attn_ratio=0.5, num_heads=4, shortcut=True) -> None:
        """Initializes the PSABlock with attention and feed-forward layers for enhanced feature extraction."""
        super().__init__()

        self.attn = Attention(c, attn_ratio=attn_ratio, num_heads=num_heads)
        self.ffn = nn.Sequential(Conv(c, c * 2, 1), Conv(c * 2, c, 1, act=False))
        self.add = shortcut

    def forward(self, x):
        """Executes a forward pass through PSABlock, applying attention and feed-forward layers to the input tensor."""
        x = x + self.attn(x) if self.add else self.attn(x)
        x = x + self.ffn(x) if self.add else self.ffn(x)
        return x





class C2PSA(nn.Module):
    """
    C2PSA module with attention mechanism for enhanced feature extraction and processing.

    This module implements a convolutional block with attention mechanisms to enhance feature extraction and processing
    capabilities. It includes a series of PSABlock modules for self-attention and feed-forward operations.

    Attributes:
        c (int): Number of hidden channels.
        cv1 (Conv): 1x1 convolution layer to reduce the number of input channels to 2*c.
        cv2 (Conv): 1x1 convolution layer to reduce the number of output channels to c.
        m (nn.Sequential): Sequential container of PSABlock modules for attention and feed-forward operations.

    Methods:
        forward: Performs a forward pass through the C2PSA module, applying attention and feed-forward operations.

    Notes:
        This module essentially is the same as PSA module, but refactored to allow stacking more PSABlock modules.

    Examples:
        >>> c2psa = C2PSA(c1=256, c2=256, n=3, e=0.5)
        >>> input_tensor = torch.randn(1, 256, 64, 64)
        >>> output_tensor = c2psa(input_tensor)
    """

    def __init__(self, c1, c2, n=1, e=0.5):
        """Initializes the C2PSA module with specified input/output channels, number of layers, and expansion ratio."""
        super().__init__()
        assert c1 == c2
        self.c = int(c1 * e)
        self.cv1 = Conv(c1, 2 * self.c, 1, 1)
        self.cv2 = Conv(2 * self.c, c1, 1)

        self.m = nn.Sequential(*(PSABlock(self.c, attn_ratio=0.5, num_heads=self.c // 64) for _ in range(n)))

    def forward(self, x):
        """Processes the input tensor 'x' through a series of PSA blocks and returns the transformed tensor."""
        a, b = self.cv1(x).split((self.c, self.c), dim=1)
        b = self.m(b)
        return self.cv2(torch.cat((a, b), 1))


class C2fPSA(C2f):
    """
    C2fPSA module with enhanced feature extraction using PSA blocks.

    This class extends the C2f module by incorporating PSA blocks for improved attention mechanisms and feature extraction.

    Attributes:
        c (int): Number of hidden channels.
        cv1 (Conv): 1x1 convolution layer to reduce the number of input channels to 2*c.
        cv2 (Conv): 1x1 convolution layer to reduce the number of output channels to c.
        m (nn.ModuleList): List of PSA blocks for feature extraction.

    Methods:
        forward: Performs a forward pass through the C2fPSA module.
        forward_split: Performs a forward pass using split() instead of chunk().

    Examples:
        >>> import torch
        >>> from ultralytics.models.common import C2fPSA
        >>> model = C2fPSA(c1=64, c2=64, n=3, e=0.5)
        >>> x = torch.randn(1, 64, 128, 128)
        >>> output = model(x)
        >>> print(output.shape)
    """

    def __init__(self, c1, c2, n=1, e=0.5):
        """Initializes the C2fPSA module, a variant of C2f with PSA blocks for enhanced feature extraction."""
        assert c1 == c2
        super().__init__(c1, c2, n=n, e=e)
        self.m = nn.ModuleList(PSABlock(self.c, attn_ratio=0.5, num_heads=self.c // 64) for _ in range(n))

1.3 11 Detect介绍

分类检测头引入了DWConv(更加轻量级,为后续二次创新提供了改进点),结构图如下(和V8的区别):

实现代码ultralytics/nn/modules/head.py

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
        self.cv2 = nn.ModuleList(
            nn.Sequential(Conv(x, c2, 3), Conv(c2, c2, 3), nn.Conv2d(c2, 4 * self.reg_max, 1)) for x in ch
        )
        self.cv3 = nn.ModuleList(
            nn.Sequential(
                nn.Sequential(DWConv(x, x, 3), Conv(x, c3, 1)),
                nn.Sequential(DWConv(c3, c3, 3), Conv(c3, c3, 1)),
                nn.Conv2d(c3, self.nc, 1),
            )
            for x in ch
        )

1.4 YOLO11和 YOLOv8的区别

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
-------------------------------   YOLO11   ----------------------------------
# Ultralytics YOLO 🚀, AGPL-3.0 license
# YOLO11 object detection model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/detect

# Parameters
nc: 80 # number of classes
scales: # model compound scaling constants, i.e. 'model=yolo11n.yaml' will call yolo11.yaml with scale 'n'
  # [depth, width, max_channels]
  n: [0.50, 0.25, 1024] # summary: 319 layers, 2624080 parameters, 2624064 gradients, 6.6 GFLOPs
  s: [0.50, 0.50, 1024] # summary: 319 layers, 9458752 parameters, 9458736 gradients, 21.7 GFLOPs
  m: [0.50, 1.00, 512] # summary: 409 layers, 20114688 parameters, 20114672 gradients, 68.5 GFLOPs
  l: [1.00, 1.00, 512] # summary: 631 layers, 25372160 parameters, 25372144 gradients, 87.6 GFLOPs
  x: [1.00, 1.50, 512] # summary: 631 layers, 56966176 parameters, 56966160 gradients, 196.0 GFLOPs

-------------------------------   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
nc: 80 # 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

2.如何训练YOLO11模型

2.1 如何训练NEU-DET数据集

2.1.1 数据集介绍

直接搬运v8的就能使用

​​

2.1.2 超参数修改

位置如下default.yaml

2.2.3 如何训练

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import warnings
warnings.filterwarnings('ignore')
from ultralytics import YOLO

if __name__ == '__main__':
    model = YOLO('ultralytics/cfg/models/11/yolo11-EMA_attention.yaml')
    #model.load('yolov8n.pt') # loading pretrain weights
    model.train(data='data/NEU-DET.yaml',
                cache=False,
                imgsz=640,
                epochs=200,
                batch=8,
                close_mosaic=10,
                device='0',
                optimizer='SGD', # using SGD
                project='runs/train',
                name='exp',
                )

2.2.4训练结果可视化结果

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
YOLO11n summary (fused): 238 layers, 2,583,322 parameters, 0 gradients, 6.3 GFLOPs
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100%|██████████| 21/21 [00:07<00:00,  2.93it/s]
                   all        324        747      0.765      0.679      0.768      0.433
               crazing         47        104      0.678      0.337      0.508       0.22
             inclusion         71        190      0.775      0.705       0.79      0.398
               patches         59        149      0.808      0.859      0.927      0.636
        pitted_surface         61         93       0.81      0.667      0.779      0.483
       rolled-in_scale         56        117      0.684      0.593       0.67      0.317
             scratches         54         94      0.833      0.915      0.934      0.544

原文链接:

https://blog.csdn.net/m0_63774211/article/details/142751248

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
前端谈谈实现五子棋
秉承着会就分享,不会就折腾的技术宗旨。自己利用周末的时间将休闲小游戏-五子棋重新梳理了一下,整理成一个小的教程,分享出来给大家指点指点。
Jimmy_is_jimmy
2019/07/31
1.5K0
实现单机五子棋,难吗?
点击上方蓝色“程序猿DD”,选择“设为星标” 回复“资源”获取独家整理的学习资料! 来源 |  blog.csdn.net/gaosanjin/article/details/108244164 「羊毛+福利」撸一波超便宜的云服务,完成任务DD另外送奖励! 这是实验室2018年底招新时的考核题目,使用Python编写一个能够完成基本对战的五子棋游戏。面向新手。 程序主要包括两个部分,图形创建与逻辑编写两部分。 程序的运行结果: 样式创建 老规矩,先把用到的包导入进来。 ''' @Auther : ga
程序猿DD
2023/04/04
6960
实现单机五子棋,难吗?
JS实现五子棋(一)目标分析
最近很久不写js了,突然决定做一个五子棋的小游戏重温一下js的魅力,做完之后觉得有必要在这里做个记录,分享一下,重点记录一下实现的思路,设计过程。
江湖安得便相忘
2019/08/21
2.9K0
JS实现五子棋(一)目标分析
微信小程序(游戏)----五子棋(棋盘,重置,对弈)
五子棋对弈、悔棋DEMO 效果图 分析 采用微信小程序的canvas制作五子棋; 确定棋盘大小及格数; 绘制棋盘----通过棋盘宽高和格数计算间距,同时保存坐标点; 黑方和白方下子----定义一个布尔
Rattenking
2021/02/01
1.6K0
微信小程序(游戏)----五子棋(棋盘,重置,对弈)
五子棋 - JavaScript 实现 - 两人对战
持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第2天,点击查看活动详情
Jimmy_is_jimmy
2022/11/12
1.1K0
五子棋 - JavaScript 实现 - 两人对战
JS实现五子棋(二)外观分析及绘制
棋盘是N*N正方形,通常是15*15,那么棋盘就是由横向16条,纵向16条的线段组合而成。
江湖安得便相忘
2019/08/21
2.5K0
JS实现五子棋(二)外观分析及绘制
10分钟-带你走进H5-五子棋
需要注意的是,想要修改 canvas标签的宽度和高度,不能直接在css中改,否则是拉伸canvas。只能在标签的属性上通过 width 和 height来改
万少
2025/02/11
1340
10分钟-带你走进H5-五子棋
大一Java课设,五子棋小游戏
该程序是基于Java的GUI图形界面,实现的双人版五子棋小游戏。该程序拥有简洁美观的图形化界面,且界面主要由棋盘、标题和游戏操作的按钮三部分组成。
全栈程序员站长
2022/09/14
2.3K0
五子棋 - JavaScript 实现 -人机交互
上一篇文章 五子棋 - JavaScript 实现 - 两人对战 我们介绍了人与人之间下棋,还挖了个坑:讲人机交互下棋。不知不觉中,把自己打包给卖了,本文就是来补坑的。
Jimmy_is_jimmy
2022/11/28
1.1K0
五子棋 - JavaScript 实现 -人机交互
JS实现五子棋(三)内部数据结构-控制及判定
上回已经完成了棋盘、线框、棋子的绘制,以及如何计算绘制的位置信息。本次内容将分享这个游戏的实质,数据结构,以及各个对象功能,以及一些对象依赖关系处理的思想。
江湖安得便相忘
2019/08/21
2.3K0
JS实现五子棋(三)内部数据结构-控制及判定
AI编程助手写面试题----写个五子棋
2017年,当时大学三本毕业前端工作一年,去深圳找工作面试,在拉勾上海投,接到腾讯前端开发团队回复询问邮箱账号,喜滋滋的以为可以有面试了。一看邮箱,抛给我一道面试题,写完发到对方邮箱。。
一起重学前端
2024/09/20
1420
一分钟自己搭建一个AI五子棋
演示网址: http://timi.iu00.cn/wzq/ 效果图: 代码: 直接随意粘贴到一个.html文件中即可 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>五子棋</title> <style type='text/css'> canvas { display: block;
Blank.
2023/04/27
5591
一分钟自己搭建一个AI五子棋
Python实现五子棋人机对战
五子棋是常见的一款小游戏,五子棋问题是人工智能中的一个经典问题。这篇文章主要介绍了python版本五子棋的实现代码,大家可以做个参考,与我的傻儿子对弈一下。
数据森麟
2019/11/07
3.3K0
五子棋Java课设
第一步:要分俩个类,一个是五子棋本身主类(包括黑白棋下棋方式),一个是棋子类(包括构建画布进行棋盘的设计,使其构成等距离的格子,正方形棋盘15*15格式)。
全栈程序员站长
2022/09/14
7940
五子棋Java课设
五子棋
一个不是很好的五子棋项目,因为以前没写过五子棋,或者说对于没有人机对决的AI五子棋,感觉没什么好写的。当然,我对算法这块也不怎么强,上次有朋友留言要五子棋项目,所以试着去写了下五子棋AI算法,用的是贪心算法,还没写完整,就先发个简单的双人对局五子棋简单版.
DeROy
2020/05/25
1.1K0
五子棋
java 五子棋
image.png ChessBoard.java //package cn.edu.ouc.fiveChess; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RadialGradi
week
2018/08/27
4.5K0
java 五子棋
小游戏——js+h5[canvas]+cs3制作【五子棋】小游戏
五子棋小游戏学习—— 有一个问题是,棋盘线的颜色,在canvas中,明明设置了灰色,但在我的预览中还是黑色的,很重的颜色。 以下是复刻的源码: 1 <!DOCTYPE html> 2 <html> 3 4 <head> 5 <meta charset="UTF-8"> 6 <title>五子棋游戏</title> 7 <meta name="Description" content="git上看到的一个很值得学习练习的简易
xing.org1^
2018/05/17
3.7K0
鸿蒙元服务实战-笑笑五子棋(4)
entry/src/main/ets/entryability/EntryAbility.ets 中统一设置
万少
2025/02/08
1060
鸿蒙元服务实战-笑笑五子棋(4)
开发HarmonyOS NEXT版五子棋游戏实战
大家好,我是 V 哥。首先要公布一个好消息,V 哥原创的《鸿蒙HarmonyOS NEXT 开发之路 卷1:ArkTS 语言篇》图书终于出版了,有正在学习鸿蒙的兄弟可以关注一下,写书真是磨人,耗时半年之久,感概一下,希望可以帮助到正在入门鸿蒙开发的小伙伴,一书在手 ArkTS无优。
威哥爱编程
2025/03/05
1130
简单五子棋,没有电脑AI
  刚学了C#委托,做了个五子棋练习,把前台绘制和后台逻辑分开,前台绘制方法用委托传给后台逻辑。
用户6362579
2019/09/29
5500
简单五子棋,没有电脑AI
相关推荐
前端谈谈实现五子棋
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验