How phatnguyen6635/glass works
Project Type and Purpose
• This is a machine-learning research codebase (full ML pipeline) implementing “GLASS,” a unified anomaly synthesis strategy for unsupervised industrial anomaly detection/localization in PyTorch.
• It provides data ingestion, synthetic anomaly generation (via gradient ascent), model training, evaluation, and optional ONNX export.
File Structure Analysis
• Top-level Python modules:
• main.py – orchestration entry point (parses args, kicks off training or testing)
• datasets/ – dataset loaders and Excel distributions for MVTec, VisA, MPDD, WFDD
• backbones.py – model backbone definitions (e.g., CNN variants)
• glass.py – core anomaly synthesis logic (gradient ascent procedure)
• model.py – wraps backbone + synthesis into full detection model
• loss.py – custom loss functions for anomaly localization
• metrics.py – performance calculators (I-AUROC, P-AUROC, etc.)
• perlin.py – utility for Perlin-noise augmentation
• onnx/ – scripts for converting PyTorch models to ONNX and running with ORT
• shell/ – bash scripts to launch experiments per dataset
• utils.py, common.py – shared utilities (logging, argument parsing, I/O)
• Configuration/build:
• requirements.txt – Python dependencies (PyTorch, numpy, etc.)
• scripts under shell/ parameterize datapaths, class lists, and hyperparameters.
Key README Insights
• Describes environment setup (conda + pip), data download/organization, experiment launch via shell scripts.
Components
a. Main Components
1. Data Ingestion & Preparation
• Raw Dataset Folders (MVTec, VisA, MPDD, WFDD, DTD)
• Excel Distribution files
• Optional foreground masks
2. Data Augmentation & Synthesis
• Perlin noise generator (perlin.py)
• GLASS synthesis module (glass.py) performing gradient ascent
3. Model Definition
• Backbone networks (backbones.py)
• Detection head + anomaly scoring (model.py)
4. Training & Evaluation Pipeline
• main.py orchestrating train vs. test based on args
• loss computation (loss.py)
• metric computation (metrics.py)
5. Results & Export
• Checkpoint save/load
• ONNX conversion (onnx/pth2onnx.py)
• Inference with ONNX Runtime (onnx/ort.py)
• Figures output under figures/ and results/
6. Experiment Orchestration
• shell/*.sh scripts driving batch runs per dataset
b. Relationships & Interactions
• Data loaders feed raw images → augmentation/synthesis → model input
• Model outputs anomaly maps/scores → loss & metrics modules
• Training loop (in main.py) updates backbone + synthesis parameters
• Post-training: checkpoint → ONNX conversion → ORT inference → final metrics
• shell scripts wrap all components: set config → call main.py or onnx scripts
c. Architectural Patterns & Principles
• Modular separation: data, synthesis, model, training, evaluation, export
• Single entry-point CLI (main.py) with explicit mode flags (train/test/ckpt)
• Extensibility: adding a new dataset involves dropping loader in datasets/ + shell script
• Clear layering: low-level utilities (perlin, common) < core algorithm (glass) < training loop < orchestration scripts
d. Technologies & Libraries
• PyTorch (model, training), NumPy, OpenCV/PIL (image I/O in datasets),
• ONNX & ONNX Runtime for model export + inference
• Bash for running experiments