Skip to main content

Advanced Anomaly Detection Environment - Deep learning library for state-of-the-art anomaly detection algorithms

Project description

๐Ÿš€ AnomaVision: Edge-Ready Visual Anomaly Detection

Python 3.9+ PyTorch 2.0+ CUDA 11.7+ ONNX Ready OpenVINO Ready TorchScript Ready License: MIT

bg

๐Ÿ”ฅ 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 through TorchBackend 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.


โœจ Benchmark Results: AnomaVision vs Anomalib (MVTec Bottle, CPU-only) bg

โœจ 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 images
  • predict(batch, gaussian_blur=True) - Detect anomalies
  • evaluate(dataloader) - Full evaluation with metrics
  • evaluate_memory_efficient(dataloader) - For large datasets
  • save_statistics(path, half=False) - Save compact statistics
  • load_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


โœจ Support & Community

๐Ÿค Getting Help

  1. ๐Ÿ“– Documentation: Check this README and code documentation
  2. ๐Ÿ” Search Issues: Someone might have had the same question
  3. ๐Ÿ’ฌ Discussions: Use GitHub Discussions for questions
  4. ๐Ÿ› Bug Reports: Create detailed issue reports with examples

๐Ÿ‘ฅ Maintainers

๐ŸŒŸ Recognition

Contributors are recognized in:

  • CONTRIBUTORS.md file
  • 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

โœจ Contact & Support

๐Ÿค Community Channels

๐Ÿ’ผ 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

๐Ÿš€ Ready to Transform Your Anomaly Detection?

Stop settling for slow, bloated solutions. Experience the future of edge-ready anomaly detection.

Get Started Run Benchmark Documentation Star Us


๐Ÿ† Benchmark Results Don't Lie: AnomaVision Wins 10/10 Metrics Deploy fast. Detect better. AnomaVision.

Made with โค๏ธ for the edge AI community

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

anomavision-3.0.10.tar.gz (51.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

anomavision-3.0.10-py3-none-any.whl (67.3 kB view details)

Uploaded Python 3

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

Hashes for anomavision-3.0.10.tar.gz
Algorithm Hash digest
SHA256 106237602636bd7b4a6709ddeb6ba385bf1827888f9939f6409bc2f7b54456b2
MD5 c32ce6433bec7d53896195b681f9dba4
BLAKE2b-256 5a71fdc72a5b81d8beb1224493803800e7942485ec11936c28acd8805db7d4d1

See more details on using hashes here.

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

Hashes for anomavision-3.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 7386eec2253b380747b517ff47389804c9081f344f4d63dde13eb6f8853b33cb
MD5 d3df92bebd4fc517c75610feabbf412a
BLAKE2b-256 40e3558e9f3f8d52360ce535b7b9139431ff678e312bc35c5c5ac0ab7dc854d0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page