
    ^j                         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mZmZmZmZmZmZmZ ddlmZmZ g dZ G d	 d
e
j2                        Z G d de
j2                        Zy)zm PyTorch FX Based Feature Extraction Helpers
Using https://pytorch.org/vision/stable/feature_extraction.html
    )CallableDictListOptionalUnionTupleTypeN)nn)	create_feature_extractorget_graph_node_namesregister_notrace_moduleregister_notrace_functionis_notrace_moduleis_notrace_functionget_notrace_functionsget_notrace_modulesFormat   )_get_feature_info_get_return_layers)
r   r   r   r   r   r   r   r   FeatureGraphNetGraphExtractNetc                        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_dictmodelout_indices.out_map
output_fmtc                     t         |           t        ||      | _        |t	        |      t	        |      k(  sJ t        |      | _        t        | j                  |      }t        ||      | _	        || _
        y N)super__init__r   feature_infolenr   r   r   r   graph_moduler   )selfr   r   r   r   r   return_nodes	__class__s          c/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/timm/models/_features_fx.pyr"   zFeatureGraphNet.__init__+   sq     	-e[Aw<3{#3333 ,)$*;*;WE4ULI&    c                 r    | j                  |      }| j                  r|S t        |j                               S r    )r%   r   listvaluesr&   xouts      r)   forwardzFeatureGraphNet.forward<   s1    "JCJJL!!r*   )NNCHWF)__name__
__module____qualname____doc__torchjitFinalbool__annotations__r
   Moduler   intr   r   strr"   r1   __classcell__r(   s   @r)   r   r   &   sp    && '+$ %'99' sCx' d^	'
 ' '""r*   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
    r   r   r'   squeeze_outc                 `    t         |           || _        t        ||      | _        || _        y r    )r!   r"   rB   r   r%   r   )r&   r   r'   rB   r   r(   s        r)   r"   zGraphExtractNet.__init__S   s/     	&4ULI&r*   returnc                     | j                  |      }| j                  r|S t        |j                               }| j                  rt        |      dk(  r|d   S |S )Nr   r   )r%   r   r,   r-   rB   r$   r.   s      r)   r1   zGraphExtractNet.forward_   sQ    "J3::< ))c#h!ms1vDDr*   )TF)r3   r4   r5   r6   r7   r8   r9   r:   r;   r
   r<   r   r   r>   r   r"   Tensorr1   r?   r@   s   @r)   r   r   C   s     && !% %
'99
'  S#XS	 9:
' 	
'
 
'EE$u||"4ell"BC Er*   r   )r6   typingr   r   r   r   r   r   r	   r7   r
   timm.layersr   r   r   r   r   r   r   r   r   	_featuresr   r   __all__r<   r   r    r*   r)   <module>rL      sY    F E E  
 
 
 ="bii ":!Ebii !Er*   