# zhn6818/deep-svdd-pytorch architecture

> Architecture diagram of the GitHub repository zhn6818/deep-svdd-pytorch, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/zhn6818/deep-svdd-pytorch
- Repository: https://github.com/zhn6818/deep-svdd-pytorch

## Overview

Based on our analysis of the file tree and README, this project is a PyTorch-based implementation of the Deep SVDD (Support Vector Data Description) method for anomaly detection using deep learning. The design supports experiments on image datasets such as MNIST and CIFAR-10, with an option for autoencoder pretraining as a parameter initialization step.

##### Main Components
- Data Preparation & Preprocessing:
- Located under src/datasets, this module includes dataset loaders (e.g., cifar10.py, mnist.py, mydata.py) and preprocessing steps.
- The “data” directory holds raw datasets, while “log” and “imgs” store outputs like results, images, and logs.
- Base & Core Classes:
- The src/base directory contains abstract base classes (base_dataset.py, base_net.py, base_trainer.py) that define common interfaces for datasets, networks, and training.
- These establish the architecture’s modular design and are used as blueprints for various implementations.
- Network Architectures:
- Found in src/networks, where various LeNet based architectures are defined (for MNIST, CIFAR-10, MyData, etc.) and adjusted with different activation functions (e.g., cifar10_LeNet_elu).
- Optimizers and Trainers:
- In the src/optim directory, you have specific trainer routines (ae_trainer.py, deepSVDD_trainer.py) that implement the actual training logic.
- Also, src/deepSVDD.py likely encapsulates the Deep SVDD-specific method.
- Experimentation Control:
- The src/main.py appears as the main entry point to run experiments. It parses user arguments, sets up datasets, networks, trainers, and handles training loops.
- Utilities:
- In src/utils, there are modules for configuration (config.py), logging and results collection (collect_results.py), and visualization (e.g., plot_images_grid.py).

##### Relationships and Interactions
- The main entry point (main.py) coordinates the system by instantiating dataset loaders (from src/datasets), network architectures (from src/networks), and trainer components (from src/optim).
- The base classes in src/base enforce a consistent interface that the actual dataset and network implementations follow.
- The training process involves preprocessing input data, feeding it through networks, and then optimizing the model using trainer logic with potential pretraining via the autoencoder approach.
- Utility modules support configuration management, result collection, and visualizations throughout the training/evaluation phases.

##### Architectural Patterns and Design Principles
- Modular Design:
- Separation between base classes, dataset management, network definitions, and training logic enables extensibility and clarity in the pipeline.
- Layered Architecture:
- Data handling at the bottom layer, network modeling in the middle, and training/optimization control at the top ensure a clean separation of concerns.
- Reusability & Abstraction:
- The use of abstract base modules encourages reusability across different datasets and network configurations.

##### Key Technologies and Frameworks
- PyTorch – used as the deep learning framework.
- Python 3.7 – environment dependency as indicated in the README.
- External Python packages as listed in requirements.txt, which support various functionalities (data handling, visualization, etc.).

## Diagram (Mermaid)

```mermaid
graph TD
    %% Data Ingestion Layer
    subgraph "Data Ingestion Layer"
        D1["Raw Data (data)"]:::data
        D2["Datasets Module (src/datasets)"]:::dataModule
    end

    %% Core Framework
    subgraph "Core Framework"
        B["Base Classes (src/base)"]:::base
    end

    %% Model Definition
    subgraph "Model Definition"
        N["Networks (src/networks)"]:::network
    end

    %% Training & Optimization
    subgraph "Training & Optimization"
        T1["DeepSVDD Trainer (src/optim/deepSVDD_trainer.py)"]:::trainer
        AE["Autoencoder Trainer (src/optim/ae_trainer.py)"]:::trainer
        DSVDD["Deep SVDD Module (src/deepSVDD.py)"]:::trainer
    end

    %% Experiment Control
    subgraph "Experiment Control"
        M["Main Entry Point (src/main.py)"]:::experiment
    end

    %% Utilities & Visualization
    subgraph "Utilities & Visualization"
        U["Utilities (src/utils)"]:::utilities
    end

    %% Outputs & Artifacts
    subgraph "Outputs & Artifacts"
        L["Logs (log)"]:::artifact
        I["Images (imgs)"]:::artifact
    end

    %% Connections
    D1 -->|"feeds"| D2
    D2 -->|"loads_data"| B
    M -->|"initiates"| D2
    M -->|"initiates"| N
    M -->|"initiates"| T1
    M -->|"initiates"| AE
    B -->|"interface"| D2
    B -->|"interface"| N
    B -->|"interface"| T1
    N -->|"processed_by"| T1
    T1 -->|"calls"| DSVDD
    T1 -->|"trains"| N
    AE -.->|"pretrains"| T1
    U -->|"supports"| M
    U -->|"supports"| T1
    T1 -->|"logs_to"| L
    T1 -->|"saves_visuals_to"| I
    U -->|"visualizes"| I

    %% Click Events
    click D1 "https://github.com/zhn6818/deep-svdd-pytorch/tree/master/data"
    click D2 "https://github.com/zhn6818/deep-svdd-pytorch/tree/master/src/datasets"
    click B "https://github.com/zhn6818/deep-svdd-pytorch/tree/master/src/base"
    click N "https://github.com/zhn6818/deep-svdd-pytorch/tree/master/src/networks"
    click T1 "https://github.com/zhn6818/deep-svdd-pytorch/blob/master/src/optim/deepSVDD_trainer.py"
    click AE "https://github.com/zhn6818/deep-svdd-pytorch/blob/master/src/optim/ae_trainer.py"
    click DSVDD "https://github.com/zhn6818/deep-svdd-pytorch/blob/master/src/deepSVDD.py"
    click M "https://github.com/zhn6818/deep-svdd-pytorch/blob/master/src/main.py"
    click U "https://github.com/zhn6818/deep-svdd-pytorch/tree/master/src/utils"
    click L "https://github.com/zhn6818/deep-svdd-pytorch/tree/master/log"
    click I "https://github.com/zhn6818/deep-svdd-pytorch/tree/master/imgs"

    %% Styles
    classDef data fill:#FFD700,stroke:#333,stroke-width:2px;
    classDef dataModule fill:#FFDEAD,stroke:#333,stroke-width:2px;
    classDef base fill:#FFA07A,stroke:#333,stroke-width:2px;
    classDef network fill:#ADD8E6,stroke:#333,stroke-width:2px;
    classDef trainer fill:#90EE90,stroke:#333,stroke-width:2px;
    classDef experiment fill:#DDA0DD,stroke:#333,stroke-width:2px;
    classDef utilities fill:#F5DEB3,stroke:#333,stroke-width:2px;
    classDef artifact fill:#E6E6FA,stroke:#333,stroke-width:2px;
```
