# lionel-w2/fap architecture

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

- Interactive diagram: https://gitdiagram.com/lionel-w2/fap
- Repository: https://github.com/lionel-w2/fap

## Overview

##### Project type and purpose
- This is a machine-learning research codebase implementing “Few-Shot Adversarial Prompt Learning” on top of a vision-language foundation model (CLIP).
- It provides multiple adversarial prompt methods (FAP, AdvVLP, AdvMaPLe, Advzsclip, ZsRobust) through a configuration-driven, modular training pipeline.

##### File structure
- Top-level modules:
- attack/         ← adversarial‐example generator (PGD)
- clip/           ← CLIP model loader, tokenizer, backbone
- zsrobust/       ← zero-shot robustness utilities and wrappers around CLIP
- datasets/       ← dataset wrappers matching Dassl API
- configs/        ← YAML files defining datasets + trainer hyperparameters
- trainers/       ← Python implementations of each prompt-learning algorithm
- scripts/        ← Bash scripts invoking train.py with specific configs
- train.py        ← central entry point that reads configs, instantiates modules, runs training + evaluation
- requirements.txt, LICENSE, readme.md
- Patterns:
- Config-first: every experiment is defined by a YAML in configs/…
- Modular “plugin” directories: attack, datasets, clip, trainers, zsrobust
- Shell orchestration in scripts/Adv/… for reproducible few-shot, base→new, cross-dataset workflows

