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 not initializing - nested files causing install issue #346

Closed
nmbphys opened this issue Oct 1, 2024 · 5 comments · Fixed by #359
Closed

Hydra not initializing - nested files causing install issue #346

nmbphys opened this issue Oct 1, 2024 · 5 comments · Fixed by #359

Comments

@nmbphys
Copy link

nmbphys commented Oct 1, 2024

This isn't a bug per se, but I ran into some errors with the exact location of the package directory. I cloned the repo today and followed the installation instructions in a conda environment (within PyCharm), then tried to create a SAM2ImagePredictor as in one of the examples. First, I got errors that hydra wasn't initialized. The init.py file wasn't even getting executed. When I initialized hydra manually, I then got the 'MissingConfigException' error.

I ended up having to copy the nested sam2 directory (sam2/sam2) into the site_packages directly so that it could actually find the package properly. I haven't worked directly with Python packages for a while, so maybe this is a well-known thing, but it might be worth a mentioning in INSTALL.md as a possible issue.

@ronghanghu
Copy link
Contributor

Hi @nmbphys, thanks for reporting this! I think this is likely due to having a previous installation under a different site package path (installed without using the -e option in pip install), and should be able to resolve by uninstalling the previous version via pip uninstall SAM-2 and the reinstalling the repo again.

@danigarciaoca
Copy link

danigarciaoca commented Oct 4, 2024

Same problem here, when installin in editable mode I get the following error:

AssertionError: GlobalHydra is not initialized, use @hydra.main() or call one of the hydra initialization methods first

If I rename sam2 to old segment-anything-2 and reinstall it, it works. Any suggestion?

PS: I tried pip uninstall SAM-2 and the reinstalling the repo again but didn't work

Steps to reproduce:

  1. Create a conda environment with the following environment.yml:

name: sam2
channels:
- pytorch
- nvidia
dependencies:
- python=3.10
- pip=24.2
- pytorch=2.4.0
- torchvision=0.19.0
- pytorch-cuda=11.8

  1. Clone sam2 repo and install it:
conda activate sam2
pip install -e ./sam2
  1. Try to load SAM2 with a minimal script
import torch
from sam2.build_sam import build_sam2
from sam2.sam2_image_predictor import SAM2ImagePredictor

checkpoint = "./checkpoints/sam2.1_hiera_large.pt"
model_cfg = "configs/sam2.1/sam2.1_hiera_l.yaml"
predictor = SAM2ImagePredictor(build_sam2(model_cfg, checkpoint))

Output:

  File "/<my_path>/solo-sam/test.py", line 7, in <module>
    predictor = SAM2ImagePredictor(build_sam2(model_cfg, checkpoint))
  File "/<my_path>/solo-sam/sam2/sam2/build_sam.py", line 69, in build_sam2
    cfg = compose(config_name=config_file, overrides=hydra_overrides_extra)
  File "/<my_conda_env_path>/lib/python3.10/site-packages/hydra/compose.py", line 32, in compose
    assert (
AssertionError: GlobalHydra is not initialized, use @hydra.main() or call one of the hydra initialization methods first

@ronghanghu
Copy link
Contributor

ronghanghu commented Oct 4, 2024

Hi @danigarciaoca regarding this observation

AssertionError: GlobalHydra is not initialized, use @hydra.main() or call one of the hydra initialization methods first

I think it's likely because you're running the test in your Step 3 directly in the parent directory of the repo (i.e. the directory where this repo https://github.com/facebookresearch/sam2 is cloned into). If you move to another directory like your home directory, would the test work?

@danigarciaoca
Copy link

danigarciaoca commented Oct 4, 2024

Hi @ronghanghu, just tested from outside and it works. Thanks!

So the question now is, why is it not working when run from the parent directory of the repo?

  • Isn't the import supposed to import sam2 dependency from the installed package and not from sam2 folder (as it is not a package itself)?

I don't really get why is this AsserionError rising when running from the parent directory.

EDIT 1:

I suppose that Getting Started instructions will lead to this same issue to everyone testing SAM-2 following the proposed order, won't they?

EDIT 2:

(from ignorance) Perhaps GlobalHydra initialization should be in another place other than __init__.py? as it is the "source" of the problem; to avoid this situation.

@ronghanghu
Copy link
Contributor

Hi @danigarciaoca, in general it's often not advised to run Python directly from the parent dir where the repo is cloned into, since the repo dir could shadow the Python package dir if they have the same name. (Same is true for many common Python repos like https://github.com/numpy/numpy).

I added a check in #359 to raise a more informative error in this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants