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

相关推荐
马***4114 小时前
适配成人英语学习痛点,打造落地性强的学习辅助方式
人工智能·学习
夜焱辰4 小时前
浏览器端 Agent 的文件版本管理:不用 Git,基于 OPFS + SQLite 自己造了一个
前端·人工智能
Ricky05534 小时前
CTRL-WORLD:一种用于机器人操控的可控生成世界模型(中美2025年联合研究)
人工智能·机器人·世界模型
jeffer_liu4 小时前
Spring AI 生产级实战:工具调用
java·人工智能·后端·spring·ai编程
阿乔外贸日记5 小时前
2026尼日利亚五项清关政策更新,拉高能源装备进口综合成本
大数据·人工智能·搜索引擎·智能手机·云计算·能源
民乐团扒谱机5 小时前
【AI笔记】短时纯音时长对音高感知偏移效应研究综述
人工智能·笔记
侃谈科技圈5 小时前
破除数据中台落地困境:2026数据治理平台差异化能力与选型决策指南
大数据·人工智能
大象说5 小时前
Python多进程共享队列无报错僵死 120G Nginx访问日志清洗踩坑全记录
人工智能·自然语言处理
Cosolar5 小时前
AutoGen 精通教程:从零到企业级多 Agent 系统架构师
人工智能·后端·面试
甲维斯6 小时前
Claude Code 省钱小妙招!200K和自动压缩
人工智能