From e440070b5016ac0377d1ce872e128627c6c32664 Mon Sep 17 00:00:00 2001 From: luca-medeiros Date: Wed, 16 Oct 2024 10:32:13 +0900 Subject: [PATCH 1/6] hatchling to setuptools --- pyproject.toml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 2a6d97d..5bae57b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,9 +18,12 @@ dynamic = ["dependencies"] dependencies = {file = ["requirements.txt"]} [build-system] -requires = ["hatchling"] -build-backend = "hatchling.build" +requires = ["setuptools", "wheel"] +build-backend = "setuptools.build_meta" +[tool.setuptools] +packages = ["lang_sam"] +include_package_data = true [tool.ruff] target-version = "py311" From dd39bc5e7339af282797fd40105e361bc0a9457d Mon Sep 17 00:00:00 2001 From: luca-medeiros Date: Wed, 16 Oct 2024 10:35:38 +0900 Subject: [PATCH 2/6] precommit --- .dockerignore | 2 +- .github/ISSUE_TEMPLATE/bugs.yaml | 8 ++++---- .github/ISSUE_TEMPLATE/documentation.yaml | 2 +- .github/ISSUE_TEMPLATE/feature-request.yaml | 4 ++-- .pre-commit-config.yaml | 2 +- Dockerfile | 2 +- README.md | 2 -- lang_sam/lang_sam.py | 3 +-- lang_sam/models/gdino.py | 2 +- lang_sam/models/sam.py | 1 + lang_sam/models/utils.py | 1 + lang_sam/server.py | 14 +++++++------- requirements.txt | 2 +- 13 files changed, 22 insertions(+), 23 deletions(-) diff --git a/.dockerignore b/.dockerignore index b694934..1d17dae 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1 +1 @@ -.venv \ No newline at end of file +.venv diff --git a/.github/ISSUE_TEMPLATE/bugs.yaml b/.github/ISSUE_TEMPLATE/bugs.yaml index acead36..21b736a 100644 --- a/.github/ISSUE_TEMPLATE/bugs.yaml +++ b/.github/ISSUE_TEMPLATE/bugs.yaml @@ -7,12 +7,12 @@ body: attributes: value: | ## Instructions To Reproduce the 🐛 Bug: - + 1. Background explanation - type: textarea attributes: - label: Full runnable code or full changes you made: + label: "Full runnable code or full changes you made:" description: Please provide the code or changes that led to the bug. placeholder: | ``` @@ -22,7 +22,7 @@ body: - type: textarea attributes: - label: What exact command you ran: + label: "What exact command you ran:" description: Describe the exact command you ran that triggered the bug. validations: required: true @@ -51,4 +51,4 @@ body: description: Indicate your environment details. options: - label: "I'm using the latest version!" - - label: "It's not a user-side mistake!" \ No newline at end of file + - label: "It's not a user-side mistake!" diff --git a/.github/ISSUE_TEMPLATE/documentation.yaml b/.github/ISSUE_TEMPLATE/documentation.yaml index f583259..ea40997 100644 --- a/.github/ISSUE_TEMPLATE/documentation.yaml +++ b/.github/ISSUE_TEMPLATE/documentation.yaml @@ -6,7 +6,7 @@ body: attributes: value: | ## 📚 Documentation Issue - + This issue category is for problems about existing documentation, not for asking how-to questions. - type: input diff --git a/.github/ISSUE_TEMPLATE/feature-request.yaml b/.github/ISSUE_TEMPLATE/feature-request.yaml index 8303a6b..a4d5dfe 100644 --- a/.github/ISSUE_TEMPLATE/feature-request.yaml +++ b/.github/ISSUE_TEMPLATE/feature-request.yaml @@ -6,7 +6,7 @@ body: attributes: value: | ## 🚀 Feature - + A clear and concise description of the feature proposal. - type: textarea @@ -25,6 +25,6 @@ body: attributes: value: | ## Note - + We only consider adding new features if they are relevant to this library. Consider if this new feature deserves to be here or should be a new library. diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e538be2..0d2cdbb 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -63,4 +63,4 @@ repos: additional_dependencies: - mdformat-gfm - mdformat_frontmatter - exclude: CHANGELOG.md \ No newline at end of file + exclude: CHANGELOG.md diff --git a/Dockerfile b/Dockerfile index 63f549c..62f1b9f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,4 +21,4 @@ RUN pip install -r requirements.txt EXPOSE 8000 # Entry point -CMD ["python3", "app.py"] \ No newline at end of file +CMD ["python3", "app.py"] diff --git a/README.md b/README.md index 0df9e52..3a2fa7e 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,6 @@ docker build --tag lang-segment-anything:latest . docker run --gpus all -p 8000:8000 lang-segment-anything:latest ``` - ### Usage To run the gradio APP: @@ -69,7 +68,6 @@ results = model.predict([image_pil], [text_prompt]) ![fruits.png](/assets/outputs/fruits.png) - ## Acknowledgments This project is based on/used the following repositories: diff --git a/lang_sam/lang_sam.py b/lang_sam/lang_sam.py index b3729b0..3c319d0 100644 --- a/lang_sam/lang_sam.py +++ b/lang_sam/lang_sam.py @@ -20,8 +20,7 @@ def predict( box_threshold: float = 0.3, text_threshold: float = 0.25, ): - """ - Predicts masks for given images and text prompts using GDINO and SAM models. + """Predicts masks for given images and text prompts using GDINO and SAM models. Parameters: images_pil (list[Image.Image]): List of input images. diff --git a/lang_sam/models/gdino.py b/lang_sam/models/gdino.py index 4522a90..86c5b04 100644 --- a/lang_sam/models/gdino.py +++ b/lang_sam/models/gdino.py @@ -1,7 +1,7 @@ -import numpy as np import torch from PIL import Image from transformers import AutoModelForZeroShotObjectDetection, AutoProcessor + from lang_sam.models.utils import get_device_type device_type = get_device_type() diff --git a/lang_sam/models/sam.py b/lang_sam/models/sam.py index 220352e..4ee9e9f 100644 --- a/lang_sam/models/sam.py +++ b/lang_sam/models/sam.py @@ -5,6 +5,7 @@ from omegaconf import OmegaConf from sam2.automatic_mask_generator import SAM2AutomaticMaskGenerator from sam2.sam2_image_predictor import SAM2ImagePredictor + from lang_sam.models.utils import get_device_type DEVICE = torch.device(get_device_type()) diff --git a/lang_sam/models/utils.py b/lang_sam/models/utils.py index bb4f003..d822d39 100644 --- a/lang_sam/models/utils.py +++ b/lang_sam/models/utils.py @@ -1,4 +1,5 @@ import logging + import torch diff --git a/lang_sam/server.py b/lang_sam/server.py index fa545ff..e460af5 100644 --- a/lang_sam/server.py +++ b/lang_sam/server.py @@ -18,8 +18,7 @@ def setup(self, device: str) -> None: print("LangSAM model initialized.") def decode_request(self, request) -> dict: - """ - Decode the incoming request to extract parameters and image bytes. + """Decode the incoming request to extract parameters and image bytes. Assumes the request is sent as multipart/form-data with fields: - sam_type: str @@ -50,15 +49,17 @@ def decode_request(self, request) -> dict: } def predict(self, inputs: dict) -> dict: - """ - Perform prediction using the LangSAM model. + """Perform prediction using the LangSAM model. Yields: dict: Contains the processed output image. """ print("Starting prediction with parameters:") print( - f"sam_type: {inputs['sam_type']}, box_threshold: {inputs['box_threshold']}, text_threshold: {inputs['text_threshold']}, text_prompt: {inputs['text_prompt']}" + f"sam_type: {inputs['sam_type']}, \ + box_threshold: {inputs['box_threshold']}, \ + text_threshold: {inputs['text_threshold']}, \ + text_prompt: {inputs['text_prompt']}" ) if inputs["sam_type"] != self.model.sam_type: @@ -96,8 +97,7 @@ def predict(self, inputs: dict) -> dict: return {"output_image": output_image} def encode_response(self, output: dict) -> Response: - """ - Encode the prediction result into an HTTP response. + """Encode the prediction result into an HTTP response. Returns: Response: Contains the processed image in PNG format. diff --git a/requirements.txt b/requirements.txt index bcb5276..26def0e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,4 +11,4 @@ uvloop==0.20.0 --extra-index-url https://download.pytorch.org/whl/cu124 torch==2.4.1 --extra-index-url https://download.pytorch.org/whl/cu124 -torchvision==0.19.1 \ No newline at end of file +torchvision==0.19.1 From 3f8a6676ff9853839321e05262d9deca3e2ef03e Mon Sep 17 00:00:00 2001 From: luca-medeiros Date: Wed, 16 Oct 2024 10:38:00 +0900 Subject: [PATCH 3/6] fix setuptools format --- pyproject.toml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 5bae57b..7a65de1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,14 +3,9 @@ name = "lang-sam" version = "0.1.0" description = "Language segment-anything" readme = "README.md" -documentation = "https://github.com/luca-medeiros/lang-segment-anything/blob/main/README.md" -repository = "https://github.com/luca-medeiros/lang-segment-anything" authors = [ { name = "Luca Medeiros", email = "lucamedeiros@outlook.com" }, ] -packages = [ - { include = "lang_sam" }, -] requires-python = ">=3.11" dynamic = ["dependencies"] From a34a8d483e00433edf39ec6929673fd713e8888d Mon Sep 17 00:00:00 2001 From: luca-medeiros Date: Wed, 16 Oct 2024 10:41:31 +0900 Subject: [PATCH 4/6] setuptools config --- pyproject.toml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 7a65de1..8897568 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,7 @@ +[build-system] +requires = ["setuptools", "wheel"] +build-backend = "setuptools.build_meta" + [project] name = "lang-sam" version = "0.1.0" @@ -12,13 +16,9 @@ dynamic = ["dependencies"] [tool.setuptools.dynamic] dependencies = {file = ["requirements.txt"]} -[build-system] -requires = ["setuptools", "wheel"] -build-backend = "setuptools.build_meta" - -[tool.setuptools] -packages = ["lang_sam"] -include_package_data = true +[tool.setuptools.packages.find] +where = ["."] +include = ["lang_sam"] [tool.ruff] target-version = "py311" From 1107c244ba1f1875b7f8294676763a6d988f4b50 Mon Sep 17 00:00:00 2001 From: luca-medeiros Date: Wed, 16 Oct 2024 10:45:03 +0900 Subject: [PATCH 5/6] docs --- README.md | 17 +++++++++++++++-- requirements.txt | 4 ---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3a2fa7e..c41efa7 100644 --- a/README.md +++ b/README.md @@ -21,16 +21,27 @@ Language Segment-Anything is an open-source project that combines the power of i ### Installation +#### Installing PyTorch Dependencies + +Before installing `lang-sam`, please install PyTorch using the following command: + +```bash +pip install torch==2.4.1 torchvision==0.19.1 --extra-index-url https://download.pytorch.org/whl/cu124 + ``` + pip install -U git+https://github.com/luca-medeiros/lang-segment-anything.git + ``` Or Clone the repository and install the required packages: ``` + git clone https://github.com/luca-medeiros/lang-segment-anything && cd lang-segment-anything pip install -e . + ``` #### Docker Installation @@ -38,10 +49,12 @@ pip install -e . Build and run the image. ``` + git clone https://github.com/luca-medeiros/lang-segment-anything && cd lang-segment-anything docker build --tag lang-segment-anything:latest . docker run --gpus all -p 8000:8000 lang-segment-anything:latest -``` + +```` ### Usage @@ -60,7 +73,7 @@ model = LangSAM() image_pil = Image.open("./assets/car.jpeg").convert("RGB") text_prompt = "wheel." results = model.predict([image_pil], [text_prompt]) -``` +```` ## Examples diff --git a/requirements.txt b/requirements.txt index 26def0e..abc556f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,9 +6,5 @@ sam-2 @ git+https://github.com/facebookresearch/segment-anything-2@7e1596c0b6462 supervision==0.23.0 ; python_full_version > '3.10' transformers==4.44.2 uvloop==0.20.0 - - ---extra-index-url https://download.pytorch.org/whl/cu124 torch==2.4.1 ---extra-index-url https://download.pytorch.org/whl/cu124 torchvision==0.19.1 From d9af411cef94e3e39e8a94028c49e13a12335299 Mon Sep 17 00:00:00 2001 From: luca-medeiros Date: Wed, 16 Oct 2024 10:47:18 +0900 Subject: [PATCH 6/6] add sub libraries --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 8897568..63b44af 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,7 +18,7 @@ dependencies = {file = ["requirements.txt"]} [tool.setuptools.packages.find] where = ["."] -include = ["lang_sam"] +include = ["lang_sam", "lang_sam.*"] [tool.ruff] target-version = "py311"