From 51007a8838c65992d264b7145707f312d7926f8e Mon Sep 17 00:00:00 2001 From: Sachidanand Alle Date: Fri, 8 Nov 2024 07:33:15 +0000 Subject: [PATCH] Fix github sam2 packaging for PyPI release Signed-off-by: Sachidanand Alle --- .github/workflows/pythonapp.yml | 2 +- .github/workflows/release.yml | 2 +- Dockerfile | 2 +- README.md | 21 ++++++++++++++------- monailabel/config.py | 19 ++++++++++++++++++- monailabel/sam2/infer.py | 11 ++--------- setup.cfg | 7 ++----- 7 files changed, 39 insertions(+), 25 deletions(-) diff --git a/.github/workflows/pythonapp.yml b/.github/workflows/pythonapp.yml index a3be67993..0ffe72520 100644 --- a/.github/workflows/pythonapp.yml +++ b/.github/workflows/pythonapp.yml @@ -128,7 +128,7 @@ jobs: rm -r build monailabel*.egg-info # install from wheel - python -m pip install `ls "$tmp_dir"/monailabel*.whl`[all] + python -m pip install "$tmp_dir"/monailabel*.whl python -c 'import monailabel; monailabel.print_config()' 2>&1 | grep -iv "unknown" python -c 'import monailabel; print(monailabel.__file__)' diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b4ea62c7d..00dc0aa11 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -65,7 +65,7 @@ jobs: rm -r build monailabel*.egg-info # install from wheel - python -m pip install `ls "$tmp_dir"/monailabel*.whl`[all] + python -m pip install "$tmp_dir"/monailabel*.whl python -c 'import monailabel; monailabel.print_config()' 2>&1 | grep -iv "unknown" python -c 'import monailabel; print(monailabel.__file__)' diff --git a/Dockerfile b/Dockerfile index c7d160d30..f47e9137a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -39,4 +39,4 @@ WORKDIR /opt/monailabel RUN apt update -y && apt install -y git curl openslide-tools python3 python-is-python3 python3-pip RUN python -m pip install --no-cache-dir pytest torch torchvision torchaudio COPY --from=build /opt/monailabel/dist/monailabel* /opt/monailabel/dist/ -RUN python -m pip install --no-cache-dir `ls /opt/monailabel/dist/monailabel*.whl`[all] +RUN python -m pip install --no-cache-dir /opt/monailabel/dist/monailabel*.whl diff --git a/README.md b/README.md index 89ea91093..d58360dd2 100644 --- a/README.md +++ b/README.md @@ -167,12 +167,6 @@ In addition, you can find a table of the basic supported fields, modalities, vie -> [**SAM2**](https://github.com/facebookresearch/sam2/) -> -> By default, SAM2 is included for all the above Apps only when **_python >= 3.10_** -> - **sam_2d**: for any organ or tissue and others over a given slice/2D image. -> - **sam_3d**: to support SAM2 propagation over multiple slices (Radiology/MONAI-Bundle). - # Getting Started with MONAI Label ### MONAI Label requires a few steps to get started: - Step 1: [Install MONAI Label](#step-1-installation) @@ -185,7 +179,7 @@ In addition, you can find a table of the basic supported fields, modalities, vie ### Current Stable Version GitHub release (latest SemVer) -
pip install -U monailabel[all]
+
pip install -U monailabel
MONAI Label supports the following OS with **GPU/CUDA** enabled. For more details instruction, please see the installation guides. - [Ubuntu](https://docs.monai.io/projects/label/en/latest/installation.html) @@ -219,6 +213,19 @@ To install the _**latest features**_ using one of the following options:
docker run --gpus all --rm -ti --ipc=host --net=host projectmonai/monailabel:latest bash
+### SAM-2 + +> By default, [**SAM2**](https://github.com/facebookresearch/sam2/) model is included for all the Apps when **_python >= 3.10_** +> - **sam_2d**: for any organ or tissue and others over a given slice/2D image. +> - **sam_3d**: to support SAM2 propagation over multiple slices (Radiology/MONAI-Bundle). + +If you are using `pip install monailabel` by default it uses [SAM-2](https://huggingface.co/facebook/sam2-hiera-large) models. +
+To use [SAM-2.1](https://huggingface.co/facebook/sam2.1-hiera-large) use one of following options. + - Use monailabel [Docker](https://hub.docker.com/r/projectmonai/monailabel) instead of pip package + - Run monailabel in dev mode (git checkout) + - If you have installed monailabel via pip then uninstall **_sam2_** package `pip uninstall sam2` and then run `pip install -r requirements.txt` or install latest **SAM-2** from it's [github](https://github.com/facebookresearch/sam2/tree/main?tab=readme-ov-file#installation). + ## Step 2 MONAI Label Sample Applications

Radiology

diff --git a/monailabel/config.py b/monailabel/config.py index bf195c7bc..cb0d36c40 100644 --- a/monailabel/config.py +++ b/monailabel/config.py @@ -8,14 +8,18 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - import os +from importlib.util import find_spec from typing import Any, Dict, List, Optional from pydantic import AnyHttpUrl from pydantic_settings import BaseSettings, SettingsConfigDict +def is_package_installed(name): + return False if find_spec(name) is None else True + + class Settings(BaseSettings): MONAI_LABEL_API_STR: str = "" MONAI_LABEL_PROJECT_NAME: str = "MONAILabel" @@ -98,6 +102,19 @@ class Settings(BaseSettings): MONAI_ZOO_REPO: str = "Project-MONAI/model-zoo/hosting_storage_v1" MONAI_ZOO_AUTH_TOKEN: str = "" + # Refer: https://github.com/facebookresearch/sam2?tab=readme-ov-file#model-description + # Refer: https://huggingface.co/facebook/sam2-hiera-large + MONAI_SAM_MODEL_PT = ( + "https://huggingface.co/facebook/sam2.1-hiera-large/resolve/main/sam2.1_hiera_large.pt" + if is_package_installed("SAM-2") + else "https://huggingface.co/facebook/sam2-hiera-large/resolve/main/sam2_hiera_large.pt" + ) + MONAI_SAM_MODEL_CFG = ( + "https://huggingface.co/facebook/sam2.1-hiera-large/resolve/main/sam2.1_hiera_l.yaml" + if is_package_installed("SAM-2") + else "https://huggingface.co/facebook/sam2-hiera-large/resolve/main/sam2_hiera_l.yaml" + ) + model_config = SettingsConfigDict( env_file=".env", case_sensitive=True, diff --git a/monailabel/sam2/infer.py b/monailabel/sam2/infer.py index a22bbab89..1ddca8ed3 100644 --- a/monailabel/sam2/infer.py +++ b/monailabel/sam2/infer.py @@ -116,15 +116,8 @@ def __init__( self._config.update(config) # Download PreTrained Model - # https://github.com/facebookresearch/sam2?tab=readme-ov-file#model-description - # https://huggingface.co/facebook/sam2-hiera-large - - pt_url = "https://huggingface.co/facebook/sam2.1-hiera-large/resolve/main/sam2.1_hiera_large.pt" - conf_url = "https://huggingface.co/facebook/sam2.1-hiera-large/resolve/main/sam2.1_hiera_l.yaml" - - # pt_url = "https://huggingface.co/facebook/sam2-hiera-large/resolve/main/sam2_hiera_large.pt" - # conf_url = "https://huggingface.co/facebook/sam2-hiera-large/resolve/main/sam2_hiera_l.yaml" - + pt_url = settings.MONAI_SAM_MODEL_PT + conf_url = settings.MONAI_SAM_MODEL_CFG sam_pt = pt_url.split("/")[-1] sam_conf = conf_url.split("/")[-1] diff --git a/setup.cfg b/setup.cfg index 1afbf0390..83b3d77e0 100644 --- a/setup.cfg +++ b/setup.cfg @@ -70,11 +70,8 @@ install_requires = scikit-learn scipy google-auth>=2.29.0 - -[options.extras_require] -all = - SAM-2 @ git+https://github.com/facebookresearch/sam2.git@c2ec8e14a185632b0a5d8b161928ceb50197eddc ; python_version >= '3.10' - #sam2>=0.4.1; python_version >= '3.10' + sam2>=0.4.1; python_version >= '3.10' + #SAM-2 @ git+https://github.com/facebookresearch/sam2.git@c2ec8e14a185632b0a5d8b161928ceb50197eddc ; python_version >= '3.10' [flake8] select = B,C,E,F,N,P,T4,W,B9