-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspp.py
More file actions
25 lines (22 loc) · 916 Bytes
/
Copy pathspp.py
File metadata and controls
25 lines (22 loc) · 916 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import math
import torch
import torch.nn.functional as F
# spp realize
class SPPLayer(torch.nn.Module):
def __init__(self, num_levels):
super(SPPLayer, self).__init__()
self.num_levels = num_levels
def forward(self, x):
num, c, h, w = x.size()
for i in range(self.num_levels):
level = i + 1
kernel_size = (math.ceil(h / level), math.ceil(w / level))
stride = (math.ceil(h / level), math.ceil(w / level))
padding = (math.floor((kernel_size[0] * level - h + 1) / 2),
math.floor((kernel_size[1] * level - w + 1) / 2))
tensor = F.max_pool2d(x, kernel_size=kernel_size, stride=stride, padding=padding).view(num, -1)
if i == 0:
result = tensor.view(num, -1)
else:
result = torch.cat((result, tensor.view(num, -1)), 1)
return result