
    Ng                        d Z ddlmZmZmZmZmZmZmZ ddl	Z	ddl	m
Z
 ddlmZmZ 	 ddlmZ ddlmZ d	Zn# e$ r d
ZY nw xY wddlmZmZmZmZmZ ddlmZmZ ddlmZ ddlm Z m!Z! ddl"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) g dZ*eee eee!ee#e$e%e&e'e(e)hZ+	 ddlm,Z, e+-                    e,           n# e$ r Y nw xY wdee
j.                 fdZ/dee
j.                 fdZ0d Z1eehZ2defdZ3defdZ4d Z5de
j.        deee6         ee6         f         fdZde
j.        deee6e6f         ee6         f         fdZ G d d e
j.                  Z7 G d! d"e
j.                  Z8dS )#zm PyTorch FX Based Feature Extraction Helpers
Using https://pytorch.org/vision/stable/feature_extraction.html
    )CallableDictListOptionalUnionTupleTypeN)nn   )_get_feature_info_get_return_layers)create_feature_extractor)get_graph_node_namesTF)
Conv2dSameScaledStdConv2dSame
CondConv2dStdConv2dSameFormat)resample_abs_pos_embedresample_abs_pos_embed_nhwc)BilinearAttnTransform)MaxPool2dSameAvgPool2dSame)BatchNormAct2dSyncBatchNormActFrozenBatchNormAct2dGroupNormActGroupNorm1ActLayerNormActLayerNormAct2d)
register_notrace_moduleis_notrace_moduleget_notrace_modulesregister_notrace_functionis_notrace_functionget_notrace_functionsr   r   FeatureGraphNetGraphExtractNet)
InplaceAbnmodulec                 :    t                               |            | S )zq
    Any module not under timm.models.layers should get this decorator if we don't want to trace through it.
    )_leaf_modulesaddr*   s    T/var/www/html/ai-engine/env/lib/python3.11/site-packages/timm/models/_features_fx.pyr!   r!   >   s     fM    c                     | t           v S N)r,   r.   s    r/   r"   r"   F   s    ]""r0   c                  *    t          t                    S r2   )listr,    r0   r/   r#   r#   J   s    r0   funcc                 :    t                               |            | S )zF
    Decorator for functions which ought not to be traced through
    )_autowrap_functionsr-   r6   s    r/   r$   r$   U   s     D!!!Kr0   c                     | t           v S r2   )r8   r9   s    r/   r%   r%   ]   s    &&&r0   c                  *    t          t                    S r2   )r4   r8   r5   r0   r/   r&   r&   a   s    #$$$r0   modelreturnc                 r    t          | t          t                    t          t                    d          S )Nleaf_modulesautowrap_functionstracer_kwargs)_get_graph_node_namesr4   r,   r8   )r<   s    r/   r   r   e   s9     '+M':':RVWjRkRkll   r0   return_nodesc                     t           s
J d            t          | |t          t                    t          t                    d          S )NKPlease update to PyTorch 1.10+, torchvision 0.11+ for FX feature extractionr?   rB   )has_fx_feature_extraction_create_feature_extractorr4   r,   r8   )r<   rE   s     r/   r   r   l   sP    $ss&ssss$|'+M':':RVWjRkRkll   r0   c                        e Zd ZU dZej        j        e         ed<   	 	 	 dde	j
        deedf         d	ee         d
edef
 fdZd Z xZS )r'   zX A FX Graph based feature extractor that works with the model feature_info metadata
    return_dictNNCHWFr<   out_indices.out_map
output_fmtc                 f   t                                                       t          s
J d            t          ||          | _        |"t          |          t          |          k    sJ t          |          | _        t          | j        |          }t          ||          | _
        || _        d S )NrG   )super__init__rH   r   feature_infolenr   rO   r   r   graph_modulerK   )selfr<   rM   rN   rO   rK   rE   	__class__s          r/   rR   zFeatureGraphNet.__init__y   s     	(ww*wwww-e[AAw<<3{#3#33333 ,,)$*;WEE4ULII&r0   c                     |                      |          }| j        r|S t          |                                          S r2   )rU   rK   r4   valuesrV   xouts      r/   forwardzFeatureGraphNet.forward   s;    "" 	JCJJLL!!!r0   )NrL   F)__name__
__module____qualname____doc__torchjitFinalbool__annotations__r
   Moduler   intr   r   strrR   r]   __classcell__rW   s   @r/   r'   r'   t   s          &&&& '+$ %' '9' sCx' d^	'
 ' ' ' ' ' ' '$" " " " " " "r0   r'   c            	            e Zd ZU dZej        j        e         ed<   	 	 dde	j
        deeeef         ee         f         dedef fdZd	eeej                 ej        f         fd
Z xZS )r(   a   A standalone feature extraction wrapper that maps dict -> list or single tensor
    NOTE:
      * one can use feature_extractor directly if dictionary output is desired
      * unlike FeatureGraphNet, this is intended to be used standalone and not with model feature_info
      metadata for builtin feature extraction mode
      * create_feature_extractor can be used directly if dictionary output is acceptable

    Args:
        model: model to extract features from
        return_nodes: node names to return features from (dict or list)
        squeeze_out: if only one output, and output in list format, flatten to single tensor
        return_dict: return as dictionary from extractor with node names as keys, ignores squeeze_out arg
    rK   TFr<   rE   squeeze_outc                     t                                                       || _        t          ||          | _        || _        d S r2   )rQ   rR   rm   r   rU   rK   )rV   r<   rE   rm   rK   rW   s        r/   rR   zGraphExtractNet.__init__   sC     	&4ULII&r0   r=   c                     |                      |          }| j        r|S t          |                                          }| j        rt          |          dk    r|d         n|S )Nr   r   )rU   rK   r4   rY   rm   rT   rZ   s      r/   r]   zGraphExtractNet.forward   s^    "" 	J3::<<  )Dc#hh!mms1vvDr0   )TF)r^   r_   r`   ra   rb   rc   rd   re   rf   r
   rg   r   r   ri   r   rR   Tensorr]   rj   rk   s   @r/   r(   r(      s           &&&& !% %
' 
'9
'  S#XS	 9:
' 	
'
 
' 
' 
' 
' 
' 
'EE$u|"4el"BC E E E E E E E Er0   r(   )9ra   typingr   r   r   r   r   r   r	   rb   r
   	_featuresr   r   %torchvision.models.feature_extractionr   rI   r   rD   rH   ImportErrortimm.layersr   r   r   r   r   r   r   timm.layers.non_local_attnr   timm.layers.pool2d_samer   r   timm.layers.norm_actr   r   r   r   r   r   r    __all__r,   r)   r-   rg   r!   r"   r#   r8   r$   r%   r&   ri   r'   r(   r5   r0   r/   <module>rz      s    F E E E E E E E E E E E E E E E E E        < < < < < < < <&kkkkkkcccccc $ & & & %& [ Z Z Z Z Z Z Z Z Z Z Z Z Z K K K K K K K K < < < < < < @ @ @ @ @ @ @ @                 e e e 2M=	&&&&&&j!!!! 	 	 	D	DO    #d29o # # # #    H    'h ' ' ' '% % %	 eDItCy4H.I    BI U4S>SWX[S\C\=]    " " " " "bi " " "<!E !E !E !E !Ebi !E !E !E !E !Es!   7 A AB+ +B32B3