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快乐两倍多。

相关推荐
武子康9 分钟前
从生成一张图到交付一套资产:怎样验收 AI 图片的连续可编辑性
人工智能·llm·agent
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ14 分钟前
Spring‑AI Document 对象 JSON 字段详解
人工智能·spring·json
hyuk的AI工坊21 分钟前
LangChain4j RAG 深度实战:从"能用"到"好用"的 4 个优化策略
人工智能
俊哥V32 分钟前
每日 AI 研究简报 · 2026-08-07
人工智能·ai
调试到凌晨35 分钟前
免费配音工具前置验证价值分析:音色选型、参数调优、克隆测试的零成本方案
人工智能·经验分享·实时音视频
badhope39 分钟前
配了三天Agent开发环境踩了18个坑,我总结了一份零冲突配置指南
人工智能·agent
武子康41 分钟前
Code Mode 什么时候更省:别只数工具调用,要数模型往返
人工智能·llm·agent
HIT_Weston42 分钟前
165、【Agent】【OpenCode】TuiThreadCmd(代理 Fetch 实现)
人工智能·agent·opencode
骄阳如火1 小时前
论文撰写SKILLS实测三|PaperSpine:每个阶段都是带硬关卡的 gate,审计能直接 BLOCK 你
人工智能
badhope1 小时前
用RAG做了个智能客服,上线第一天就被用户骂了——我的7天实战复盘
人工智能·langchain