# rupeshgarsondiya/ocr architecture

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

- Interactive diagram: https://gitdiagram.com/rupeshgarsondiya/ocr
- Repository: https://github.com/rupeshgarsondiya/ocr

## Overview

##### Project Type and Purpose
- This is a machine-learning/vision project for Optical Character Recognition (OCR) using a Vision Transformer model.
- The codebase is organized around data handling, model definition, GPU setup, training, and testing scripts.

##### File Structure Analysis
- Top-level directories:
- data/ – raw image and CSV datasets, data loading helpers (data.py)
- src/ – core Python modules
· config/ – global configuration (config.py)
· gpu_config/ – GPU checks and setup (check.py)
· nn_arch/ – the neural network architecture (trocr.py)
· ocrdata.py – data-pipeline wrappers
· train.py – training loop
· test.py – evaluation/inference
- No external services; it’s a stand-alone pipeline.

##### Key Insights from README
- Implements a Vision Transformer (ViT) for text extraction from images.
- No mention of deployment or web UI—focus is on model development.

##### Main Components
a. Data Layer
- Raw Image Files & CSV manifests.
- data/data.py: data ingestion logic.
b. Preprocessing Layer
- ocrdata.py: image transforms, tokenization of text.
c. Configuration Layer
- src/config/config.py: hyperparameters, file paths, I/O settings.
- src/gpu_config/check.py: GPU availability and settings.
d. Model Architecture
- src/nn_arch/trocr.py: Vision Transformer definition, forward pass.
e. Training Engine
- src/train.py: training loop, loss computation, optimizer steps, checkpointing.
f. Evaluation Engine
- src/test.py: inference on test set, metric computation, saving outputs.
g. Artifacts Storage
- Checkpoints, logs, evaluation reports.

##### Component Interactions and Data Flow
1. Data Storage → Data Ingestion (data.py)
2. Data Ingestion → Preprocessing (ocrdata.py)
3. Preprocessing ↔ Configuration (config.py)
4. GPU Setup (check.py) → Training Engine
5. Preprocessed Batches + Model (trocr.py) → Training Loop (train.py)
6. Trained Model Checkpoint → Evaluation Engine (test.py)
7. Outputs → Artifacts Storage (logs, metrics, predictions)

##### Architectural Patterns & Design Principles
- Layered pipeline separating data I/O, preprocessing, model definition, training, and evaluation.
- Centralized configuration to avoid hard-coded values.
- Modular NN architecture for easy experimentation.
- GPU‐agnostic entry point (gpu_config) to support CPU/GPU execution.

##### Detailed Capture of Architectural Elements
- Don’t just show “Model”—break it down into “Transformer Encoder,” “Cross‐Attention,” etc., if needed.
- “Checkpoint Manager” in the Training block.
- Where metrics/logging libraries integrate (e.g., TensorBoard).
- If auxiliary scripts exist (e.g., hyperparameter sweeps), represent them as side-branches.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Layer
    RawData(["Raw Images & CSV"]):::storage
    DataIngest["Data Ingestion\n(data/data.py)"]:::data
    RawData -->|"images & manifests"| DataIngest

    %% Preprocessing Layer
    Preprocess["Preprocessing\n(src/ocrdata.py)"]:::data
    DataIngest -->|"raw samples"| Preprocess

    %% Configuration & GPU Setup
    subgraph "Configuration Layer"
        Config["Config Module\n(src/config/config.py)"]:::config
        GPUCheck["GPU Check\n(src/gpu_config/check.py)"]:::config
    end
    Preprocess -->|"requires params"| Config
    Preprocess -->|"requires device"| GPUCheck

    %% Model Architecture
    Model["Vision Transformer\n(src/nn_arch/trocr.py)"]:::model
    Config --> Model

    %% Training Engine
    Train["Training Engine\n(src/train.py)"]:::compute
    GPUCheck --> Train
    Preprocess -->|"batches"| Train
    Model -->|"model definition"| Train

    %% Checkpoint Storage
    Checkpoints(["Model Checkpoints"]):::storage
    Train -->|"checkpoints"| Checkpoints
    Train -->|"logs & metrics"| Artifacts

    %% Evaluation Engine
    Test["Evaluation Engine\n(src/test.py)"]:::compute
    Checkpoints -->|"load checkpoint"| Test
    Test -->|"predictions & metrics"| Artifacts

    %% Artifacts Storage
    Artifacts(["Logs & Reports"]):::storage

    %% Legend
    subgraph Legend
        direction TB
        A["Rectangle: Modules"]:::module
        B["Cylinder: Storage"]:::storage
        C["Blue: Data"]:::data
        D["Green: Compute/Model"]:::compute
        E["Orange: Config"]:::config
        F["Gray: Storage"]:::storage
    end

    %% Click Events
    click DataIngest "https://github.com/rupeshgarsondiya/ocr/blob/main/data/data.py"
    click RawData "https://github.com/rupeshgarsondiya/ocr/tree/main/data/raw/"
    click Preprocess "https://github.com/rupeshgarsondiya/ocr/blob/main/src/ocrdata.py"
    click Config "https://github.com/rupeshgarsondiya/ocr/blob/main/src/config/config.py"
    click GPUCheck "https://github.com/rupeshgarsondiya/ocr/blob/main/src/gpu_config/check.py"
    click Model "https://github.com/rupeshgarsondiya/ocr/blob/main/src/nn_arch/trocr.py"
    click Train "https://github.com/rupeshgarsondiya/ocr/blob/main/src/train.py"
    click Test "https://github.com/rupeshgarsondiya/ocr/blob/main/src/test.py"

    %% Styles
    classDef data fill:#D0E8FF,stroke:#0288D1,color:#000
    classDef compute fill:#E0F7FA,stroke:#00838F,color:#000
    classDef model fill:#C8E6C9,stroke:#388E3C,color:#000
    classDef config fill:#FFE0B2,stroke:#FB8C00,color:#000
    classDef storage fill:#F5F5F5,stroke:#9E9E9E,color:#000
    classDef module fill:#FFFFFF,stroke:#000000,color:#000
```
