Metadata-Version: 2.4
Name: optik-vision
Version: 0.1.0
Summary: The optical lens for AI model interpretability - analyze what object detection models have learned
Author: optik contributors
License: MIT
Project-URL: Homepage, https://github.com/optik-ai/optik-vision
Project-URL: Documentation, https://optik-ai.github.io/optik-vision
Project-URL: Repository, https://github.com/optik-ai/optik-vision
Project-URL: Issues, https://github.com/optik-ai/optik-vision/issues
Keywords: machine-learning,deep-learning,interpretability,explainability,xai,yolo,object-detection,gradcam,pytorch
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Image Recognition
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: torch<2.9.0,>=2.0.0
Requires-Dist: numpy>=1.20.0
Requires-Dist: Pillow>=9.0.0
Requires-Dist: jinja2>=3.0.0
Requires-Dist: ultralytics>=8.0.0
Provides-Extra: dev
Requires-Dist: pytest>=7.0.0; extra == "dev"
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
Requires-Dist: black>=23.0.0; extra == "dev"
Requires-Dist: ruff>=0.1.0; extra == "dev"
Requires-Dist: mypy>=1.0.0; extra == "dev"
Provides-Extra: docs
Requires-Dist: mkdocs>=1.5.0; extra == "docs"
Requires-Dist: mkdocs-material>=9.0.0; extra == "docs"
Dynamic: license-file
Dynamic: requires-python

# optik 🔬

**The optical lens for AI model interpretability.**

[![Python 3.9+](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/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

```bash
pip install optik-vision
```

### Windows Users (CPU only)
PyTorch 2.9+ has DLL issues on Windows. Install torch separately first:
```bash
pip install torch==2.8.0+cpu --index-url https://download.pytorch.org/whl/cpu
pip install optik-vision
```

## Quick Start

```python
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

```bash
# 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.

```python
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.

```python
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.

```python
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

```python
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

```python
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](LICENSE) for details.

## Citation

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

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