
    hP%                         S SK r S SKrS SKrS SKJr  S SKJrJr  S SKJr  S SK	r	\ R                  " \5      r " S S\R                  5      r    SS\S\S	\S
\S\S\4S jjr SS\S\4S jjrg)    N)SAM2Base)compare_tensors_with_tolerancerandom_sam2_input_image)nnc            
          ^  \ rS rSrS\SS4U 4S jjr S
S\R                  S\S\	\R                  \R                  \R                  4   4S jjr
S	rU =r$ )SAM2ImageEncoder   	sam_modelreturnNc                 r   > [         TU ]  5         Xl        UR                  U l        UR                  U l        g )N)super__init__modelimage_encoderno_mem_embed)selfr
   	__class__s     l/var/www/fran/franai/venv/lib/python3.13/site-packages/onnxruntime/transformers/models/sam2/image_encoder.pyr   SAM2ImageEncoder.__init__   s/    
&44%22    imageenable_nvtx_profilec           	         SnU(       a  SSK Jn  U" SS/5      nUb  UR                  S5        U R                  U5      nUb"  UR	                  S5        UR                  S5        U R
                  R                  R                  US   S   5      US   S'   U R
                  R                  R                  US   S   5      US   S'   US   U R
                  R                  * S nUS   U R
                  R                  * S nU Vs/ s H   oR                  S	   UR                  S
   4PM"     n	nU Vs/ s H$  oR                  S5      R                  SSS5      PM&     n
nU
S
   U R                  -   U
S
'   [        U
SSS
2   U	SSS
2   SS9 VVs/ s H*  u  pUR                  SSS5      R                  " SS
/UQ76 PM,     snnSSS
2   nUb!  UR	                  S5        UR!                  5         US   US   US   4$ s  snf s  snf s  snnf )a  
Encodes images into features.

Only supports H=W=1024. If you want to use different image sizes like 512x512,
see https://github.com/facebookresearch/segment-anything-2/issues/138.

Args:
    image (torch.Tensor): images of shape [B, 3, H, W], B is batch size, H and W are height and width.
    enable_nvtx_profile (bool): enable NVTX profiling.

Returns:
    image_features_0: image features of shape [B, 32, H/4, W/4] - high resolution features of level 0
    image_features_1: image features of shape [B, 64, H/8, W/8] - high resolution features of level 1
    image_embeddings: image features of shape [B, 256, H/16, W/16] - 16 is the backbone_stride
