Skip to content

Commit

Permalink
Fix github sam2 packaging for PyPI release
Browse files Browse the repository at this point in the history
Signed-off-by: Sachidanand Alle <[email protected]>
  • Loading branch information
SachidanandAlle committed Nov 8, 2024
1 parent a70344c commit 51007a8
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pythonapp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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__)'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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__)'
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,6 @@ In addition, you can find a table of the basic supported fields, modalities, vie
<tr>
</table>

> [**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)
Expand All @@ -185,7 +179,7 @@ In addition, you can find a table of the basic supported fields, modalities, vie

### Current Stable Version
<a href="https://pypi.org/project/monailabel/#history"><img alt="GitHub release (latest SemVer)" src="https://img.shields.io/github/v/release/project-monai/monailabel"></a>
<pre>pip install -U monailabel[all]</pre>
<pre>pip install -U monailabel</pre>

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)
Expand Down Expand Up @@ -219,6 +213,19 @@ To install the _**latest features**_ using one of the following options:
<pre>docker run --gpus all --rm -ti --ipc=host --net=host projectmonai/monailabel:latest bash</pre>
</details>

### 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.
<br/>
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

<h3>Radiology</h3>
Expand Down
19 changes: 18 additions & 1 deletion monailabel/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 2 additions & 9 deletions monailabel/sam2/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
7 changes: 2 additions & 5 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 51007a8

Please sign in to comment.