##### README for additional insights
- Confirms this builds on the Dassl & CoOp frameworks
- Illustrates a core pipeline: (1) sample few-shot data → (2) adversarial prompt training → (3) evaluate zero-/few-shot robustness
- Mentions external dependencies: Dassl, CoOp, CoCoOp, MaPLe, Zsrobust

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Components
    ConfigManager["Configuration Manager"]:::config
    Orchestrator["Orchestrator\n(train.py + scripts)"]:::orchestrator
    DataLoader["Dataset Loader"]:::data
    AttackModule["Adversarial Attack Module\n(pgd)"]:::attack
    subgraph "CLIP Backbone"
        ClipCore["CLIP Model & Tokenizer"]:::core
    end
    subgraph "ZS-Robust Wrappers"
        ZSClip["ZSRobust CLIP Wrappers"]:::core
    end
    subgraph "Prompt Trainer"
        FAP["FAP"]:::core
        AdvMaple["AdvMaPLe"]:::core
        AdvZsclip["Advzsclip"]:::core
        AdviVLP["AdvVLP"]:::core
        CoOp["CoOp"]:::core
        ZsRobustTrainer["ZsRobust"]:::core
    end
    Output["Output / Logging / Metrics"]:::output
    ExternalFramework["External Frameworks\n(Dassl, CoOp)"]:::external

    %% Data Flow and Control Flow
    ConfigManager -->|reads YAML| Orchestrator
    ConfigManager -->|reads YAML| DataLoader
    ConfigManager -->|reads YAML| AttackModule
    ConfigManager -->|reads YAML| ClipCore
    ConfigManager -->|reads YAML| FAP
    ConfigManager -->|reads YAML| AdvMaple
    ConfigManager -->|reads YAML| AdvZsclip
    ConfigManager -->|reads YAML| AdviVLP
    ConfigManager -->|reads YAML| CoOp
    ConfigManager -->|reads YAML| ZsRobustTrainer

    Orchestrator -->|load few-shot data| DataLoader
    DataLoader -->|natural examples| ClipCore
    DataLoader -->|examples| AttackModule
    AttackModule -->|adversarial examples| ClipCore
    ClipCore -->|features| FAP
    ClipCore -->|features| AdvMaple
    ClipCore -->|features| AdvZsclip
    ClipCore -->|features| AdviVLP
    ClipCore -->|features| CoOp
    ClipCore -->|features| ZsRobustTrainer
    ZSClip -->|zero-shot utils| ZsRobustTrainer

    FAP -->|checkpoints & metrics| Output
    AdvMaple -->|checkpoints & metrics| Output
    AdvZsclip -->|checkpoints & metrics| Output
    AdviVLP -->|checkpoints & metrics| Output
    CoOp -->|checkpoints & metrics| Output
    ZsRobustTrainer -->|checkpoints & metrics| Output

    ExternalFramework -->|provides training loops| FAP
    ExternalFramework -->|provides training loops| CoOp
    ExternalFramework -->|provides data pipelines| DataLoader

    %% Click events
    click ConfigManager "https://github.com/lionel-w2/fap/tree/main/configs/"
    click DataLoader "https://github.com/lionel-w2/fap/tree/main/datasets/"
    click AttackModule "https://github.com/lionel-w2/fap/blob/main/attack/pgd.py"
    click ClipCore "https://github.com/lionel-w2/fap/blob/main/clip/clip.py"
    click ClipCore "https://github.com/lionel-w2/fap/blob/main/clip/model.py"
    click ClipCore "https://github.com/lionel-w2/fap/blob/main/clip/simple_tokenizer.py"
    click ZSClip "https://github.com/lionel-w2/fap/blob/main/zsrobust/clip/clip.py"
    click ZSClip "https://github.com/lionel-w2/fap/blob/main/zsrobust/clip/model.py"
    click ZSClip "https://github.com/lionel-w2/fap/blob/main/zsrobust/clip/simple_tokenizer.py"
    click ZSClip "https://github.com/lionel-w2/fap/blob/main/zsrobust/models/prompters.py"
    click ZSClip "https://github.com/lionel-w2/fap/blob/main/zsrobust/utils.py"
    click FAP "https://github.com/lionel-w2/fap/blob/main/trainers/fap.py"
    click AdvMaple "https://github.com/lionel-w2/fap/blob/main/trainers/advmaple.py"
    click AdvZsclip "https://github.com/lionel-w2/fap/blob/main/trainers/Advzsclip.py"
    click AdviVLP "https://github.com/lionel-w2/fap/blob/main/trainers/advindependentVL.py"
    click CoOp "https://github.com/lionel-w2/fap/blob/main/trainers/coop.py"
    click ZsRobustTrainer "https://github.com/lionel-w2/fap/blob/main/trainers/zsrobust.py"
    click Orchestrator "https://github.com/lionel-w2/fap/blob/main/train.py"
    click Orchestrator "https://github.com/lionel-w2/fap/tree/main/scripts/Adv/fap/"
    click Orchestrator "https://github.com/lionel-w2/fap/tree/main/scripts/Adv/advmaple/"
    click Orchestrator "https://github.com/lionel-w2/fap/tree/main/scripts/Adv/advzsclip/"
    click Orchestrator "https://github.com/lionel-w2/fap/tree/main/scripts/Adv/advivlp/"
    click Orchestrator "https://github.com/lionel-w2/fap/tree/main/scripts/Adv/zsrobust/"
    click ConfigManager "https://github.com/lionel-w2/fap/tree/main/configs/datasets/"
    click ConfigManager "https://github.com/lionel-w2/fap/tree/main/configs/trainers/"
    click ExternalFramework "https://github.com/lionel-w2/fap/tree/main/framework/"
    click ExternalFramework "https://github.com/lionel-w2/fap/blob/main/requirements.txt"
    click ExternalFramework "https://github.com/lionel-w2/fap/blob/main/readme.md"

    %% Styles
    classDef config fill:#D0E6F8,stroke:#0366d6,color:#0366d6
    classDef data fill:#D0E6F8,stroke:#0366d6,color:#0366d6
    classDef core fill:#D0F8D0,stroke:#28A745,color:#28A745
    classDef attack fill:#F8D0D0,stroke:#d73a49,color:#d73a49
    classDef orchestrator fill:#F8E1B3,stroke:#d18600,color:#d18600
    classDef external fill:#E0E0E0,stroke:#6a737d,color:#6a737d
    classDef output fill:#FFF5BA,stroke:#dbab09,color:#735c0f
```