Nr   )
NvtxHelperr   post_processbackbone_fpn   vision_pos_enc   F)strict)nvtx_helperr   start_profiler   stop_profiler   sam_mask_decoderconv_s0conv_s1num_feature_levelsshapeflattenpermuter   zipreshapeprint_latency)r   r   r   r#   r   backbone_outfeature_mapsvision_pos_embedsx
feat_sizesvision_featsfeat	feat_sizefeatss                 r   forwardSAM2ImageEncoder.forward   s.   ( .$o~%FGK"%%o6))%0"$$_5%%n5 +/***E*E*M*Ml[iNjklNm*n^$Q'*.***E*E*M*Ml[iNjklNm*n^$Q' $N3TZZ5R5R4R4TU()9:DJJ<Y<Y;Y;[\:KL:KQwwr{AGGBK0:K
L @LL|!		!,,Q15|L'+d.?.??R $'|DbD'9:dd;KTY#Z
#Z LLAq!))!R<)<#Z
 B$
 "$$^4%%'Qxq58++# M M
s   'G:1+G?1H)r   r   r   F)__name__
__module____qualname____firstlineno__r   r   torchTensorbooltupler9   __static_attributes____classcell__)r   s   @r   r   r      s]    3( 3t 3 %*<,||<, "<, 
u||U\\5<<7	8	<, <,r   r   
sam2_modelonnx_model_pathdynamic_batch_axesverbosedynamoclear_dynamo_metadatac                 Z   [        5       n[        U 5      R                  5       nU" U5      u  pn
[        R	                  SUR
                  5        [        R	                  SUR
                  5        [        R	                  SU	R
                  5        [        R	                  SU
R
                  5        S nU(       a  SS0SS0SS0SS0S.n[        R                  " 5          U(       dF  [        R                  " S[        R                  R                  S	9  [        R                  " S[        S	9  U(       d*  [        R                  R                  UUUS
SS
S// SQUS9	  GO,S
[        R                  R                   l        [        R                  R                  UU4SS[        R                  R$                  R&                  0/S9n[        R                  R                  USSS// SQS
S9nUR)                  5         UR+                  US-   SS9  SS KnSSKJn  UR1                  US-   S
S9nU(       a  SUR2                  R4                  S   R6                  R8                  R
                  R:                  S   l        [?        S5       HL  nSUR2                  R@                  U   R6                  R8                  R
                  R:                  S   l        MN     U" U5      nURC                  5         U(       a  URE                  5         SS K#nURH                  RK                  U5      (       a  URM                  U5        URH                  RK                  US-   5      (       a  URM                  US-   5        URN                  RQ                  US
S
S
S9  S S S 5        [S        SU5        g ! , (       d  f       N= f)Nimage.shape: %simage_features_0.shape: %simage_features_1.shape: %simage_embeddings.shape: %sr   
batch_size)r   image_features_0image_features_1image_embeddingsignore)categoryT   r   )rR   rS   rT   )export_paramsopset_versiondo_constant_foldinginput_namesoutput_namesdynamic_axesF)argsr"   dynamic_shapes )rY   r[   r\   rJ   z.dynamo.onnx)external_data)DynamoOnnxHelper)load_external_data   z.data)use_external_data_formatall_tensors_to_one_fileconvert_attributezencoder onnx model saved to)*r   r   cpuloggerinfor*   warningscatch_warningsfilterwarningsr@   jitTracerWarningUserWarningonnxexport_dynamoconfigcapture_scalar_outputsDimAUTOoptimizesave+onnxruntime.transformers.dynamo_onnx_helperrb   
load_modelgraphinputtypetensor_typedim	dim_paramrangeoutput!convert_constants_to_initializersclear_metadataospathexistsremover   save_model_to_fileprint)rF   rG   rH   rI   rJ   rK   r   sam2_encoderrR   rS   rT   r]   eponnx_programrq   rb   
onnx_modelionnx_model_helperr   s                       r   export_image_encoder_onnxr   X   s     $%E#J/335L;G;N8(8
KK!5;;/
KK,.>.D.DE
KK,.>.D.DE
KK,.>.D.DEL&!"L 1!"L 1!"L 1	
 
	 	 	"##Huyy7N7NO##H{CJJ" $($IY)  
 ;?EMM  7$$X((--. 	 % B !::,, $IY - L !!#o>eTT>)I^bcJ!T`
  &&q)..::@@DDQGQqAYeJ$$++A.33??EEII!LV " !1 <??A$!002ww~~o..		/*ww~~o788		/G34##66$X\pt 7 y 
#@ 

'9A 
#	"s   J+N
N*c                 2   [         R                  " US/S9nUR                  5       n[        [	        U5      5       Vs/ s H  oTU   R
                  PM     nn[        R                  SU5        UR                  5       n[        [	        U5      5       Vs/ s H  oWU   R
                  PM     nn[        R                  SU5        U(       a  SS/OS/n	U	 GH  n
[        U
5      n[        U 5      R                  5       nU" UR                  5       5      u  pn[        R                  SUR                  5        [        R                  SUR                  5        [        R                  S	UR                  5        [        R                  S
UR                  5        UR                  USUR                  5       05      n[!        U5       H*  u  nn[        R                  SUUU   R                  5        M,     Uu  nnn[#        SU[$        R&                  " U5      SS9(       a^  [#        SU[$        R&                  " U5      SS9(       a9  [#        SU[$        R&                  " U5      SS9(       a  [)        SU
 SU 35        GM  [)        SU
 SU 35        GM     g s  snf s  snf )NCPUExecutionProvider)	providerszinput_names: %szoutput_names: %sr   r!   rM   rN   rO   rP   r   zoutput %s shape %srR   )mismatch_percentage_tolerancerS   rT   z,onnx model has been verified for batch_size=z: z.onnx model verification failed for batch_size=)onnxruntimeInferenceSession
get_inputsr   lennameri   rj   get_outputsr   r   rh   cloner*   runnumpy	enumerater   r@   tensorr   )rF   rG   rH   ort_sessionmodel_inputsr   r[   model_outputsr\   batch_sizesrQ   r   r   rR   rS   rT   outputsoutput_nameort_image_features_0ort_image_features_1ort_image_embeddingss                        r   test_image_encoder_onnxr      s=   
 ..KaJbcK))+L16s<7H1IJ1IA?''1IKJ
KK!;/++-M38]9K3LM3La!$))3LLM
KK"L1.1a&QCK!
'
3'
3779?KEKKM?Z<,<%u{{302B2H2HI02B2H2HI02B2H2HI//,%++-0HI'5NA{KK,k71:;K;KL 6KRH24H +" 12./	 /" 12./	 /" 12./	 @BN_`aB:,bQ`PabcO " K Ns   JJ)FFFFr;   )loggingrk   r@   sam2.modeling.sam2_baser   
sam2_utilsr   r   r   r   	getLoggerr<   ri   Moduler   strrB   r   r   r`   r   r   <module>r      s   
    , N  			8	$C,ryy C,R  %"'Z:Z:Z: Z: 	Z:
 Z:  Z:@ 7d7d7dr   