利用pytorch复现spatial pyramid pooling层
sppnet不讲了,懒得写。。。直接上代码
from math import floor, ceil
import torch
import torch.nn as nn
import torch.nn.functional as F class SpatialPyramidPooling2d(nn.Module):
r"""apply spatial pyramid pooling over a 4d input(a mini-batch of 2d inputs
with additional channel dimension) as described in the paper
'Spatial Pyramid Pooling in deep convolutional Networks for visual recognition'
Args:
num_level:
pool_type: max_pool, avg_pool, Default:max_pool
By the way, the target output size is num_grid:
num_grid = 0
for i in range num_level:
num_grid += (i + 1) * (i + 1)
num_grid = num_grid * channels # channels is the channel dimension of input data
examples:
>>> input = torch.randn((1,3,32,32), dtype=torch.float32)
>>> net = torch.nn.Sequential(nn.Conv2d(in_channels=3,out_channels=32,kernel_size=3,stride=1),\
nn.ReLU(),\
SpatialPyramidPooling2d(num_level=2,pool_type='avg_pool'),\
nn.Linear(32 * (1*1 + 2*2), 10))
>>> output = net(input)
""" def __init__(self, num_level, pool_type='max_pool'):
super(SpatialPyramidPooling2d, self).__init__()
self.num_level = num_level
self.pool_type = pool_type def forward(self, x):
N, C, H, W = x.size()
for i in range(self.num_level):
level = i + 1
kernel_size = (ceil(H / level), ceil(W / level))
stride = (ceil(H / level), ceil(W / level))
padding = (floor((kernel_size[0] * level - H + 1) / 2), floor((kernel_size[1] * level - W + 1) / 2)) if self.pool_type == 'max_pool':
tensor = (F.max_pool2d(x, kernel_size=kernel_size, stride=stride, padding=padding)).view(N, -1)
else:
tensor = (F.avg_pool2d(x, kernel_size=kernel_size, stride=stride, padding=padding)).view(N, -1) if i == 0:
res = tensor
else:
res = torch.cat((res, tensor), 1)
return res
def __repr__(self):
return self.__class__.__name__ + '(' \
+ 'num_level = ' + str(self.num_level) \
+ ', pool_type = ' + str(self.pool_type) + ')' class SPPNet(nn.Module):
def __init__(self, num_level=3, pool_type='max_pool'):
super(SPPNet,self).__init__()
self.num_level = num_level
self.pool_type = pool_type
self.feature = nn.Sequential(nn.Conv2d(3,64,3),\
nn.ReLU(),\
nn.MaxPool2d(2),\
nn.Conv2d(64,64,3),\
nn.ReLU())
self.num_grid = self._cal_num_grids(num_level)
self.spp_layer = SpatialPyramidPooling2d(num_level)
self.linear = nn.Sequential(nn.Linear(self.num_grid * 64, 512),\
nn.Linear(512, 10))
def _cal_num_grids(self, level):
count = 0
for i in range(level):
count += (i + 1) * (i + 1)
return count def forward(self, x):
x = self.feature(x)
x = self.spp_layer(x)
print(x.size())
x = self.linear(x)
return x if __name__ == '__main__':
a = torch.rand((1,3,64,64))
net = SPPNet()
output = net(a)
print(output)
利用pytorch复现spatial pyramid pooling层的更多相关文章
- SPP(Spatial Pyramid Pooling)详解
一直对Fast RCNN中ROI Pooling层不解,不同大小的窗口输入怎么样才能得到同样大小的窗口输出呢,今天看到一篇博文讲得挺好的,摘录一下,方便查找. Introduction 在一般的CNN ...
- 空间金字塔池化(Spatial Pyramid Pooling, SPP)原理和代码实现(Pytorch)
想直接看公式的可跳至第三节 3.公式修正 一.为什么需要SPP 首先需要知道为什么会需要SPP. 我们都知道卷积神经网络(CNN)由卷积层和全连接层组成,其中卷积层对于输入数据的大小并没有要求,唯一对 ...
- 空间金字塔池化(Spatial Pyramid Pooling,SPP)
基于空间金字塔池化的卷积神经网络物体检测 原文地址:http://blog.csdn.net/hjimce/article/details/50187655 作者:hjimce 一.相关理论 本篇博文 ...
- Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition
Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition Kaiming He, Xiangyu Zh ...
- 深度学习论文翻译解析(九):Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition
论文标题:Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition 标题翻译:用于视觉识别的深度卷积神 ...
- Spatial pyramid pooling (SPP)-net (空间金字塔池化)笔记(转)
在学习r-cnn系列时,一直看到SPP-net的身影,许多有疑问的地方在这篇论文里找到了答案. 论文:Spatial Pyramid Pooling in Deep Convolutional Net ...
- 目标检测--Spatial pyramid pooling in deep convolutional networks for visual recognition(PAMI, 2015)
Spatial pyramid pooling in deep convolutional networks for visual recognition 作者: Kaiming He, Xiangy ...
- SPPNet论文翻译-空间金字塔池化Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition
http://www.dengfanxin.cn/?p=403 原文地址 我对物体检测的一篇重要著作SPPNet的论文的主要部分进行了翻译工作.SPPNet的初衷非常明晰,就是希望网络对输入的尺寸更加 ...
- SPP Net(Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition)论文理解
论文地址:https://arxiv.org/pdf/1406.4729.pdf 论文翻译请移步:http://www.dengfanxin.cn/?p=403 一.背景: 传统的CNN要求输入图像尺 ...
随机推荐
- JS中对数组元素进行增删改移
在js中对数组元素进行增删改移,简单总结了一下方法: 方法 说明 实例 push( ); 在原来数组中的元素最后面添加元素 arr.push("再见58"); unshift( ) ...
- js如何调用电脑的摄像头
闲来无事,用js写了一个调用摄像头的demo,并用canvas显示保存.这个功能很实用,比如上传用户的头像,即时拍照及时上传. Html: <video width="200px&qu ...
- android 日期 时间
/** * 给定一个日期型字符串,返回加减n天后的日期型字符串 * * @param basicDate * @param nDays * @return */ public static Strin ...
- _T(x) _TEXT(x) L 代表什么?
首先 <tchar.h>中 #ifdef _UNICODE .... #define __T(x) L ## x //替换 #else /* ndef _UNICODE ...
- (五)使用Docker镜像(上)
1. 获取镜像 # 获取镜像 docker pull image:tag // 不使用tag 默认下载latest标签的镜像,即最新的镜像. 2. 查看镜像信息 # 查看镜像信息docker imag ...
- JavaScript异步仿同步(控制流)的实现
在前端开发中尤其是在nodejs开发中经常会遇到这样的场景(以ajax为例):有3个(或者更多个)Ajax请求,并且第2个请求依赖于第1个,第3个请求依赖于第2个,那我们可能就会在发第一个Ajax后回 ...
- OpenCV2:第十一章 图像转换
一.简介 二.例子 #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #inclu ...
- [置顶] IIS应用程序池多工作进程设置及Session共享
[置顶] IIS应用程序池多工作进程设置及Session共享 在调优iis的时候,朋友分享给我一个特别棒的设置方法步骤,感谢好朋友的分享. IIS应用程序池多工作进程设置及Session共享 1 ...
- Hibernate映射文件配置(hbm.xml和注解方式)
一:通过*.hbm.xml配置实体的实现方式 mappingResources用于指定少量的hibernate配置文件像这样 Xml代码 <property name="mappin ...
- 三. python面向对象
第七章.面向对象基础 1.面向对象基础 类和对象: a. 创建类 class 类名: def 方法名(self,xxx): pass b. 创建对象 对象 = 类名() c. 通过对象执行方法 对象. ...