a
    yfy/                  	   @   s0  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dZd,ddZd-ddZd.ddZd/ddZd0ddZd1ddZd2ddZd3ddZ dg dd g d!g d"d#d#gg d$dfd%d&Z!eeeeeeeed'Z"d4d)d*Z#dS )5    )partialN)attempt_download_asset   )MaskDecoder)FpnNeckHieraImageEncoderImageEncoderViTMemoryEncoderPromptEncoder)MemoryAttentionMemoryAttentionLayer)	SAM2ModelSAMModel)TinyViT)TwoWayTransformerc                 C   s   t dddg d| dS )zaBuilds and returns 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    r#   X/var/www/html/django/DPS/env/lib/python3.9/site-packages/ultralytics/models/sam/build.pybuild_sam_vit_h   s    r%   c                 C   s   t dddg d| dS )zaBuilds and returns a Segment Anything Model (SAM) l-size model with specified encoder parameters.      r   )         r   r   r    r"   r#   r#   r$   build_sam_vit_l"   s    r+   c                 C   s   t dddg d| dS )zgConstructs and returns a Segment Anything Model (SAM) with b-size architecture and optional checkpoint.      )   r(      r)   r   r    r"   r#   r#   r$   build_sam_vit_b-   s    r0   c                 C   s    t g dg dg ddd| dS )zaBuilds and returns a Mobile Segment Anything Model (Mobile-SAM) for efficient image segmentation.)@         i@  )r.   r.      r.   )r.      r(   
   NT)r   r   r   r   
mobile_samr   r    r"   r#   r#   r$   build_mobile_sam8   s    r8   c              	   C   s&   t dg ddg dg dg d| dS )zlBuilds and returns a Segment Anything Model 2 (SAM2) tiny-size model with specified architecture parameters.`   )r   r.   r   r.   r   )r(   r   	   r/   r5      r   r,   i     r9   r   encoder_stagesr   encoder_global_att_blocksencoder_window_specencoder_backbone_channel_listr   _build_sam2r"   r#   r#   r$   build_sam2_tD   s    rF   c              	   C   s&   t dg ddg dg dg d| dS )zeBuilds and returns a small-size Segment Anything Model (SAM2) with specified architecture parameters.r9   )r   r.   r)   r.   r   )r   r6      r;   r=   r?   rD   r"   r#   r#   r$   build_sam2_sQ   s    rH   c              
   C   s,   t dg ddg dg dddgg d| dS )	zQBuilds and returns a SAM2 base-size model with specified architecture parameters.p   )r.      r   rJ   r.   )r-   r      r;   r<   )i  i     rI   )r   r@   r   rA   rB   encoder_window_spatial_sizerC   r   rD   r"   r#   r#   r$   build_sam2_b^   s    rN   c              	   C   s&   t dg ddg dg dg d| dS )zeBuilds and returns a large-size Segment Anything Model (SAM2) with specified architecture parameters.   r.   r4   $   r5   r.   )r   !   +   r/   r5   r   r/   i  i@  i   rO   r?   rD   r"   r#   r#   r$   build_sam2_ll   s    rV   Fc                 C   s  d}d}d}|| }	|r@t ddd| ||g ddddd	ddd
dn*t|| |dttjjdd||dd|d|d}
t|
t||	|	f||fddtdt	d|ddd|dddg dg dd}|durt
|}t|d}t|}W d   n1 s0    Y  || |  |S )aA  
    Builds 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): Path to the model checkpoint file.
        mobile_sam (bool): 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   rJ   i  )r   r   r<   r   g      @g        Fg?)img_sizeZin_chansZnum_classesZ
