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
This page documents only symbols exported publicly from src/bnnr/__init__.py.
Core training API
BNNRConfigBNNRTrainerquick_runBNNRRunResultCheckpointInfo
Model adapter API
ModelAdapterXAICapableModelSimpleTorchAdapterDetectionAdapter
Reporting and events API
Reporterload_reportcompare_runsJsonlEventSinkEVENT_SCHEMA_VERSIONreplay_events
Config helpers
load_configsave_configvalidate_configmerge_configsapply_xai_presetget_xai_presetlist_xai_presets
Augmentation API
BaseAugmentationAugmentationRegistryAugmentationRunnerTorchvisionAugmentationKorniaAugmentationAlbumentationsAugmentationcreate_kornia_pipelinekornia_availablealbumentations_available
Built-in classification augmentations:
ChurchNoiseBasicAugmentationDifPresetsDrustLuxferGlassProCAMSmugsTeaStains
Preset helpers:
auto_select_augmentationsget_presetlist_presets
XAI API (classification)
Explainers and generation:
BaseExplainerOptiCAMExplainerNMFConceptExplainerCRAFTExplainerRealCRAFTExplainerRecursiveCRAFTExplainergenerate_saliency_mapsgenerate_craft_conceptsgenerate_nmf_conceptssave_xai_visualization
Analysis and scoring:
analyze_xai_batchanalyze_xai_batch_richcompute_xai_quality_scoregenerate_class_diagnosisgenerate_class_insightgenerate_epoch_summarygenerate_rich_epoch_summary
Cache:
XAICache
ICD variants:
ICDAICD
Detection API
Augmentations and presets:
BboxAwareAugmentationAlbumentationsBboxAugmentationDetectionHorizontalFlipDetectionVerticalFlipDetectionRandomRotate90DetectionRandomScaleMosaicAugmentationDetectionMixUpget_detection_preset
XAI-driven detection augmentations:
DetectionICDDetectionAICD
Data helpers and metrics:
detection_collate_fndetection_collate_fn_with_indexcalculate_detection_metrics
Dashboard helper
start_dashboard
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)Minimal detection integration
import torch
from bnnr import BNNRConfig, BNNRTrainer
from bnnr import DetectionAdapter, DetectionHorizontalFlip
model = ...
optimizer = torch.optim.SGD(model.parameters(), lr=0.005, momentum=0.9)
adapter = DetectionAdapter(model=model, optimizer=optimizer, device="cpu")
config = BNNRConfig(task="detection", m_epochs=1, max_iterations=1, device="cpu")
augmentations = [DetectionHorizontalFlip(probability=0.5, random_state=42)]
trainer = BNNRTrainer(adapter, train_loader, val_loader, augmentations, config)
result = trainer.run()Detection dataloader contract:
- batch item:
(image, target, index) target["boxes"]:FloatTensor[N,4]target["labels"]:IntTensor[N]
quick_run() helper
quick_run() builds SimpleTorchAdapter internally.
from bnnr import quick_run
result = quick_run(
model=model,
train_loader=train_loader,
val_loader=val_loader,
)Useful arguments include augmentations, config/overrides, criterion, optimizer, target_layers, and eval_metrics.