# trevillie/magnet architecture

> Architecture diagram of the GitHub repository trevillie/magnet, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/trevillie/magnet
- Repository: https://github.com/trevillie/magnet

## Overview

##### Project type and purpose
- This is a research/demo machine-learning project (“MagNet”) that implements and validates a two-pronged defense against adversarial examples on image data (MNIST).
- The README makes clear it’s a pipeline for training autoencoder-based defenses, then testing them against precomputed adversarial examples, and plotting results.

##### File structure
- Top-level Python scripts define each pipeline stage:
- setup_mnist.py – download/prepare MNIST data
- train_models.py – (likely) trains baseline classifiers
- train_defense.py – trains the defense autoencoders/filters
- test_defense.py – runs defense on adversarial examples and reports metrics
- defensive_models.py – definitions/architectures of autoencoders and detectors
- worker.py – utility for batch processing, maybe parallel defense runs
- utils.py – shared helper functions (data loaders, metrics, plotting helpers)
- No web UI or frontend; it’s purely a computation pipeline.
- There’s an expectation of external “support data” (attacks + classifier) dropped into the MagNet/ folder.
- Output: a PDF graph under graph/defense_performance.pdf (implied).

##### Key architectural insights from README
- Dependency on Keras/TensorFlow for model definition and training
- `train_defense.py` → builds autoencoders from `defensive_models.py`
- `test_defense.py` → uses trained models + adversarial examples → defense results + plots
- No attack code is implemented here – attacks are precomputed and loaded.

##### Final remarks
- Be detailed: show each script, model definition, data artifact, and how they connect.
- Both the logical flow (pipeline stages) and the physical components (Python files, data files, libraries).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Preparation Layer
    subgraph "Data Preparation" 
        direction TB
        RawMNIST[(Raw MNIST Dataset)]:::data
        Adversarial[(Precomputed Adversarial Examples + Base Classifier)]:::data
        Setup["setup_mnist.py"]:::data
        Utils_DP["utils.py"]:::utility
    end

    %% Training Layer
    subgraph "Training" 
        direction TB
        TrainBase["train_models.py"]:::train
        TrainDef["train_defense.py"]:::train
        DefModels["defensive_models.py"]:::train
        TrainedModels[(Trained Models Store)]:::data
    end

    %% Evaluation Layer
    subgraph "Evaluation" 
        direction TB
        TestDef["test_defense.py"]:::eval
        Worker["worker.py"]:::eval
        Utils_EV["utils.py"]:::utility
        ReportPDF[/defense_performance.pdf/]:::output
    end

    %% External Dependencies
    subgraph "External Dependencies"
        direction TB
        Keras["Keras/TensorFlow"]:::external
        NumPy["NumPy/SciPy"]:::external
        Matplotlib["Matplotlib"]:::external
    end

    %% Data flows
    RawMNIST -->|loads & normalizes| Setup
    Setup -->|normalized images| Utils_DP
    Utils_DP -->|feeds data| TrainBase
    Utils_DP -->|feeds data| TrainDef

    Adversarial -->|loads attacks| TestDef
    Adversarial -->|loads attacks| Worker

    TrainBase -->|baseline models| TrainedModels
    TrainDef -->|autoencoder & detector| DefModels
    DefModels -->|defense architectures| TrainedModels

    TrainedModels -->|models| Worker
    TrainedModels -->|models| TestDef

    Worker -->|defense scores| Utils_EV
    TestDef -->|evaluation metrics| Utils_EV
    Utils_EV -->|plots results| ReportPDF

    %% Dependencies usage
    TrainDef --> Keras
    DefModels --> Keras
    TrainBase --> Keras
    TestDef --> Keras
    Utils_DP --> NumPy
    Utils_DP --> Matplotlib
    Utils_EV --> Matplotlib

    %% Click Events
    click Setup "https://github.com/trevillie/magnet/blob/master/setup_mnist.py"
    click Utils_DP "https://github.com/trevillie/magnet/blob/master/utils.py"
    click TrainBase "https://github.com/trevillie/magnet/blob/master/train_models.py"
    click TrainDef "https://github.com/trevillie/magnet/blob/master/train_defense.py"
    click DefModels "https://github.com/trevillie/magnet/blob/master/defensive_models.py"
    click Worker "https://github.com/trevillie/magnet/blob/master/worker.py"
    click TestDef "https://github.com/trevillie/magnet/blob/master/test_defense.py"
    click README "https://github.com/trevillie/magnet/blob/master/README.md"
    click LICENSE "https://github.com/trevillie/magnet/tree/master/LICENSE"

    %% Styles
    classDef data fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef train fill:#90EE90,stroke:#333,stroke-width:1px
    classDef eval fill:#FFA500,stroke:#333,stroke-width:1px
    classDef utility fill:#D3D3D3,stroke:#333,stroke-width:1px
    classDef external fill:#F0F0F0,stroke:#333,stroke-width:1px
    classDef output fill:#FFFFE0,stroke:#333,stroke-width:1px
```
