Ë
    Fêñi<2  ã                   ó$  — d dl mZ d dlZd dlmZ d dlmZ ddlmZ ddl	m
Z
mZmZmZmZmZ ddlmZmZ dd	lmZmZ dd
lmZ ddlmZ d„ Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Z dd„Z!dd„Z"	 	 dd„Z#	 	 	 	 	 	 	 	 dd„Z$eeeeee e!e"ee e!e"dœZ%dd„Z&y)é    )ÚpartialN)Úattempt_download_asset)Ú
torch_loadé   )ÚMaskDecoder)ÚFpnNeckÚHieraÚImageEncoderÚImageEncoderViTÚMemoryEncoderÚPromptEncoder)ÚMemoryAttentionÚMemoryAttentionLayer)Ú	SAM2ModelÚSAMModel)ÚTinyViT)ÚTwoWayTransformerc                 óÖ   — |€| S t        |«      }t        |d«      5 }t        |«      }ddd«       dv rt        |d   t        «      r|d   }| j                  |«       | S # 1 sw Y   Œ8xY w)z*Load checkpoint into model from file path.NÚrbÚmodel)r   Úopenr   Ú
isinstanceÚdictÚload_state_dict)r   Ú
checkpointÚfÚ
state_dicts       ú^/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/ultralytics/models/sam/build.pyÚ_load_checkpointr      sv   € àÐØˆä'¨
Ó3€JÜ	ˆj˜$Ó	ð # 1Ü “]ˆ
÷#ð �*Ñ¤¨J°wÑ,?ÄÔ!FØ Ñ(ˆ
Ø	×Ñ˜*Ô%Ø€L÷#ð #ús   œAÁA(c                 ó&   — t        dddg d¢| ¬«      S )z_Build and return a Segment Anything Model (SAM) h-size model with specified encoder parameters.é   é    é   ©é   é   é   é   ©Úencoder_embed_dimÚencoder_depthÚencoder_num_headsÚencoder_global_attn_indexesr   ©Ú
_build_sam©r   s    r   Úbuild_sam_vit_hr1   '   ó   € äØØØÚ$3Øôð ó    c                 ó&   — t        dddg d¢| ¬«      S )z_Build and return a Segment Anything Model (SAM) l-size model with specified encoder parameters.é   é   r#   )é   é   é   r'   r)   r.   r0   s    r   Úbuild_sam_vit_lr:   2   r2   r3   c                 ó&   — t        dddg d¢| ¬«      S )z_Build and return a Segment Anything Model (SAM) b-size model with specified encoder parameters.é   é   )é   r7   é   r8   r)   r.   r0   s    r   Úbuild_sam_vit_br@   =   s   € äØØØÚ$1Øôð r3   c                 ó0   — t        g d¢g d¢g d¢dd| ¬«      S )z_Build and return a Mobile Segment Anything Model (Mobile-SAM) for efficient image segmentation.)é@   é€   é    i@  )r>   r>   é   r>   )r>   é   r7   é
   NT)r*   r+   r,   r-   Ú
