File size: 18,010 Bytes
3ab6ebf |
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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 |
#!/usr/bin/env python3
"""
OCULUS Detection Head Training
Trains the detection (box) and point heads on COCO detection data.
Uses the frozen vision encoders + trained projector, only trains the heads.
"""
import os
import sys
import json
import time
import random
from pathlib import Path
from dataclasses import dataclass
from typing import List, Dict, Tuple, Optional
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
from PIL import Image
OCULUS_ROOT = Path(__file__).parent
# Add to path
sys.path.insert(0, str(OCULUS_ROOT))
from oculus_unified_model import OculusForConditionalGeneration, OculusConfig
@dataclass
class DetectionTrainingConfig:
"""Training configuration."""
# Data
data_dir: str = "data/coco"
annotations_file: str = "annotations/instances_train2017.json"
images_subdir: str = "images"
# Training
batch_size: int = 4
learning_rate: float = 1e-4
num_epochs: int = 3
warmup_steps: int = 100
max_samples: int = 3000 # Limit for faster training
# Model
checkpoint_path: str = "checkpoints/oculus_coco/final"
# Checkpointing
save_every: int = 200
checkpoint_dir: str = "checkpoints/oculus_detection"
# Logging
log_every: int = 25
class COCODetectionDataset(Dataset):
"""COCO Detection dataset."""
# COCO 80 class names
COCO_CLASSES = [
'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck',
'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench',
'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra',
'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove',
'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange',
'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse',
'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink',
'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier',
'toothbrush'
]
def __init__(self, data_dir: str, annotations_file: str, images_subdir: str, max_samples: int = None):
self.data_dir = Path(data_dir)
self.images_dir = self.data_dir / images_subdir
# Load annotations
annotations_path = self.data_dir / annotations_file
print(f" Loading annotations from {annotations_path}...")
with open(annotations_path) as f:
coco_data = json.load(f)
# Build category ID to index mapping
self.cat_id_to_idx = {}
for i, cat in enumerate(coco_data['categories']):
self.cat_id_to_idx[cat['id']] = i
# Build image ID to annotations mapping
img_to_anns = {}
for ann in coco_data['annotations']:
img_id = ann['image_id']
if img_id not in img_to_anns:
img_to_anns[img_id] = []
img_to_anns[img_id].append(ann)
# Build samples list
self.samples = []
for img_info in coco_data['images']:
img_id = img_info['id']
if img_id not in img_to_anns:
continue
# Check if image exists
img_path = self.images_dir / img_info['file_name']
if not img_path.exists():
continue
anns = img_to_anns[img_id]
# Convert annotations to boxes
boxes = []
labels = []
for ann in anns:
if 'bbox' not in ann or ann.get('iscrowd', 0):
continue
# COCO bbox format: [x, y, width, height]
x, y, w, h = ann['bbox']
# Convert to normalized [x1, y1, x2, y2]
x1 = x / img_info['width']
y1 = y / img_info['height']
x2 = (x + w) / img_info['width']
y2 = (y + h) / img_info['height']
# Clamp to [0, 1]
x1, y1, x2, y2 = max(0, x1), max(0, y1), min(1, x2), min(1, y2)
boxes.append([x1, y1, x2, y2])
labels.append(self.cat_id_to_idx[ann['category_id']])
if boxes:
self.samples.append({
'image_path': str(img_path),
'boxes': boxes,
'labels': labels,
'width': img_info['width'],
'height': img_info['height']
})
if max_samples and len(self.samples) >= max_samples:
break
print(f" Loaded {len(self.samples):,} images with detections")
def __len__(self):
return len(self.samples)
def __getitem__(self, idx):
return self.samples[idx]
class DetectionTrainer:
"""Trainer for detection heads."""
def __init__(self, config: DetectionTrainingConfig):
self.config = config
print("\n" + "=" * 60)
print("๐ฏ OCULUS DETECTION TRAINER")
print("=" * 60)
self._load_model()
self._load_dataset()
self._create_optimizer()
self.checkpoint_dir = Path(config.checkpoint_dir)
self.checkpoint_dir.mkdir(parents=True, exist_ok=True)
def _load_model(self):
"""Load model with trained projector."""
print("\n[Loading Model]")
checkpoint_path = OCULUS_ROOT / self.config.checkpoint_path
self.model = OculusForConditionalGeneration.from_pretrained(checkpoint_path)
# Load vision encoders
self.model.vision_encoder.load_encoders()
# Freeze vision encoder and projector
for param in self.model.vision_encoder.parameters():
param.requires_grad = False
for param in self.model.projector.parameters():
param.requires_grad = False
# Make sure detection/point heads are trainable
for param in self.model.detection_head.parameters():
param.requires_grad = True
for param in self.model.point_head.parameters():
param.requires_grad = True
# Count trainable params
trainable = sum(p.numel() for p in self.model.parameters() if p.requires_grad)
total = sum(p.numel() for p in self.model.parameters())
print(f" โ Trainable: {trainable:,} / {total:,} parameters")
def _load_dataset(self):
"""Load COCO detection dataset."""
print("\n[Loading Dataset]")
self.dataset = COCODetectionDataset(
self.config.data_dir,
self.config.annotations_file,
self.config.images_subdir,
max_samples=self.config.max_samples
)
def _create_optimizer(self):
"""Create optimizer for detection heads only."""
print("\n[Optimizer]")
# Only optimize detection heads
params = list(self.model.detection_head.parameters()) + \
list(self.model.point_head.parameters())
if self.model.vision_adapter is not None:
params += list(self.model.vision_adapter.parameters())
self.optimizer = torch.optim.AdamW(params, lr=self.config.learning_rate, weight_decay=0.01)
print(f" โ AdamW (lr={self.config.learning_rate})")
def encode_image(self, image_path: str) -> torch.Tensor:
"""Encode image to vision tokens."""
image = Image.open(image_path).convert('RGB')
with torch.no_grad():
vision_tokens = self.model.encode_image(image)
return vision_tokens
def compute_detection_loss(
self,
vision_tokens: torch.Tensor,
target_boxes: List[List[float]],
target_labels: List[int]
) -> Tuple[torch.Tensor, Dict]:
"""Compute detection loss."""
# Get predictions
cls_logits, box_preds = self.model.detection_head(vision_tokens)
batch_size = vision_tokens.shape[0]
num_tokens = vision_tokens.shape[1]
# For each ground truth box, assign it to the nearest predicted "slot"
total_cls_loss = 0
total_box_loss = 0
num_matches = 0
target_boxes_t = torch.tensor(target_boxes, dtype=torch.float32)
target_labels_t = torch.tensor(target_labels, dtype=torch.long)
for i in range(batch_size):
if len(target_boxes) == 0:
continue
# Get predictions for this sample
pred_boxes = box_preds[i] # [num_tokens, 4]
pred_cls = cls_logits[i] # [num_tokens, num_classes]
# For each GT box, find best matching prediction
for gt_idx, (gt_box, gt_label) in enumerate(zip(target_boxes, target_labels)):
gt_box_t = torch.tensor(gt_box, dtype=torch.float32)
# Compute IoU with all predictions
ious = self._compute_iou(pred_boxes, gt_box_t.unsqueeze(0).expand(num_tokens, -1))
# Find best match
best_idx = ious.argmax()
# Classification loss for best match
cls_loss = F.cross_entropy(
pred_cls[best_idx:best_idx+1],
torch.tensor([gt_label], dtype=torch.long)
)
# Box regression loss (L1)
box_loss = F.l1_loss(pred_boxes[best_idx], gt_box_t)
total_cls_loss += cls_loss
total_box_loss += box_loss
num_matches += 1
if num_matches > 0:
total_cls_loss /= num_matches
total_box_loss /= num_matches
# Combined loss
total_loss = total_cls_loss + 5.0 * total_box_loss # Weight box loss higher
return total_loss, {
'cls_loss': float(total_cls_loss) if num_matches > 0 else 0,
'box_loss': float(total_box_loss) if num_matches > 0 else 0,
'num_matches': num_matches
}
def _compute_iou(self, boxes1: torch.Tensor, boxes2: torch.Tensor) -> torch.Tensor:
"""Compute IoU between two sets of boxes."""
# boxes format: [x1, y1, x2, y2]
x1 = torch.max(boxes1[:, 0], boxes2[:, 0])
y1 = torch.max(boxes1[:, 1], boxes2[:, 1])
x2 = torch.min(boxes1[:, 2], boxes2[:, 2])
y2 = torch.min(boxes1[:, 3], boxes2[:, 3])
inter_area = torch.clamp(x2 - x1, min=0) * torch.clamp(y2 - y1, min=0)
area1 = (boxes1[:, 2] - boxes1[:, 0]) * (boxes1[:, 3] - boxes1[:, 1])
area2 = (boxes2[:, 2] - boxes2[:, 0]) * (boxes2[:, 3] - boxes2[:, 1])
union_area = area1 + area2 - inter_area + 1e-8
return inter_area / union_area
def train_step(self, sample: Dict) -> Tuple[float, Dict]:
"""Single training step."""
self.optimizer.zero_grad()
try:
# Encode image (with gradients through adapter if needed)
image = Image.open(sample['image_path']).convert('RGB')
# Get vision features from frozen encoders
with torch.no_grad():
vision_features = self.model.vision_encoder(image)
# Check for dimension mismatch and create adapter
actual_dim = vision_features.shape[-1]
expected_dim = self.model.config.fused_vision_dim
if actual_dim != expected_dim:
if self.model.vision_adapter is None:
print(f" [Adapter] Creating: {actual_dim} -> {expected_dim}")
self.model.vision_adapter = nn.Linear(actual_dim, expected_dim)
nn.init.xavier_uniform_(self.model.vision_adapter.weight)
nn.init.zeros_(self.model.vision_adapter.bias)
# Add adapter params to optimizer
self.optimizer.add_param_group({
'params': self.model.vision_adapter.parameters()
})
vision_features = self.model.vision_adapter(vision_features)
# Project to tokens
vision_tokens = self.model.projector(vision_features)
# Compute detection loss
loss, metrics = self.compute_detection_loss(
vision_tokens,
sample['boxes'],
sample['labels']
)
if loss.requires_grad:
loss.backward()
self.optimizer.step()
return float(loss), metrics
except Exception as e:
print(f" โ ๏ธ Error: {e}")
return 0.0, {}
def save_checkpoint(self, step: int, loss: float):
"""Save checkpoint."""
checkpoint_path = self.checkpoint_dir / f"step_{step:06d}"
checkpoint_path.mkdir(exist_ok=True)
# Save detection heads
torch.save({
'detection': self.model.detection_head.state_dict(),
'point': self.model.point_head.state_dict(),
'adapter': self.model.vision_adapter.state_dict() if self.model.vision_adapter else None,
}, checkpoint_path / "heads.pth")
# Save state
state = {'step': step, 'loss': loss}
with open(checkpoint_path / "state.json", "w") as f:
json.dump(state, f, indent=2)
print(f" ๐พ Checkpoint: {checkpoint_path}")
def train(self):
"""Main training loop."""
print("\n" + "=" * 60)
print("๐ STARTING DETECTION TRAINING")
print("=" * 60)
print(f" Dataset: {len(self.dataset):,} samples")
print(f" Epochs: {self.config.num_epochs}")
print(f" Learning rate: {self.config.learning_rate}")
global_step = 0
best_loss = float('inf')
start_time = time.time()
for epoch in range(self.config.num_epochs):
print(f"\n๐ Epoch {epoch + 1}/{self.config.num_epochs}")
print("-" * 40)
# Shuffle
indices = list(range(len(self.dataset)))
random.shuffle(indices)
epoch_loss = 0
epoch_box_loss = 0
epoch_cls_loss = 0
num_batches = 0
for i, idx in enumerate(indices):
sample = self.dataset[idx]
loss, metrics = self.train_step(sample)
if loss == 0:
continue
epoch_loss += loss
epoch_box_loss += metrics.get('box_loss', 0)
epoch_cls_loss += metrics.get('cls_loss', 0)
num_batches += 1
global_step += 1
# Logging
if global_step % self.config.log_every == 0:
elapsed = time.time() - start_time
avg_loss = epoch_loss / num_batches
print(f" Step {global_step:5d} | Loss: {loss:.4f} | "
f"Avg: {avg_loss:.4f} | Box: {metrics.get('box_loss', 0):.4f} | "
f"Cls: {metrics.get('cls_loss', 0):.4f} | {elapsed:.0f}s")
# Checkpointing
if global_step % self.config.save_every == 0:
self.save_checkpoint(global_step, loss)
if loss < best_loss:
best_loss = loss
avg_epoch_loss = epoch_loss / max(num_batches, 1)
print(f"\n โ Epoch {epoch + 1} | Avg loss: {avg_epoch_loss:.4f} | "
f"Box: {epoch_box_loss/max(num_batches,1):.4f} | "
f"Cls: {epoch_cls_loss/max(num_batches,1):.4f}")
# Final save
print("\n" + "=" * 60)
print("๐พ Saving Final Model")
print("=" * 60)
final_path = self.checkpoint_dir / "final"
final_path.mkdir(exist_ok=True)
# Save heads
torch.save({
'detection': self.model.detection_head.state_dict(),
'point': self.model.point_head.state_dict(),
'adapter': self.model.vision_adapter.state_dict() if self.model.vision_adapter else None,
}, final_path / "heads.pth")
# Also copy over the projector
import shutil
src_projector = OCULUS_ROOT / self.config.checkpoint_path / "projector.npz"
src_config = OCULUS_ROOT / self.config.checkpoint_path / "config.json"
if src_projector.exists():
shutil.copy(src_projector, final_path / "projector.npz")
if src_config.exists():
shutil.copy(src_config, final_path / "config.json")
print(f"โ
Training complete! Model: {final_path}")
return final_path
def main():
config = DetectionTrainingConfig(
data_dir="data/coco",
max_samples=2000, # Start smaller for faster iteration
num_epochs=2,
learning_rate=5e-4,
save_every=200,
log_every=25,
)
trainer = DetectionTrainer(config)
trainer.train()
if __name__ == "__main__":
main()
|