Skip to content

DeepKnowledge1/AnomaVision

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ 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 anodet; 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 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!")

πŸ” Detect Anomalies Instantly

# πŸ“Š 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 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 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 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

anodet.Padim - The Heart of AnomaVision

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 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

anodet.AnodetDataset - Smart Data Loading with Flexible Sizing

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
)

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 = 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)

βš™οΈ Configuration Management

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:

πŸš€ 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

About

Optimized PaDim based on Anodet

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published