Skip to main content

The optical lens for AI model interpretability - analyze what object detection models have learned

Project description

optik 🔬

The optical lens for AI model interpretability.

Python 3.9+ License: MIT

Analyze what object detection models (YOLO, etc.) have learned through innovative visualization and interpretability methods.

Features

  • 🔥 DetectionFlow - Multi-scale FPN attribution with separate class/bbox analysis
  • 🎯 CounterfactualProbe - Find minimal changes that flip detections
  • 📊 GridCellTrace - Visualize grid cell activations (anchor-free YOLO v8+)
  • 📄 HTML Reports - Interactive analysis reports

Installation

pip install optik-vision

Windows Users (CPU only)

PyTorch 2.9+ has DLL issues on Windows. Install torch separately first:

pip install torch==2.8.0+cpu --index-url https://download.pytorch.org/whl/cpu
pip install optik-vision

Quick Start

from optik import Optik

# Load your trained YOLO model
analyzer = Optik.from_yolo("path/to/best.pt")

# Analyze an image
results = analyzer.analyze("test_image.jpg")

# Generate HTML report
analyzer.report(results, "report.html")

CLI Usage

# Analyze images and generate report
optik analyze model.pt --images ./test_images --output report.html

Analysis Methods

DetectionFlow

Multi-scale attribution showing what image regions contribute to each detection.

flow = analyzer.detection_flow("image.jpg", detection_idx=0)
# Returns: P3/P4/P5 attribution maps, class/bbox separate attributions
print(f"Scale contributions: {flow.scale_contributions}")
# {'p3': 0.34, 'p4': 0.57, 'p5': 0.09}

What it tells you:

  • Which FPN scale (P3=small, P4=medium, P5=large objects) contributed most
  • What image regions the model focused on for classification vs bbox regression

CounterfactualProbe

Find minimal perturbations that would remove or change a detection.

cf = analyzer.counterfactual("image.jpg", detection_idx=0, target="remove")
# Returns: perturbation mask, critical regions

What it tells you:

  • Which pixels are critical for the detection
  • How robust the detection is

GridCellTrace

Visualize which grid cells activated across FPN scales.

trace = analyzer.grid_trace("image.jpg")
# Returns: per-scale activation maps, winning cells, runner-ups
print(f"Detections per scale: {trace.detections_per_scale}")

What it tells you:

  • How detections are distributed across scales
  • Which cells almost made it but were suppressed by NMS

Visualization

from optik.utils import load_image, create_detection_overlay, save_image

image = load_image("test.jpg")
results = analyzer.analyze("test.jpg")

# Create heatmap overlay focused on detection bbox
for i, flow in enumerate(results["detection_flow"]):
    overlay = create_detection_overlay(
        image, 
        flow.final_attribution,
        flow.detection.bbox,
        alpha=0.5
    )
    save_image(overlay, f"detection_{i}_heatmap.jpg")

Extensibility

optik is designed to support multiple model architectures. Currently supported:

  • ✅ Ultralytics YOLO (v8, v10, v11)

Future support planned:

  • ⏳ DETR / RT-DETR
  • ⏳ EfficientDet
  • ⏳ Custom PyTorch models

Adding a new model adapter

from optik.core.base import BaseModelAdapter

class MyModelAdapter(BaseModelAdapter):
    def get_fpn_layers(self):
        # Return FPN layer modules for hooking
        return {'p3': ..., 'p4': ..., 'p5': ...}
    
    def predict(self, image):
        # Run inference
        return [Detection(...), ...]

Requirements

  • Python 3.9+
  • PyTorch 2.0-2.8 (2.9+ has Windows issues)
  • ultralytics
  • Pillow
  • NumPy
  • Jinja2

License

MIT License - see LICENSE for details.

Citation

If you use optik-vision in your research, please cite:

@software{optik_vision2025,
  title = {optik-vision: The optical lens for AI model interpretability},
  year = {2025},
  url = {https://github.com/optik-ai/optik-vision}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

optik_vision-0.1.0.tar.gz (29.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

optik_vision-0.1.0-py3-none-any.whl (32.6 kB view details)

Uploaded Python 3

File details

Details for the file optik_vision-0.1.0.tar.gz.

File metadata

  • Download URL: optik_vision-0.1.0.tar.gz
  • Upload date:
  • Size: 29.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for optik_vision-0.1.0.tar.gz
Algorithm Hash digest
SHA256 547355ae37bf20d76fe2f11fb9fa9aab59f1db724b907aa9c1861f5eb4b89ced
MD5 e424f51490619d84fd6510ec6d6073b7
BLAKE2b-256 ab7f72b3bc51263883e39a27a70ce97a1323ed85e54e1b429169c3591e4e5b0b

See more details on using hashes here.

Provenance

The following attestation bundles were made for optik_vision-0.1.0.tar.gz:

Publisher: publish.yml on petitoff/optik

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file optik_vision-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: optik_vision-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 32.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for optik_vision-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d0377855710618ad47712f1ccfdfbb823a406af759738dcf3c273427400cb3e5
MD5 55ec91370fc0a3a46e9d680df98a7e9c
BLAKE2b-256 5466579d6fb7a1020be81b0b99f344cc199a86899df31b205ffc4af1dedddf79

See more details on using hashes here.

Provenance

The following attestation bundles were made for optik_vision-0.1.0-py3-none-any.whl:

Publisher: publish.yml on petitoff/optik

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page