# liuyuxinict/prenet architecture

> Architecture diagram of the GitHub repository liuyuxinict/prenet, 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/liuyuxinict/prenet
- Repository: https://github.com/liuyuxinict/prenet

## Overview

##### Project Type and Purpose
- This repository implements a deep learning model—the Progressive Region Enhancement Network (PRENet)—for large scale visual food recognition.
- The system is chiefly responsible for training and inference on food image datasets using a progressively trained, multi-scale network with self-attention mechanisms.
- The README emphasizes the training strategy (progressive stages, region feature enhancement via self-attention, and concatenation of local & global features) and includes instructions for preparing data, training, and inference.

##### Analyzing the File Structure
- Key files include:
- main.py: Contains the primary execution flow, handling training and inference.
- Resnet.py and senet.py: Likely include backbone network implementations (or modifications thereof) for feature extraction.
- model.py: Probably holds the overall network architecture integrating progressive regional feature learning.
- data_loader.py: Responsible for reading and preparing the dataset.
- layer_self_attention.py and self_attention.py: Implementation of self-attention layers used in region feature enhancement.
- utils.py: Contains helper functions used across the project.
- prenet: This directory might contain additional modules or configurations associated with the PRENet’s progressive training or model building.
- The presence of multiple Python files focused on model components (especially self-attention variants) indicates an architecture that integrates several sub-modules, including separate modules for feature extraction, local feature improvements, and fusion of multi-scale information.

- Data Input & Preparation:
▫ Data Loader module (data_loader.py) that reads food images from the dataset directory.
▫ External dataset structure: The image data is organized by class folders, as shown in the README.
- Model Backbone/Feature Extraction:
▫ Modules like Resnet.py and senet.py which likely implement CNN feature extractors.
▫ These serve as the global feature learning branch.
- Progressive Local Feature Learning:
▫ Model components in model.py and perhaps additional routines in prenet that implement progressive training stages.
▫ The progressive training is aimed at learning fine-grained, local features.
- Region Feature Enhancement:
▫ Self-attention modules (from self_attention.py and layer_self_attention.py) that enhance local feature representations.
- Feature Fusion & Classification:
▫ A concat layer is employed to merge enhanced local features with global ones.
▫ The final classification layer that outputs predictions for food recognition.
- Training & Loss Components:
▫ Inclusion of progressive training stages and KL-divergence loss to enforce differentiation between stages.
- Inference Pipeline:
▫ Separate execution path in main.py activated by command-line arguments for testing (flag: --test).

##### Relationships and Data Flow
- The data flows from the input stage (data_loader) to the backbone (Resnet/senet) for initial global feature extraction.
- In parallel (or sequentially, as per design), local features are extracted progressively, enhanced via self-attention mechanisms.
- A fusion step (using concat) combines both types of features.
- Output is passed to a classifier, and during training, a specialized loss (with KL-divergence) is calculated.

##### Architectural Patterns and Design Principles
- Modular structure: Each file represents a distinct functional component, making it clear that a modular design or even a “layered” approach is used.
- Progressive training and multi-scale feature extraction: Clearly denote the separation between the progressive stage modules and the final fusion.
- The use of self-attention signifies an emphasis on context-aware feature enhancement—a design point worth highlighting.

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["Training & Inference Controller (main.py)"]:::controller
    B["Data Loader (data_loader.py)"]:::processing

    subgraph "Global Feature Extraction"
        C1["Resnet Feature Extractor (Resnet.py)"]:::extraction
        C2["Senet Feature Extractor (senet.py)"]:::extraction
    end

    subgraph "Progressive Local Feature Learning"
        D1["Progressive Model (model.py)"]:::extraction
        D2["PRENet Modules (prenet)"]:::extraction
    end

    subgraph "Region Feature Enhancement"
        E1["Self-Attention Module (self_attention.py)"]:::enhancement
        E2["Layer Self-Attention (layer_self_attention.py)"]:::enhancement
    end

    F["Feature Fusion & Classification (Concat Layer)"]:::fusion
    G["Prediction Output"]:::processing

    A --> B
    B --> C1
    B --> C2
    B --> D1
    B --> D2
    C1 --> F
    C2 --> F
    D1 --> E1
    D2 --> E2
    E1 --> F
    E2 --> F
    F --> G

    click A "https://github.com/liuyuxinict/prenet/blob/master/main.py"
    click B "https://github.com/liuyuxinict/prenet/blob/master/data_loader.py"
    click C1 "https://github.com/liuyuxinict/prenet/blob/master/Resnet.py"
    click C2 "https://github.com/liuyuxinict/prenet/blob/master/senet.py"
    click D1 "https://github.com/liuyuxinict/prenet/blob/master/model.py"
    click D2 "https://github.com/liuyuxinict/prenet/tree/master/prenet"
    click E1 "https://github.com/liuyuxinict/prenet/blob/master/self_attention.py"
    click E2 "https://github.com/liuyuxinict/prenet/blob/master/layer_self_attention.py"

    classDef controller fill:#FFCC99,stroke:#333,stroke-width:2px;
    classDef processing fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef extraction fill:#d5f5e3,stroke:#333,stroke-width:2px;
    classDef enhancement fill:#f9e79f,stroke:#333,stroke-width:2px;
    classDef fusion fill:#fadbd8,stroke:#333,stroke-width:2px;
```
