Safemotion Lib
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | List of all members
fastreid.layers.non_local.Non_local Class Reference
Inheritance diagram for fastreid.layers.non_local.Non_local:

Public Member Functions

 __init__ (self, in_channels, bn_norm, reduc_ratio=2)
 
 forward (self, x)
 

Public Attributes

 in_channels
 
 inter_channels
 
 g
 
 W
 
 theta
 
 phi
 

Detailed Description

Definition at line 9 of file non_local.py.

Constructor & Destructor Documentation

◆ __init__()

fastreid.layers.non_local.Non_local.__init__ ( self,
in_channels,
bn_norm,
reduc_ratio = 2 )

Definition at line 10 of file non_local.py.

10 def __init__(self, in_channels, bn_norm, reduc_ratio=2):
11 super(Non_local, self).__init__()
12
13 self.in_channels = in_channels
14 self.inter_channels = in_channels // reduc_ratio
15
16 self.g = nn.Conv2d(in_channels=self.in_channels, out_channels=self.inter_channels,
17 kernel_size=1, stride=1, padding=0)
18
19 self.W = nn.Sequential(
20 nn.Conv2d(in_channels=self.inter_channels, out_channels=self.in_channels,
21 kernel_size=1, stride=1, padding=0),
22 get_norm(bn_norm, self.in_channels),
23 )
24 nn.init.constant_(self.W[1].weight, 0.0)
25 nn.init.constant_(self.W[1].bias, 0.0)
26
27 self.theta = nn.Conv2d(in_channels=self.in_channels, out_channels=self.inter_channels,
28 kernel_size=1, stride=1, padding=0)
29
30 self.phi = nn.Conv2d(in_channels=self.in_channels, out_channels=self.inter_channels,
31 kernel_size=1, stride=1, padding=0)
32

Member Function Documentation

◆ forward()

fastreid.layers.non_local.Non_local.forward ( self,
x )
        :param x: (b, t, h, w)
        :return x: (b, t, h, w)

Definition at line 33 of file non_local.py.

33 def forward(self, x):
34 """
35 :param x: (b, t, h, w)
36 :return x: (b, t, h, w)
37 """
38 batch_size = x.size(0)
39 g_x = self.g(x).view(batch_size, self.inter_channels, -1)
40 g_x = g_x.permute(0, 2, 1)
41
42 theta_x = self.theta(x).view(batch_size, self.inter_channels, -1)
43 theta_x = theta_x.permute(0, 2, 1)
44 phi_x = self.phi(x).view(batch_size, self.inter_channels, -1)
45 f = torch.matmul(theta_x, phi_x)
46 N = f.size(-1)
47 f_div_C = f / N
48
49 y = torch.matmul(f_div_C, g_x)
50 y = y.permute(0, 2, 1).contiguous()
51 y = y.view(batch_size, self.inter_channels, *x.size()[2:])
52 W_y = self.W(y)
53 z = W_y + x
54 return z

Member Data Documentation

◆ g

fastreid.layers.non_local.Non_local.g

Definition at line 16 of file non_local.py.

◆ in_channels

fastreid.layers.non_local.Non_local.in_channels

Definition at line 13 of file non_local.py.

◆ inter_channels

fastreid.layers.non_local.Non_local.inter_channels

Definition at line 14 of file non_local.py.

◆ phi

fastreid.layers.non_local.Non_local.phi

Definition at line 30 of file non_local.py.

◆ theta

fastreid.layers.non_local.Non_local.theta

Definition at line 27 of file non_local.py.

◆ W

fastreid.layers.non_local.Non_local.W

Definition at line 19 of file non_local.py.


The documentation for this class was generated from the following file: