# yuangongnd/psla architecture

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

- Interactive diagram: https://gitdiagram.com/yuangongnd/psla
- Repository: https://github.com/yuangongnd/psla

## Overview

##### Project Type and Purpose
- This is a research-grade machine-learning training and inference pipeline (PSLA) for audio tagging.
- It is built in Python (PyTorch) and organized as a modular CLI tool with two example “recipes” (AudioSet and FSD50K).
- Key features: data preparation, dataset-agnostic training engine, label-enhancement, ensemble & weight-averaging, pretrained model storage.

##### Main Architectural Components
A. Recipe Layer (egs/)
- Shell wrappers (run.sh) for each dataset recipe (AudioSet, FSD50K).
- Data-prep scripts (prep_fsd.py, weight file generation).
B. Orchestrator / CLI (src/run.py)
- Parses command-line args & config.
- Invokes: data loaders, training/testing engine, optionally label enhancement or pretrained-model inference.
C. Data Layer
- src/dataloaders/: Dataset classes that read JSON files and yield spectrogram tensors + labels.
- Utilities for sampling weights (gen_weight_file.py).
- External datasets: AudioSet JSON + raw wavs, FSD50K audio files.
D. Core Training Engine (src/traintest.py)
- Implements train/validate/test loops.
- Supports checkpointing, weight averaging, metrics logging.
E. Model Layer (src/models/)
- CNN+Attention architectures (EffNetAttention).
- Model factory & weight-loading logic.
F. Post-processing Modules
- Ensemble (src/ensemble/ensemble.py) & Weight Averaging (src/ensemble/weight_averaging.py).
G. Utilities (src/utilities/)
- ontology.json, stats, generic helpers.
H. Artifacts & Storage
- pretrained_models/: Drop-in model checkpoints.
- fig/: Illustrations.
- logs, experiment CSV outputs under egs/*/exp/.

##### Component Relationships & Data Flow
1. User runs egs/<dataset>/run.sh
→ Invokes src/run.py with parameters
→ src/run.py calls data-prep if needed (prep_fsd.py or gen_weight_file.py)
→ src/run.py loads data via src/dataloaders/*_dataset.py
→ src/run.py calls train/test engine (src/traintest.py)
→ train/test calls models from src/models, logs metrics to disk
2. After base training:
→ Optionally src/run.py or separate call invokes label enhancement modules (src/label_enhancement)
→ Generates enhanced JSON label files under egs/*/datafiles
→ Rerun training for improved labels
3. Ensemble & Weight Averaging:
→ src/ensemble/weight_averaging.py can be called by src/traintest or standalone
→ src/ensemble/ensemble.py loads multiple checkpoints or runs, aggregates predictions
→ Outputs final CSV metrics
4. Pretrained-Model Inference:
→ User points run.py to pretrained_models/ and dataset JSON → instant tagging inference

##### Architectural Patterns & Principles
- Modular Separation of Concerns: data, model, training, post-processing are in distinct folders.
- Dataset-Agnostic Pipeline: run.py and traintest.py don’t hard-code dataset logic.
- Config-Driven: all hyperparameters and file paths are specified in shell scripts.
- Extensibility Points: add new model in src/models, new dataset in src/dataloaders, new recipe under egs/.

