结合NWD的Shape-IoU!助力yolo v5涨点,源码复现

yolo增加Shape-IoU,完美超越SIoU/EIoU/CIoU 作为检测定位分支的重要组成部分,边界框回归损失在目标检测任务中起着重要作用。现有的边界框回归方法通常考虑GT框和预测框之间的几何关系,通过相对位置和框形来计算损失,而忽略边界框的固有属性(如边界框的形状和尺寸)对边界框回归的影响。为了弥补现有研究的不足,本文提出了一种关注边界框自身形状和尺寸的边界框回归方法。首先,分析了边界框的回归特性,并发现边界框自身的形状和尺寸因素将对回归结果产生影响。基于上述结论提出了Shape IoU方法,该方法通过关注边界框自身的形状和尺寸来计算损失,从而使边界框回归更准确。 阅读详情
AI摘要

本文推荐开源工作Shape-IoU和NWD。现有边界框回归损失忽略边界框形状和尺度影响,Shape-IoU关注此因素计算损失,使回归更准确。NWD将框建模成高斯分布,用Wasserstein距离度量相似度,对小目标检测更稳定。将二者加入YOLO5复现,实验证明其优于现有方法。

在这里插入图片描述
标题:Shape-IoU: More Accurate Metric considering Bounding Box Shape and Scale
作者:Hao Zhang, Shuaijie Zhang
原文链接:https://arxiv.org/abs/2312.17663
代码链接:https://github.com/malagoutou/S

shape iou

边界框回归损失在目标检测中非常重要,典型Loss就是IoU、GIoU、CIoU、SIoU。改动方案基本上都是在IoU上添加新的几何约束(GT框和 Anchor 框的距离、形状和角度),但是都忽略了边界框自身的形状和尺寸的影响。今天笔者为大家推荐一篇最新的开源工作Shape-IoU,借助边界框本身的形状和尺度来计算损失,使边界框回归更加准确,可以有效地提高YOLO系列检测器的性能!

1、概述

作为检测器定位分支的重要组成部分,边界框回归损失在目标检测任务中发挥着重要作用。现有的边界框回归方法通常考虑GT框和预测框之间的几何关系,利用边界框的相对位置和形状来计算损失,而忽略了边界框的形状和尺度等固有属性对边界框回归的影响。为了弥补现有研究的不足,本文提出了一种关注包围盒自身形状和尺度的包围盒回归方法。首先,我们对边界框的回归特性进行了分析,发现边界框本身的形状和尺度因素会对回归结果产生影响。基于以上结论,我们提出了Shape IoU方法,该方法可以通过关注边界框本身的形状和尺度来计算损失,从而使得边界框回归更加准确。最后,我们通过大量的对比实验对我们的方法进行了验证,结果表明我们的方法可以有效地提高检测性能,并且优于现有的方法,在不同的检测任务中达到了先进的性能。
在这里插入图片描述

2、原理

在这里插入图片描述

如图所示,边界框回归样本A和B中GT框的尺度相同,而C和D中GT框的尺度相同。A和D中GT盒的形状相同,而B和C中GT盒的形状相同。C和D中包围盒的尺度大于A和B中包围盒的尺度,图a中所有包围盒的回归样本具有相同的偏差,形状偏差为0。图a与图b的不同之处在于,图b中所有包围盒回归样本的形状偏差相同,偏差为0。并观察到如下发现:

图a中A和B的偏差相同,但IoU值存在差异。

图a中C和D的偏差相同,但IoU值存在差异,且相比图a中的A和B,IoU值差异不显著。

图b中A和B的形状偏差相同,但IoU值存在差异。

图b中C和D的形状偏差相同,但IoU值存在差异,与图a中A和B相比,IoU值差异不显著。

