
    .h1                     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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)
TORCH_1_13   )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    Z/var/www/html/ai-service/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_lr0   #   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_br6   .   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@  )r4   r4      r4   )r4      r-   
   NT)r   r   r    r!   
mobile_samr"   r#   r%   s    r&   build_mobile_samr?   9   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   r4   r   r4   r   )r-   r   	   r5   r<      r   r2   i     rA   r   encoder_stagesr    encoder_global_att_blocksencoder_window_specencoder_backbone_channel_listr"   _build_sam2r%   s    r&   build_sam2_trN   E   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.rA   )r   r4   r.   r4   r   )r   r=      rC   rE   rG   rL   r%   s    r&   build_sam2_srQ   R   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   )r4      r   rT   r4   )r3   r      rC   rD   )i  i     rS   )r   rH   r    rI   rJ   encoder_window_spatial_sizerK   r"   rL   r%   s    r&   build_sam2_brX   _   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.   r4   r;   $   r<   r4   )r   !   +   r5   r<   r   r5   i  i@  i   rZ   rG   rL   r%   s    r&   build_sam2_lra   m   s$    $".)&; r)   c                 L   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      }|ct        |      }t        |d      5 }t        rt        j                  |d	      n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   rT   i  )r   r   rD   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ư>)epsTrD   )depth	embed_dimrd   rk   
norm_layerri   
patch_sizeqkv_biasuse_rel_posglobal_attn_indexeswindow_size	out_chans)rt   image_embedding_sizeinput_image_sizemask_in_chansr4   i   r5   )rs   embedding_dimmlp_dimri   )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weights_only)r   r   r   torchnn	LayerNormr   r   r   r   r   openr   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$   z   so   4 JN%7$ ! 	( '&  #	
" 'uxx11t<'% ;&
% B #$&"68L!M(*5	
 !"#).	 - #
 -)+C. +J7
*d# 	\q>HAE:ejjYZmJ	\J'HHJJ		\ 	\s   <3DD#r   r[   r4   r   r`   r   r_   c           
         t        t        | |||||      t        d|ddgd      d      }t        dd	d
t	                     }	t        d      }
|duxr d|v }t        d8i 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      }|ft        |      }t        |d5      5 }t        rt        j                  |d-6      nt        j                  |      d7   }ddd       |j                         |j                          |S # 1 sw Y   ,xY w)9ae  
    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()
    )rt   ri   stagesglobal_att_blocks!window_pos_embed_bkg_spatial_sizewindow_specrc   r4   rT   nearest)d_modelbackbone_channel_listfpn_top_down_levelsfpn_interp_modelr   )trunkneckscalpTr<   )r   pos_enc_at_input
num_layerslayerr8   )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   r   model )r
   r	   r   r   r   r   r   dictr   r   r   r   r   r   r   )r   rH   r    rI   rK   rW   rJ   r"   r   r   r   	is_sam2_1sam2r   r   s                  r&   rM   rM      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# 	gq?I%**QU;uzzZ[}^efJ	gZ(IIKK		g 	gs   6D==E)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   H  sx    , Mt9D! 1==)--a0M1 4&(`anasasau`v wxxr)   )N)NF)r   )&	functoolsr   r   ultralytics.utils.downloadsr   ultralytics.utils.torch_utilsr   modules.decodersr   modules.encodersr   r	   r
   r   r   r   modules.memory_attentionr   r   modules.samr   r   modules.tiny_encoderr   modules.transformerr   r'   r0   r6   r?   rN   rQ   rX   ra   r$   rM   r   r   r   r)   r&   <module>r      s      > 4 ) i i K , ) 2	


$ \@  -"7!"A%\@  % r)   