mobile_samr   r.   r0   s    r   Úbuild_mobile_samrI   H   s!   € äÚ-Ú"Ú'Ø$(ØØôð r3   c           	      ó6   — t        dg d¢dg d¢g d¢g d¢| ¬«      S )zjBuild and return a Segment Anything Model 2 (SAM2) tiny-size model with specified architecture parameters.é`   )r   r>   r%   r>   r   )r7   r%   é	   ©r?   rF   é   r%   ©r<   i€  éÀ   rK   ©r*   Úencoder_stagesr,   Úencoder_global_att_blocksÚencoder_window_specÚencoder_backbone_channel_listr   ©Ú_build_sam2r0   s    r   Úbuild_sam2_trX   T   s$   € äØÚ#ØÚ"+Ú)Ú&9Øôð r3   c           	      ó6   — t        dg d¢dg d¢g d¢g d¢| ¬«      S )zeBuild and return a small-size Segment Anything Model 2 (SAM2) with specified architecture parameters.rK   )r   r>   r8   r>   r   )r%   rG   é   rM   rO   rQ   rV   r0   s    r   Úbuild_sam2_sr[   a   s$   € äØÚ$ØÚ"-Ú)Ú&9Øôð r3   c           
      ó<   — t        dg d¢dg d¢g d¢ddgg d¢| ¬«      S )	zjBuild and return a Segment Anything Model 2 (SAM2) base-size model with specified architecture parameters.ép   )r>   é   r#   r^   r>   )r=   r#   é   rM   rN   )i€  iÀ  éà   r]   )r*   rR   r,   rS   rT   Úencoder_window_spatial_sizerU   r   rV   r0   s    r   Úbuild_sam2_brb   n   s+   € äØÚ$ØÚ".Ú)Ø%'¨ HÚ&:Øô	ð 	r3   c           	      ó6   — t        dg d¢dg d¢g d¢g d¢| ¬«      S )zeBuild and return a large-size Segment Anything Model 2 (SAM2) with specified architecture parameters.é�   ©r>   rE   é$   rF   r>   )r'   é!   é+   ©r?   rF   r#   r?   ©i€  i@  i   rd   rQ   rV   r0   s    r   Úbuild_sam2_lrk   |   s$   € äØÚ$ØÚ".Ú)Ú&;Øôð r3   c                 ó†  — d}d}d}||z  }	|rt        ddd| ||g d¢dddd	ddd
¬«      n9t        || |dt        t        j                  j
                  d¬«      ||dd|d|¬«      }
t        |
t        ||	|	f||fd¬«      t        dt        d|dd¬«      |dd¬«      g d¢g d¢¬«      }|�t        ||«      }|j                  «        |S )aO  Build a Segment Anything Model (SAM) with specified encoder parameters.

    Args:
        encoder_embed_dim (int | list[int]): Embedding dimension for the encoder.
        encoder_depth (int | list[int]): Depth of the encoder.
        encoder_num_heads (int | list[int]): Number of attention heads in the encoder.
        encoder_global_attn_indexes (list[int] | None): Indexes for global attention in the encoder.
        checkpoint (str | None, optional): Path to the model checkpoint file.
        mobile_sam (bool, optional): Whether to build a Mobile-SAM model.

    Returns:
        (SAMModel): A Segment Anything Model instance with the specified architecture.

    Examples:
        >>> sam = _build_sam(768, 12, 12, [2, 5, 8, 11])
        >>> sam = _build_sam([64, 128, 160, 320], [2, 2, 6, 2], [2, 4, 5, 10], None, mobile_sam=True)
    é   r5   r#   r^   iè  )r%   r%   rN   r%   g      @g        Fgš™™™™™é?)Úimg_sizeÚin_chansÚnum_classesÚ
embed_dimsÚdepthsÚ	num_headsÚwindow_sizesÚ	mlp_ratioÚ	drop_rateÚdrop_path_rateÚuse_checkpointÚmbconv_expand_ratioÚlocal_conv_sizeÚlayer_lr_decayrF   g�íµ ÷Æ°>)ÚepsTrN   )ÚdepthÚ	embed_dimrn   ru   Ú
norm_layerrs   Ú
patch_sizeÚqkv_biasÚuse_rel_posÚglobal_attn_indexesÚwindow_sizeÚ	out_chans)r~   Úimage_embedding_sizeÚinput_image_sizeÚmask_in_chansr>   i   r?   )r}   Úembedding_dimÚmlp_dimrs   )Únum_multimask_outputsÚtransformerÚtransformer_dimÚiou_head_depthÚiou_head_hidden_dim)g33333ë^@gR¸…ë]@gR¸…ëáY@)gÃõ(\�2M@g�Âõ(\�L@g     °L@)Úimage_encoderÚprompt_encoderÚmask_decoderÚ
pixel_meanÚ	pixel_std)r   r   r   ÚtorchÚnnÚ	LayerNormr   r   r   r   r   Úeval)r*   r+   r,   r-   r   rH   Úprompt_embed_dimÚ
image_sizeÚvit_patch_sizer†   r�   Úsams               r   r/   r/   ‰   s!  € ð2 ÐØ€JØ€NØ%¨Ñ7Ðñ$ ô! 	ØØØØ(Ø Ø'Ú&ØØØØ Ø #ØØõ	
ô" ØØ'ØØÜœuŸx™x×1Ñ1°tÔ<Ø'Ø%ØØØ ;ØØ&ô
ð% ôB Ø#Ü$Ø&Ø"6Ð8LÐ!MØ(¨*Ð5Øô	
ô !Ø"#Ü)ØØ.ØØô	ð -ØØ #ô
ò -Ú)ô+€Cð. ÐÜ˜s JÓ/ˆØ‡H�H„JØ€Jr3   c           
      óÆ  — t        t        | |||||¬«      t        d|ddgd¬«      d¬«      }t        dd	d
