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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
B
_cã@sŠdZddlZddlmZddlmZddlmZmZddlm    Z    Gdd„dej
ƒZ Gd    d
„d
ej
ƒZ d d „Z d d„Zdd„Zddd„ZdS)z9
@author:  liaoxingyu
@contact: sherlockliao01@gmail.com
éN)Únn)Ú BatchNorm2d)ÚReLUÚ    LeakyReLU)Ú    Parametercs4eZdZ‡fdd„Zdd„Zdd„Zdd„Z‡ZS)    ÚTLUcs0tt|ƒ ¡||_tt |¡ƒ|_| ¡dS)z9max(y, tau) = max(y - tau, 0) + tau = ReLU(y - tau) + tauN)    ÚsuperrÚ__init__Ú num_featuresrÚtorchÚTensorÚtauÚreset_parameters)Úselfr
)Ú    __class__©ú./layers/frn.pyr    sz TLU.__init__cCstj |j¡dS)N)rÚinitÚzeros_r )rrrrrszTLU.reset_parameterscCsdjf|jŽS)Nznum_features={num_features})ÚformatÚ__dict__)rrrrÚ
extra_reprszTLU.extra_reprc    Cst ||j d|jdd¡¡S)Né)r Úmaxr Úviewr
)rÚxrrrÚforwardsz TLU.forward)Ú__name__Ú
__module__Ú __qualname__r    rrrÚ __classcell__rr)rrrs rcs6eZdZd ‡fdd„    Zdd„Zdd„Zd    d
„Z‡ZS) ÚFRNçíµ ÷ư>Fcsvtt|ƒ ¡||_||_||_tt |¡ƒ|_    tt |¡ƒ|_
|rVtt d¡ƒ|_ n|  dt |g¡¡|  ¡dS)zë
        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.
        rÚepsN)rr!r    r
Úinit_epsÚis_eps_leanablerr r ÚweightÚbiasr#Úregister_bufferr)rr
r#r%)rrrr    !sz FRN.__init__cCs8tj |j¡tj |j¡|jr4tj |j|j    ¡dS)N)
rrÚones_r&rr'r%Ú    constant_r#r$)rrrrr7szFRN.reset_parameterscCsdjf|jŽS)Nz+num_features={num_features}, eps={init_eps})rr)rrrrr=szFRN.extra_reprcCs`| d¡jddgdd}|t ||j ¡¡}|j d|jdd¡||j     d|jdd¡}|S)ai
        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)
        ééT)ÚdimÚkeepdimr)
ÚpowÚmeanr Úrsqrtr#Úabsr&rr
r')rrZnu2rrrr@s ,z FRN.forward)r"F)rrrr    rrrr rr)rrr! sr!cCsš|}d}d}d}x„| ¡D]x\}}|rpt|ttfƒrpt|tƒrT| |t|jd¡ntƒ‚| |t    |jd¡n| |t
|ƒ¡|}|}t|tƒ}qW|S)z5
    Convert 'BatchNorm2d + ReLU' to 'FRN + TLU'
    NF)r
) Únamed_childrenÚ
isinstancerrrÚ
add_moduler!r
ÚNotImplementedErrorrÚ bnrelu_to_frn)ÚmoduleÚmodZ before_nameZ before_childÚ is_before_bnÚnameÚchildrrrr7Vs 
r7cCsŽ|}d}x€| ¡D]t\}}t||ƒrtt||ƒrtt|tƒrR|j}| |t|jd¡t|tt    fƒr†| |t
|d¡q| |t ||ƒ¡qW|S)N)r
) r3ÚhasattrÚgetattrr4rr
r5r!rrrÚconvert)r8Ú    flag_namer9Z    before_chr;r<rrrr?ss
r?cCsX|}xN| ¡D]B\}}t|dƒr>t||ƒ| |t||ƒ¡q| |t||ƒ¡qW|S)NÚis_convert_frn)r3r=Údelattrr5Ú remove_flags)r8r@r9r;r<rrrrCƒs
 
rC©r,é€rEr+rAc    sÎtƒ‰tƒ‰dg‰‡‡‡‡fdd„}dg‰‡‡‡‡fdd„}t|tƒrL|g}‡fdd„|Dƒ}ˆ |¡ˆ |¡ˆ|Ž}| ¡ ¡xˆD] }| ¡qŒWxˆD] }| ¡q¢Wtˆˆd‰tˆˆd‰ˆS)    NFcs&‡‡‡‡fdd„}ˆ ˆ |¡¡dS)Ncsbtˆtjtjfƒsˆˆkr(ˆ d¡dSˆdo<t|ttfƒ}|rNt|ˆdƒˆ t|tƒ¡dS)NFéÿÿÿÿT)    r4rÚ
SequentialÚ
ModuleListÚappendrrÚsetattrr)rÚinputÚoutputÚ is_converted)r@r:Úmodelr8rrÚhook•s
 z;bnrelu_to_frn2.<locals>.register_forward_hook.<locals>.hook)rIÚregister_forward_hook)r8rO)r@Ú forard_hooksr:rN)r8rrP”s z-bnrelu_to_frn2.<locals>.register_forward_hookcs&‡‡‡‡fdd„}ˆ ˆ |¡¡dS)Ncsbtˆtjtjfƒsˆˆkr(ˆ d¡dSˆdo8t|tƒ}|rJt|ˆdƒˆ t|ttfƒ¡dS)NFrFT)    r4rrGrHrIrrJrr)rrKrLrM)r@Úis_before_relurNr8rrrO¥s
 z<bnrelu_to_frn2.<locals>.register_backward_hook.<locals>.hook)rIÚregister_backward_hook)r8rO)Úbackward_hooksr@rRrN)r8rrS¤s    z.bnrelu_to_frn2.<locals>.register_backward_hookcsg|]}tjˆf|žŽ‘qSr)r Úrand)Ú.0Zin_size)Ú
batch_sizerrú
<listcomp>µsz"bnrelu_to_frn2.<locals>.<listcomp>)r@)    Úlistr4ÚtupleÚapplyÚsumÚbackwardÚremover?rC)    rNÚ
input_sizerWr@rPrSrrLÚhr)rTrWr@rQr:rRrNrÚbnrelu_to_frn2Žs(
 
 
 
 
   ra)rDr+rA)Ú__doc__r rZtorch.nn.modules.batchnormrÚtorch.nnrrZtorch.nn.parameterrÚModulerr!r7r?rCrarrrrÚ<module>s   6