File size: 8,654 Bytes
61cc71c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
#!/usr/bin/env python3
"""
Oculus 0.2 Unified Demo
Demonstrates all features of the unified Oculus model:
- Text mode (captioning, VQA)
- Point mode (counting objects)
- Box mode (detection with bounding boxes)
- Polygon mode (segmentation)
- Optional reasoning with thinking traces
- Focus system for fine-grained perception
"""
import os
import sys
import requests
from pathlib import Path
from io import BytesIO
from PIL import Image
import torch
# Add parent to path
sys.path.insert(0, str(Path(__file__).parent))
from oculus_unified_model import OculusForConditionalGeneration, OculusConfig
def download_image(url: str) -> Image.Image:
"""Download image from URL."""
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status()
return Image.open(BytesIO(response.content)).convert('RGB')
def print_header(title: str):
print("\n" + "=" * 70)
print(f"๐ฎ {title}")
print("=" * 70)
def print_section(title: str):
print(f"\n{'โ' * 70}")
print(f" {title}")
print(f"{'โ' * 70}")
def demo():
print_header("OCULUS 0.2 UNIFIED MODEL DEMO")
# ================================================================
# Load Model
# ================================================================
print("\n[1] Loading Oculus Model...")
# Check if we have trained weights
weights_path = Path(__file__).parent / "checkpoints" / "oculus_coco" / "final"
if weights_path.exists():
print(f" Found trained weights at: {weights_path}")
model = OculusForConditionalGeneration.from_pretrained(weights_path)
else:
print(" Using default configuration")
config = OculusConfig(
reasoning_enabled=True,
enable_focus=True,
)
model = OculusForConditionalGeneration(config)
print(" โ Model loaded!")
# ================================================================
# Test Images
# ================================================================
test_images = [
{
"name": "Cat on Couch",
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg"
},
{
"name": "Golden Gate Bridge",
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/0/0c/GoldenGateBridge-001.jpg/1200px-GoldenGateBridge-001.jpg"
},
]
for test in test_images:
print_header(f"Testing: {test['name']}")
try:
print("\n[Downloading image...]")
image = download_image(test["url"])
print(f" Image size: {image.size}")
# ========================================================
# Mode 1: TEXT (Captioning)
# ========================================================
print_section("๐ TEXT MODE - Captioning")
output = model.generate(
image=image,
prompt="Describe this image in detail",
mode="text",
think=False
)
print(f" Caption: \"{output.text}\"")
# ========================================================
# Mode 2: TEXT with Reasoning
# ========================================================
print_section("๐ง TEXT MODE - With Reasoning")
output = model.generate(
image=image,
prompt="What is the main subject of this image?",
mode="text",
think=True # Enable thinking traces
)
if output.thinking_trace:
print(f" ๐ญ Thinking: {output.thinking_trace[:200]}...")
print(f" Answer: \"{output.text}\"")
# ========================================================
# Mode 3: TEXT (VQA)
# ========================================================
print_section("โ TEXT MODE - VQA")
questions = [
"What colors are visible in this image?",
"Is this indoors or outdoors?",
]
for q in questions:
output = model.generate(
image=image,
prompt=q,
mode="text"
)
print(f" Q: {q}")
print(f" A: {output.text}")
# ========================================================
# Mode 4: POINT (Counting)
# ========================================================
print_section("๐ POINT MODE - Object Counting")
output = model.generate(
image=image,
prompt="Find objects",
mode="point"
)
print(f" Detected {len(output.points)} points")
for i, (pt, label, conf) in enumerate(zip(
output.points[:5],
output.labels[:5],
output.confidences[:5]
)):
print(f" Point {i+1}: {pt} (class={label}, conf={conf:.2f})")
# ========================================================
# Mode 5: BOX (Detection)
# ========================================================
print_section("๐ฆ BOX MODE - Object Detection")
output = model.generate(
image=image,
prompt="Detect all objects",
mode="box"
)
print(f" Detected {len(output.boxes)} boxes")
for i, (box, label, conf) in enumerate(zip(
output.boxes[:5],
output.labels[:5],
output.confidences[:5]
)):
print(f" Box {i+1}: {[f'{b:.2f}' for b in box]} (class={label}, conf={conf:.2f})")
# ========================================================
# Mode 6: POLYGON (Segmentation)
# ========================================================
print_section("๐ท POLYGON MODE - Segmentation")
output = model.generate(
image=image,
prompt="Segment the scene",
mode="polygon"
)
print(f" Segmentation mask shape: {output.mask.shape if output.mask is not None else 'N/A'}")
print(f" Detected {len(output.polygons)} regions")
for i, (poly, label) in enumerate(zip(
output.polygons[:3],
output.labels[:3]
)):
print(f" Region {i+1}: class={label}, vertices={len(poly)}")
print("\n โ
All modes successful!")
except Exception as e:
print(f"\n โ Error: {e}")
import traceback
traceback.print_exc()
# ================================================================
# Summary
# ================================================================
print_header("DEMO COMPLETE")
print("""
Oculus 0.2 supports:
๐ TEXT MODE
- Image captioning
- Visual question answering
- With optional reasoning traces
๐ POINT MODE
- Object counting
- Point localization
๐ฆ BOX MODE
- Object detection
- Bounding box prediction
๐ท POLYGON MODE
- Semantic segmentation
- Instance segmentation
๐ง REASONING
- Optional thinking traces
- Multi-step reasoning
๐ FOCUS SYSTEM
- Zoom & crop for fine-grained perception
- Automatic region detection
Usage:
```python
from oculus_unified_model import OculusForConditionalGeneration
model = OculusForConditionalGeneration.from_pretrained("./checkpoints/oculus_coco/final")
# Caption
output = model.generate(image, mode="text", prompt="Describe this")
# VQA with reasoning
output = model.generate(image, mode="text", prompt="What color is it?", think=True)
# Detection
output = model.generate(image, mode="box", prompt="Find cars")
# Segmentation
output = model.generate(image, mode="polygon")
```
""")
if __name__ == "__main__":
demo()
|