Advanced Anomaly Detection Environment - Deep learning library for state-of-the-art anomaly detection algorithms
Project description
๐ AnomaVision: Edge-Ready Visual Anomaly Detection
๐ฅ 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.ptcheckpoints - Plug-and-play loading:
.pthloads seamlessly throughTorchBackendand 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.pthdirectly - 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.
โจ Benchmark Results: AnomaVision vs Anomalib (MVTec Bottle, CPU-only)
โจ Installation
๐ Prerequisites
- Python: 3.9+
- CUDA: 11.7+ for GPU acceleration
- PyTorch: 2.0+ (automatically installed)
๐ฏ Method 1: Poetry (Recommended)
git clone https://github.com/DeepKnowledge1/AnomaVision.git
cd AnomaVision
poetry install
poetry shell
๐ฏ Method 2: pip
git clone https://github.com/DeepKnowledge1/AnomaVision.git
cd AnomaVision
pip install -r requirements.txt
โ Verify Installation
python -c "import anomavision; print('๐ AnomaVision installed successfully!')"
๐ณ Docker Support
# Build Docker image (coming soon)
docker build -t anomavision:latest .
docker run --gpus all -v $(pwd):/workspace anomavision:latest
โจ Quick Start
๐ฏ Train Your First Model (2 minutes)
import anomavision
import torch
from torch.utils.data import DataLoader
# ๐ Load your "good" training images
dataset = anomavision.anomavisionDataset(
"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 = anomavision.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!")
๐ Detect Anomalies Instantly
# ๐ Load test data and detect anomalies (uses same preprocessing as training)
test_dataset = anomavision.anomavisionDataset("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 = anomavision.classification(image_scores, threshold=13)
print(f"๐ฅ Anomaly scores: {image_scores.tolist()}")
print(f"๐ Predictions: {predictions.tolist()}")
break
๐ Export for Production Deployment
# ๐ฆ 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
๐ฅ๏ธ Command Line Interface
๐ Train a High-Performance Model
# 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"
๐ Run Lightning-Fast Inference
# 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
๐ Comprehensive Model Evaluation
# 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 Multiple Formats
# 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
๐ Universal Model Format Support
from anomavision.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 Integration
// C++ ONNX Runtime integration example
#include <onnxruntime_cxx_api.h>
#include <iostream>
#include <vector>
#include <string>
#include <chrono>
#include <numeric>
#include <algorithm>
.
.
.
.
โจ Configuration Guide
๐ฏ Training Parameters
| 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 |
๐ Image Processing Parameters
| 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 |
๐ Inference Parameters
| 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 |
๐ Configuration File Structure
# =========================
# 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
๐ง Core Classes
anomavision.Padim - The Heart of AnomaVision
model = anomavision.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
anomavision.anomavisionDataset - Smart Data Loading with Flexible Sizing
dataset = anomavision.anomavisionDataset(
"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 = anomavision.MVTecDataset(
"path/to/mvtec",
class_name="bottle",
is_train=True,
resize=[300, 300], # Square resize
crop_size=[224, 224], # Final crop
normalize=True
)
ModelWrapper - Universal Model Interface
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!
๐ ๏ธ Utility Functions
# ๐ท๏ธ Smart classification with optimal thresholds
predictions = anomavision.classification(scores, threshold=15)
# ๐ Comprehensive evaluation metrics
images, targets, masks, scores, maps = model.evaluate(dataloader)
# ๐จ Rich visualization functions
boundary_images = anomavision.visualization.framed_boundary_images(images, classifications)
heatmap_images = anomavision.visualization.heatmap_images(images, score_maps)
highlighted_images = anomavision.visualization.highlighted_images(images, classifications)
โ๏ธ Configuration Management
from anomavision.config import load_config
from anomavision.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 = anomavision.anomavisionDataset(
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/
โโโ ๐ง anomavision/ # 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:
๐ Quick Start for Contributors
# ๐ฅ 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
๐ Development Guidelines
- 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 & Feature Requests
- Bug Reports: Use the bug report template
- Feature Requests: Use the feature request template
- Questions: Use GitHub Discussions
โจ Support & Community
๐ค Getting Help
- ๐ 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
๐ฅ Maintainers
- Core Team: @DeepKnowledge1
- Contributors: See CONTRIBUTORS.md
๐ Recognition
Contributors are recognized in:
CONTRIBUTORS.mdfile- Release notes
- GitHub contributors page
โจ Roadmap
๐ Q4 2025
- ๐ TensorRT Backend: NVIDIA GPU acceleration
- ๐ฑ Mobile Export: CoreML and TensorFlow Lite support
- ๐ง C++ API: Native C++ library with Python bindings
- ๐ฏ AutoML: Automatic hyperparameter optimization
๐ Q1 2026
- ๐ง 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
๐ Q2 2026
- ๐ฅ 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
๐ MIT License
AnomaVision is released under the MIT License - see LICENSE for details.
๐ Citation
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}
}
๐ Acknowledgments
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
- anomavision: anomaly detection
โจ Contact & Support
๐ค Community Channels
- ๐ฌ GitHub Discussions: Community Forum
- ๐ Issues: Bug Reports & Features
- ๐ง Email: deepp.knowledge@gmail.com
- ๐ Documentation: Wiki
๐ผ Enterprise Support
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file anomavision-3.0.10.tar.gz.
File metadata
- Download URL: anomavision-3.0.10.tar.gz
- Upload date:
- Size: 51.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.9.0 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
106237602636bd7b4a6709ddeb6ba385bf1827888f9939f6409bc2f7b54456b2
|
|
| MD5 |
c32ce6433bec7d53896195b681f9dba4
|
|
| BLAKE2b-256 |
5a71fdc72a5b81d8beb1224493803800e7942485ec11936c28acd8805db7d4d1
|
File details
Details for the file anomavision-3.0.10-py3-none-any.whl.
File metadata
- Download URL: anomavision-3.0.10-py3-none-any.whl
- Upload date:
- Size: 67.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.9.0 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7386eec2253b380747b517ff47389804c9081f344f4d63dde13eb6f8853b33cb
|
|
| MD5 |
d3df92bebd4fc517c75610feabbf412a
|
|
| BLAKE2b-256 |
40e3558e9f3f8d52360ce535b7b9139431ff678e312bc35c5c5ac0ab7dc854d0
|