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

相关推荐
神奇小汤圆2 小时前
从 0 用 AgentTeams 搭一个多 Agent 模拟面试系统
人工智能
潘正翔2 小时前
DeepSeek Harness从0到1部署
人工智能·开发·codex·deepseek·harness·deepseekharness·cludecode
bulingg2 小时前
bert输入长度有限,如何处理超长文本?
人工智能·深度学习·bert
神奇小汤圆2 小时前
DeepSeek Harness 这波,搞得全世界都在安装 Node.js
人工智能
nanawinona3 小时前
2026年手工思路量化后,工具重点会怎样变化
人工智能·python
武子康3 小时前
DeepSeek Harness、Codex、Claude Code、LangGraph 应该怎么选:先判断你缺的是产品、底盘还是工作流
人工智能·llm·agent
美团技术团队3 小时前
美团搜索3.0:LLM 语义表征在排序模型的探索与应用
人工智能
爱学堂IT分享3 小时前
DeepSeek+SpringAI实战AI家庭医生应用-慕课网实战课程
人工智能
科技发布3 小时前
传播易整合商圈媒体,商场停车场灯箱广告高效落地
大数据·人工智能·媒体
今天AI了吗3 小时前
从聊天到委派:AI Agent 如何推进长期任务
数据库·人工智能·python·sql·rust