SPP和SPPF的比较

SPP的结构是将输入并行通过多个不同大小的MaxPool层,然后做进一步融合,能在一定程度上解决多尺度问题。

而SPPF结构则是讲输入串行通过多个5*5的MaxPool层,这里需要注意两个5*5的MaxPool层和一个9*9的MaxPool的计算结果是一样的,而串行三个5*5的MaxPool层和一个13*13的MaxPool层计算结果是一样的。

做个实验对比一下:

复制代码
import time
import torch
import torch.nn as nn

class SPP(nn.Module):
    def __init__(self):
        super().__init__()
        self.maxpool1 = nn.MaxPool2d(5, 1, padding=2)
        self.maxpool2 = nn.MaxPool2d(9, 1, padding=4)
        self.maxpool3 = nn.MaxPool2d(13, 1, padding=6)

    def forward(self, x):
        o1 = self.maxpool1(x)
        o2 = self.maxpool2(x)
        o3 = self.maxpool3(x)
        return torch.cat([x, o1, o2, o3], dim=1)
    
class SPPF(nn.Module):
    def __init__(self):
        super().__init__()
        self.maxpool = nn.MaxPool2d(5, 1, padding=2)

    def forward(self, x):
        o1 = self.maxpool(x)
        o2 = self.maxpool(o1)
        o3 = self.maxpool(o2)
        return torch.cat([x, o1, o2, o3], dim=1)
    
def main():
    input_tensor = torch.rand(8, 32, 16, 16)
    spp = SPP()
    sppf = SPPF()
    output1 = spp(input_tensor)
    output2 = sppf(input_tensor)

    print(torch.equal(output1, output2))

    t_start = time.time()
    for _ in range(100):
        spp(input_tensor)
    print(f"spp time : {time.time()- t_start}")

    t_start = time.time()
    for _ in range(100):
        sppf(input_tensor)
    print(f"sppf time : {time.time()- t_start}")

if __name__== '__main__':
    main()

最终输出为:

通过对比发现,两者的计算结果是一模一样的,但是计算时间SPPF比SPP快乐两倍多。

相关推荐
SMT贴片河南芯途电子1 小时前
工业硬件ODM开发怎么做?从需求到量产的完整流程解析!
大数据·人工智能
荆棘鸟智能1 小时前
果蔬AI分选系统架构设计:视觉识别、边缘推理与气阀时序控制
人工智能·系统架构
想七想八不如114081 小时前
元学习简单入门
人工智能·pytorch·python·深度学习·学习·机器学习
198******126341 小时前
2026企业AI办公工具选型指南:企业部署AI前需要考虑哪些问题
大数据·人工智能
Raas1001 小时前
MAI Gateway(魔芋企业级AI网关)选购攻略:AI网关哪个好?避坑指南与对比
大数据·人工智能·ai·gateway·ai网关·企业级ai网关
广东帝工智能安防1 小时前
航道数智化升级实战:基于“云-网-数-智“架构的信息化基础设施建设方案
大数据·运维·人工智能
小鹿软件办公1 小时前
微软公布更多 AI 功能加持的 Windows 11 全新“照片”应用
人工智能·microsoft
匠测AI说1 小时前
AI for Testing 提效实战 · 执行自动化(四):脚本坏了让 AI 自愈——它说修好了,你凭什么信
人工智能·测试
YHL1 小时前
造一个"读过《天龙八部》"的 AI 助手 —— advance-rag
人工智能
光影少年1 小时前
AI Agent、自动化与未来工作
人工智能·算法·架构