o
    i4                  	   @   s   d dl Z d dl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
 ejeje
jd  dr6eddd	d
ddddddZdddg dfddZdddg dfddZdd Zdd Zdd Zdd ZdS )     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           	      K   sd   |r|  }|g d7 }t| |d}t| t|jdd}t|| ||}|dkr0|  |S )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    r   R/home/ubuntu/.local/lib/python3.10/site-packages/torchao/_models/sam2/build_sam.py
build_sam2G   s   	


r   c           
      K   st   dg}|r|  }|g d7 }|| t| |d}t| t|jdd}	t|	| |	|}	|dkr8|		  |	S )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   r   r   r   build_sam2_video_predictorc   s   





r"   c                 C   s,   ddl m} t|  \}}|| |d}||fS )Nr   )hf_hub_download)repo_idfilename)huggingface_hubr#   HF_MODEL_ID_TO_FILENAMES)model_idr#   r   checkpoint_namer   r   r   r   _hf_download   s   r*   c                 K       t | \}}td||d|S N)r   r   r   )r*   r   r(   r   r   r   r   r   r   build_sam2_hf   s   r.   c                 K   r+   r,   )r*   r"   r-   r   r   r   build_sam2_video_predictor_hf   s   r/   c                 C   sd   |d ur0t j|dddd }| |\}}|rt| t |r)t| t td d S d S )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_keysr   r   r   r      s   

r   )r6   osr3   hydrar   hydra.utilsr   	omegaconfr   torchao._modelsr   pathisdirjoin__path__r8   r'   r   r"   r*   r.   r/   r   r   r   r   r   <module>   sH   &

%