
    Ng                        d Z ddlmZmZmZ ddlZddlmZ ddlmc m	Z
 ddlmZmZ eeeeef         f         ZddZddefd	Zddefd
ZddefdZ G d dej                  Z G d dej                  Z G d dej                  Z G d dej                  Z G d dej                  Z G d dej                  Z G d dej                  ZdS )a   PyTorch selectable adaptive pooling
Adaptive pooling with the ability to select the type of pooling from:
    * 'avg' - Average pooling
    * 'max' - Max pooling
    * 'avgmax' - Sum of average and max pooling re-scaled by 0.5
    * 'avgmaxc' - Concatenation of average and max pooling along feature dim, doubles feature dim

Both a functional and a nn.Module version of the pooling is provided.

Hacked together by / Copyright 2020 Ross Wightman
    )OptionalTupleUnionN   )get_spatial_dimget_channel_dimavgc                 4    |                      d          rdS dS )N	catavgmax   r   )endswith	pool_types    \/var/www/html/ai-engine/env/lib/python3.11/site-packages/timm/layers/adaptive_avgmax_pool.pyadaptive_pool_feat_multr      s"    +&& qq    output_sizec                 f    t          j        | |          }t          j        | |          }d||z   z  S )N      ?)Fadaptive_avg_pool2dadaptive_max_pool2dxr   x_avgx_maxs       r   adaptive_avgmax_pool2dr      s5    !![11E!![11E%%-  r   c                     t          j        | |          }t          j        | |          }t          j        ||fd          S Nr   )r   r   r   torchcatr   s       r   adaptive_catavgmax_pool2dr"   $   s<    !![11E!![11E9eU^Q'''r   c                     |dk    rt          j        | |          } nW|dk    rt          | |          } n@|dk    rt          | |          } n)|dk    rt          j        | |          } nJ d|z              | S )zFSelectable global pooling function with dynamic input kernel size
    r	   avgmaxr   maxFInvalid pool type: %s)r   r   r   r"   r   )r   r   r   s      r   select_adaptive_pool2dr'   *   s     E!![11	h		"1k22	k	!	!%a55	e		!![119-	9999Hr   c                   0     e Zd Zddedef fdZd Z xZS )FastAdaptiveAvgPoolFNCHWflatten	input_fmtc                     t          t          |                                            || _        t	          |          | _        d S N)superr)   __init__r+   r   dimselfr+   r,   	__class__s      r   r0   zFastAdaptiveAvgPool.__init__;   :    !4((11333"9--r   c                 F    |                     | j        | j                   S Nkeepdim)meanr1   r+   r3   r   s     r   forwardzFastAdaptiveAvgPool.forward@       vvdhDL(8v999r   Fr*   )__name__
__module____qualname__boolr   r0   r<   __classcell__r4   s   @r   r)   r)   :   s_        . . . . . . . . .
: : : : : : :r   r)   c                   0     e Zd Zddedef fdZd Z xZS )FastAdaptiveMaxPoolFr*   r+   r,   c                     t          t          |                                            || _        t	          |          | _        d S r.   )r/   rF   r0   r+   r   r1   r2   s      r   r0   zFastAdaptiveMaxPool.__init__E   r5   r   c                 F    |                     | j        | j                   S r7   )amaxr1   r+   r;   s     r   r<   zFastAdaptiveMaxPool.forwardJ   r=   r   r>   r?   r@   rA   rB   strr0   r<   rC   rD   s   @r   rF   rF   D   s_        . . . . . . . . .
: : : : : : :r   rF   c                   0     e Zd Zddedef fdZd Z xZS )FastAdaptiveAvgMaxPoolFr*   r+   r,   c                     t          t          |                                            || _        t	          |          | _        d S r.   )r/   rM   r0   r+   r   r1   r2   s      r   r0   zFastAdaptiveAvgMaxPool.__init__O   s:    $d++44666"9--r   c                     |                     | j        | j                   }|                    | j        | j                   }d|z  d|z  z   S )Nr8   r   )r:   r1   r+   rI   r3   r   r   r   s       r   r<   zFastAdaptiveAvgMaxPool.forwardT   sM    txT\)9::txT\)9::U{S5[((r   r>   rJ   rD   s   @r   rM   rM   N   s_        . . . . . . . . .
) ) ) ) ) ) )r   rM   c                   0     e Zd Zddedef fdZd Z xZS )FastAdaptiveCatAvgMaxPoolFr*   r+   r,   c                     t          t          |                                            || _        t	          |          | _        |r	d| _        d S t          |          | _        d S r   )r/   rR   r0   r+   r   
dim_reducedim_catr   r2   s      r   r0   z"FastAdaptiveCatAvgMaxPool.__init__[   s\    '..77999))44 	6DLLL*955DLLLr   c                     |                     | j        | j                   }|                    | j        | j                   }t	          j        ||f| j                  S r7   )r:   rT   r+   rI   r    r!   rU   rP   s       r   r<   z!FastAdaptiveCatAvgMaxPool.forwardd   sR    tDL0@AAtDL0@AAy%666r   r>   rJ   rD   s   @r   rR   rR   Z   s_        6 6 6 6 6 6 6 6 67 7 7 7 7 7 7r   rR   c                   ,     e Zd Zddef fdZd Z xZS )AdaptiveAvgMaxPool2dr   r   c                 d    t          t          |                                            || _        d S r.   )r/   rX   r0   r   r3   r   r4   s     r   r0   zAdaptiveAvgMaxPool2d.__init__k   s.    "D))22444&r   c                 ,    t          || j                  S r.   )r   r   r;   s     r   r<   zAdaptiveAvgMaxPool2d.forwardo   s    %a)9:::r   r   r?   r@   rA   _int_tuple_2_tr0   r<   rC   rD   s   @r   rX   rX   j   sX        ' 'N ' ' ' ' ' '; ; ; ; ; ; ;r   rX   c                   ,     e Zd Zddef fdZd Z xZS )AdaptiveCatAvgMaxPool2dr   r   c                 d    t          t          |                                            || _        d S r.   )r/   r`   r0   r   rZ   s     r   r0   z AdaptiveCatAvgMaxPool2d.__init__t   s.    %t,,55777&r   c                 ,    t          || j                  S r.   )r"   r   r;   s     r   r<   zAdaptiveCatAvgMaxPool2d.forwardx   s    (D,<===r   r\   r]   rD   s   @r   r`   r`   s   sX        ' 'N ' ' ' ' ' '> > > > > > >r   r`   c            	       V     e Zd ZdZ	 	 	 	 ddededed	ef fd
Zd Zd Z	d Z
d Z xZS )SelectAdaptivePool2dzCSelectable global pooling layer with dynamic input kernel size
    r   fastFr*   r   r   r+   r,   c                    t          t          |                                            |dv sJ |pd| _        |                                }|sHt          j                    | _        |rt          j        d          nt          j                    | _	        d S |
                    d          s|dk    r|dk    s
J d            |                    d          rt          ||          | _        n|                    d	          rt          ||          | _        nk|                    d
          rt          ||          | _        n?|dk    s|                    d          rt          ||          | _        nJ d|z              t          j                    | _	        d S |dk    sJ |d	k    rt!          |          | _        nh|dk    rt#          |          | _        nM|d
k    rt          j        |          | _        n-|dk    rt          j        |          | _        nJ d|z              |rt          j        d          nt          j                    | _	        d S )N)r*   NHWC r   re   r*   zAFast pooling and non NCHW input formats require output_size == 1.r   )r,   r$   r%   r	   Fr&   )r/   rd   r0   r   lowernnIdentitypoolFlattenr+   
startswithr   rR   rM   rF   r)   rX   r`   AdaptiveMaxPool2dAdaptiveAvgPool2d)r3   r   r   r+   r,   r4   s        r   r0   zSelectAdaptivePool2d.__init__   s^    	"D))22444,,,,,"bOO%%	 	GDI,3F2:a===DLLL!!&)) 	GY&-@-@!###%h###!!+.. 	B5gSSS		##H-- B27iPPP		##E** B/9MMM		f$$	(:(:5(A(A$/9MMM		A5	AAAA;==DLLL&&&&H$$0==		k))3K@@		e##0==		e##0==		A5	AAAA,3F2:a===DLLLr   c                     | j          S r.   r   r3   s    r   is_identityz SelectAdaptivePool2d.is_identity   s    >!!r   c                 Z    |                      |          }|                     |          }|S r.   )rl   r+   r;   s     r   r<   zSelectAdaptivePool2d.forward   s%    IIaLLLLOOr   c                 *    t          | j                  S r.   )r   r   rr   s    r   	feat_multzSelectAdaptivePool2d.feat_mult   s    &t~666r   c                 l    | j         j        dz   dz   | j        z   dz   t          | j                  z   dz   S )N(z
pool_type=z
, flatten=))r4   r?   r   rK   r+   rr   s    r   __repr__zSelectAdaptivePool2d.__repr__   sH    ~&, $/ #DL 1 12478 	8r   )r   re   Fr*   )r?   r@   rA   __doc__r^   rK   rB   r0   rs   r<   rv   rz   rC   rD   s   @r   rd   rd   |   s          +,#!#'G 'G''G 'G 	'G
 'G 'G 'G 'G 'G 'GR" " "  
7 7 78 8 8 8 8 8 8r   rd   )r	   r\   )r	   r   )r{   typingr   r   r   r    torch.nnrj   torch.nn.functional
functionalr   formatr   r   intr^   r   r   r"   r'   Moduler)   rF   rM   rR   rX   r`   rd    r   r   <module>r      sF  
 
 * ) ) ) ) ) ) ) ) )                 4 4 4 4 4 4 4 4sE#s(O+,   ! !> ! ! ! !( (n ( ( ( ( N     : : : : :") : : :: : : : :") : : :	) 	) 	) 	) 	)RY 	) 	) 	)7 7 7 7 7	 7 7 7 ; ; ; ; ;29 ; ; ;> > > > >bi > > >:8 :8 :8 :8 :829 :8 :8 :8 :8 :8r   