Skip to content

Commit

Permalink
Merge pull request #81 from luca-medeiros/feat/setuptools
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-medeiros authored Oct 16, 2024
2 parents e82f70f + d9af411 commit eb722e6
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.venv
.venv
8 changes: 4 additions & 4 deletions .github/ISSUE_TEMPLATE/bugs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
```
Expand All @@ -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
Expand Down Expand Up @@ -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!"
- label: "It's not a user-side mistake!"
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/documentation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/feature-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ body:
attributes:
value: |
## 🚀 Feature
A clear and concise description of the feature proposal.
- type: textarea
Expand All @@ -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.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ repos:
additional_dependencies:
- mdformat-gfm
- mdformat_frontmatter
exclude: CHANGELOG.md
exclude: CHANGELOG.md
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ RUN pip install -r requirements.txt
EXPOSE 8000

# Entry point
CMD ["python3", "app.py"]
CMD ["python3", "app.py"]
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,40 @@ 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
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
Expand All @@ -61,15 +73,14 @@ model = LangSAM()
image_pil = Image.open("./assets/car.jpeg").convert("RGB")
text_prompt = "wheel."
results = model.predict([image_pil], [text_prompt])
```
````

## Examples

![car.png](/assets/outputs/car.png)

![fruits.png](/assets/outputs/fruits.png)


## Acknowledgments

This project is based on/used the following repositories:
Expand Down
3 changes: 1 addition & 2 deletions lang_sam/lang_sam.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lang_sam/models/gdino.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
1 change: 1 addition & 0 deletions lang_sam/models/sam.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
1 change: 1 addition & 0 deletions lang_sam/models/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging

import torch


Expand Down
14 changes: 7 additions & 7 deletions lang_sam/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
16 changes: 7 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"

[project]
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 = "[email protected]" },
]
packages = [
{ include = "lang_sam" },
]
requires-python = ">=3.11"
dynamic = ["dependencies"]

[tool.setuptools.dynamic]
dependencies = {file = ["requirements.txt"]}

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.setuptools.packages.find]
where = ["."]
include = ["lang_sam", "lang_sam.*"]

[tool.ruff]
target-version = "py311"
Expand Down
6 changes: 1 addition & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
torchvision==0.19.1

0 comments on commit eb722e6

Please sign in to comment.