Scheaven
2021-09-18 291deeb1fcf45dbf39a24aa72a213ff3fd6b3405
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
B
_zã@sTdZddlZddlZddlmZddlmmZddlmZGdd„dej    ƒZ
dS)z9
@author:  liaoxingyu
@contact: sherlockliao01@gmail.com
éN)Ú    Parametercs,eZdZ‡fdd„Zdd„Zdd„Z‡ZS)ÚArcfacecs tƒ ¡||_||_|jjj|_|jjj|_    t
  |j    ¡|_ t
  |j    ¡|_t
  t
j|j    ¡|_t
  t
j|j    ¡|j    |_tt ||¡ƒ|_| dt d¡¡dS)NÚté)ÚsuperÚ__init__Úin_featÚ _num_classesÚMODELZHEADSÚSCALEÚ_sÚMARGINÚ_mÚmathÚcosÚcos_mÚsinÚsin_mÚpiÚ    thresholdÚmmrÚtorchÚTensorÚweightÚregister_bufferÚzeros)ÚselfÚcfgrÚ num_classes)Ú    __class__©ú./layers/arcface.pyrs
  zArcface.__init__c     Cst t |¡t |j¡¡}| dd¡}|t d| d¡¡|f dd¡}t     dt 
|d¡¡}||j ||j }||k}t  ||jk|||j¡}||}    t ¡| ¡dd|j|_WdQRX|    |j|    ||<| d| dd¡ ¡|¡||j}
|
S)Néÿÿÿÿrrgð?ég{®Gáz„?g®Gáz®ï?)ÚFÚlinearÚ    normalizerÚclamprÚarangeÚsizeÚviewÚsqrtÚpowrrÚwhererrÚno_gradÚmeanrÚscatter_Úlongr ) rÚfeaturesÚtargetsZ    cos_thetaZ target_logitZ    sin_thetaZ cos_theta_mÚmaskZfinal_target_logitZ hard_exampleÚpred_class_logitsr r r!Úforwards "
"
zArcface.forwardcCsd |j|j|j|j¡S)Nz3in_features={}, num_classes={}, scale={}, margin={})Úformatrr    r r)rr r r!Ú
extra_repr3szArcface.extra_repr)Ú__name__Ú
__module__Ú __qualname__rr6r8Ú __classcell__r r )rr!rs r) Ú__doc__rrÚtorch.nnÚnnÚtorch.nn.functionalÚ
functionalr$rÚModulerr r r r!Ú<module>s