mirror of
				https://github.com/facebookresearch/sam2.git
				synced 2025-11-04 19:42:12 +08:00 
			
		
		
		
	First draft
This commit is contained in:
		
							parent
							
								
									57bc94b739
								
							
						
					
					
						commit
						b72a8a97f0
					
				@ -11,6 +11,8 @@ from hydra import compose
 | 
				
			|||||||
from hydra.utils import instantiate
 | 
					from hydra.utils import instantiate
 | 
				
			||||||
from omegaconf import OmegaConf
 | 
					from omegaconf import OmegaConf
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from huggingface_hub import hf_hub_download
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def build_sam2(
 | 
					def build_sam2(
 | 
				
			||||||
    config_file,
 | 
					    config_file,
 | 
				
			||||||
@ -76,6 +78,18 @@ def build_sam2_video_predictor(
 | 
				
			|||||||
    return model
 | 
					    return model
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def build_sam2_hf(model_id, **kwargs):
 | 
				
			||||||
 | 
					    config_file = hf_hub_download(repo_id=model_id, filename=f"{model_id}.yaml")
 | 
				
			||||||
 | 
					    ckpt_path = hf_hub_download(repo_id=model_id, filename=f"{model_id}.pt")
 | 
				
			||||||
 | 
					    return build_sam2_video_predictor(config_file=config_file, ckpt_path=ckpt_path, **kwargs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def build_sam2_video_predictor_hf(model_id, **kwargs):
 | 
				
			||||||
 | 
					    config_file = hf_hub_download(repo_id=model_id, filename=f"{model_id}.yaml")
 | 
				
			||||||
 | 
					    ckpt_path = hf_hub_download(repo_id=model_id, filename=f"{model_id}.pt")
 | 
				
			||||||
 | 
					    return build_sam2_video_predictor(config_file=config_file, ckpt_path=ckpt_path, **kwargs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def _load_checkpoint(model, ckpt_path):
 | 
					def _load_checkpoint(model, ckpt_path):
 | 
				
			||||||
    if ckpt_path is not None:
 | 
					    if ckpt_path is not None:
 | 
				
			||||||
        sd = torch.load(ckpt_path, map_location="cpu")["model"]
 | 
					        sd = torch.load(ckpt_path, map_location="cpu")["model"]
 | 
				
			||||||
@ -86,4 +100,4 @@ def _load_checkpoint(model, ckpt_path):
 | 
				
			|||||||
        if unexpected_keys:
 | 
					        if unexpected_keys:
 | 
				
			||||||
            logging.error(unexpected_keys)
 | 
					            logging.error(unexpected_keys)
 | 
				
			||||||
            raise RuntimeError()
 | 
					            raise RuntimeError()
 | 
				
			||||||
        logging.info("Loaded checkpoint sucessfully")
 | 
					        logging.info("Loaded checkpoint sucessfully")
 | 
				
			||||||
@ -13,7 +13,7 @@ import torch
 | 
				
			|||||||
from PIL.Image import Image
 | 
					from PIL.Image import Image
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from sam2.modeling.sam2_base import SAM2Base
 | 
					from sam2.modeling.sam2_base import SAM2Base
 | 
				
			||||||
 | 
					from sam2.build_sam import build_sam2_hf
 | 
				
			||||||
from sam2.utils.transforms import SAM2Transforms
 | 
					from sam2.utils.transforms import SAM2Transforms
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -62,6 +62,20 @@ class SAM2ImagePredictor:
 | 
				
			|||||||
            (64, 64),
 | 
					            (64, 64),
 | 
				
			||||||
        ]
 | 
					        ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def from_pretrained(model_id: str, **kwargs) -> "SAM2ImagePredictor":
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        Load a pretrained model from the Hugging Face model hub.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Arguments:
 | 
				
			||||||
 | 
					          model_id (str): The Hugging Face repository ID.
 | 
				
			||||||
 | 
					          **kwargs: Additional arguments to pass to the model constructor.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Returns:
 | 
				
			||||||
 | 
					          (SAM2ImagePredictor): The loaded model.
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        sam_model = build_sam2_hf(model_id, **kwargs)
 | 
				
			||||||
 | 
					        return SAM2ImagePredictor(sam_model)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @torch.no_grad()
 | 
					    @torch.no_grad()
 | 
				
			||||||
    def set_image(
 | 
					    def set_image(
 | 
				
			||||||
        self,
 | 
					        self,
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user