图a中A和B的Io U值存在差异的原因是它们的GT盒形状不同,偏离方向分别对应它们的长边和短边方向。对于A,其GT盒沿长边方向的偏差对其影响较小。而对B而言,短边方向的偏差对其Io U值的影响更大。与大尺度包围盒相比,小尺度包围盒对Io U值的变化更敏感,GT盒形状对小尺度包围盒的Io U值影响更显著。由于A和B比C和D的尺度小,在形状和偏差相同的情况下,Io U值的变化更显著。同理,在图b中,从形状偏差的角度分析包围盒回归,发现回归样本中GT盒的形状会影响其在回归过程中的Io U值。
基于以上分析,可以得出以下结论:
(1)假设GT盒不是正方形,有长边和短边,当偏差和形状偏差相同且不全为0时,回归样本中包围盒形状和尺度的不同会导致其Io U值的差异。
(2)对于相同尺度的包围盒回归样本,当回归样本的偏差和形状偏差相同且不全为0时,包围盒的形状会对回归样本的Io U值产生影响。包围盒短边方向的偏差和形状偏差对应的Io U值变化更为显著。
(3)对于具有相同形状包围盒的回归样本,当回归样本偏差和形状偏差相同且不全为0时,相比于规模较大的回归样本,规模较小的包围盒回归样本的Io U值受GT盒形状的影响更为显著。

3、shape iou 源代码

