
    h4              	       @   S SK r S SKrS SKrS SKJr  S SKJr  S SKJr  S SK	J
r
  \R                  R                  \R                  R                  \
R                  S    S5      5      (       a  \" S5      eSS	S
SSSSSS.rSSS/ S4S jrSSS/ S4S jrS rS rS rS rg)    N)compose)instantiate)	OmegaConf)sam2r   a  You're likely running Python from the parent directory of the sam2 repository (i.e. the directory where https://github.com/facebookresearch/sam2 is cloned into). This is not supported since the `sam2` Python package could be shadowed by the repository name (the repository is also named `sam2` and contains the Python package in `sam2/sam2`). Please run Python from another directory (e.g. from the repo dir rather than its parent dir, or from your home directory) after installing SAM 2.)zconfigs/sam2/sam2_hiera_t.yamlzsam2_hiera_tiny.pt)zconfigs/sam2/sam2_hiera_s.yamlzsam2_hiera_small.pt)zconfigs/sam2/sam2_hiera_b+.yamlzsam2_hiera_base_plus.pt)zconfigs/sam2/sam2_hiera_l.yamlzsam2_hiera_large.pt)z"configs/sam2.1/sam2.1_hiera_t.yamlzsam2.1_hiera_tiny.pt)z"configs/sam2.1/sam2.1_hiera_s.yamlzsam2.1_hiera_small.pt)z#configs/sam2.1/sam2.1_hiera_b+.yamlzsam2.1_hiera_base_plus.pt)z"configs/sam2.1/sam2.1_hiera_l.yamlzsam2.1_hiera_large.pt)zfacebook/sam2-hiera-tinyzfacebook/sam2-hiera-smallzfacebook/sam2-hiera-base-pluszfacebook/sam2-hiera-largezfacebook/sam2.1-hiera-tinyzfacebook/sam2.1-hiera-smallzfacebook/sam2.1-hiera-base-pluszfacebook/sam2.1-hiera-largecudaevalTc                    U(       a  UR                  5       nU/ SQ-  n[        XS9n[        R                  " U5        [	        UR
                  SS9n[        X5        UR                  U5      nUS:X  a  UR                  5         U$ )N)H++model.sam_mask_decoder_extra_args.dynamic_multimask_via_stability=trueJ++model.sam_mask_decoder_extra_args.dynamic_multimask_stability_delta=0.05K++model.sam_mask_decoder_extra_args.dynamic_multimask_stability_thresh=0.98config_name	overridesT_recursive_r   )	copyr   r   resolver   model_load_checkpointtor   )	config_file	ckpt_pathdevicemodehydra_overrides_extraapply_postprocessingkwargscfgr   s	            X/home/james-whalen/.local/lib/python3.13/site-packages/torchao/_models/sam2/build_sam.py
build_sam2r    G   s{      5 : : < "
 	
 k
KCc		t4EU&HHVEv~

L    c                 4   S/nU(       a  UR                  5       nU/ SQ-  nUR                  U5        [        XS9n[        R                  " U5        [        UR                  SS9n	[        X5        U	R                  U5      n	US:X  a  U	R                  5         U	$ )NzM++model._target_=torchao._models.sam2.sam2_video_predictor.SAM2VideoPredictor)r
   r   r   z/++model.binarize_mask_from_pts_for_mem_enc=truez++model.fill_hole_area=8r   Tr   r   )
r   extendr   r   r   r   r   r   r   r   )
r   r   r   r   r   r   r   hydra_overridesr   r   s
             r   build_sam2_video_predictorr%   c   s     	XO  5 : : < 	"
 		
 01 k
ECc		t4EU&HHVEv~

Lr!   c                 6    SSK Jn  [        U    u  p#U" XS9nX$4$ )Nr   )hf_hub_download)repo_idfilename)huggingface_hubr'   HF_MODEL_ID_TO_FILENAMES)model_idr'   r   checkpoint_namer   s        r   _hf_downloadr.      s%    /#;H#E KKI!!r!   c                 6    [        U 5      u  p#[        SX#S.UD6$ N)r   r    )r.   r    r,   r   r   r   s       r   build_sam2_hfr3      s"    )(3KM+MfMMr!   c                 6    [        U 5      u  p#[        SX#S.UD6$ r0   )r.   r%   r2   s       r   build_sam2_video_predictor_hfr5      s,    )(3K% 8> r!   c                 ,   Ub  [         R                  " USSS9S   nU R                  U5      u  p4U(       a   [        R                  " U5        [        5       eU(       a   [        R                  " U5        [        5       e[        R                  " S5        g g )NcpuT)map_locationweights_onlyr   zLoaded checkpoint sucessfully)torchloadload_state_dictloggingerrorRuntimeErrorinfo)r   r   sdmissing_keysunexpected_keyss        r   r   r      sr    ZZ	DI'R(-(=(=b(A%MM,'. MM/*. 45 r!   )r=   osr:   hydrar   hydra.utilsr   	omegaconfr   torchao._modelsr   pathisdirjoin__path__r?   r+   r    r%   r.   r3   r5   r   r1   r!   r   <module>rM      s     	   #   
 77==dmmA.788
 	[ !"&"#$($;! L 	< 	"J"N

6r!   