# 2abet/meat-grader-training architecture

> Architecture diagram of the GitHub repository 2abet/meat-grader-training, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/2abet/meat-grader-training
- Repository: https://github.com/2abet/meat-grader-training

## Overview

Based on our analysis of the file tree and the README, the project is a deep learning application that classifies meat images into three categories (Fresh, Half-Fresh, and Spoiled) by fine-tuning a pre-trained ResNet18 model. The application is built using PyTorch. The project is relatively small in terms of files, with a single training script (training.py) and a requirements file for dependencies. However, the README gives us insight into the full workflow, including dataset structure, preprocessing, training, validation, and evaluation processes.

##### Main Components
a. Data Input Module:
- This involves loading the dataset from the expected folder structure (meat2/train/images, meat2/valid/images) along with CSV metadata files (_classes.csv).
b. Data Preprocessing Component:
- Responsible for transforming image data (using libraries such as Pillow, Pandas, and potentially torchvision transforms) before feeding it into the model.
c. Model Component:
- The deep learning section using PyTorch: the pre-trained ResNet18 model, which is fine-tuned in the script.
d. Training Loop/Execution Module:
- The section in training.py that coordinates training by iterating over data, performing forward/backward passes, calculating losses, and adjusting weights.
e. Validation & Evaluation Module:
- This includes running the model on the validation dataset, generating classification reports, and confusion matrices.
f. Model Persistence:
- Saving (and possibly loading) the model's weights in a file (model_weights.pth).

##### Relationships and Interactions
- The Data Input Module feeds into the Preprocessing Component.
- Processed data is then directed into the Model Component.
- The Training Loop oversees the coordination between the data pipeline and the model, performing both training iterations and periodic validation.
- The Validation & Evaluation Module uses the trained model to assess performance on the validation set.
- Lastly, the Model Persistence module interacts with the Model Component by saving or loading weights.

##### Important Architectural Patterns
- Data Flow: The sequential pipeline from data ingestion to preprocessing, model training, evaluation, and finally, saving the model.
- Transfer Learning: The model component is built upon the pre-trained ResNet18 architecture.
- Modularity: Even though the project is simple, conceptually separate each functionality into its own module (e.g., data loading, model training, evaluation).

##### Relevant Technologies
- PyTorch & Torchvision (for deep learning and model handling).
- Pandas (for managing CSV data).
- Pillow (for image processing).
- Matplotlib (potentially for plotting performance metrics).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Data Pipeline"
        A["Data Input Module\n(Load images from 'meat2/train/images' & 'meat2/valid/images'\nRead '_classes.csv' with Pandas)"]:::module
        B["Data Preprocessing Component\n(Apply image transformations & augmentation using Pillow and torchvision)"]:::module
        A -->|"feeds"| B
    end

    subgraph "Model & Training"
        C["Model Component\nResNet18 (Pre-trained, Fine-Tuned using PyTorch)"]:::module
        D["Training Loop/Execution Module\n(Conduct forward/backward passes, loss computation, and weight updates)"]:::module
        E["Validation & Evaluation Module\n(Generate classification report and confusion matrix)"]:::module
        F["Model Persistence\n(Save model weights to 'model_weights.pth')"]:::module
        B -->|"processedData"| C
        C -->|"trains"| D
        D -->|"evaluates"| E
        C -->|"savesWeights"| F
    end

    G["Dependencies:\nPyTorch, Torchvision, Pandas, Pillow"]:::external

    %% Click Events from component mapping
    click A "https://github.com/2abet/meat-grader-training/blob/main/training.py"
    click B "https://github.com/2abet/meat-grader-training/blob/main/training.py"
    click C "https://github.com/2abet/meat-grader-training/blob/main/training.py"
    click D "https://github.com/2abet/meat-grader-training/blob/main/training.py"
    click E "https://github.com/2abet/meat-grader-training/blob/main/training.py"
    click F "https://github.com/2abet/meat-grader-training/blob/main/training.py"

    %% Styles
    classDef module fill:#cce5ff,stroke:#003399,stroke-width:2px;
    classDef external fill:#d1e7dd,stroke:#0f5132,stroke-width:2px;
```