t	        «       ¬«      }	t        d¬«      }
|duxr d|v }t        d5i d|“d|	“d|
“dd“dd“dd“dd“dd	“dd	“dd	“dd	“dd	“d d	“d!d	“d"d	“d#d	“d$d	“d%d	“d&d	“d'd	“d(d)“d*d“d+d	“d,d-“d.|“d/|“d0|“d1t        d	d2d3¬4«      “Ž}|�t        ||«      }|j                  «        |S )6a`  Build and return a Segment Anything Model 2 (SAM2) with specified architecture parameters.

    Args:
        encoder_embed_dim (int, optional): Embedding dimension for the encoder.
        encoder_stages (list[int], optional): Number of blocks in each stage of the encoder.
        encoder_num_heads (int, optional): Number of attention heads in the encoder.
        encoder_global_att_blocks (list[int], optional): Indices of global attention blocks in the encoder.
        encoder_backbone_channel_list (list[int], optional): Channel dimensions for each level of the encoder backbone.
        encoder_window_spatial_size (list[int], optional): Spatial size of the window for position embeddings.
        encoder_window_spec (list[int], optional): Window specifications for each stage of the encoder.
        checkpoint (str | None, optional): Path to the checkpoint file for loading pre-trained weights.

    Returns:
        (SAM2Model): A configured and initialized SAM2 model.

    Examples:
        >>> sam2_model = _build_sam2(encoder_embed_dim=96, encoder_stages=[1, 2, 7, 2])
        >>> sam2_model.eval()
    )r~   rs   ÚstagesÚglobal_att_blocksÚ!window_pos_embed_bkg_spatial_sizeÚwindow_specrm   r>   r^   Únearest)Úd_modelÚbackbone_channel_listÚfpn_top_down_levelsÚfpn_interp_modelr   )ÚtrunkÚneckÚscalpTrF   )r£   Úpos_enc_at_inputÚ
num_layersÚlayerrB   )Úout_dimNzsam2.1r�   Úmemory_attentionÚmemory_encoderÚnum_maskmemr%   rš   r5   Úsigmoid_scale_for_mem_encg      4@Úsigmoid_bias_for_mem_encg      $ÀÚ$use_mask_input_as_output_without_samÚdirectly_add_no_mem_embedÚuse_high_res_features_in_samÚmultimask_output_in_samÚiou_prediction_use_sigmoidÚuse_obj_ptrs_in_encoderÚadd_tpos_enc_to_obj_ptrsÚ"only_obj_ptrs_in_the_past_for_evalÚpred_obj_scoresÚpred_obj_scores_mlpÚfixed_no_obj_ptrÚmultimask_output_for_trackingÚuse_multimask_token_for_obj_ptrÚmultimask_min_pt_numr   Úmultimask_max_pt_numÚuse_mlp_for_obj_ptr_projÚcompile_image_encoderFÚno_obj_embed_spatialÚproj_tpos_enc_in_obj_ptrsÚuse_signed_tpos_enc_to_obj_ptrsÚsam_mask_decoder_extra_argsgš™™™™™©?g\�Âõ(\ï?)Údynamic_multimask_via_stabilityÚ!dynamic_multimask_stability_deltaÚ"dynamic_multimask_stability_thresh© )
r
   r	   r   r   r   r   r   r   r   r˜   )r*   rR   r,   rS   rU   ra   rT   r   r�   r®   r¯   Ú	is_sam2_1Úsam2s                r   rW   rW   ä   sÌ  € ô: !ÜØ'Ø'Ø!Ø7Ø.IØ+ô
