# tushky/cnninterpreter architecture

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

- Interactive diagram: https://gitdiagram.com/tushky/cnninterpreter
- Repository: https://github.com/tushky/cnninterpreter

## Overview

Based on our analysis of the file tree and README, CNNInterpreter is a specialized Python library that implements a wide collection of CNN interpretation methods. Its purpose is to help users (mainly researchers and developers) debug and understand predictions of pre-trained CNN models by generating various visualization maps (e.g., CAM, Grad-CAM, Grad-CAM++, Score-CAM, sensitivity maps, Guided Backpropagation, and Deep Dream).

##### Main Components
- Library Core (“CNNInterpreter Engine”): Represent the overall interpretation library.
- Method Modules:
- Class Activation Map Based Methods: Contains implementations for CAM, Grad-CAM, Grad-CAM++, Score-CAM, and related guided variations (found in files such as class_activation_maps.py and classes.py).
- Sensitivity Maps Based Methods: This block covers Vanilla Saliency Map, Integrated Gradients, Smooth Gradients as implemented in sensitivity_maps.py.
- Deconvolution Network Based Methods: Includes Deconvolution Network and Guided Backpropagation functionalities from deconvolution_network.py.
- Deep Dream Module: Shown by deep_dream.py.
- Helper Utilities: Utilities and hooks for intermediate computations (e.g., hook.py and utils.py).
- Input / Preprocessing Component: (Implicitly indicated in API examples) handling image read-ins, often wrapping external libraries (PIL, numpy, etc.).
- External Dependencies: Pre-trained CNN models (which are PyTorch models) and related libraries (PyTorch, numpy, scipy, matplotlib, tqdm).
- Result Output: The outputs (visualizations and CAM overlays) are stored in the results directory.

##### Relationships and Data Flow
- The image and a pre-trained CNN model (from external libraries such as torchvision.models) are provided to the appropriate interpretation method.
- Each method module (CAM block, Sensitivity Maps block, Deconvolution block, Deep Dream block) takes the input, attaches necessary hooks (using hook.py), and computes gradients/activations.
- Utility functions (from utils.py and others) support pre-/post-processing within each module.
- Finally, the interpretation results are generated as visualizations and saved in the results directory.
- Optionally, a module (e.g., create_cam_video.py) shows that there are extended functionalities for video generation using CAMs.

##### Architectural Patterns and Design Principles
- Reusability & Extensibility: The individual modules (e.g., CAM and sensitivity maps) partially share common helper utilities and rely on hook mechanisms to interface with any given CNN model. This is inherent in the design.
- Decoupling: The separation of UI/image input, computation (interpretation methods), and output (results) allows the system parts to be independent. External dependencies (like PyTorch) are isolated in the modules that require them.

##### Technologies and External Libraries
- That the core computational engine uses PyTorch, as well as other scientific libraries (numpy, scipy) and visualizations (matplotlib).
- You may create a separate “External Services/Resources” block to mention these libraries or simply annotate the method modules with these dependencies.

##### Elements
– Core modules (e.g., rectangles for main algorithmic modules),
– Utility/helper components (e.g., ovals or rounded rectangles),
- Annotations: Include annotations when needed to indicate key functions (e.g., “attach hooks”, “compute gradients”, “generate CAM overlay”).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Input & External"
        I("Image Input / Preprocessing"):::input
        EX("Pre-trained CNN Model"):::external
        LIB("External Dependencies (PyTorch, numpy, scipy, matplotlib, tqdm)"):::external
    end

    I -->|"feeds"| N("CNNInterpreter Engine"):::core
    EX -->|"supplies"| N
    LIB -->|"supports"| N

    subgraph "Interpretation Modules"
        CAM("CAM Module (CAM, Grad-CAM, Grad-CAM++, Score-CAM)"):::module
        SM("Sensitivity Maps Module (Vanilla, Integrated, Smooth Gradients)"):::module
        DEC("Deconvolution Module (DeconvNet & Guided Backpropagation)"):::module
        DD("Deep Dream Module"):::module
    end

    N -->|"routes to"| CAM
    N -->|"routes to"| SM
    N -->|"routes to"| DEC
    N -->|"routes to"| DD

    subgraph "Helper Utilities"
        H("Hook Utility"):::utility
        U2("Utils Utility"):::utility
    end

    CAM -->|"attaches"| H
    CAM -->|"utilizes"| U2
    SM -->|"attaches"| H
    SM -->|"utilizes"| U2
    DEC -->|"attaches"| H
    DEC -->|"utilizes"| U2
    DD -->|"attaches"| H
    DD -->|"utilizes"| U2

    CAM -->|"extension"| V("CAM Video Generator"):::extension

    CAM -->|"outputs to"| R("Result Output"):::output
    SM -->|"outputs to"| R
    DEC -->|"outputs to"| R
    DD -->|"outputs to"| R
    V -->|"outputs to"| R

    %% Click Events
    click N "https://github.com/tushky/cnninterpreter/tree/main/model/"
    click CAM "https://github.com/tushky/cnninterpreter/blob/main/model/class_activation_maps.py"
    click SM "https://github.com/tushky/cnninterpreter/blob/main/model/sensitivity_maps.py"
    click DEC "https://github.com/tushky/cnninterpreter/blob/main/model/deconvolution_network.py"
    click DD "https://github.com/tushky/cnninterpreter/blob/main/model/deep_dream.py"
    click H "https://github.com/tushky/cnninterpreter/blob/main/model/hook.py"
    click U2 "https://github.com/tushky/cnninterpreter/blob/main/model/utils.py"
    click V "https://github.com/tushky/cnninterpreter/blob/main/model/create_cam_video.py"
    click R "https://github.com/tushky/cnninterpreter/tree/main/results/"

    %% Styles
    classDef input fill:#f9c,stroke:#333,stroke-width:2px;
    classDef external fill:#ffdead,stroke:#333,stroke-width:2px;
    classDef core fill:#add8e6,stroke:#333,stroke-width:2px;
    classDef module fill:#90ee90,stroke:#333,stroke-width:2px;
    classDef utility fill:#ffe4b5,stroke:#333,stroke-width:2px;
    classDef extension fill:#dda0dd,stroke:#333,stroke-width:2px;
    classDef output fill:#ffc0cb,stroke:#333,stroke-width:2px;
```
