U
    ¯Ê¹jl  ã                   @   sÚ  d Z ddl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 zddlmZ dZW n ek
rp   d	ZY nX dd
lmZmZ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" dddddddddg	Z#eeeeeeeeeeee e!e"hZ$zddlm%Z% e$ &e%¡ W n ek
�r0   Y nX eej' dœdd„Z(eej' dœdd„Z)dd„ Z*e+ƒ Z,edœdd„Z-edœdd„Z.dd„ Z/ej'eee0e0f ee0 f d œd!d„ZG d"d„ dej'ƒZ1G d#d„ dej'ƒZ2dS )$zm PyTorch FX Based Feature Extraction Helpers
Using https://pytorch.org/vision/stable/feature_extraction.html
é    )ÚCallableÚListÚDictÚUnionÚTypeN)Únné   )Ú_get_feature_infoÚ_get_return_layers)Úcreate_feature_extractorTF)Ú
Conv2dSameÚScaledStdConv2dSameÚ
CondConv2dÚStdConv2dSame)Ú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   ÚFeatureGraphNetÚGraphExtractNet)Ú
InplaceAbn©Úmodulec                 C   s   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Úaddr#   © r'   úW/var/www/html/bwave/python/venv/lib/python3.8/site-packages/timm/models/_features_fx.pyr   ;   s    
c                 C   s   | t kS ©N)r%   r#   r'   r'   r(   r   C   s    c                   C   s   t tƒS r)   )Úlistr%   r'   r'   r'   r(   r   G   s    ©Úfuncc                 C   s   t  | ¡ | S )zF
    Decorator for functions which ought not to be traced through
    )Ú_autowrap_functionsr&   r+   r'   r'   r(   r   O   s    
c                 C   s   | t kS r)   )r-   r+   r'   r'   r(   r   W   s    c                   C   s   t tƒS r)   )r*   r-   r'   r'   r'   r(   r   [   s    ©ÚmodelÚreturn_nodesc                 C   s(   t stdƒ‚t| |ttƒttƒdœd�S )NúKPlease update to PyTorch 1.10+, torchvision 0.11+ for FX feature extraction)Zleaf_modulesZautowrap_functions)Ztracer_kwargs)Úhas_fx_feature_extractionÚAssertionErrorÚ_create_feature_extractorr*   r%   r-   r.   r'   r'   r(   r   _   s     þc                       s*   e Zd ZdZd‡ fdd„	Zdd„ Z‡  ZS )r    zX A FX Graph based feature extractor that works with the model feature_info metadata
    Nc                    sZ   t ƒ  ¡  tstdƒ‚t||ƒ| _|d k	r>t|ƒt|ƒks>t‚t| j|ƒ}t||ƒ| _	d S )Nr1   )
ÚsuperÚ__init__r2   r3   r	   Zfeature_infoÚlenr
   r   Úgraph_module)Úselfr/   Zout_indicesZout_mapr0   ©Ú	__class__r'   r(   r6   j   s    
zFeatureGraphNet.__init__c                 C   s   t |  |¡ ¡ ƒS r)   )r*   r8   Úvalues)r9   Úxr'   r'   r(   Úforwards   s    zFeatureGraphNet.forward)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r6   r>   Ú__classcell__r'   r'   r:   r(   r    g   s   	c                       s\   e Zd ZdZd	eeeef ee f edœ‡ fdd„Z	eee
j e
jf dœdd„Z‡  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
    T)r0   Úsqueeze_outc                    s    t ƒ  ¡  || _t||ƒ| _d S r)   )r5   r6   rD   r   r8   )r9   r/   r0   rD   r:   r'   r(   r6   „   s    
zGraphExtractNet.__init__)Úreturnc                 C   s0   t |  |¡ ¡ ƒ}| jr,t|ƒdkr,|d S |S )Nr   r   )r*   r8   r<   rD   r7   )r9   r=   Úoutr'   r'   r(   r>   ‰   s    zGraphExtractNet.forward)T)r?   r@   rA   rB   r   r   Ústrr   Úboolr6   ÚtorchZTensorr>   rC   r'   r'   r:   r(   r!   w   s   *)3rB   Útypingr   r   r   r   r   rI   r   Z	_featuresr	   r
   Z%torchvision.models.feature_extractionr   r4   r2   ÚImportErrorZtimm.layersr   r   r   r   Ztimm.layers.non_local_attnr   Ztimm.layers.pool2d_samer   r   Ztimm.layers.norm_actr   r   r   r   r   r   r   Ú__all__r%   r"   r&   ÚModuler   r   r   Úsetr-   r   r   r   rG   r    r!   r'   r'   r'   r(   Ú<module>   sf   
$
    þ    õ&