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
22
23
24
25
26
27
28
29
30
31
U
x¨–_üã@s>dZddlZddlmmZddlmZGdd„deƒZ    dS)z9
@author:  l1aoxingyu
@contact: sherlockliao01@gmail.com
éN)Úget_event_storagec@s.eZdZdZdd„Zed
dd„ƒZdd„Zd    S) ÚCrossEntropyLossz^
    A class that stores information and compute losses about outputs of a Baseline head.
    cCs:|jjj|_|jjjj|_|jjjj|_    |jjjj
|_ dS)N) ÚMODELÚHEADSÚ NUM_CLASSESÚ _num_classesÚLOSSESÚCEÚEPSILONÚ_epsÚALPHAÚ_alphaÚSCALEÚ_scale)ÚselfÚcfg©rú&./modeling/losses/cross_entroy_loss.pyÚ__init__s zCrossEntropyLoss.__init__©éc Cs¢| d¡}t|ƒ}| |ddd¡\}}| ¡}| | dd¡ |¡¡}g}|D]8}    |d|    … d¡ ¡jddd}
|     |
 
d|¡¡qNt ƒ} |   d|d¡dS)    z;
        Log the accuracy metrics to EventStorage.
        rrTéÿÿÿÿN)ÚdimÚkeepdimgð?Z cls_accuracy) ÚsizeÚmaxÚtopkÚtÚeqÚviewÚ    expand_asÚfloatÚsumÚappendÚmul_rZ
put_scalar) Úpred_class_logitsÚ
gt_classesrÚbszZmaxkÚ_Z
pred_classÚcorrectÚretÚkZ    correct_kÚstoragerrrÚ log_accuracys
 zCrossEntropyLoss.log_accuracyc    Csº|jdkr|j}n2tj|dd}|j|t | d¡¡|f d¡}tj|dd}t     ¡<t 
|¡}|||j d9}|  d|j  d¡d|¡W5QRX| | d¡ ¡}||jS)z{
        Compute the softmax cross entropy loss for box classification.
        Returns:
            scalar Tensor
        rr)r)r ÚFÚsoftmaxr ÚtorchÚarangerÚ    unsqueezeÚ log_softmaxÚno_gradÚ    ones_likerÚscatter_ÚdataÚmeanr"r)rr%r&Z smooth_paramZ
soft_labelÚ    log_probsÚtargetsÚlossrrrÚ__call__*s
$
 
$zCrossEntropyLoss.__call__N)r)Ú__name__Ú
__module__Ú __qualname__Ú__doc__rÚ staticmethodr-r<rrrrr s
 r)
r@r0Útorch.nn.functionalÚnnÚ
functionalr.Z utils.eventsrÚobjectrrrrrÚ<module>s