
    |hU1                     &   d dl mZ d dl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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g ddg dg dddgg ddfdZ!eeeeeeeeeeeedZ"d dZ#y)!    )partialN)attempt_download_asset   )MaskDecoder)FpnNeckHieraImageEncoderImageEncoderViTMemoryEncoderPromptEncoder)MemoryAttentionMemoryAttentionLayer)	SAM2ModelSAMModel)TinyViT)TwoWayTransformerc                 &    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_indexes
checkpoint
_build_samr!   s    [/var/www/html/test/engine/venv/lib/python3.12/site-packages/ultralytics/models/sam/build.pybuild_sam_vit_hr&          $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"   r$   s    r%   build_sam_vit_lr/   "   r'   r(   c                 &    t        dddg d|       S )z_Build and return a Segment Anything Model (SAM) b-size model with specified encoder parameters.      )   r,      r-   r   r"   r$   s    r%   build_sam_vit_br5   -   s    $1 r(   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@  )r3   r3      r3   )r3      r,   
   NT)r   r   r   r    
mobile_samr!   r"   r$   s    r%   build_mobile_samr>   8   s!    -"'$( r(   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   r3   r   r3   r   )r,   r   	   r4   r;      r   r1   i     r@   r   encoder_stagesr   encoder_global_att_blocksencoder_window_specencoder_backbone_channel_listr!   _build_sam2r$   s    r%   build_sam2_trM   D   s$    #"+)&9 r(   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.r@   )r   r3   r-   r3   r   )r   r<      rB   rD   rF   rK   r$   s    r%   build_sam2_srP   Q   s$    $"-)&9 r(   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   )r3      r   rS   r3   )r2   r      rB   rC   )i  i     rR   )r   rG   r   rH   rI   encoder_window_spatial_sizerJ   r!   rK   r$   s    r%   build_sam2_brW   ^   s+    $".)%'H&:	 	r(   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.   r3   r:   $   r;   r3   )r   !   +   r4   r;   r   r4   i  i@  i   rY   rF   rK   r$   s    r%   build_sam2_lr`   l   s$    $".)&; r(   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      }|Ft        |      }t        |d      5 }t        j                  |      }ddd       |j                         |j                          |S # 1 sw Y   ,xY w)aT  
    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)
       r*   r   rS   i  )r   r   rC   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_decayr;   gư>)epsTrC   )depth	embed_dimrc   rj   
norm_layerrh   
patch_sizeqkv_biasuse_rel_posglobal_attn_indexeswindow_size	out_chans)rs   image_embedding_sizeinput_image_sizemask_in_chansr3   i   r4   )rr   embedding_dimmlp_dimrh   )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_stdNrb)r   r
   r   torchnn	LayerNormr   r   r   r   r   openloadload_state_dicteval)r   r   r   r    r!   r=   prompt_embed_dim
image_sizevit_patch_sizer{   r   samf
state_dicts                 r%   r#   r#   y   s[   4 JN%7$ ! 	( '&  #	
" 'uxx11t<'% ;&
% B #$&"68L!M(*5	
 !"#).	 - #
 -)+C. +J7
*d# 	'qAJ	'J'HHJJ		' 	's   <C==Dr   rZ   r3   r   r_   r   r^   c           
      X   t        t        | |||||      t        d|ddgd      d      }t        dd	d
t	                     }	t        d      }
|duxr d|v }t        d7i 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	d2d34      }|It        |      }t        |d5      5 }t        j                  |      d6   }ddd       |j                         |j                          |S # 1 sw Y   ,xY w)8ae  
    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()
    )rs   rh   stagesglobal_att_blocks!window_pos_embed_bkg_spatial_sizewindow_specrb   r3   rS   nearest)d_modelbackbone_channel_listfpn_top_down_levelsfpn_interp_modelr   )trunkneckscalpTr;   )r   pos_enc_at_input
num_layerslayerr7   )out_dimNzsam2.1r   memory_attentionmemory_encodernum_maskmemr   r   r*   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_threshr   model )r	   r   r   r   r   r   r   dictr   r   r   r   r   r   )r   rG   r   rH   rJ   rV   rI   r!   r   r   r   	is_sam2_1sam2r   r   s                  r%   rL   rL      s   < !''!7.I+
 "?!"A&	
 M" 'sTVW_s_uv"2.N$&A8z+AI !#!)! &! 	!
 ! #'! "'! .2! #'! &*! !%! $(! !%! "&! ,0!  !!" !#!$ %!& '+'!( )-)!* +!, -!. "&/!0 $1!2 '3!4 #,5!6 )27!8 %),0.2/3%
9!DF +J7
*d# 	0qAw/J	0Z(IIKK		0 	0s   D  D))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        j                         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keysendswithgetFileNotFoundError)ckptmodel_builderks      r%   	build_samr   G  sx    , Mt9D! 1==)--a0M1 4&(`anasasau`v wxxr(   )N)NF)r   )$	functoolsr   r   ultralytics.utils.downloadsr   modules.decodersr   modules.encodersr   r   r	   r
   r   r   modules.memory_attentionr   r   modules.samr   r   modules.tiny_encoderr   modules.transformerr   r&   r/   r5   r>   rM   rP   rW   r`   r#   rL   r   r   r   r(   r%   <module>r      s      > ) i i K , ) 2	


$ \@  -"7!"A%\@  % r(   