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

Public Member Functions

 __init__ (self, num_features, eps=1e-6, is_eps_leanable=False)
 
 reset_parameters (self)
 
 extra_repr (self)
 
 forward (self, x)
 

Public Attributes

 num_features
 
 init_eps
 
 is_eps_leanable
 
 weight
 
 bias
 
 eps
 

Detailed Description

Definition at line 32 of file frn.py.

Constructor & Destructor Documentation

◆ __init__()

fastreid.layers.frn.FRN.__init__ ( self,
num_features,
eps = 1e-6,
is_eps_leanable = False )
weight = gamma, bias = beta
beta, gamma:
    Variables of shape [1, 1, 1, C]. if TensorFlow
    Variables of shape [1, C, 1, 1]. if PyTorch
eps: A scalar constant or learnable variable.

Definition at line 33 of file frn.py.

33 def __init__(self, num_features, eps=1e-6, is_eps_leanable=False):
34 """
35 weight = gamma, bias = beta
36 beta, gamma:
37 Variables of shape [1, 1, 1, C]. if TensorFlow
38 Variables of shape [1, C, 1, 1]. if PyTorch
39 eps: A scalar constant or learnable variable.
40 """
41 super(FRN, self).__init__()
42
43 self.num_features = num_features
44 self.init_eps = eps
45 self.is_eps_leanable = is_eps_leanable
46
47 self.weight = Parameter(torch.Tensor(num_features))
48 self.bias = Parameter(torch.Tensor(num_features))
49 if is_eps_leanable:
50 self.eps = Parameter(torch.Tensor(1))
51 else:
52 self.register_buffer('eps', torch.Tensor([eps]))
53 self.reset_parameters()
54

Member Function Documentation

◆ extra_repr()

fastreid.layers.frn.FRN.extra_repr ( self)

Definition at line 61 of file frn.py.

61 def extra_repr(self):
62 return 'num_features={num_features}, eps={init_eps}'.format(**self.__dict__)
63

◆ forward()

fastreid.layers.frn.FRN.forward ( self,
x )
0, 1, 2, 3 -> (B, H, W, C) in TensorFlow
0, 1, 2, 3 -> (B, C, H, W) in PyTorch
TensorFlow code
    nu2 = tf.reduce_mean(tf.square(x), axis=[1, 2], keepdims=True)
    x = x * tf.rsqrt(nu2 + tf.abs(eps))
    # This Code include TLU function max(y, tau)
    return tf.maximum(gamma * x + beta, tau)

Definition at line 64 of file frn.py.

64 def forward(self, x):
65 """
66 0, 1, 2, 3 -> (B, H, W, C) in TensorFlow
67 0, 1, 2, 3 -> (B, C, H, W) in PyTorch
68 TensorFlow code
69 nu2 = tf.reduce_mean(tf.square(x), axis=[1, 2], keepdims=True)
70 x = x * tf.rsqrt(nu2 + tf.abs(eps))
71 # This Code include TLU function max(y, tau)
72 return tf.maximum(gamma * x + beta, tau)
73 """
74 # Compute the mean norm of activations per channel.
75 nu2 = x.pow(2).mean(dim=[2, 3], keepdim=True)
76
77 # Perform FRN.
78 x = x * torch.rsqrt(nu2 + self.eps.abs())
79
80 # Scale and Bias
81 x = self.weight.view(1, self.num_features, 1, 1) * x + self.bias.view(1, self.num_features, 1, 1)
82 # x = self.weight * x + self.bias
83 return x
84
85

◆ reset_parameters()

fastreid.layers.frn.FRN.reset_parameters ( self)

Definition at line 55 of file frn.py.

55 def reset_parameters(self):
56 nn.init.ones_(self.weight)
57 nn.init.zeros_(self.bias)
58 if self.is_eps_leanable:
59 nn.init.constant_(self.eps, self.init_eps)
60

Member Data Documentation

◆ bias

fastreid.layers.frn.FRN.bias

Definition at line 48 of file frn.py.

◆ eps

fastreid.layers.frn.FRN.eps

Definition at line 50 of file frn.py.

◆ init_eps

fastreid.layers.frn.FRN.init_eps

Definition at line 44 of file frn.py.

◆ is_eps_leanable

fastreid.layers.frn.FRN.is_eps_leanable

Definition at line 45 of file frn.py.

◆ num_features

fastreid.layers.frn.FRN.num_features

Definition at line 43 of file frn.py.

◆ weight

fastreid.layers.frn.FRN.weight

Definition at line 47 of file frn.py.


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