# hiram64/ocsvm-anomaly-detection architecture

> Architecture diagram of the GitHub repository hiram64/ocsvm-anomaly-detection, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hiram64/ocsvm-anomaly-detection
- Repository: https://github.com/hiram64/ocsvm-anomaly-detection

## Overview

This repository is a small CIFAR-10-oriented unsupervised anomaly-detection workflow. CIFAR-10 batches are converted to labeled NPZ data; a convolutional autoencoder learns image representations and exports flattened encoded features; a one-class SVM is trained on the selected normal class and evaluated against sampled anomalies using ROC and PR AUC. The README also supports user-provided NPZ datasets in the same format. The main calls are visible in the sampled scripts; no additional runtime subsystems are evidenced.

Main entry points: [`cae.py`](https://github.com/hiram64/ocsvm-anomaly-detection/blob/master/cae.py) (CAE training runner), [`anomaly_detection_ocsvm.py`](https://github.com/hiram64/ocsvm-anomaly-detection/blob/master/anomaly_detection_ocsvm.py) (OC-SVM evaluation), [`data`](https://github.com/hiram64/ocsvm-anomaly-detection/tree/master/data) (Labeled image NPZ), [`make_cifar10_npz.py`](https://github.com/hiram64/ocsvm-anomaly-detection/blob/master/make_cifar10_npz.py) (CIFAR NPZ builder)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data_prep["Data preparation"]
  node_npz_builder["CIFAR NPZ builder"]
  node_image_npz["Labeled image NPZ"]
end

subgraph group_feature_learning["Feature learning"]
  node_cae_runner["CAE training runner<br/>[cae.py]"]
  node_cae_model["Autoencoder model<br/>[model.py]"]
  node_encoded_npz["Encoded feature NPZ"]
end

subgraph group_detection["Anomaly detection"]
  node_ocsvm_runner["OC-SVM evaluation"]
  node_svm_model["One-Class SVM"]
  node_auc_metrics["ROC and PR AUC"]
end

node_user(("Dataset user"))
node_cifar_batches["CIFAR-10 batches"]

node_user -->|"provides"| node_cifar_batches
node_cifar_batches -->|"input batches"| node_npz_builder
node_npz_builder -->|"writes images"| node_image_npz
node_user -.->|"may provide"| node_image_npz
node_image_npz -->|"loads images"| node_cae_runner
node_cae_runner -->|"builds"| node_cae_model
node_cae_runner -->|"writes features"| node_encoded_npz
node_cae_runner -->|"exports labels"| node_encoded_npz
node_encoded_npz -->|"loads features"| node_ocsvm_runner
node_ocsvm_runner -->|"trains and scores"| node_svm_model
node_ocsvm_runner -->|"calculates"| node_auc_metrics

click node_npz_builder "https://github.com/hiram64/ocsvm-anomaly-detection/blob/master/make_cifar10_npz.py"
click node_image_npz "https://github.com/hiram64/ocsvm-anomaly-detection/tree/master/data"
click node_cae_runner "https://github.com/hiram64/ocsvm-anomaly-detection/blob/master/cae.py"
click node_cae_model "https://github.com/hiram64/ocsvm-anomaly-detection/blob/master/model.py"
click node_encoded_npz "https://github.com/hiram64/ocsvm-anomaly-detection/tree/master/data"
click node_ocsvm_runner "https://github.com/hiram64/ocsvm-anomaly-detection/blob/master/anomaly_detection_ocsvm.py"
click node_svm_model "https://github.com/hiram64/ocsvm-anomaly-detection/blob/master/anomaly_detection_ocsvm.py"
click node_auc_metrics "https://github.com/hiram64/ocsvm-anomaly-detection/blob/master/anomaly_detection_ocsvm.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_npz_builder,node_image_npz,node_user toneBlue
class node_cae_runner,node_cae_model,node_encoded_npz toneAmber
class node_ocsvm_runner,node_svm_model,node_auc_metrics toneMint
class node_cifar_batches toneIndigo
```

## Components

### Data preparation

- [CIFAR NPZ builder](https://github.com/hiram64/ocsvm-anomaly-detection/blob/master/make_cifar10_npz.py): `make_cifar10_npz.py`
- [Labeled image NPZ](https://github.com/hiram64/ocsvm-anomaly-detection/tree/master/data): `data`

### Feature learning

- [CAE training runner](https://github.com/hiram64/ocsvm-anomaly-detection/blob/master/cae.py): `cae.py`
- [Autoencoder model](https://github.com/hiram64/ocsvm-anomaly-detection/blob/master/model.py): `model.py`
- [Encoded feature NPZ](https://github.com/hiram64/ocsvm-anomaly-detection/tree/master/data): `data`

### Anomaly detection

- [OC-SVM evaluation](https://github.com/hiram64/ocsvm-anomaly-detection/blob/master/anomaly_detection_ocsvm.py): `anomaly_detection_ocsvm.py`
- [One-Class SVM](https://github.com/hiram64/ocsvm-anomaly-detection/blob/master/anomaly_detection_ocsvm.py): `anomaly_detection_ocsvm.py`
- [ROC and PR AUC](https://github.com/hiram64/ocsvm-anomaly-detection/blob/master/anomaly_detection_ocsvm.py): `anomaly_detection_ocsvm.py`

### Other components

- **Dataset user**
- **CIFAR-10 batches**

## Connections

- Dataset user → CIFAR-10 batches: provides (evidence: `README.md`)
- CIFAR-10 batches → CIFAR NPZ builder: input batches (evidence: `make_cifar10_npz.py`)
- CIFAR NPZ builder → Labeled image NPZ: writes images (evidence: `make_cifar10_npz.py`)
- Dataset user → Labeled image NPZ: may provide (evidence: `README.md`)
- Labeled image NPZ → CAE training runner: loads images (evidence: `cae.py`)
- CAE training runner → Autoencoder model: builds (evidence: `cae.py`)
- CAE training runner → Encoded feature NPZ: writes features (evidence: `cae.py`)
- CAE training runner → Encoded feature NPZ: exports labels (evidence: `cae.py`)
- Encoded feature NPZ → OC-SVM evaluation: loads features (evidence: `anomaly_detection_ocsvm.py`)
- OC-SVM evaluation → One-Class SVM: trains and scores (evidence: `anomaly_detection_ocsvm.py`)
- OC-SVM evaluation → ROC and PR AUC: calculates (evidence: `anomaly_detection_ocsvm.py`)
