How yeonghyeon/cvae-anomalydetection-pytorch works
Project Type and Purpose
• This is a standalone machine-learning experiment implementing Anomaly Detection with a Convolutional Variational Auto-Encoder (CVAE) in PyTorch.
• It trains on MNIST or Fashion-MNIST data, learning a low-dimensional latent representation and flagging outliers by reconstruction error.
File Structure Analysis
• Top‐level files:
• README.md (project description, results, architecture)
• run.py (CLI entry point to configure and launch training/evaluation)
• source/ directory:
• datamanager.py – data loading, preprocessing, batching (MNIST & Fashion-MNIST)
• neuralnet.py – defines the CVAE model (encoder, decoder, reparameterization)
• solver.py – training loop, loss (reconstruction + KL divergence), optimizer setup, model checkpointing, evaluation metrics (AUROC)
Key Components
a. Data Pipeline
• Raw Dataset (MNIST / Fashion-MNIST)
• DataManager module: preprocessing → batching → train/test split
b. Model
• CVAE Encoder: convolutional layers → μ, σ outputs
• Latent Sampling: reparameterization trick
• CVAE Decoder: transpose convolutions → reconstructed image
c. Solver / Training
• Loss Computation: reconstruction loss + KL divergence
• Optimizer (e.g., Adam)
• Epoch loop, backpropagation, weight updates
d. Evaluation & Visualization
• Reconstruction outputs, latent-space projections
• Histogram of reconstruction errors → anomaly thresholding → AUROC
• Figures module (Matplotlib)
Relationships and Data Flow
• Encoder → Latent Code → Decoder
• Decoder → Reconstruction → Loss
• Loss → Solver → Weight updates → Model
• After each epoch/model variant: feed test set → generate plots/histograms → compute AUROC
Architectural Patterns and Principles
• Modular separation: DataManager, Model, Solver, Run Script
• Single responsibility per module (loading, modeling, training, orchestrating)
• Pipeline pattern: sequential data flow from ingestion → transformation → output
Relevant Technologies and Libraries
• PyTorch (torch, torch.nn, torch.optim)
• NumPy for array ops
• Matplotlib for visualizations
• Scikit-learn for AUROC metric
• Python 3.7 runtime
Final Advice
• Hyperparameters (latent dim, batch size) and metrics (AUROC).