
π₯ Production-ready anomaly detection powered by state-of-the-art PaDiM algorithm Deploy anywhere, run everywhere - from edge devices to cloud infrastructure
β¨ Supported Export Formats
Format | Status | Use Case | Language Support |
---|---|---|---|
PyTorch | β Ready | Development & Research | Python |
Statistics (.pth) | β Ready | Ultra-compact deployment (2-4x smaller) | Python |
ONNX | β Ready | Cross-platform deployment | Python, C++ |
TorchScript | β Ready | Production Python deployment | Python |
OpenVINO | β Ready | Intel hardware optimization | Python |
TensorRT | π§ Coming Soon | NVIDIA GPU acceleration | Python |
β¨ What's New (September 2025)
- Slim artifacts (
.pth
): Save only PaDiM statistics (mean, cov_inv, channel indices, layer indices, backbone) for 2β4Γ smaller files vs. full.pt
checkpoints - Plug-and-play loading:
.pth
loads seamlessly throughTorchBackend
and exporter via lightweight runtime (PadimLite
) with same.predict(...)
interface - CPU-first pipeline: Everything works on machines without a GPU. FP16 used only for storage; compute happens in FP32 on CPU
- Export from
.pth
: ONNX/TorchScript/OpenVINO export now accepts stats-only.pth
directly - Test coverage: New pytest cases validate saving stats, loading via
PadimLite
, CPU inference, and exporter compatibility
β¨ Why Choose AnomaVision?
π― Unmatched Performance β’ π Multi-Format Support β’ π¦ Production Ready β’ π¨ Rich Visualizations β’ π Flexible Image Dimensions
AnomaVision transforms the cutting-edge PaDiM (Patch Distribution Modeling) algorithm into a production-ready powerhouse for visual anomaly detection. Whether you're detecting manufacturing defects, monitoring infrastructure, or ensuring quality control, AnomaVision delivers enterprise-grade performance with research-level accuracy.
β¨ Installation
- Python: 3.9+
- CUDA: 11.7+ for GPU acceleration
- PyTorch: 2.0+ (automatically installed)
git clone https://github.com/DeepKnowledge1/AnomaVision.git
cd AnomaVision
poetry install
poetry shell
git clone https://github.com/DeepKnowledge1/AnomaVision.git
cd AnomaVision
pip install -r requirements.txt
python -c "import anodet; print('π AnomaVision installed successfully!')"
# Build Docker image (coming soon)
docker build -t anomavision:latest .
docker run --gpus all -v $(pwd):/workspace anomavision:latest
β¨ Quick Start
import anodet
import torch
from torch.utils.data import DataLoader
# π Load your "good" training images
dataset = anodet.AnodetDataset(
"path/to/train/good",
resize=[256, 192], # Flexible width/height
crop_size=[224, 224], # Final crop size
normalize=True # ImageNet normalization
)
dataloader = DataLoader(dataset, batch_size=4)
# π§ Initialize PaDiM with optimal settings
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = anodet.Padim(
backbone='resnet18', # Fast and accurate
device=device,
layer_indices=[0, 1], # Multi-scale features
feat_dim=100 # Optimal feature dimension
)
# π₯ Train the model (surprisingly fast!)
print("π Training model...")
model.fit(dataloader)
# πΎ Save for production deployment
torch.save(model, "anomaly_detector.pt")
model.save_statistics("compact_model.pth", half=True) # 4x smaller!
print("β
Model trained and saved!")
# π Load test data and detect anomalies (uses same preprocessing as training)
test_dataset = anodet.AnodetDataset("path/to/test/images")
test_dataloader = DataLoader(test_dataset, batch_size=4)
for batch, images, _, _ in test_dataloader:
# π― Get anomaly scores and detailed heatmaps
image_scores, score_maps = model.predict(batch)
# π·οΈ Classify anomalies (threshold=13 works great for most cases)
predictions = anodet.classification(image_scores, threshold=13)
print(f"π₯ Anomaly scores: {image_scores.tolist()}")
print(f"π Predictions: {predictions.tolist()}")
break
# π¦ Export to ONNX for universal deployment
python export.py \
--model_data_path "./models/" \
--model "padim_model.pt" \
--format onnx \
--opset 17
print("β
ONNX model ready for deployment!")
β¨ Real-World Examples
# Using command line arguments
python train.py \
--dataset_path "data/bottle" \
--class_name "bottle" \
--model_data_path "./models/" \
--backbone resnet18 \
--batch_size 8 \
--layer_indices 0 1 2 \
--feat_dim 200 \
--resize 256 224 \
--crop_size 224 224 \
--normalize
# Or using config file (recommended)
python train.py --config config.yml
Sample config.yml:
# Dataset configuration
dataset_path: "D:/01-DATA"
class_name: "bottle"
resize: [256, 224] # Width, Height - flexible dimensions!
crop_size: [224, 224] # Final square crop
normalize: true
norm_mean: [0.485, 0.456, 0.406]
norm_std: [0.229, 0.224, 0.225]
# Model configuration
backbone: "resnet18"
feat_dim: 100
layer_indices: [0, 1]
batch_size: 8
# Output configuration
model_data_path: "./distributions/bottle_exp"
output_model: "padim_model.pt"
run_name: "bottle_experiment"
# Automatically uses training configuration
python detect.py \
--model_data_path "./distributions/bottle_exp" \
--model "padim_model.pt" \
--img_path "data/bottle/test/broken_large" \
--batch_size 16 \
--thresh 13 \
--enable_visualization \
--save_visualizations
# Multi-format support
python detect.py --model padim_model.pt # PyTorch
python detect.py --model padim_model.torchscript # TorchScript
python detect.py --model padim_model.onnx # ONNX Runtime
python detect.py --model padim_model_openvino # OpenVINO
# Or using config file (recommended)
python train.py --config config.yml
# Uses saved configuration automatically
python eval.py \
--model_data_path "./distributions/bottle_exp" \
--model "padim_model.pt" \
--dataset_path "data/mvtec" \
--class_name "bottle" \
--batch_size 8
# Or using config file (recommended)
python eval.py --config config.yml
# Export to all formats
python export.py \
--model_data_path "./distributions/bottle_exp" \
--model "padim_model.pt" \
--format all
# Or using config file (recommended)
python export.py --config config.yml
from anodet.inference.model.wrapper import ModelWrapper
# π― Automatically detect and load ANY supported format
pytorch_model = ModelWrapper("model.pt", device='cuda') # PyTorch
onnx_model = ModelWrapper("model.onnx", device='cuda') # ONNX Runtime
torchscript_model = ModelWrapper("model.torchscript", device='cuda') # TorchScript
openvino_model = ModelWrapper("model_openvino/model.xml", device='cpu') # OpenVINO
# π Unified prediction interface - same API for all formats!
scores, maps = pytorch_model.predict(batch)
scores, maps = onnx_model.predict(batch)
# π§Ή Always clean up resources
pytorch_model.close()
onnx_model.close()
// C++ ONNX Runtime integration example
#include <onnxruntime_cxx_api.h>
#include <iostream>
#include <vector>
#include <string>
#include <chrono>
#include <numeric>
#include <algorithm>
.
.
.
.
β¨ Configuration Guide
Parameter | Description | Default | Range | Pro Tip |
---|---|---|---|---|
backbone |
Feature extractor | resnet18 |
resnet18 , wide_resnet50 |
Use ResNet18 for speed, Wide-ResNet50 for accuracy |
layer_indices |
ResNet layers | [0] |
[0, 1, 2, 3] |
[0, 1] gives best speed/accuracy balance |
feat_dim |
Feature dimensions | 50 |
1-2048 |
Higher = more accurate but slower |
batch_size |
Training batch size | 2 |
1-64 |
Use largest size that fits in memory |
Parameter | Description | Default | Example | Pro Tip |
---|---|---|---|---|
resize |
Initial resize | [224, 224] |
[256, 192] |
Flexible width/height, maintains aspect ratio |
crop_size |
Final crop size | None |
[224, 224] |
Square crops often work best for CNN models |
normalize |
ImageNet normalization | true |
true/false |
Usually improves performance with pretrained models |
norm_mean |
RGB mean values | [0.485, 0.456, 0.406] |
Custom values | Use ImageNet stats for pretrained backbones |
norm_std |
RGB std values | [0.229, 0.224, 0.225] |
Custom values | Match your training data distribution |
Parameter | Description | Default | Range | Pro Tip |
---|---|---|---|---|
thresh |
Anomaly threshold | 13 |
1-100 |
Start with 13, tune based on your data |
enable_visualization |
Show results | false |
true/false |
Great for debugging and demos |
save_visualizations |
Save images | false |
true/false |
Essential for production monitoring |
# =========================
# Dataset / preprocessing (shared by train, detect, eval)
# =========================
dataset_path: "D:/01-DATA" # Root dataset folder
class_name: "bottle" # Class name for MVTec dataset
resize: [224, 224] # Resize dimensions [width, height]
crop_size: [224, 224] # Final crop size [width, height]
normalize: true # Whether to normalize images
norm_mean: [0.485, 0.456, 0.406] # ImageNet normalization mean
norm_std: [0.229, 0.224, 0.225] # ImageNet normalization std
# =========================
# Model / training
# =========================
backbone: "resnet18" # Backbone CNN architecture
feat_dim: 50 # Feature dimension size
layer_indices: [0] # Which backbone layers to use
model_data_path: "./distributions/exp" # Path to store model data
output_model: "padim_model.pt" # Saved model filename
batch_size: 2 # Training/inference batch size
device: "auto" # Device: "cpu", "cuda", or "auto"
# =========================
# Inference (detect.py)
# =========================
img_path: "D:/01-DATA/bottle/test/broken_large" # Test images path
thresh: 13.0 # Anomaly detection threshold
enable_visualization: true # Enable visualizations
save_visualizations: true # Save visualization results
viz_output_dir: "./visualizations/" # Visualization output directory
# =========================
# Export (export.py)
# =========================
format: "all" # Export format: onnx, torchscript, openvino, all
opset: 17 # ONNX opset version
dynamic_batch: true # Allow dynamic batch size
fp32: false # Export precision (false = FP16 for OpenVINO)
β¨ Complete API Reference
model = anodet.Padim(
backbone='resnet18', # 'resnet18' | 'wide_resnet50'
device=torch.device('cuda'), # Target device
layer_indices=[0, 1, 2], # ResNet layers [0-3]
feat_dim=100, # Feature dimensions (1-2048)
channel_indices=None # Optional channel selection
)
π₯ Methods:
fit(dataloader, extractions=1)
- Train on normal imagespredict(batch, gaussian_blur=True)
- Detect anomaliesevaluate(dataloader)
- Full evaluation with metricsevaluate_memory_efficient(dataloader)
- For large datasetssave_statistics(path, half=False)
- Save compact statisticsload_statistics(path, device, force_fp32=True)
- Load statistics
dataset = anodet.AnodetDataset(
"path/to/images", # Image directory
resize=[256, 192], # Flexible width/height resize
crop_size=[224, 224], # Final crop dimensions
normalize=True, # ImageNet normalization
mean=[0.485, 0.456, 0.406], # Custom mean values
std=[0.229, 0.224, 0.225] # Custom std values
)
# For MVTec format with same flexibility
mvtec_dataset = anodet.MVTecDataset(
"path/to/mvtec",
class_name="bottle",
is_train=True,
resize=[300, 300], # Square resize
crop_size=[224, 224], # Final crop
normalize=True
)
wrapper = ModelWrapper(
model_path="model.onnx", # Any supported format (.pt, .onnx, .torchscript, etc.)
device='cuda' # Target device
)
# π― Unified API for all formats
scores, maps = wrapper.predict(batch)
wrapper.close() # Always clean up!
# π·οΈ Smart classification with optimal thresholds
predictions = anodet.classification(scores, threshold=15)
# π Comprehensive evaluation metrics
images, targets, masks, scores, maps = model.evaluate(dataloader)
# π¨ Rich visualization functions
boundary_images = anodet.visualization.framed_boundary_images(images, classifications)
heatmap_images = anodet.visualization.heatmap_images(images, score_maps)
highlighted_images = anodet.visualization.highlighted_images(images, classifications)
from anodet.config import load_config
from anodet.utils import merge_config
# Load configuration from file
config = load_config("config.yml")
# Merge with command line arguments
final_config = merge_config(args, config)
# Image processing with automatic parameter application
dataset = anodet.AnodetDataset(
image_path,
resize=config.resize, # From config: [256, 224]
crop_size=config.crop_size, # From config: [224, 224]
normalize=config.normalize, # From config: true
mean=config.norm_mean, # From config: ImageNet values
std=config.norm_std # From config: ImageNet values
)
β¨ Architecture Overview
AnomaVision/
βββ π§ anodet/ # Core AI library
β βββ π padim.py # PaDiM implementation
β βββ π padim_lite.py # Lightweight runtime module
β βββ π feature_extraction.py # ResNet feature extraction
β βββ π mahalanobis.py # Distance computation
β βββ π datasets/ # Dataset loaders with flexible sizing
β βββ π visualization/ # Rich visualization tools
β βββ π inference/ # Multi-format inference engine
β β βββ π wrapper.py # Universal model wrapper
β β βββ π modelType.py # Format detection
β β βββ π backends/ # Format-specific backends
β β βββ π base.py # Backend interface
β β βββ π torch_backend.py # PyTorch support
β β βββ π onnx_backend.py # ONNX Runtime support
β β βββ π torchscript_backend.py # TorchScript support
β β βββ π tensorrt_backend.py # TensorRT (coming soon)
β β βββ π openvino_backend.py # OpenVINO support
β βββ π config/ # Configuration management
β βββ π utils.py # Utility functions
βββ π train.py # Training script with config support
βββ π detect.py # Inference script
βββ π eval.py # Evaluation script
βββ π export.py # Multi-format export utilities
βββ π config.yml # Default configuration
βββ π notebooks/ # Interactive examples
β¨ Contributing
We love contributions! Here's how to make AnomaVision even better:
# π₯ Fork and clone
git clone https://github.com/yourusername/AnomaVision.git
cd AnomaVision
# π§ Setup development environment
poetry install --dev
pre-commit install
# πΏ Create feature branch
git checkout -b feature/awesome-improvement
# π¨ Make your changes
# ... code, test, commit ...
# π Submit pull request
git push origin feature/awesome-improvement
- Code Style: Follow PEP 8 with 88-character line limit (Black formatting)
- Type Hints: Add type hints to all new functions and methods
- Docstrings: Use Google-style docstrings for all public functions
- Tests: Add pytest tests for new functionality
- Documentation: Update README and docstrings as needed
- Bug Reports: Use the bug report template
- Feature Requests: Use the feature request template
- Questions: Use GitHub Discussions
β¨ Support & Community
- π Documentation: Check this README and code documentation
- π Search Issues: Someone might have had the same question
- π¬ Discussions: Use GitHub Discussions for questions
- π Bug Reports: Create detailed issue reports with examples
- Core Team: @DeepKnowledge1
- Contributors: See CONTRIBUTORS.md
Contributors are recognized in:
CONTRIBUTORS.md
file- Release notes
- GitHub contributors page
β¨ Roadmap
- π TensorRT Backend: NVIDIA GPU acceleration
- π± Mobile Export: CoreML and TensorFlow Lite support
- π§ C++ API: Native C++ library with Python bindings
- π― AutoML: Automatic hyperparameter optimization
- π§ Transformer Models: Vision Transformer (ViT) backbone support
- π Online Learning: Continuous model updates
- π MLOps Integration: MLflow, Weights & Biases support
- π Web Interface: Browser-based inference and visualization
- π₯ Video Anomaly Detection: Temporal anomaly detection
- π Multi-Class Support: Beyond binary anomaly detection
- β‘ Quantization: INT8 optimization for edge devices
- π Integration: Kubernetes operators and Helm charts
β¨ License & Citation
AnomaVision is released under the MIT License - see LICENSE for details.
If AnomaVision helps your research or project, we'd appreciate a citation:
@software{anomavision2025,
title={AnomaVision: Edge-Ready Visual Anomaly Detection},
author={DeepKnowledge Contributors},
year={2025},
url={https://github.com/DeepKnowledge1/AnomaVision},
version={2.0.46},
note={High-performance anomaly detection library optimized for edge deployment}
}
AnomaVision builds upon the excellent work of:
- PaDiM: Original algorithm by Defard et al.
- PyTorch: Deep learning framework
- ONNX: Open Neural Network Exchange
- OpenVINO: Intel's inference optimization toolkit
- Anomalib: Intel's anomaly detection library (for inspiration)
β¨ Related Projects
- Anodet: anomaly detection
β¨ Contact & Support
- π¬ GitHub Discussions: Community Forum
- π Issues: Bug Reports & Features
- π§ Email: [email protected]
- π Documentation: Wiki
For enterprise deployments, custom integrations, or commercial support:
- π’ Enterprise Consulting: Available upon request
- π Training Workshops: Custom training for your team
- π§ Custom Development: Tailored solutions for your use case