# kaiyangzhou/coop architecture

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

- Interactive diagram: https://gitdiagram.com/kaiyangzhou/coop
- Repository: https://github.com/kaiyangzhou/coop

## Overview

##### Main Components
- Model Components:
– The “clip” directory holds the core CLIP functionalities (e.g., clip.py, model.py, simple_tokenizer.py), which encapsulate the vision–language model operations.
– The “lpclip” module provides additional routines for linear probing, including feature extraction and evaluation.
- Trainer Modules:
– The “trainers” directory contains the implementation for different prompt learning methods (i.e., CoOp and CoCoOp).
– The “scripts” folders (both “scripts/coop” and “scripts/cocoop”) include entry-point shell scripts that orchestrate experiments and evaluations.
- Dataset Handlers:
– The “datasets” directory has Python scripts designed to load and preprocess specific datasets (like ImageNet, Caltech101, etc.).
– The “configs/datasets” folder provides YAML files to configure dataset-specific parameters.
- Configuration & Experiment Management:
– The “configs/trainers” directory includes YAML configuration files for different training experiments (with various backbones like RN50, RN101, ViT configurations, etc.).
– The “requirements.txt” file and other environment configuration tools (e.g., GitHub instructions to set up Dassl.pytorch) provide the necessary setup for the overall pipeline.
- Utilities & Evaluation:
– The “train.py” file serves as an important entry point for launching training experiments.

##### Relationships and Interactions
- Data Flow:
– The workflow starts with datasets being loaded and preprocessed by the modules in “datasets” (guided by corresponding YAML files in “configs/datasets”).
– These preprocessed inputs are then provided to the training routines in the “trainers” module (via the “train.py” entry script or the shell scripts in “scripts”).
– The trainers make use of the CLIP model utilities (in “clip”) to encode images and text, using prompt parameters that are learned during training.
– After training or during evaluation, additional modules (like those in “lpclip” and evaluation scripts in “scripts”) extract features, run linear probe experiments, and generate performance curves.

- Configuration Management:
– YAML configuration files from “configs/trainers” and “configs/datasets” dictate hyperparameters, training specifics, and dataset properties, which are loaded by the trainers and dataset scripts.
– This keeps the architecture flexible and modular.

##### Architectural Patterns & Design Principles
- Modular Design: Each component (data handling, model implementation, trainer routines, and evaluation) is separated into its own directory, making it easier for future extensions.
- Config-Driven Workflow: The use of YAML configuration files offers flexibility to change experimental setups without touching the core code.
- Extensibility: The code is built upon the Dassl.pytorch framework, which is itself modular and extensible, enabling you to easily plug in new models or training strategies.

##### Including Relevant Technologies and Frameworks
– CLIP (for the vision-language backbone)
– Dassl.pytorch (foundational ML toolbox)
– PyTorch (underlying deep learning framework)
– YAML-based configuration for experiment management
– Shell scripts and Python utilities for evaluation and plotting

