Run this model with HF Jobs
Run google/owlv2-large-patch14-ensemble on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/google/owlv2-large-patch14-ensemble. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/google/owlv2-large-patch14-ensemble on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use google/owlv2-large-patch14-ensemble with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use google/owlv2-large-patch14-ensemble with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("zero-shot-object-detection", model="google/owlv2-large-patch14-ensemble")# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModelForZeroShotObjectDetection processor = AutoProcessor.from_pretrained("google/owlv2-large-patch14-ensemble") model = AutoModelForZeroShotObjectDetection.from_pretrained("google/owlv2-large-patch14-ensemble", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Update code snippet
#9
by nielsr HF Staff - opened
33 unmodified lines3435363738394041424344451 unmodified line47484950515253545556575859606162636465666768697071727374757677787933 unmodified lines```pythonimport requestsfrom PIL import Imageimport numpy as npimport torchfrom transformers import AutoProcessor, Owlv2ForObjectDetectionfrom transformers.utils.constants import OPENAI_CLIP_MEAN, OPENAI_CLIP_STDprocessor = AutoProcessor.from_pretrained("google/owlv2-large-patch14-ensemble")model = Owlv2ForObjectDetection.from_pretrained("google/owlv2-large-patch14-ensemble")url = "http://images.cocodataset.org/val2017/000000039769.jpg"1 unmodified linetexts = [["a photo of a cat", "a photo of a dog"]]inputs = processor(text=texts, images=image, return_tensors="pt")# forward passwith torch.no_grad():outputs = model(**inputs)# Note: boxes need to be visualized on the padded, unnormalized image# hence we'll set the target image sizes (height, width) based on thatdef get_preprocessed_image(pixel_values):pixel_values = pixel_values.squeeze().numpy()unnormalized_image = (pixel_values * np.array(OPENAI_CLIP_STD)[:, None, None]) + np.array(OPENAI_CLIP_MEAN)[:, None, None]unnormalized_image = (unnormalized_image * 255).astype(np.uint8)unnormalized_image = np.moveaxis(unnormalized_image, 0, -1)unnormalized_image = Image.fromarray(unnormalized_image)return unnormalized_imageunnormalized_image = get_preprocessed_image(inputs.pixel_values)target_sizes = torch.Tensor([unnormalized_image.size[::-1]])# Convert outputs (bounding boxes and class logits) to final bounding boxes and scoresresults = processor.post_process_object_detection(outputs=outputs, threshold=0.2, target_sizes=target_sizes)i = 0 # Retrieve predictions for the first image for the corresponding text queriestext = texts[i]boxes, scores, labels = results[i]["boxes"], results[i]["scores"], results[i]["labels"]for box, score, label in zip(boxes, scores, labels):box = [round(i, 2) for i in box.tolist()]print(f"Detected {text[label]} with confidence {round(score.item(), 3)} at location {box}")33 unmodified lines34353637383940414243441 unmodified line4647484950515253545556575859606133 unmodified lines```pythonimport requestsfrom PIL import Imageimport torchfrom transformers import Owlv2Processor, Owlv2ForObjectDetectionprocessor = Owlv2Processor.from_pretrained("google/owlv2-large-patch14-ensemble")model = Owlv2ForObjectDetection.from_pretrained("google/owlv2-large-patch14-ensemble")url = "http://images.cocodataset.org/val2017/000000039769.jpg"1 unmodified linetexts = [["a photo of a cat", "a photo of a dog"]]inputs = processor(text=texts, images=image, return_tensors="pt")with torch.no_grad():outputs = model(**inputs)# Target image sizes (height, width) to rescale box predictions [batch_size, 2]target_sizes = torch.Tensor([image.size[::-1]])# Convert outputs (bounding boxes and class logits) to Pascal VOC Format (xmin, ymin, xmax, ymax)results = processor.post_process_object_detection(outputs=outputs, target_sizes=target_sizes, threshold=0.1)i = 0 # Retrieve predictions for the first image for the corresponding text queriestext = texts[i]boxes, scores, labels = results[i]["boxes"], results[i]["scores"], results[i]["labels"]for box, score, label in zip(boxes, scores, labels):box = [round(i, 2) for i in box.tolist()]print(f"Detected {text[label]} with confidence {round(score.item(), 3)} at location {box}")
README.md+8-26