# runwenhu/cnn-prediction-based-reversible-data-hiding architecture

> Architecture diagram of the GitHub repository runwenhu/cnn-prediction-based-reversible-data-hiding, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-02-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/runwenhu/cnn-prediction-based-reversible-data-hiding
- Repository: https://github.com/runwenhu/cnn-prediction-based-reversible-data-hiding

## Overview

##### Project Overview and Identifying the Main Components
- The project is a specialized image processing application that implements a reversible data hiding method using a CNN-based predictor (as introduced in the referenced paper). Its core purpose is to calculate the PSNR (Peak Signal-to-Noise Ratio) for watermarked images by utilizing two embedding techniques: histogram shifting and expansion embedding.
- Key components include:
- The Main Application (main.py) which orchestrates the execution according to command-line parameters.
- The CNN-based predictor implemented in the “model” directory, comprising files like cnnp.py and predict_model.py.
- Model parameters stored in the “model_parameter” folder.
- The test images (in “standard_test_images”) used as sample inputs.
- Supporting utility scripts (utils.py) and several MATLAB scripts (calculate_complexity.m, calculate_tp_tn.m, cnn_expansion.m, cnn_histogram_shifting.m, num2bitlist.m) that might support or validate certain operations.

##### Analyzing the File Structure for Architectural Insights
- Top-Level Files/Directories:
- README.md: Provides project description, usage instructions, and parameter details.
- Python source code files (main.py, utils.py) that serve as the entry point and helper functions.
- “model” folder: Contains CNN predictor implementations. This indicates a modular separation where core processing logic (the CNN predictor) is decoupled from the main orchestration.
- “model_parameter” folder: Stores pretrained model weights. Its separation shows an adherence to the separation of concerns.
- The directory names and file names provide clues; for example, “cnn_expansion.m” and “cnn_histogram_shifting.m” suggest distinct modules for each embedding type. This implies a branching or conditional flow in the data embedding pipeline within the application.

##### Extracting Insights from the README
- The README clearly states how to use the software with different modes (histogram shifting vs. expansion embedding), as well as the required environment details (Windows 10, Python 3.7, PyTorch 1.6.0, MATLAB 2019a).
- The application depends on a pretrained model (from “model_parameter”) loaded within the CNN predictor logic.

a. Main Components to Include:
- Input Component: “Standard Test Images” folder (e.g., Barbara.bmp, Lena.bmp, etc.).
- User Interface/CLI: The command-line interface handled by main.py that receives parameters such as image size, model file path, folder, mode, and data length.
- Processing Core:
- Data Handling Module: Responsible for loading images and preprocessing.
- CNN-Predictor Module: Located in the “model” folder (using cnnp.py and predict_model.py), which implements the CNN-based predictor.
- Embedding Logic: Divided into two branches/components for:
- Histogram Shifting embedding (supported by cnn_histogram_shifting.m).
- Expansion Embedding (supported by cnn_expansion.m).
- Utility Module: Supporting functions in utils.py.
- Model Parameter Loader: A component that loads the model weights from “model_parameter/model_state.pth”.
- Evaluation Module: Calculation and reporting of PSNR values.
- Auxiliary Scripts: MATLAB scripts (calculate_complexity.m, calculate_tp_tn.m, num2bitlist.m) that might be used for additional processing or evaluation.

b. Relationships and Data Flow:
- The controller chooses between histogram shifting and expansion embedding based on command-line mode, directing the flow to the respective embedding logic.
- Calls are made to the CNN predictor module that uses model parameters for predictions.
- The processed images are then evaluated for PSNR.
- Optionally, MATLAB scripts might be invoked separately (or serve as reference implementations) for auxiliary computations.
- Data flows are directional: from image input -> processing (CNN prediction and embedding) -> evaluation -> output (PSNR value).

c. Architectural Patterns & Design Principles:
- Modular Design: Separating the CNN predictor from the main orchestration code.
- Parameterization: External model parameters are decoupled and loaded at runtime.
- Multi-environment Integration: Blend of Python (main processing) and MATLAB (auxiliary functions).
- Conditional Branching: Different embedding techniques encapsulated in separate modules.

