Instructions to use Thomasboosinger/owlvit-base-patch32 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Thomasboosinger/owlvit-base-patch32 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("zero-shot-object-detection", model="Thomasboosinger/owlvit-base-patch32")# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModelForZeroShotObjectDetection processor = AutoProcessor.from_pretrained("Thomasboosinger/owlvit-base-patch32") model = AutoModelForZeroShotObjectDetection.from_pretrained("Thomasboosinger/owlvit-base-patch32", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download handler.py from Thomasboosinger/owlvit-base-patch32: direct link, hf CLI and curl.
- Browser
- Download file 1.25 kB
-
https://huggingface.co/Thomasboosinger/owlvit-base-patch32/resolve/main/handler.py
- Command line
-
hf download hf://Thomasboosinger/owlvit-base-patch32/handler.py
-
curl -L -o handler.py https://huggingface.co/Thomasboosinger/owlvit-base-patch32/resolve/main/handler.py
1.25 kB
| from transformers import pipeline | |
| import torch | |
| from PIL import Image | |
| import base64 | |
| from io import BytesIO | |
| class EndpointHandler: | |
| def __init__(self, model_path=""): | |
| # Dynamically assign computing device based on availability. | |
| self.device = "cuda" if torch.cuda.is_available() else "cpu" | |
| print(f"Using {'GPU: ' + torch.cuda.get_device_name(0) if self.device == 'cuda' else 'CPU'}") | |
| # Initialize model with the capability to automatically adjust to GPU or CPU. | |
| self.pipeline = pipeline("zero-shot-object-detection", model=model_path, device=0 if self.device == 'cuda' else -1) | |
| def __call__(self, data): | |
| """ | |
| Decode image, run zero-shot object detection, and return results. | |
| Args: | |
| data (dict): Contains base64-encoded image and candidate labels. | |
| Returns: | |
| list[dict]: Each dict contains a label and its score from object detection. | |
| """ | |
| # Decode the base64 image to PIL format. | |
| image = Image.open(BytesIO(base64.b64decode(data['inputs']['image']))) | |
| # Run detection and obtain results. | |
| results = self.pipeline(image=image, candidate_labels=data['inputs']['candidates'], threshold = .01) | |
| return results | |