Python API Reference
What you will find here
User-facing Python API for integrating BNNR with your own model and dataloaders.
When to use this page
Use this when CLI presets are not enough and you need full control.
Source of truth
Implemented in src/bnnr/. The package exposes two import tiers:
- Stable API — names in
bnnr.__all__(recommended for new code). - Backward-compatible imports — additional names importable as
from bnnr import …but emitDeprecationWarning; preferfrom bnnr.<subpackage> import …(see tests/test_backward_compat.py).
Detection, events, config I/O, and extended XAI symbols are not in __all__; import them from the submodules listed below.
Stable API (bnnr.__all__)
| Symbol | Role |
|---|---|
__version__ | Package version string |
BNNRConfig, BNNRTrainer | Config model and training loop |
quick_run | One-call classification training |
ModelAdapter, SimpleTorchAdapter | Model integration protocols |
BNNRRunResult, Reporter, load_report, compare_runs | Run outputs and comparison |
BaseAugmentation, BasicAugmentation, ChurchNoise | Core augmentation types |
ICD, AICD | Saliency-guided augmentations |
auto_select_augmentations, get_preset, list_presets | Preset helpers |
OptiCAMExplainer, generate_saliency_maps | Classification XAI |
start_dashboard | Live dashboard helper |
analyze_model, AnalysisReport | Standalone model diagnostics |
Quickstart API (recommended)
quick_run(model, train_loader, val_loader, ...)— one-call classification training withdefault_train_config()defaults (m_epochs=3,max_iterations=2,device=auto, XAI on). Inferstarget_layersfrom the lastConv2dwhen omitted. Setdashboard=Trueto start the live dashboard beforerun()(non-blocking afterward). Override via kwargs, e.g.m_epochs=1. Detection and multi-label: see Golden Path.
import bnnr
result = bnnr.quick_run(model, train_loader, val_loader)
print(result.best_metrics)Returns BNNRRunResult.
Core training API (low-level)
Stable: BNNRConfig, BNNRTrainer, BNNRRunResult.
Backward-compatible: CheckpointInfo — from bnnr.reporting import CheckpointInfo.
Model adapter API
Stable: ModelAdapter, SimpleTorchAdapter.
Backward-compatible: XAICapableModel — from bnnr.adapter import XAICapableModel.
Analysis API (standalone model diagnostics)
analyze_model(adapter, val_loader, *, task, output_dir, run_data_quality, max_worst, xai_enabled, xai_method, xai_samples, cv_folds, data_quality_max_samples)— run full analysis (metrics, XAI, data quality, failure analysis, patterns, recommendations) on an adapter and validation loader; returnsAnalysisReport.AnalysisReport— dataclass; core attributes:metrics,per_class_accuracy,confusion,xai_insights,xai_diagnoses,xai_quality_summary,data_quality_result,failure_patterns,recommendations; extended attributes (v0.2+):schema_version,executive_summary,findings,recommendations_structured,class_diagnostics,true_distribution,pred_distribution,distribution_summary,failure_patterns_extended,xai_quality_per_class,xai_examples_per_class,data_quality_summary,cv_results,calibration_summary,confusion_pair_xai,best_worst_examples,analysis_scope; methods:save(output_dir),to_html(path),failure_patterns_list().
See analyze.md for usage and CLI.
Reporting and events API
Stable: Reporter, load_report, compare_runs.
Backward-compatible (prefer submodule imports):
JsonlEventSink,EVENT_SCHEMA_VERSION("2.1"),replay_events—from bnnr.events import …
Config helpers (backward-compatible)
Import from bnnr.config (top-level from bnnr import … is deprecated):
load_config,save_config,validate_config,merge_configsapply_xai_preset,get_xai_preset,list_xai_presets— presetsxai_light,xai_full,xai_adaptive
CLI defaults without YAML: default_train_config() (m_epochs=3, max_iterations=2, device="auto"), default_demo_config() (m_epochs=1, max_iterations=1).
Augmentation API
Stable: BaseAugmentation, BasicAugmentation, ChurchNoise, ICD, AICD, auto_select_augmentations, get_preset, list_presets.
Backward-compatible — from bnnr.augmentations import … / from bnnr.augmentation_runner import …:
AugmentationRegistry,AugmentationRunner,TorchvisionAugmentationDifPresets,Drust,LuxferGlass,ProCAM,Smugs,TeaStains
Optional backends — from bnnr.kornia_aug import …, from bnnr.albumentations_aug import …:
KorniaAugmentation,create_kornia_pipeline,kornia_availableAlbumentationsAugmentation,albumentations_available
XAI API (classification)
Stable: OptiCAMExplainer, generate_saliency_maps, ICD, AICD.
Backward-compatible — from bnnr.xai import …, from bnnr.xai_cache import …, from bnnr.xai_analysis import …:
- Explainers:
BaseExplainer,NMFConceptExplainer,CRAFTExplainer,RealCRAFTExplainer,RecursiveCRAFTExplainer generate_craft_concepts,generate_nmf_concepts,save_xai_visualizationanalyze_xai_batch,analyze_xai_batch_rich,compute_xai_quality_scoregenerate_class_diagnosis,generate_class_insight,generate_epoch_summary,generate_rich_epoch_summaryXAICache
Dashboard helper
start_dashboard— returns the LAN URL when the background server starts, orNoneif optional dashboard dependencies are missing (no misleading localhost URL).
Minimal classification integration
import torch
import torch.nn as nn
from bnnr import BNNRConfig, BNNRTrainer, SimpleTorchAdapter, auto_select_augmentations
model = ...
train_loader = ... # (image, label, index)
val_loader = ...
adapter = SimpleTorchAdapter(
model=model,
criterion=nn.CrossEntropyLoss(),
optimizer=torch.optim.Adam(model.parameters(), lr=1e-3),
target_layers=[...],
device="auto",
)
config = BNNRConfig(m_epochs=3, max_iterations=2, device="auto")
trainer = BNNRTrainer(adapter, train_loader, val_loader, auto_select_augmentations(), config)
result = trainer.run()
print(result.best_metrics)Detection
Import detection symbols from submodules (top-level from bnnr import DetectionAdapter is deprecated).
Model adapters
from bnnr.detection_adapter import DetectionAdapter, UltralyticsDetectionAdapter
DetectionAdapter(model, optimizer, target_layers=None, device="cuda", scheduler=None, use_amp=False, score_threshold=0.05)— wraps torchvision-style detectors (Faster R-CNN, RetinaNet, SSD, FCOS). In train mode callsmodel(images, targets)for losses; in eval mode callsmodel(images)for prediction dicts.deviceacceptscuda,cpu, orauto.UltralyticsDetectionAdapter(model_name="yolov8n.pt", device="cuda", score_threshold=0.05, num_classes=None, lr=1e-3, optimizer=None, use_amp=False)— wraps Ultralytics YOLO. Runs inference viaYOLO.predictwith 0–255 scaling, and exposespredict_detection_dicts(batch_bchw)for XAI and probe snapshots.
Both adapters implement train_step, eval_step, epoch_end_eval, epoch_end, state_dict, load_state_dict, get_target_layers, and get_model.
For raw ultralytics.nn.tasks modules without this adapter, detection XAI stays disabled; see troubleshooting.md §17.
Collate functions
from bnnr.detection_collate import detection_collate_fn, detection_collate_fn_with_index
detection_collate_fn(batch)→(Tensor[B,C,H,W], list[dict])— stack images, keep targets as listdetection_collate_fn_with_index(batch)→(Tensor[B,C,H,W], list[dict], Tensor[B])— same, with sample indices
Detection augmentations
from bnnr.detection_augmentations import … — bbox-aware transforms (subclass BboxAwareAugmentation):
DetectionHorizontalFlip— horizontal flip with bbox mirroringDetectionVerticalFlip— vertical flip with bbox mirroringDetectionRandomRotate90— 90° rotation with bbox transformDetectionRandomScale(scale_range=(0.8, 1.2))— random resize with bbox scalingMosaicAugmentation(output_size=(640, 640))— 4-image mosaic composition (requiresset_pool)DetectionMixUp(alpha_range=(0.3, 0.7))— alpha-blend two images with merged targets (requiresset_pool)AlbumentationsBboxAugmentation(transform)— Albumentations wrapper with bbox support
XAI-driven augmentations:
DetectionICD(threshold_percentile=70.0, tile_size=8, fill_strategy="gaussian_blur")— masks high-saliency (object) tilesDetectionAICD(threshold_percentile=70.0, tile_size=8, fill_strategy="gaussian_blur")— masks low-saliency (background) tiles
Presets: from bnnr.detection_augmentations import get_detection_preset — name ∈ {"light", "standard", "aggressive"}.
Detection metrics
from bnnr.detection_metrics import …
calculate_detection_metrics(predictions, targets, iou_thresholds=None, score_threshold=0.0)→{"map_50": float, "map_50_95": float}calculate_per_class_ap(predictions, targets, iou_threshold=0.5, class_names=None)→ per-class AP dictcalculate_detection_confusion_matrix(predictions, targets, num_classes=None, iou_threshold=0.5)→{"labels", "matrix"}
Detection XAI
from bnnr.detection_xai import …
generate_detection_saliency(model, images, target_layers, device="cpu", forward_layout="torchvision_list"|"ultralytics_bchw")— backbone activation–based class-agnostic saliencycompute_detection_box_saliency_occlusion(...)— per-box occlusion grid saliencydraw_boxes_on_image(...)— draw xyxy boxes with optional labels, scores, and class namesoverlay_saliency_heatmap(...)— blend normalized saliency with colormapsave_detection_xai_panels(...)— writes{stem}_gt.png,{stem}_sal.png,{stem}_pred.pngtriptych
See Detection for the full detection guide with examples.