d. Relevant Technologies:
- Python 3.7 (Core scripting and application logic)
- PyTorch 1.6.0 (For CNN implementation in the “model” folder)
- MATLAB 2019a (For supporting .m functions)
- Image processing libraries (implied by image loading and processing, though not explicitly shown in the file tree, likely in utils.py or imported in main.py)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Top Level Components
    CLI["Command-Line Interface (main.py)"]:::cli
    UTILS["Utility Functions (utils.py)"]:::utils
    IMG["Standard Test Images"]:::input
    PREPROC["Image Loader and Preprocessor"]:::preproc
    CNN["CNN Predictor Module (model/cnnp.py & model/predict_model.py)"]:::cnn
    MODEL["Model Parameter Loader"]:::param
    DECISION["Select_Embedding_Method"]:::decision
    HS["Histogram_Shifting Embedding (cnn_histogram_shifting.m)"]:::emb
    EE["Expansion Embedding (cnn_expansion.m)"]:::emb
    PSNR["PSNR Calculation and Output"]:::evaluation
    OUTPUT["Output: PSNR Display"]:::output

    %% Auxiliary MATLAB Subgraph
    subgraph "MATLAB Auxiliary Scripts"
        MC["calculate_complexity.m"]:::matlab
        TP["calculate_tp_tn.m"]:::matlab
        NB["num2bitlist.m"]:::matlab
    end

    %% Data Flow
    CLI -->|"provides parameters"| PREPROC
    IMG -->|"supplies images"| PREPROC
    PREPROC -->|"preprocessed data"| CNN
    UTILS ---|"support functions"| PREPROC
    MODEL -->|"loads weights"| CNN
    CNN -->|"prediction output"| DECISION
    DECISION -->|"histogram"| HS
    DECISION -->|"expansion"| EE
    HS -->|"embedded image"| PSNR
    EE --> PSNR
    PSNR -->|"final evaluation"| OUTPUT
    PSNR ---|"auxiliary analysis"| MC
    PSNR --- TP
    PSNR --- NB

    %% Click Events
    click CLI "https://github.com/runwenhu/cnn-prediction-based-reversible-data-hiding/blob/main/main.py"
    click IMG "https://github.com/runwenhu/cnn-prediction-based-reversible-data-hiding/tree/main/standard_test_images"
    click UTILS "https://github.com/runwenhu/cnn-prediction-based-reversible-data-hiding/blob/main/utils.py"
    click CNN "https://github.com/runwenhu/cnn-prediction-based-reversible-data-hiding/blob/main/model/cnnp.py"
    click CNN "https://github.com/runwenhu/cnn-prediction-based-reversible-data-hiding/blob/main/model/predict_model.py"
    click MODEL "https://github.com/runwenhu/cnn-prediction-based-reversible-data-hiding/blob/main/model_parameter/model_state.pth"
    click HS "https://github.com/runwenhu/cnn-prediction-based-reversible-data-hiding/blob/main/cnn_histogram_shifting.m"
    click EE "https://github.com/runwenhu/cnn-prediction-based-reversible-data-hiding/blob/main/cnn_expansion.m"
    click MC "https://github.com/runwenhu/cnn-prediction-based-reversible-data-hiding/blob/main/calculate_complexity.m"
    click TP "https://github.com/runwenhu/cnn-prediction-based-reversible-data-hiding/blob/main/calculate_tp_tn.m"
    click NB "https://github.com/runwenhu/cnn-prediction-based-reversible-data-hiding/blob/main/num2bitlist.m"

    %% Styles
    classDef cli fill:#ffcccc,stroke:#333,stroke-width:2px;
    classDef input fill:#ccffcc,stroke:#333,stroke-width:2px;
    classDef preproc fill:#ffffcc,stroke:#333,stroke-width:2px;
    classDef utils fill:#fafad2,stroke:#333,stroke-width:2px;
    classDef cnn fill:#d1e7dd,stroke:#333,stroke-width:2px;
    classDef param fill:#cfe2f3,stroke:#333,stroke-width:2px;
    classDef decision fill:#fce5cd,stroke:#333,stroke-width:2px;
    classDef emb fill:#fdebd0,stroke:#333,stroke-width:2px;
    classDef evaluation fill:#e2e3e5,stroke:#333,stroke-width:2px;
    classDef output fill:#d6eaf8,stroke:#333,stroke-width:2px;
    classDef matlab fill:#d5f5e3,stroke:#333,stroke-width:2px;
```
