Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hydra initialization fix #188

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion sam2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
# LICENSE file in the root directory of this source tree.

from hydra import initialize_config_module
from hydra.core.global_hydra import GlobalHydra

initialize_config_module("sam2_configs", version_base="1.2")
if not GlobalHydra().is_initialized():
initialize_config_module("sam2_configs", version_base="1.2")
4 changes: 3 additions & 1 deletion sam2/build_sam.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def build_sam2(

def build_sam2_video_predictor(
config_file,
config_dir=None,
ckpt_path=None,
device="cuda",
mode="eval",
Expand All @@ -66,7 +67,8 @@ def build_sam2_video_predictor(
hydra_overrides.extend(hydra_overrides_extra)

# Read config and init model
cfg = compose(config_name=config_file, overrides=hydra_overrides)
config_name = f'{config_dir}/{config_file}' if config_dir is not None else config_file
cfg = compose(config_name=config_name, overrides=hydra_overrides)
OmegaConf.resolve(cfg)
model = instantiate(cfg.model, _recursive_=True)
_load_checkpoint(model, ckpt_path)
Expand Down
2 changes: 1 addition & 1 deletion sam2/sam2_image_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def from_pretrained(cls, model_id: str, **kwargs) -> "SAM2ImagePredictor":
from sam2.build_sam import build_sam2_hf

sam_model = build_sam2_hf(model_id, **kwargs)
return cls(sam_model)
return sam_model

@torch.no_grad()
def set_image(
Expand Down
2 changes: 1 addition & 1 deletion sam2/sam2_video_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def from_pretrained(cls, model_id: str, **kwargs) -> "SAM2VideoPredictor":
from sam2.build_sam import build_sam2_video_predictor_hf

sam_model = build_sam2_video_predictor_hf(model_id, **kwargs)
return cls(sam_model)
return sam_model

def _obj_id_to_idx(self, inference_state, obj_id):
"""Map client-side object id to model-side object index."""
Expand Down