##### Capturing Essential Details
- Configs and hyperparameters (in run.sh) are noted.
- That run.py is dataset-agnostic and delegates to specific dataloaders.
- Evaluation outputs (CSV logs, mAP/AUC numbers) as separate nodes.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Recipe Layer (Blue)
    subgraph "Recipe Layer" 
        direction TB
        AS_run["run.sh (AudioSet)"]:::blue
        AS_csv["class_labels_indices.csv"]:::blue
        FS_run["run.sh (FSD50K)"]:::blue
        FS_prep["prep_fsd.py"]:::blue
        FS_csv["class_labels_indices.csv"]:::blue
    end

    %% Orchestrator / CLI (Blue)
    ORCH["src/run.py"]:::blue

    %% Data Layer (Orange)
    subgraph "Data Layer"
        direction TB
        DL_as["audioset_dataset.py"]:::orange
        DL_fsd["fsd50k_dataset.py"]:::orange
        GW["gen_weight_file.py"]:::orange
        DS_json["AudioSet JSON"]:::orange
        DS_audio["Raw WAVs / FSD50K audio"]:::orange
    end

    %% Core Engine & Model (Green)
    subgraph "Core Engine & Model"
        direction TB
        ENGINE["traintest.py"]:::green
        Models["Models.py"]:::green
        HModels["HigherModels.py"]:::green
    end

    %% Post-processing (Purple)
    subgraph "Label Enhancement" 
        direction TB
        CL["check_label_error.py"]:::purple
        F1["fix_type1.py"]:::purple
        F2["fix_type2.py"]:::purple
        MERGE["merge_type_1_2.py"]:::purple
    end

    subgraph "Ensemble & Weight Averaging"
        direction TB
        ENS["ensemble.py"]:::purple
        WA["weight_averaging.py"]:::purple
    end

    %% Utilities & Storage (Gray)
    subgraph "Utilities"
        direction TB
        ONT["ontology.json"]:::gray
        STATS["stats.py"]:::gray
        UTIL["util.py"]:::gray
    end

    subgraph "Artifacts & Storage"
        direction TB
        PRE["pretrained_models"]:::gray
        FIG["fig"]:::gray
    end

    %% Connections
    AS_run -->|invokes| ORCH
    FS_run -->|invokes| ORCH

    ORCH -->|loads| DL_as
    ORCH -->|loads| DL_fsd
    ORCH -->|calls| GW

    DL_as -->|reads| DS_json
    DL_as -->|reads| DS_audio
    DL_fsd -->|reads| DS_audio

    ORCH -->|starts| ENGINE
    ENGINE -->|uses| Models
    ENGINE -->|uses| HModels
    ENGINE -->|writes| logs["Experiment CSV & logs"]:::gray

    ORCH -->|optionally runs| CL
    CL --> F1
    CL --> F2
    F1 --> MERGE
    F2 --> MERGE
    MERGE -->|outputs| enhanced["Enhanced JSON labels"]:::orange
    enhanced --> ORCH

    ENGINE -->|can call| WA
    WA --> ENS
    ENS -->|outputs| finalCSV["Final CSV metrics"]:::gray

    ORCH -->|can point to| PRE

    ORCH -->|reads| ONT
    ORCH -->|uses| STATS
    ORCH -->|uses| UTIL

    %% Click events
    click AS_run "https://github.com/yuangongnd/psla/blob/main/egs/audioset/run.sh"
    click AS_csv "https://github.com/yuangongnd/psla/blob/main/egs/audioset/class_labels_indices.csv"
    click FS_run "https://github.com/yuangongnd/psla/blob/main/egs/fsd50k/run.sh"
    click FS_prep "https://github.com/yuangongnd/psla/blob/main/egs/fsd50k/prep_fsd.py"
    click FS_csv "https://github.com/yuangongnd/psla/blob/main/egs/fsd50k/class_labels_indices.csv"
    click ORCH "https://github.com/yuangongnd/psla/blob/main/src/run.py"
    click DL_as "https://github.com/yuangongnd/psla/blob/main/src/dataloaders/audioset_dataset.py"
    click GW "https://github.com/yuangongnd/psla/blob/main/src/gen_weight_file.py"
    click ENGINE "https://github.com/yuangongnd/psla/blob/main/src/traintest.py"
    click Models "https://github.com/yuangongnd/psla/blob/main/src/models/Models.py"
    click HModels "https://github.com/yuangongnd/psla/blob/main/src/models/HigherModels.py"
    click CL "https://github.com/yuangongnd/psla/blob/main/src/label_enhancement/check_label_error.py"
    click F1 "https://github.com/yuangongnd/psla/blob/main/src/label_enhancement/fix_type1.py"
    click F2 "https://github.com/yuangongnd/psla/blob/main/src/label_enhancement/fix_type2.py"
    click MERGE "https://github.com/yuangongnd/psla/blob/main/src/label_enhancement/merge_type_1_2.py"
    click ENS "https://github.com/yuangongnd/psla/blob/main/src/ensemble/ensemble.py"
    click WA "https://github.com/yuangongnd/psla/blob/main/src/ensemble/weight_averaging.py"
    click ONT "https://github.com/yuangongnd/psla/blob/main/src/utilities/ontology.json"
    click STATS "https://github.com/yuangongnd/psla/blob/main/src/utilities/stats.py"
    click UTIL "https://github.com/yuangongnd/psla/blob/main/src/utilities/util.py"

    %% Styles
    classDef blue fill:#cce5ff,stroke:#3399ff,color:#000;
    classDef green fill:#d4edda,stroke:#28a745,color:#000;
    classDef orange fill:#fff3cd,stroke:#ffcc00,color:#000;
    classDef purple fill:#e2d1f9,stroke:#8a2be2,color:#000;
    classDef gray fill:#e2e3e5,stroke:#6c757d,color:#000;
```