ô ØØ"?Ø!" A Ø&ô	
ð ô€Mô" '¨sÀTÐVWÔ_sÓ_uÔvÐÜ"¨2Ô.€Nà $Ð&ÒA¨8°zÐ+A€IÜò !Ù#ð!á)ð!ñ &ð!ñ ð	!ñ
 ð!ñ #'ð!ñ "'ð!ñ .2ð!ñ #'ð!ñ &*ð!ñ !%ð!ñ $(ð!ñ !%ð!ñ "&ð!ñ ,0ð!ñ  ð!!ñ" !ð#!ñ$ ð%!ñ& '+ð'!ñ( )-ð)!ñ* ð+!ñ, ð-!ñ. "&ð/!ñ0 $ð1!ñ2 'ð3!ñ4 #,ð5!ñ6 )2ð7!ô8 %)Ø,0Ø.2Ø/3õ%
ð9!€DðF ÐÜ  jÓ1ˆØ‡I�I„KØ€Kr3   )zsam_h.ptzsam_l.ptúsam_b.ptzmobile_sam.ptz	sam2_t.ptz	sam2_s.ptz	sam2_b.ptz	sam2_l.ptzsam2.1_t.ptzsam2.1_s.ptzsam2.1_b.ptzsam2.1_l.ptc                 óØ   — d}t        | «      } t        D ])  }| j                  |«      sŒt        j                  |«      }Œ+ |s"t	        | › dt        j                  «       › �«      ‚ || «      S )aÍ  Build and return a Segment Anything Model (SAM) based on the provided checkpoint.

    Args:
        ckpt (str | Path, optional): Path to the checkpoint file or name of a pre-defined SAM model.

    Returns:
        (SAMModel | SAM2Model): A configured and initialized SAM or SAM2 model instance.

    Raises:
        FileNotFoundError: If the provided checkpoint is not a supported SAM model.

    Examples:
        >>> sam_model = build_sam("sam_b.pt")
        >>> sam_model = build_sam("path/to/custom_checkpoint.pt")

    Notes:
        Supported pre-defined models include:
        - SAM: 'sam_h.pt', 'sam_l.pt', 'sam_b.pt', 'mobile_sam.pt'
        - SAM2: 'sam2_t.pt', 'sam2_s.pt', 'sam2_b.pt', 'sam2_l.pt'
    Nz7 is not a supported SAM model. Available models are: 
 )ÚstrÚsam_model_mapÚendswithÚgetÚFileNotFoundErrorÚkeys)ÚckptÚmodel_builderÚks      r   Ú	build_samrÙ   O  so   € ð* €MÜˆt‹9€DÜò 1ˆØ�=‰=˜ÕÜ)×-Ñ-¨aÓ0‰Mð1ñ Ü 4 &Ð(`Ôan×asÑasÓauÐ`vÐ wÓxÐxá˜ÓÐr3   )N)NF)r!   re   r>   r$   rj   )r%   r%   ri   N)rÎ   )'Ú	functoolsr   r•   Úultralytics.utils.downloadsr   Úultralytics.utils.patchesr   Úmodules.decodersr   Úmodules.encodersr   r	   r
   r   r   r   Úmodules.memory_attentionr   r   Úmodules.samr   r   Úmodules.tiny_encoderr   Úmodules.transformerr   r   r1   r:   r@   rI   rX   r[   rb   rk   r/   rW   rÑ   rÙ   rË   r3   r   ú<module>rã      s·   ðõ ã å >Ý 0å )ß i× iß Kß ,Ý )Ý 2òóóóó	ó
ó
óó
ð$ ØóXðx Ø ØØ-Ø"7Ø &Ø%ØóXðx  ØØØ%ØØØØØØØØñ€ô r3   