Skip to content

Commit

Permalink
add github action, use load image
Browse files Browse the repository at this point in the history
  • Loading branch information
capjamesg committed Dec 6, 2023
1 parent f26c23d commit c1d9e6c
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 81 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Publish WorkFlow

on:
release:
types: [created]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.8]
steps:
- name: 🛎️ Checkout
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}
- name: 🐍 Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: 🦾 Install dependencies
run: |
python -m pip install --upgrade pip
pip install ".[dev]"
- name: 🚀 Publish to PyPi
env:
PYPI_USERNAME: ${{ secrets.PYPI_USERNAME }}
PYPI_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
PYPI_TEST_PASSWORD: ${{ secrets.PYPI_TEST_PASSWORD }}
run: |
make publish -e PYPI_USERNAME=$PYPI_USERNAME -e PYPI_PASSWORD=$PYPI_PASSWORD -e PYPI_TEST_PASSWORD=$PYPI_TEST_PASSWORD
30 changes: 30 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Test WorkFlow

on:
pull_request:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7, 3.8, 3.9]
steps:
- name: 🛎️ Checkout
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}
- name: 🐍 Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: 🦾 Install dependencies
run: |
python -m pip install --upgrade pip
pip install ".[dev]"
- name: 🧹 Lint with flake8
run: |
make check_code_quality
- name: 🧪 Test
run: "python -m pytest ./test"
16 changes: 16 additions & 0 deletions .github/workflows/welcome.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
on:
issues:
types: [opened]
pull_request_target:
types: [opened]

jobs:
build:
name: 👋 Welcome
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
issue-message: "Hello there, thank you for opening an Issue ! 🙏🏻 The team was notified and they will get back to you soon."
pr-message: "Hello there, thank you for opening an PR ! 🙏🏻 The team was notified and they will get back to you soon."
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ pip3 install autodistill-owlv2

```python
from autodistill_owlv2 import OWLv2
from autodistill.detection import CaptionOntology
from autodistill.utils import plot
import cv2

# define an ontology to map class names to our OWLv2 prompt
# the ontology dictionary has the format {caption: class}
Expand All @@ -42,10 +45,21 @@ base_model = OWLv2(
ontology=CaptionOntology(
{
"person": "person",
"a forklift": "forklift"
"dog": "dog"
}
)
)

# run inference on a single image
results = base_model.predict("dog.jpeg")

plot(
image=cv2.imread("dog.jpeg"),
classes=base_model.ontology.classes(),
detections=results
)

# label a folder of images
base_model.label("./context_images", extension=".jpeg")
```

Expand Down
61 changes: 0 additions & 61 deletions README_EXAMPLE.md

This file was deleted.

2 changes: 1 addition & 1 deletion autodistill_owlv2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from autodistill_owlv2.owlv2_model import OWLv2

__version__ = "0.1.0"
__version__ = "0.1.1"
48 changes: 30 additions & 18 deletions autodistill_owlv2/owlv2_model.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,63 @@
import os
import subprocess
from dataclasses import dataclass
from typing import Any

from PIL import Image
import numpy as np
import supervision as sv
import torch

import subprocess

import supervision as sv
from autodistill.detection import CaptionOntology, DetectionBaseModel
from autodistill.helpers import load_image

HOME = os.path.expanduser("~")
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")


@dataclass
class OWLv2(DetectionBaseModel):
ontology: CaptionOntology

def __init__(self, ontology: CaptionOntology):
# install transformers from source, since OWLv2 is not yet in a release
# (as of October 26th, 2023)
try:
from transformers import Owlv2Processor, Owlv2ForObjectDetection
from transformers import Owlv2ForObjectDetection, Owlv2Processor
except:
subprocess.run(["pip3", "install", "git+https://github.com/huggingface/transformers"])
from transformers import Owlv2Processor, Owlv2ForObjectDetection

self.processor = Owlv2Processor.from_pretrained("google/owlv2-base-patch16-ensemble")
self.model = Owlv2ForObjectDetection.from_pretrained("google/owlv2-base-patch16-ensemble")
subprocess.run(
["pip3", "install", "git+https://github.com/huggingface/transformers"]
)
from transformers import Owlv2ForObjectDetection, Owlv2Processor

self.processor = Owlv2Processor.from_pretrained(
"google/owlv2-base-patch16-ensemble"
)
self.model = Owlv2ForObjectDetection.from_pretrained(
"google/owlv2-base-patch16-ensemble"
)
self.ontology = ontology

def predict(self, input: str, confidence: int = 0.1) -> sv.Detections:
image = Image.open(input)
def predict(self, input: Any, confidence: int = 0.1) -> sv.Detections:
image = load_image(input, return_format="PIL")
texts = [self.ontology.prompts()]

inputs = self.processor(text=texts, images=image, return_tensors="pt")
outputs = self.model(**inputs)

target_sizes = torch.Tensor([image.size[::-1]])

results = self.processor.post_process_object_detection(outputs=outputs, target_sizes=target_sizes, threshold=0.1)
results = self.processor.post_process_object_detection(
outputs=outputs, target_sizes=target_sizes, threshold=0.1
)

i = 0
text = texts[i]

boxes, scores, labels = results[i]["boxes"], results[i]["scores"], results[i]["labels"]
boxes, scores, labels = (
results[i]["boxes"],
results[i]["scores"],
results[i]["labels"],
)

final_boxes, final_scores, final_labels = [], [], []

Expand All @@ -57,13 +70,12 @@ def predict(self, input: str, confidence: int = 0.1) -> sv.Detections:
final_boxes.append(box)
final_scores.append(score.item())
final_labels.append(label.item())
print(f"Detected {text[label]} with confidence {round(score.item(), 3)} at location {box}")

if len(final_boxes) == 0:
return sv.Detections.empty()

return sv.Detections(
xyxy=np.array(final_boxes),
class_id=np.array(final_labels),
confidence=np.array(final_scores),
)
)

0 comments on commit c1d9e6c

Please sign in to comment.