def shape_iou(box1, box2, scale1=0.5, eps=1e-7):
    (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_

    # 计算交集面积
    inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \
            (torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0)
    
    # 计算并集面积
    union = (w1 * h1 + w2 * h2 - inter + eps)  # 添加 eps 防止除零

    # 计算 IoU
    iou = inter / union

    # 计算形状距离
    ww = 2 * torch.pow(w2, scale1) / (torch.pow(w2, scale1) + torch.pow(h2, scale1))
    hh = 2 * torch.pow(h2, scale1) / (torch.pow(w2, scale1) + torch.pow(h2, scale1))

    # 计算凸包的宽度和高度
    cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1)
    ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1)

    c2 = cw ** 2 + ch ** 2 + eps  # 凸包对角线的平方

    # 计算中心距离
    center_distance_x = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2) / 4
    center_distance_y = ((b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4
    center_distance = hh * center_distance_x + ww * center_distance_y
    distance = center_distance / (c2 + eps)  # 归一化距离,添加 eps 防止除零

    # 计算形状相似度
    omiga_w = hh * torch.abs(w1 - w2) / torch.max(w1, w2)
    omiga_h = ww * 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)

    # 最终的 Shape-IoU
    iou = iou - distance - 0.5 * shape_cost
    return iou  # 返回融合了形状和距离的 IoU

4、A Normalized Gaussian Wasserstein Distance for Tiny Object Detection(NWD)提升小目标检测能力

4.1 nwd简介

论文链接:nwd
NWD是一个新的度量方法来计算框和框之间的相似度,就是把框建模成高斯分布,然后用Wasserstein距离来度量这两个分布之间的相
似度,来代替loU。这个距离的好处是,即便是2个框完全不重叠,或者重叠很少,还是可以度量出相似度出来。另外,NWD对于目标的
尺度不敏感,对于小目标的更加的稳定。
相比于loU,NWD有以下好处:
1.尺度不变性。
2. 对于位置的差别变换平缓。
3.具有度量不想交的框的相似度的能力。
在这里插入图片描述
在这里插入图片描述

4.2 nwd代码

根据shape iou论文提到的nwd撰写代码:
在这里插入图片描述

def nwd(box1, box2, scale1=0.5, eps=1e-7):
    (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_

    # 计算形状距离
    ww = 2 * torch.pow(w2, scale1) / (torch.pow(w2, scale1) + torch.pow(h2, scale1))
    hh = 2 * torch.pow(h2, scale1) / (torch.pow(w2, scale1) + torch.pow(h2, scale1))

    # 计算 NWD

    B = (torch.pow(w1 - w2,2)+torch.pow(h1 - h2,2))/4
    center_distance_x1 = (b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2
    center_distance_y1 = (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2
    D =(hh*center_distance_x1+ww*center_distance_y1+B)**(1/2)
    nwd=torch.exp(-D/3)
 
    return nwd  # 返回融合了形状和距离的 IoU

5、shape iou加入yolo5复现

5.1在metrics.py中插入上述shape iou代码

def shape_iou(box1, box2, scale1=0.5, eps=1e-7):
    (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_

    # 计算交集面积
    inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \
            (torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0)
    
    # 计算并集面积
    union = (w1 * h1 + w2 * h2 - inter + eps)  # 添加 eps 防止除零

    # 计算 IoU
    iou = inter / union

    # 计算形状距离
    ww = 2 * torch.pow(w2, scale1) / (torch.pow(w2, scale1) + torch.pow(h2, scale1))
    hh = 2 * torch.pow(h2, scale1) / (torch.pow(w2, scale1) + torch.pow(h2, scale1))

    # 计算凸包的宽度和高度
    cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1)
    ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1)

    c2 = cw ** 2 + ch ** 2 + eps  # 凸包对角线的平方

    # 计算中心距离
    center_distance_x = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2) / 4
    center_distance_y = ((b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4
    center_distance = hh * center_distance_x + ww * center_distance_y
    distance = center_distance / (c2 + eps)  # 归一化距离,添加 eps 防止除零

    # 计算形状相似度
    omiga_w = hh * torch.abs(w1 - w2) / torch.max(w1, w2)
    omiga_h = ww * 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)

    # 最终的 Shape-IoU
    iou = iou - distance - 0.5 * shape_cost
    return iou  # 返回融合了形状和距离的 IoU

5.2在metrics.py中插入上述nwd代码

def nwd(box1, box2, scale1=0.5, eps=1e-7):
    (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_

    # 计算形状距离
    ww = 2 * torch.pow(w2, scale1) / (torch.pow(w2, scale1) + torch.pow(h2, scale1))
    hh = 2 * torch.pow(h2, scale1) / (torch.pow(w2, scale1) + torch.pow(h2, scale1))

    # 计算 NWD

    B = (torch.pow(w1 - w2,2)+torch.pow(h1 - h2,2))/4
    center_distance_x1 = (b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2
    center_distance_y1 = (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2
    D =(hh*center_distance_x1+ww*center_distance_y1+B)**(1/2)
    nwd=torch.exp(-D/3)
    # 最终的 Shape-IoU
    # iou = iou - distance - 0.5 * shape_cost - 0.5 * nwd
    return nwd  # 返回融合了形状和距离的 IoU

5.3在loss.py中插入下面代码

首先在loss.py导入shape_iou和nwd

from utils.metrics import shape_iou,nwd

然后再computeloss函数里面将原来ciou loss改为下面代码(这里比重设置为0.8:0.2)

                iou = shape_iou(pbox, tbox[i])  # 使用 shape_iou 计算 IoU
                nwd1=nwd(pbox, tbox[i])
                lbox += 0.8*(1.0 - iou).mean()+0.2*(1.0-nwd1).mean()   # 使用 shape IoU 损失

6、总结

本文为大家推荐了一篇最新的开源工作Shape - IoU和Nwd。这篇文章指出现有Loss侧重于考虑GT框与预测框之间的几何约束,而忽略了边界框本身的形状、尺度等几何因素对回归结果的影响。然后提出了Shape - IoU方法,可以关注包围盒本身的形状和尺度来计算损失,从而提高精度,而nwd则有利于提升小目标的检测效果,最后使用SOTA单阶段检测器在不同规模的数据集上进行了一系列的对比实验,证明Shape - IoU或者结合Nwd的Shape - IoU优于现有的方法。

7、其他

可以结合一些可落地项目,在项目的基础上进行算法改进完成论文创新撰写;
相应的落地应用链接可点击:https://mbd.pub/o/author-aGqammlpYw==/work

yolov5增加NWD loss提升小目标检测能力 在utils/loss.py中添加。 阅读详情

相关推荐

YOLOV7改进-针对小目标的NWD(损失函数)

7、iou_ration是超参,可以调,如果小目标比较多的话,这个值可以低一些,从而增加nwd损失的比例。3、把这几行复制到utiils的loss.py。4、先对CoputerLoss类做修改。2、utils-loss,这里加。5、把那一行替换成这个。8、还有一个部分要改。9、这个box也要改。

毕竟是Shy哥 3343

Shape-IoU:考虑边框形状与尺度的度量

目标检测是计算机视觉中的基本任务之一,其目标是在图像中定位和识别物体。根据是否生成锚点,目标检测可以分为基于锚点和无锚点的方法。基于锚点的算法包括Faster R-CNN [1]、YOLO系列(You Only Look Once) [2]、SSD(Single Shot MultiBox Detector) [3]和RetinaNet [4]。

m0_47867638的博客 3189

YOLOv5改进实战 | 更换损失函数(四)之NWD(小目标检测)篇

w94ghz的博客 1万+

损失函数篇 | YOLOv8 引入 Shape-IoU 考虑边框形状与尺度的度量

现有的边框回归方法,通常考虑了GT框与预测框之间的几何关系,通过使用边框间的相对位置与相对形状等计算损失,而忽略了边框其自身的形状与尺度等固有属性对边框回归的影响。

Coding... 5481

YOLOv8 损失函数改进 | 引入 Shape-IoU 考虑边框形状与尺度的度量

- YOLOv8轻松上手,适用技术小白,文章代码齐全,仅需一键train,解决YOLOv8的技术突破和创新潜能。

qq_44224801的博客 1604

YOLOv9改进策略【损失函数篇】| Shape-IoU:考虑边界框形状和尺度的更精确度量

本文记录的是改进YOLOv9的损失函数,将其替换成Shape-IoU。现有边界框回归方法通常考虑GT(Ground Truth)框与预测框之间的几何关系,通过边界框的相对位置和形状计算损失,但忽略了边界框本身的形状和尺度等固有属性对边界框回归的影响。为了弥补现有研究的不足,Shape-IoU提出了一种关注边界框本身形状和尺度的边界框回归方法。

Limiiiing的博客 2215

YOLOv8改进:IoU系列篇 | Shape-IoU关注边界框本身的形状和尺度来计算损失 | 2023年12月最新IoU改进

提出了一种新颖的Shape-IoU,小目标检测实现涨点,更加关注边界框本身的形状和尺度来计算损失

会AI的学姐 2264

RT-DETR算法优化改进:提出一种新的Shape IoU,更加关注边界框本身的形状和尺度,对小目标检测也很友好 | 2023.12.29收录

一种新的Shape IoU方法,该方法可以通过关注边界框本身的形状和尺度来计算损失,解决边界盒的形状和规模等固有属性对边界盒回归的影响。

①答疑群聊服务;②YOLO大模型知识问答系统;③计算机视觉论文生成智能体; 2373

芒果YOLOv5改进33:损失函数篇之NWDLoss:首发最新原创:改进用于微小目标检测的Normalized Gaussian Wasserstein Distance助力YOLOv5涨点

YOLOv5代码实践|结合提升小目标检测的一种新的包围框相似度,助力YOLOv5涨点

包括YOLOv5、YOLOv7、YOLOv8等模型改进 5813

YOLOv5独家原创改进:提出一种新的Shape IoU,更加关注边界框本身的形状和尺度,对小目标检测也很友好 | 2023.12.29收录

对小目标检测涨点明显,在VisDrone2019、PASCAL VOC均有涨点

①答疑群聊服务;②YOLO大模型知识问答系统;③计算机视觉论文生成智能体; 2437

GD32F303驱动WS2812B灯带:用TIMER+PWM+DMA解放CPU,实现流畅灯效(附完整代码)

本文详细介绍了基于GD32F303的TIMER+PWM+DMA全自动控制方案,用于高效驱动WS2812B灯带。通过硬件外设协同工作,该方案不仅解放了CPU资源,还能实现复杂的灯光效果,如渐变、流水等。文章提供了完整的代码实现和性能优化技巧,适合嵌入式开发者参考。

weixin_42561143的博客 383

工业质检封神!YOLOv8实战金属表面缺陷检测,自建数据集mAP突破95%

本文详细介绍了基于YOLOv8的金属表面缺陷检测实战方案。针对工业质检中的光照不均、纹理复杂、小样本等挑战,作者构建了12000张图像的数据集,提出针对性数据增强策略和两阶段训练方法。通过优化损失函数、调整超参数等技术手段,最终在测试集上达到95.1%的mAP@0.5,检测速度32FPS。相比传统方法,该方案在精度和鲁棒性上均有显著提升,为工业视觉质检提供了可复现的落地解决方案。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 430

钉钉多维表格开发

从0到1完成钉钉多维表格开发SDK对接与实践案例。

沉修所长,勇逐新机;安而不怠,静而思变 3686

XCP详解「3.1·ASAP2新建A2L文件」

后面是ASAP2新建A2L的操作步骤,如果是APE中新建的A2L就无需以下操作,APE只需要导入现成的A2L即可。退出点击保存时会提示保存A2L文件,是因为前面导出的是1.70版本,工程最低1.71,可以另外起个名字即可。导入elf文件,elf路径建议直接索引编译生成的Debug下的,防止编译后忘记更新。导出V1.70格式A2L文件,APE17支持V1.70。通过ASAP2生成需要A2L文件更常用些。同一个信号可以创建不同属性量。ASAP2中新建A2L文件。设置标定信号数量最大值。XCP详解「总目录」

光明磊学习&积累的地方 3065

【项目实战】DevOps制品管理之JFrog Artifactory介绍

JFrog Artifactory是一款领先的二进制软件制品仓库管理工具,由JFrog公司开发。

奶爸的编程之路,也就一周冷个三天 1360

RK3568适配MIPI屏幕触摸,触摸延时很大

我用的是系统,接了一个,设备树已经正确配置,evtest测试坐标值和触摸位置都是准确的。但是出现了一个:我在屏幕上点一下,要等大约 2 秒才能在evtest里看到坐标事件打印。这种现象在 Goodix 系列触摸驱动里比较少见,一般意味着。

小灰灰的博客 1369

弥散圆以及最大弥散圆

1弥散圆的形成? 原本景物中的一点应该经过透镜本应该也是一点才对。但是因为离焦,球差的存在,最终成像会投影成一个光斑圆圈。越小越清晰,越大就互相干涉变得越模糊。 同时人们规定二零最大弥散圆,就是人眼能够感受清晰模糊的一个边界,直径大于它,感受到模糊;直径小于它,感受不到模糊。 下面分别是离焦(左),球差(右)。 2弥散圆大小怎么计算呢? ,f为焦距;<1> ,D为光圈有效孔径;<2> F=f/D; <3> 3条公式合并可得出: ...

camerall 3120

电化学-论文分享-NanoStat: An open source, fully wireless potentiostat

本文主要分享论文NanoStat: An open source, fully wireless potentiostat,并记录学习笔记。如果有条件,建议看原文最好。

分享技术笔记,希望对大家能够有一定的帮助,同时也通过归纳总结提升自己的个人能力。 2880

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

上一篇: 基于yolov5的危险区域闯入检测系统(UI界面增强版,Python代码,关键点解析)
下一篇: 【YOLO11系列】YOLO11的创新Get!!!
反复入门哥
博客等级 码龄7年 53粉丝 6原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址