# junz-debug/lsn architecture

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

- Interactive diagram: https://gitdiagram.com/junz-debug/lsn
- Repository: https://github.com/junz-debug/lsn

## Overview

##### Project type and purpose
- This is research/experimental code (“lsn”) accompanying the ICLR 2024 paper “Out-of-Distribution Detection with Negative Prompts.”
- It is structured as a set of Python modules and command-line scripts that (a) load a pre-trained CLIP model, (b) generate text and image embeddings (including “negative prompt” variants), and (c) compute OOD scores on ImageNet subsets and other datasets.

##### File structure
Top level:
- clip/               – the CLIP library (model.py, clip.py, tokenizer, vocabulary)
- coop.py             – implementation of the “COOP” OOD detection method
- feature_extra.py    – code to extract and manipulate embeddings (e.g., negative prompts)
- get_few_shot_imagenet.py, get_subset_few_shot_imagenet.py
– scripts to sample few-shot subsets of ImageNet
- get_score.py        – script to compute OOD detection scores
- main_feature_100_upload.py
– main workflow script to generate and upload features
- show_pictures.py    – visualization utilities
- data files (.txt, .npy) – class lists and arrays used by the scripts

##### README
- Very brief; confirms that this repo implements the negative-prompt OOD detection pipeline based on CLIP.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Workflow Scripts
    subgraph "Workflow Scripts"
        direction TB
        MAIN["main_feature_100_upload.py"]:::scripts
        FEW["get_few_shot_imagenet.py"]:::scripts
        SUBSET["get_subset_few_shot_imagenet.py"]:::scripts
        SCORE["get_score.py"]:::scripts
    end

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        CLIST["class_list_100.txt"]:::data
        CNAME["class_name_100.txt"]:::data
        CLSNPY["cls_name_100.npy"]:::data
        CLEAN["imagenet_class_clean.npy"]:::data
        INDEX["imagenet_class_index.txt"]:::data
    end

    %% Model Layer
    subgraph "Model Layer"
        direction TB
        MODEL["clip/model.py"]:::core
        CLIPAPI["clip/clip.py"]:::core
        TOKEN["clip/simple_tokenizer.py"]:::core
        VOCAB["clip/bpe_simple_vocab_16e6.txt.gz"]:::core
    end

    %% Embedding & Prompt Module
    subgraph "Embedding & Prompt Module"
        direction TB
        FEAT["feature_extra.py"]:::processing
        COOP["coop.py"]:::processing
    end

    %% Output & Visualization
    subgraph "Output & Visualization"
        direction TB
        RESULTS[".npy features, score logs, plots"]:::data
        VIS["show_pictures.py"]:::scripts
    end

    %% Data Flow Connections
    MAIN -->|orchestrates| FEW
    MAIN -->|orchestrates| SUBSET
    FEW -->|generates samples| CLIST
    SUBSET -->|generates samples| CNAME
    MAIN --> CLIST
    MAIN --> CNAME
    MAIN --> CLSNPY
    MAIN --> CLEAN
    MAIN --> INDEX

    CLIST -->|loads| MODEL
    CNAME -->|loads| MODEL
    CLSNPY -->|loads| MODEL
    CLEAN -->|loads| MODEL
    INDEX -->|loads| MODEL

    MODEL -->|embeddings| FEAT
    TOKEN -->|tokenizes prompts| MODEL
    VOCAB -->|provides vocab| TOKEN

    FEAT -->|creates prompt embeddings| COOP
    COOP -->|fuses embeddings| SCORE

    MODEL -->|image/text embeddings| SCORE
    FEAT -->|negative prompts| SCORE

    SCORE -->|writes results| RESULTS
    RESULTS -->|visualized by| VIS

    %% Click Events
    click CLIST "https://github.com/junz-debug/lsn/blob/main/class_list_100.txt"
    click CNAME "https://github.com/junz-debug/lsn/blob/main/class_name_100.txt"
    click CLSNPY "https://github.com/junz-debug/lsn/blob/main/cls_name_100.npy"
    click CLEAN "https://github.com/junz-debug/lsn/blob/main/imagenet_class_clean.npy"
    click INDEX "https://github.com/junz-debug/lsn/blob/main/imagenet_class_index.txt"
    click MODEL "https://github.com/junz-debug/lsn/blob/main/clip/model.py"
    click CLIPAPI "https://github.com/junz-debug/lsn/blob/main/clip/clip.py"
    click TOKEN "https://github.com/junz-debug/lsn/blob/main/clip/simple_tokenizer.py"
    click VOCAB "https://github.com/junz-debug/lsn/blob/main/clip/bpe_simple_vocab_16e6.txt.gz"
    click FEAT "https://github.com/junz-debug/lsn/blob/main/feature_extra.py"
    click COOP "https://github.com/junz-debug/lsn/blob/main/coop.py"
    click FEW "https://github.com/junz-debug/lsn/blob/main/get_few_shot_imagenet.py"
    click SUBSET "https://github.com/junz-debug/lsn/blob/main/get_subset_few_shot_imagenet.py"
    click SCORE "https://github.com/junz-debug/lsn/blob/main/get_score.py"
    click MAIN "https://github.com/junz-debug/lsn/blob/main/main_feature_100_upload.py"
    click VIS "https://github.com/junz-debug/lsn/blob/main/show_pictures.py"

    %% Styles
    classDef core fill:#cce5ff,stroke:#3399ff,color:#000;
    classDef processing fill:#d4edda,stroke:#28a745,color:#000;
    classDef scripts fill:#fff3cd,stroke:#ffcd39,color:#000;
    classDef data fill:#e2e3e5,stroke:#6c757d,color:#000;
```
