Swarm-SLAM  1.0.0
C-SLAM Framework
layers.py
Go to the documentation of this file.
1 
2 import torch
3 import torch.nn as nn
4 import torch.nn.functional as F
5 from torch.nn.parameter import Parameter
6 
7 
8 def gem(x, p=3, eps=1e-6):
9  return F.avg_pool2d(x.clamp(min=eps).pow(p), (x.size(-2), x.size(-1))).pow(1./p)
10 
11 
12 class GeM(nn.Module):
13  def __init__(self, p=3, eps=1e-6):
14  super().__init__()
15  self.pp = Parameter(torch.ones(1)*p)
16  self.epseps = eps
17  def forward(self, x):
18  return gem(x, p=self.pp, eps=self.epseps)
19  def __repr__(self):
20  return self.__class__.__name__ + '(' + 'p=' + '{:.4f}'.format(self.pp.data.tolist()[0]) + ', ' + 'eps=' + str(self.epseps) + ')'
21 
22 
23 class Flatten(torch.nn.Module):
24  def __init__(self):
25  super().__init__()
26  def forward(self, x):
27  assert x.shape[2] == x.shape[3] == 1, f"{x.shape[2]} != {x.shape[3]} != 1"
28  return x[:,:,0,0]
29 
30 
31 class L2Norm(nn.Module):
32  def __init__(self, dim=1):
33  super().__init__()
34  self.dimdim = dim
35  def forward(self, x):
36  return F.normalize(x, p=2, dim=self.dimdim)
37 
def forward(self, x)
Definition: layers.py:26
def __init__(self)
Definition: layers.py:24
def __init__(self, p=3, eps=1e-6)
Definition: layers.py:13
def __repr__(self)
Definition: layers.py:19
def forward(self, x)
Definition: layers.py:17
def __init__(self, dim=1)
Definition: layers.py:32
def forward(self, x)
Definition: layers.py:35
def gem(x, p=3, eps=1e-6)
Definition: layers.py:8