From e06b02860d9ad3a1ca695ada4860203d48acbccf Mon Sep 17 00:00:00 2001 From: Ronghang Hu Date: Sat, 5 Oct 2024 00:03:56 -0700 Subject: [PATCH] [doc] Check and raise an error if the user is running Python from the parent directory of the sam2 repo If the user has "sam2/sam2" in their path, they are likey importing the repo itself as "sam2" rather than importing the "sam2" python package (i.e. "sam2/sam2" directory). This typically happens because the user is running Python from the parent directory that contains the sam2 repo they cloned. In general, the user should not run Python from the parent dir when the repo is cloned into (same is true for e.g. Numpy repo that contains names like `numpy/numpy` where the module and the repo have the same name. --- README.md | 4 ++-- sam2/build_sam.py | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 81fdc4a2..65654f5a 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,9 @@ SAM 2 needs to be installed first before use. The code requires `python>=3.10`, as well as `torch>=2.3.1` and `torchvision>=0.18.1`. Please follow the instructions [here](https://pytorch.org/get-started/locally/) to install both PyTorch and TorchVision dependencies. You can install SAM 2 on a GPU machine using: ```bash -git clone https://github.com/facebookresearch/sam2.git +git clone https://github.com/facebookresearch/sam2.git && cd sam2 -cd sam2 & pip install -e . +pip install -e . ``` If you are installing on Windows, it's strongly recommended to use [Windows Subsystem for Linux (WSL)](https://learn.microsoft.com/en-us/windows/wsl/install) with Ubuntu. diff --git a/sam2/build_sam.py b/sam2/build_sam.py index 7346adc0..7cfc4513 100644 --- a/sam2/build_sam.py +++ b/sam2/build_sam.py @@ -5,12 +5,33 @@ # LICENSE file in the root directory of this source tree. import logging +import os import torch from hydra import compose from hydra.utils import instantiate from omegaconf import OmegaConf +import sam2 + +# Check if the user is running Python from the parent directory of the sam2 repo +# (i.e. the directory where this repo is cloned into) -- this is not supported since +# it could shadow the sam2 package and cause issues. +if os.path.isdir(os.path.join(sam2.__path__[0], "sam2")): + # If the user has "sam2/sam2" in their path, they are likey importing the repo itself + # as "sam2" rather than importing the "sam2" python package (i.e. "sam2/sam2" directory). + # This typically happens because the user is running Python from the parent directory + # that contains the sam2 repo they cloned. + raise RuntimeError( + "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." + ) + + HF_MODEL_ID_TO_FILENAMES = { "facebook/sam2-hiera-tiny": ( "configs/sam2/sam2_hiera_t.yaml",