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

Public Member Functions

 __init__ (self, cfg, in_feat, num_classes)
 
 forward (self, features, targets)
 
 extra_repr (self)
 

Public Attributes

 in_features
 
 s
 
 m
 
 weight
 
 in_feat
 

Protected Attributes

 _num_classes
 

Detailed Description

Implement of large margin cosine distance:
Args:
    in_feat: size of each input sample
    num_classes: size of each output sample

Definition at line 13 of file am_softmax.py.

Constructor & Destructor Documentation

◆ __init__()

fastreid.layers.am_softmax.AMSoftmax.__init__ ( self,
cfg,
in_feat,
num_classes )

Definition at line 20 of file am_softmax.py.

20 def __init__(self, cfg, in_feat, num_classes):
21 super().__init__()
22 self.in_features = in_feat
23 self._num_classes = num_classes
24 self.s = cfg.MODEL.HEADS.SCALE
25 self.m = cfg.MODEL.HEADS.MARGIN
26 self.weight = Parameter(torch.Tensor(num_classes, in_feat))
27 nn.init.xavier_uniform_(self.weight)
28

Member Function Documentation

◆ extra_repr()

fastreid.layers.am_softmax.AMSoftmax.extra_repr ( self)

Definition at line 40 of file am_softmax.py.

40 def extra_repr(self):
41 return 'in_features={}, num_classes={}, scale={}, margin={}'.format(
42 self.in_feat, self._num_classes, self.s, self.m
43 )

◆ forward()

fastreid.layers.am_softmax.AMSoftmax.forward ( self,
features,
targets )

Definition at line 29 of file am_softmax.py.

29 def forward(self, features, targets):
30 # --------------------------- cos(theta) & phi(theta) ---------------------------
31 cosine = F.linear(F.normalize(features), F.normalize(self.weight))
32 phi = cosine - self.m
33 # --------------------------- convert label to one-hot ---------------------------
34 targets = F.one_hot(targets, num_classes=self._num_classes)
35 output = (targets * phi) + ((1.0 - targets) * cosine)
36 output *= self.s
37
38 return output
39

Member Data Documentation

◆ _num_classes

fastreid.layers.am_softmax.AMSoftmax._num_classes
protected

Definition at line 23 of file am_softmax.py.

◆ in_feat

fastreid.layers.am_softmax.AMSoftmax.in_feat

Definition at line 42 of file am_softmax.py.

◆ in_features

fastreid.layers.am_softmax.AMSoftmax.in_features

Definition at line 22 of file am_softmax.py.

◆ m

fastreid.layers.am_softmax.AMSoftmax.m

Definition at line 25 of file am_softmax.py.

◆ s

fastreid.layers.am_softmax.AMSoftmax.s

Definition at line 24 of file am_softmax.py.

◆ weight

fastreid.layers.am_softmax.AMSoftmax.weight

Definition at line 26 of file am_softmax.py.


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