# fpthink/spnet architecture

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

- Interactive diagram: https://gitdiagram.com/fpthink/spnet
- Repository: https://github.com/fpthink/spnet

## Overview

##### Project type and purpose
- This is a research-oriented deep learning framework (SPNet) for point-cloud over-segmentation.
- It comprises a PyTorch-based training/inference pipeline, C/C++ CUDA custom operators, configuration management, dataset handling, and experiment logging.

##### File structure
Top-level directories and their roles:
- config: YAML configuration files (model settings, paths)
- dataset: raw point-cloud data folders (S3DIS, ScanNet, vKITTI)
- exp: output folder for experiments (checkpoints, logs)
- lib:
– ply_c: C++ library for PLY file I/O and graph extraction
– pointops: custom CUDA kernels for point operations (knn, grouping, sampling)
– sync_bn: synchronized batchnorm utilities for multi-GPU
- model: Python modules defining network architecture and modules
- tool: shell & Python scripts to launch training and testing
- util: Python utilities for data loading, graph computation (compute_spg), metrics, transforms

##### Extract additional insights from README
- Requires manual CMake build of ply_c and Python install of pointops.
- Training/testing invoked via tool/sh_train.sh and tool/sh_test.sh, passing dataset key, experiment ID, and config file.

##### Out main components and interactions
a. Components:
1) Configuration Loader (config/spnet.yaml → util/config.py)
2) Data Ingestion & Preprocessing
– Dataset modules (util/S3DIS_dataset.py, SCANNET_dataset.py, etc.)
– compute_spg graph generator (util/compute_spg)
– transform and provider utilities (util/transform.py, util/provider.py)
3) Custom Operator Libraries
– ply_c (lib/ply_c → CMake/C++ layer)
– pointops (lib/pointops → CUDA kernels exposed via Python)
– sync_bn (lib/sync_bn for multi-GPU batchnorm)
4) Model Core
– network.py (defines SPNet architecture)
– modules.py (building blocks: graph convolution, pooling)
5) Training & Evaluation Scripts
– tool/train.py, tool/test.py orchestrate data loader → model → optimizer → loss → metrics
– shell wrappers (tool/sh_train.sh, tool/sh_test.sh) inject parameters and launch processes
6) Experiment Management & Artifacts
– exp/… directory stores checkpoints (saved_model/*.pth), logs, and evaluation outputs

b. Relationships & Data Flow
– Config Loader → Training Script & Data Ingestion
– Data Ingestion → compute_spg (calls ply_c to read PLY and build superpoint graph)
– Data → pointops custom CUDA kernels for neighborhood queries, grouping, sampling
– Preprocessed batches → Model Core → Forward Pass → Loss & Metric computation
– Backward pass → Custom ops + PyTorch → Parameter updates
– Checkpoint saving → exp directory

c. Architectural patterns & design principles
– Layered separation: Python orchestration (top) vs. high-performance C/CUDA kernels (bottom)
– Modular design: clear boundaries between data loader, custom ops, model definitions, and training loops
– Configuration-driven: YAML files control all hyperparameters and paths
– Experiment reproducibility: all artifacts stored under exp/<dataset>/<exp_id>

d. Key technologies, frameworks, libraries
– Python 3.6.6, PyTorch 1.4.0, CUDA 10.1
– CMake, Boost, Eigen3 (for lib/ply_c)
– Custom CUDA kernels (lib/pointops)
– YAML for configuration

##### Diagramming recommendations
– Config file → Config Loader → Scripts
– Dataset storage → Data loader → compute_spg → pointops → Model
– Model → exp (checkpoint/storage)

##### Detailed element checklist
- Custom-op layer showing submodules (knnquery, grouping, sampling)
- Build-time steps (cmake & make for ply_c; python setup.py install for pointops)
- Experiment directory structure and checkpoint saving

Top layer: Config & Orchestration
Middle layer: Data ingestion & Preprocessing → Custom Ops → Model Core
Bottom layer: C/C++/CUDA libraries and GPU resources

This will capture the essential architectural elements without over-splitting, giving a clear, accurate view of SPNet’s system design.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Config & Orchestration Layer
    subgraph "Config & Orchestration" 
        direction TB
        ConfigFile[/spnet.yaml/]:::config
        ConfigLoader["Config Loader"]:::python
        TrainScript["train.py"]:::python
        TestScript["test.py"]:::python
        ShellTrain["sh_train.sh"]:::shell
        ShellTest["sh_test.sh"]:::shell
    end

    %% Data Ingestion & Preprocessing Layer
    subgraph "Data Ingestion & Preprocessing"
        direction TB
        ds_s3dis[(Dataset S3DIS)]:::storage
        ds_scannet[(Dataset ScanNet)]:::storage
        ds_vkitti[(Dataset vKITTI)]:::storage
        ds_semanticposs[(Dataset SemanticPOSS)]:::storage

        S3DISLoader["S3DIS_dataset.py"]:::python
        SCANNETLoader["SCANNET_dataset.py"]:::python
        VKITTILoader["VKITTI_dataset.py"]:::python
        SEMANTICLoader["SEMANTICPOSS_dataset.py"]:::python

        ComputeSPG["compute_spg/"]:::python
        Transform["transform.py"]:::python
        Provider["provider.py"]:::python
    end

    %% Custom Operator Layer
    subgraph "Custom Operator Libraries"
        direction TB
        PlyC["ply_c (C++ Lib)"]:::cpp
        PointOps["pointops (CUDA Lib)"]:::cpp
        SyncBN["sync_bn (Sync BatchNorm)"]:::cpp
    end

    %% Model Core Layer
    subgraph "Model Core"
        direction TB
        Network["network.py"]:::python
        Modules["modules.py"]:::python
    end

    %% Experiment Storage
    ExpStorage[(Experiment Outputs)]:::storage

    %% Data Storage
    DatasetStorage[(Raw Datasets)]:::storage

    %% Relationships
    ConfigFile -->|loads| ConfigLoader
    ConfigLoader -->|injects| TrainScript
    ConfigLoader -->|injects| TestScript
    TrainScript -->|uses| S3DISLoader
    TrainScript -->|uses| SCANNETLoader
    TrainScript -->|uses| VKITTILoader
    TrainScript -->|uses| SEMANTICLoader
    S3DISLoader -->|reads| ds_s3dis
    SCANNETLoader -->|reads| ds_scannet
    VKITTILoader -->|reads| ds_vkitti
    SEMANTICLoader -->|reads| ds_semanticposs

    S3DISLoader -->|preprocess| ComputeSPG
    SCANNETLoader -->|preprocess| ComputeSPG
    VKITTILoader -->|preprocess| ComputeSPG
    SEMANTICLoader -->|preprocess| ComputeSPG

    ComputeSPG -->|calls C++ I/O| PlyC
    ComputeSPG -->|generates graph| Transform
    Transform --> Provider
    Provider -->|batches| Network

    Network --> Modules
    Modules -->|forward/backward| PointOps
    PointOps -->|kernel launch| CUDA[GPU]:::gpu

    SyncBN -->|sync| Modules

    Network -->|saves checkpoint| ExpStorage
    TestScript -->|evaluates| Network

    ShellTrain -->|executes| TrainScript
    ShellTest -->|executes| TestScript

    %% Click Events
    click ConfigFile "https://github.com/fpthink/spnet/blob/main/config/spnet.yaml"
    click ConfigLoader "https://github.com/fpthink/spnet/blob/main/util/config.py"
    click TrainScript "https://github.com/fpthink/spnet/blob/main/tool/train.py"
    click TestScript "https://github.com/fpthink/spnet/blob/main/tool/test.py"
    click ShellTrain "https://github.com/fpthink/spnet/blob/main/tool/sh_train.sh"
    click ShellTest "https://github.com/fpthink/spnet/blob/main/tool/sh_test.sh"
    click S3DISLoader "https://github.com/fpthink/spnet/blob/main/util/S3DIS_dataset.py"
    click SCANNETLoader "https://github.com/fpthink/spnet/blob/main/util/SCANNET_dataset.py"
    click VKITTILoader "https://github.com/fpthink/spnet/blob/main/util/VKITTI_dataset.py"
    click SEMANTICLoader "https://github.com/fpthink/spnet/blob/main/util/SEMANTICPOSS_dataset.py"
    click ComputeSPG "https://github.com/fpthink/spnet/tree/main/util/compute_spg/"
    click Transform "https://github.com/fpthink/spnet/blob/main/util/transform.py"
    click Provider "https://github.com/fpthink/spnet/blob/main/util/provider.py"
    click PlyC "https://github.com/fpthink/spnet/blob/main/lib/ply_c/CMakeLists.txt"
    click PointOps "https://github.com/fpthink/spnet/blob/main/lib/pointops/setup.py"
    click SyncBN "https://github.com/fpthink/spnet/blob/main/lib/sync_bn/batchnorm.py"
    click Network "https://github.com/fpthink/spnet/blob/main/model/network.py"
    click Modules "https://github.com/fpthink/spnet/blob/main/model/modules.py"
    click ExpStorage "https://github.com/fpthink/spnet/blob/main/exp/s3dis/20220121/saved_model/train_epoch_850.pth"
    click ds_s3dis "https://github.com/fpthink/spnet/tree/main/dataset/s3dis"
    click ds_scannet "https://github.com/fpthink/spnet/tree/main/dataset/scannet"
    click ds_vkitti "https://github.com/fpthink/spnet/tree/main/dataset/vkitti"

    %% Styles
    classDef python fill:#8fd3f4,stroke:#036,stroke-width:1px
    classDef cpp fill:#f4a261,stroke:#933,stroke-width:1px
    classDef shell fill:#e9c46a,stroke:#b07,stroke-width:1px
    classDef config fill:#a8dadc,stroke:#036,stroke-width:1px,stroke-dasharray: 5 5
    classDef storage fill:#c9e4ca,stroke:#264,stroke-width:1px
    classDef gpu fill:#ffb4a2,stroke:#b30,stroke-width:1px,module
```