##### Bringing It All Together
- Below that, branch out to “Dataset Module” and “Configuration Module.”
- Next, converge these components into a “Trainer” block that selects a specific prompt learning method (such as CoOp or CoCoOp) and uses parameters from YAML configurations.
- The “Trainer” then interacts with the “CLIP Model” block, which handles the vision-language processing.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Top Level
    User("User/Experiment Initiation"):::user
    train("Experiment Entry Point: train.py"):::entry
    User --> train

    %% Data & Configuration Subgraph
    subgraph "Data & Configuration"
        direction TB
        dataset_loader("Dataset Loader & Preprocessors"):::module
        dataset_config("Dataset Configuration Files"):::module
        dataset_loader --> dataset_config
    end
    train --> dataset_loader

    %% Trainer Modules Subgraph
    subgraph "Trainer Modules"
        direction TB
        coop_trainer("CoOp Trainer"):::module
        cocoop_trainer("CoCoOp Trainer"):::module
        coop_scripts("CoOp Scripts"):::module
        cocoop_scripts("CoCoOp Scripts"):::module
        coop_config("CoOp Configurations"):::module
        cocoop_config("CoCoOp Configurations"):::module
        coop_trainer --> coop_scripts
        cocoop_trainer --> cocoop_scripts
        coop_config --> coop_trainer
        cocoop_config --> cocoop_trainer
    end
    dataset_config --> coop_config

    %% CLIP Model & Utilities Subgraph
    subgraph "CLIP Model & Utilities"
        direction TB
        clip_core("CLIP Core"):::module
        model_def("Model Definition"):::module
        tokenizer("Tokenizer"):::module
    end
    coop_trainer --> clip_core
    cocoop_trainer --> clip_core

    %% Evaluation & Visualization Subgraph
    subgraph "Evaluation & Visualization"
        direction TB
        feat_ext("Feature Extraction"):::module
        linear_probe("Linear Probe"):::module
        draw_curves("Draw Curves"):::module
        parse_test("Parse Test Results"):::module
        interpret_prompt("Interpret Prompt"):::module
    end
    clip_core --> feat_ext
    feat_ext --> linear_probe
    linear_probe --> draw_curves
    draw_curves --> parse_test
    parse_test --> interpret_prompt

    %% Dependencies Subgraph
    subgraph "Dependencies"
        direction TB
        requirements("Requirements (requirements.txt)"):::external
        dassl("Dassl.pytorch"):::external
        pytorch("PyTorch"):::external
        clip_lib("CLIP Library"):::external
    end
    train --- requirements

    %% Click Events for CLIP Model & Utilities
    click clip_core "https://github.com/kaiyangzhou/coop/blob/main/clip/clip.py"
    click model_def "https://github.com/kaiyangzhou/coop/blob/main/clip/model.py"
    click tokenizer "https://github.com/kaiyangzhou/coop/blob/main/clip/simple_tokenizer.py"

    %% Click Events for Linear Probe & Evaluation (lpclip)
    click feat_ext "https://github.com/kaiyangzhou/coop/blob/main/lpclip/feat_extractor.py"
    click linear_probe "https://github.com/kaiyangzhou/coop/blob/main/lpclip/linear_probe.py"

    %% Click Events for Trainer Modules
    click coop_trainer "https://github.com/kaiyangzhou/coop/blob/main/trainers/coop.py"
    click cocoop_trainer "https://github.com/kaiyangzhou/coop/blob/main/trainers/cocoop.py"
    click coop_config "https://github.com/kaiyangzhou/coop/tree/main/configs/trainers/CoOp/"
    click cocoop_config "https://github.com/kaiyangzhou/coop/tree/main/configs/trainers/CoCoOp/"
    click coop_scripts "https://github.com/kaiyangzhou/coop/tree/main/scripts/coop/"
    click cocoop_scripts "https://github.com/kaiyangzhou/coop/tree/main/scripts/cocoop/"

    %% Click Events for Dataset Loader & Preprocessors
    click dataset_loader "https://github.com/kaiyangzhou/coop/tree/main/datasets/"
    click dataset_config "https://github.com/kaiyangzhou/coop/tree/main/configs/datasets/"

    %% Click Event for Experiment Entry Point
    click train "https://github.com/kaiyangzhou/coop/blob/main/train.py"

    %% Click Events for Evaluation Utilities
    click draw_curves "https://github.com/kaiyangzhou/coop/blob/main/draw_curves.py"
    click parse_test "https://github.com/kaiyangzhou/coop/blob/main/parse_test_res.py"
    click interpret_prompt "https://github.com/kaiyangzhou/coop/blob/main/interpret_prompt.py"

    %% Click Event for Requirements
    click requirements "https://github.com/kaiyangzhou/coop/blob/main/requirements.txt"

    %% Styles
    classDef user fill:#ffcc00,stroke:#333,stroke-width:2px;
    classDef entry fill:#ccffcc,stroke:#333,stroke-width:2px;
    classDef module fill:#cce5ff,stroke:#333,stroke-width:1px;
    classDef external fill:#ffcccc,stroke:#333,stroke-width:1px;
```