embed_dimsZdepths	num_headsZwindow_sizes	mlp_ratioZ	drop_rateZdrop_path_rateZuse_checkpointZmbconv_expand_ratioZlocal_conv_sizeZlayer_lr_decayr5   gư>)ZepsTr<   )depth	embed_dimrX   rZ   Z
norm_layerrY   Z
patch_sizeZqkv_biasZuse_rel_posZglobal_attn_indexesZwindow_sizeZ	out_chans)r\   image_embedding_sizeZinput_image_sizeZmask_in_chansr.   i   r/   )r[   Zembedding_dimZmlp_dimrY   )Znum_multimask_outputsZtransformerZtransformer_dimZiou_head_depthZiou_head_hidden_dim)g33333^@gR]@gRY@)g(\2M@g(\L@g     L@)image_encoderZprompt_encoderZmask_decoderZ
pixel_meanZ	pixel_stdNrb)r   r	   r   torchnnZ	LayerNormr   r   r   r   r   openloadload_state_dicteval)r   r   r   r   r   r7   Zprompt_embed_dim
image_sizeZvit_patch_sizer]   r^   Zsamf
state_dictr#   r#   r$   r!   y   s    !
(
r!   r   rP   r.   r   rU   r   rT   c                 C   s   t t| |||||dtd|ddgdddd}tdd	d
t d}	tdd}
t||	|
ddddd	d	d	d	d	d	d	d	d	d	d	d	d	ddd	dtd	dddd}|durt|}t	|d}t
|d }W d   n1 s0    Y  || |  |S )a  
    Builds and returns a Segment Anything Model 2 (SAM2) with specified architecture parameters.

    Args:
        encoder_embed_dim (int): Embedding dimension for the encoder.
        encoder_stages (List[int]): Number of blocks in each stage of the encoder.
        encoder_num_heads (int): Number of attention heads in the encoder.
        encoder_global_att_blocks (List[int]): Indices of global attention blocks in the encoder.
        encoder_backbone_channel_list (List[int]): Channel dimensions for each level of the encoder backbone.
        encoder_window_spatial_size (List[int]): Spatial size of the window for position embeddings.
        encoder_window_spec (List[int]): Window specifications for each stage of the encoder.
        checkpoint (str | None): 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\   rY   ZstagesZglobal_att_blocksZ!window_pos_embed_bkg_spatial_sizeZwindow_specrW   r.   rJ   Znearest)d_modelZbackbone_channel_listZfpn_top_down_levelsZfpn_interp_modelr   )ZtrunkZneckZscalpTr5   )ri   Zpos_enc_at_inputZ
num_layerslayerr1   )Zout_dimr   r&   g      4@g      $r   Fg?g\(\?)Zdynamic_multimask_via_stabilityZ!dynamic_multimask_stability_deltaZ"dynamic_multimask_stability_thresh)r^   memory_attentionmemory_encoderZnum_maskmemrf   Zsigmoid_scale_for_mem_encZsigmoid_bias_for_mem_encZ$use_mask_input_as_output_without_samZdirectly_add_no_mem_embedZuse_high_res_features_in_samZmultimask_output_in_samZiou_prediction_use_sigmoidZuse_obj_ptrs_in_encoderZadd_tpos_enc_to_obj_ptrsZ"only_obj_ptrs_in_the_past_for_evalZpred_obj_scoresZpred_obj_scores_mlpZfixed_no_obj_ptrZmultimask_output_for_trackingZuse_multimask_token_for_obj_ptrZmultimask_min_pt_numZmultimask_max_pt_numZuse_mlp_for_obj_ptr_projZcompile_image_encoderZsam_mask_decoder_extra_argsNr_   model)r   r   r   r   r   r
   r   dictr   rb   r`   rc   rd   re   )r   r@   r   rA   rC   rM   rB   r   r^   rk   rl   Zsam2rg   rh   r#   r#   r$   rE      sr    
 ,
rE   )zsam_h.ptzsam_l.ptsam_b.ptzmobile_sam.ptz	sam2_t.ptz	sam2_s.ptz	sam2_b.ptz	sam2_l.ptro   c                 C   sP   d}t | } t D ]}| |rt|}q|sHt|  dt  || S )a  
    Builds and returns a Segment Anything Model (SAM) based on the provided checkpoint.

    Args:
        ckpt (str | Path): 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)ZckptZmodel_builderkr#   r#   r$   	build_sam?  s    
rw   )N)N)N)N)N)N)N)N)NF)ro   )$	functoolsr   r`   Zultralytics.utils.downloadsr   Zmodules.decodersr   Zmodules.encodersr   r   r   r	   r
   r   Zmodules.memory_attentionr   r   Zmodules.samr   r   Zmodules.tiny_encoderr   Zmodules.transformerr   r%   r+   r0   r8   rF   rH   rN   rV   r!   rE   rq   rw   r#   r#   r#   r$   <module>	   sL    







  
`
\