Mithil Maske_
All writing

February 25, 2026 · 4 min read

Quantizing OWLv2 for Efficient Open Vocabulary Object Detection

Open vocabulary object detection models allow detection of arbitrary objects using natural language prompts instead of fixed training classes. Among these models, OWLv2 is one of the most powerful transformer-based architectures for flexible object detection.

However, OWLv2 models are computationally expensive and difficult to deploy for real-time or large-scale video processing.

This article presents a practical and engineering-focused guide to quantizing OWLv2 using HuggingFace Optimum Quanto and optimizing inference for video workloads.

The techniques described here are based on real-world implementation with a fine-tuned OWLv2 model.

Understanding OWLv2 Architecture

OWLv2 is built on a dual-encoder transformer architecture similar to CLIP. The model consists of three major components:

1. Vision Encoder

The vision encoder extracts features from the input image using a Vision Transformer backbone.

Input image → Patch embeddings → Transformer layers → Visual features

2. Text Encoder

The text encoder converts text prompts into semantic embeddings.

Text prompts → Tokenization → Transformer → Text embeddings

3. Detection Head

The detection head aligns visual features with text embeddings and predicts bounding boxes and class scores.

Visual features + Text embeddings → Cross-attention → Bounding boxes + logits

The detection process can be represented as:

Image → Vision Transformer → Image Features
Text → Text Transformer → Text Features
Image Features × Text Features → Detection Head → Predictions

Because both the vision encoder and text encoder are transformer-based, OWLv2 requires significant compute resources.

Why Quantization is Needed

Transformer models like OWLv2 contain large numbers of linear layers. These layers dominate both memory usage and inference latency.

Typical OWLv2 Base model characteristics:

Model Size: ~850 MB (FP16)
GPU Memory: ~3–4 GB
Inference Speed: ~1 second per frame

Quantization reduces model weight precision from floating point to integer representation.

INT8 quantization reduces memory by approximately 50 percent while maintaining accuracy.

After quantization:

Model Size: ~430 MB
GPU Memory: ~1.5–2 GB
Inference Speed: significantly improved

Quantization makes OWLv2 suitable for practical deployment scenarios.

Quantization Method: Optimum Quanto

Optimum Quanto is a PyTorch-based quantization backend designed for transformer models. It replaces floating-point linear layers with quantized implementations.

Supported formats include:

INT8 weights
INT4 weights
Float8 weights

INT8 provides the best balance between speed and accuracy for OWLv2.

Environment Setup

Install dependencies:

pip install torch transformers optimum-quanto accelerate safetensors

Loading OWLv2

import torch
from transformers import Owlv2Processor
from transformers import Owlv2ForObjectDetection

MODEL_ID = "google/owlv2-base-patch16-ensemble"
processor = Owlv2Processor.from_pretrained(MODEL_ID)
model = Owlv2ForObjectDetection.from_pretrained(
MODEL_ID,
torch_dtype=torch.float16
)
model.eval()

Quantizing OWLv2

Quantization replaces linear layers with quantized equivalents.

from optimum.quanto import quantize
from optimum.quanto import freeze
from optimum.quanto import qint8

quantize(model, weights=qint8)
freeze(model)

Save quantized model:

model.save_pretrained("owlv2_quantized")
processor.save_pretrained("owlv2_quantized")

The quantized model can be loaded exactly like a standard Transformers model.

Loading the Quantized Model

processor = Owlv2Processor.from_pretrained("owlv2_quantized")

model = Owlv2ForObjectDetection.from_pretrained(
"owlv2_quantized",
torch_dtype=torch.float16
)
model.cuda()
model.eval()

No special quantization loader is required.

OWLv2 Detection Pipeline

Detection involves three steps:

  1. Image preprocessing
  2. Model inference
  3. Bounding box decoding

Preparing Text Prompts

Open vocabulary detection requires text prompts representing object classes.

CLASS_NAMES = [
"air filter",
"car battery",
"brake pad",
"license plate"
]

TEXT_PROMPTS = CLASS_NAMES

Tokenization is performed once:

text_inputs = processor(
text=[TEXT_PROMPTS],
return_tensors="pt"
)

text_inputs = {k:v.cuda() for k,v in text_inputs.items()}

Caching text tokens avoids recomputation during video processing.

Frame Inference

def detect_frame(frame):
H,W = frame.shape[:2]
image = Image.fromarray(frame[:,:,::-1])
image_inputs = processor(
images=image,
return_tensors="pt"
)
pixel_values = image_inputs["pixel_values"].cuda()
with torch.inference_mode():
outputs = model(
pixel_values=pixel_values,
input_ids=text_inputs["input_ids"],
attention_mask=text_inputs["attention_mask"]
)

Bounding Box Prediction

OWL models output normalized bounding boxes.

Each box is represented as:

(cx, cy, width, height)

Values range between 0 and 1.

Extract predictions:

logits = outputs.logits[0]
boxes = outputs.pred_boxes[0]
probs = torch.softmax(logits, dim=-1)
labels = probs.argmax(dim=-1)
scores = probs.max(dim=-1).values

Bounding Box Conversion

Convert normalized coordinates to pixel coordinates.

detections = []
for box,label,score in zip(boxes,labels,scores):
if score < 0.3:
continue
cx,cy,bw,bh = box
x1 = int((cx - bw/2) * W)
y1 = int((cy - bh/2) * H)
x2 = int((cx + bw/2) * W)
y2 = int((cy + bh/2) * H)
detections.append(
[x1,y1,x2,y2,label,score.item()]
)

Manual decoding is often more reliable for fine-tuned OWLv2 models than automatic postprocessing.

Video Processing Pipeline

cap = cv2.VideoCapture("input.mp4")

while True:
ret,frame = cap.read()
if not ret:
break
detections = detect_frame(frame)
for x1,y1,x2,y2,label,score in detections:
name = CLASS_NAMES[label]
cv2.rectangle(
frame,
(x1,y1),
(x2,y2),
(0,255,0),
2
)

Performance Optimization

Major speed improvements come from:

1. Quantization

Reduces memory and compute cost.

2. Cached Text Tokens

Avoids text tokenization every frame.

3. GPU Inference Mode

torch.inference_mode()

Reduces overhead.

4. Fixed Resolution Input

Consistent frame sizes improve throughput.

Performance Results

Testing environment:

Google Colab GPU
OWLv2 Base Model

ConfigurationSpeedFP16 Model~1.1 sec per frameQuantized Model~0.7 sec per frameOptimized Quantized Model~0.4 sec per frame

Common Implementation Issues

Bounding Boxes Appear as Lines

Cause:

Incorrect bounding box decoding.

Solution:

Manual conversion from normalized coordinates.

Slow Inference

Cause:

Text tokenization per frame.

Solution:

Cache text tokens.

Label Index Errors

Cause:

Detection queries exceed class count.

Solution:

Filter labels outside class list.

When to Use OWLv2

OWLv2 is ideal for:

Dynamic object detection tasks
Industrial inspection
Vehicle inspection
Custom detection systems
Rapid prototyping

OWLv2 is especially powerful when object categories change frequently.

Conclusion

OWLv2 provides a flexible approach to object detection using language prompts. However, its computational requirements make naive deployment impractical.

Quantization with Optimum Quanto reduces memory usage and improves performance significantly while maintaining detection quality.

With proper optimization, OWLv2 becomes a viable solution for real-world video detection applications.