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
U
_8
ã@s†dZddlZddlmmZddlmZGdd„dejƒZGdd„dejƒZGdd    „d    eƒZ    Gd
d „d ejƒZ
Gd d „d ejƒZ dS)z9
@author:  l1aoxingyu
@contact: sherlockliao01@gmail.com
éN)Únnc@seZdZdd„ZdS)ÚFlattencCs| | d¡d¡S)Nréÿÿÿÿ)ÚviewÚsize)ÚselfÚinput©r    ú./layers/pooling.pyÚforward szFlatten.forwardN)Ú__name__Ú
__module__Ú __qualname__r r    r    r    r
r srcs2eZdZdZd
‡fdd„    Zdd„Zdd    „Z‡ZS) ÚGeneralizedMeanPoolingaÄApplies a 2D power-average adaptive pooling over an input signal composed of several input planes.
    The function computed is: :math:`f(X) = pow(sum(pow(X, p)), 1/p)`
        - At p = infinity, one gets Max Pooling
        - At p = 1, one gets Average Pooling
    The output is of size H x W, for any input size.
    The number of output features is equal to the number of input planes.
    Args:
        output_size: the target output size of the image of the form H x W.
                     Can be a tuple (H, W) or a single H for a square image H x H
                     H and W can be either a ``int``, or ``None`` which means the size will
                     be the same as that of the input.
    éçíµ ÷ư>cs4tt|ƒ ¡|dkst‚t|ƒ|_||_||_dS)Nr)ÚsuperrÚ__init__ÚAssertionErrorÚfloatÚpÚ output_sizeÚeps©rÚnormrr©Ú    __class__r    r
rs
 
zGeneralizedMeanPooling.__init__cCs4|j|jd |j¡}tjj ||j¡ d|j¡S)N)Úmingð?)    ÚclamprÚpowrÚtorchrÚ
functionalÚadaptive_avg_pool2dr)rÚxr    r    r
r &szGeneralizedMeanPooling.forwardcCs,|jjdt|jƒddt|jƒdS)Nú(z, z output_size=ú))rr Ústrrr©rr    r    r
Ú__repr__*s
ÿÿþþþzGeneralizedMeanPooling.__repr__)rr)r r rÚ__doc__rr r(Ú __classcell__r    r    rr
rs rcs"eZdZdZd‡fdd„    Z‡ZS)ÚGeneralizedMeanPoolingPz! Same, but norm is trainable
    érrcs.tt|ƒ |||¡t t d¡|¡|_dS©Nr)rr+rrÚ    Parameterr Úonesrrrr    r
r4sz GeneralizedMeanPoolingP.__init__)r,rr)r r rr)rr*r    r    rr
r+0sr+cs$eZdZ‡fdd„Zdd„Z‡ZS)ÚAdaptiveAvgMaxPool2dcstt|ƒ ¡tƒ|_dS©N)rr0rÚFastGlobalAvgPool2dÚavgpoolr'rr    r
r:szAdaptiveAvgMaxPool2d.__init__cCs&| ||j¡}t |d¡}||}|Sr-)r3rÚFÚadaptive_max_pool2d)rr#Zx_avgZx_maxr    r    r
r >s zAdaptiveAvgMaxPool2d.forward©r r rrr r*r    r    rr
r09s r0cs&eZdZd‡fdd„    Zdd„Z‡ZS)r2Fcstt|ƒ ¡||_dSr1)rr2rÚflatten)rr7rr    r
rFszFastGlobalAvgPool2d.__init__cCsj|jr.| ¡}| |d|ddf¡jddS| | d¡| d¡d¡ d¡ | d¡| d¡dd¡SdS)Nrrré)Údim)r7rrÚmean)rr#Úin_sizer    r    r
r Js zFastGlobalAvgPool2d.forward)Fr6r    r    rr
r2Esr2) r)r Útorch.nn.functionalrr!r4ÚModulerrr+r0r2r    r    r    r
Ú<module>s