# sreejithpa21112006/opticbin architecture

> Architecture diagram of the GitHub repository sreejithpa21112006/opticbin, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sreejithpa21112006/opticbin
- Repository: https://github.com/sreejithpa21112006/opticbin

## Overview

#### Purpose and entry points
OpticBin is an offline edge-AI waste-material classifier for glass, paper, cardboard, plastic, and metal. It presents real-time predictions and XAI heatmap overlays for uploaded images or webcam frames.

Primary entry points:
- Dashboard and mode dispatch: `app.py`
- Training/fine-tuning CLI: `train.py`
- Dataset acquisition: `download_dataset.py`
- ONNX export and INT8 quantization: `models/export_onnx.py`
- Performance measurement: `models/benchmark.py`

#### Configuration and shared contracts
Central typed configuration defines model, preprocessing, runtime, and application defaults. It is the boundary that keeps training, inference, camera processing, and UI behavior aligned.
- `config/schema.py`
- `config/settings.py`
- `config/__init__.py`

#### Model lifecycle: training to deployable artifacts
Training builds one of two hot-swappable `timm` backbones—EfficientNetV2-S or MobileViT-XS—with a five-class output head, evaluates it, and saves PyTorch checkpoints under the local artifact directory. The export stage converts checkpoints to ONNX and produces dynamically quantized INT8 variants for lower-memory, local inference.
- `train.py`
- `src/trainer.py`
- `src/model_factory.py`
- `models/export_onnx.py`
- `models/weights/.gitkeep`

#### Model factory and inference boundary
The model factory is the architecture abstraction: it creates the selected backbone, loads checkpoints, and identifies CAM-compatible target layers. The inference engine standardizes execution across PyTorch and ONNX Runtime models, isolating caller code from model format and execution-provider details.
- `src/model_factory.py`
- `src/inference_engine.py`
- `models/benchmark.py`

#### Input and preprocessing pipeline
Inputs enter either from uploaded files or a threaded webcam capture path. Frames/images pass through a shared preprocessing pipeline before inference, preserving consistent model input shape and normalization across dashboard and training/runtime paths.
- `src/camera.py`
- `src/preprocessor.py`
- `ui/image_view.py`
- `ui/webcam_view.py`

#### Explainability and visualization
For each prediction, the XAI engine produces Grad-CAM or attention-oriented activation maps using the selected model’s target layer. The renderer converts those maps into blended visual overlays; views display the source and explanation alongside prediction confidence and class information.
- `src/xai_engine.py`
- `src/xai_renderer.py`
- `ui/image_view.py`
- `ui/webcam_view.py`

#### Presentation, state, and runtime dependencies
Streamlit is the interactive runtime and owns dashboard rendering and session state. UI components and state management coordinate selected model, input mode, camera lifecycle, and result presentation. The system is designed for fully offline operation: no cloud APIs or external services are in the prediction path. Material dependencies are Python 3.10+, PyTorch/timm, ONNX Runtime with CPU or optional CUDA providers, OpenCV/Albumentations, and Streamlit.
- `app.py`
- `ui/state_manager.py`
- `ui/components.py`
- `requirements.txt`

Main entry points: [`src/inference_engine.py`](https://github.com/sreejithpa21112006/opticbin/blob/main/src/inference_engine.py) (Inference engine), [`app.py`](https://github.com/sreejithpa21112006/opticbin/blob/main/app.py) (Dashboard + dispatch), [`src/model_factory.py`](https://github.com/sreejithpa21112006/opticbin/blob/main/src/model_factory.py) (Model factory), [`src/preprocessor.py`](https://github.com/sreejithpa21112006/opticbin/blob/main/src/preprocessor.py) (Preprocessor)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_lifecycle["Model lifecycle"]
  node_dataset_download["Dataset acquisition<br/>dataset CLI"]
  node_training_cli["Training CLI<br/>training entry<br/>[train.py]"]
  node_trainer["Trainer<br/>training loop<br/>[trainer.py]"]
  node_checkpoints[("PyTorch checkpoints<br/>model artifacts<br/>[.gitkeep]")]
  node_onnx_export["ONNX export + INT8<br/>artifact pipeline<br/>[export_onnx.py]"]
  node_onnx_models[("ONNX model artifacts<br/>deployable artifacts")]
end

subgraph group_runtime["Inference runtime"]
  node_model_factory{{"Model factory<br/>model abstraction<br/>[model_factory.py]"}}
  node_benchmark["Performance benchmark<br/>measurement CLI<br/>[benchmark.py]"]
  node_preprocessor["Preprocessor<br/>input pipeline<br/>[preprocessor.py]"]
  node_inference_engine{{"Inference engine<br/>execution boundary"}}
  node_xai_engine["XAI engine<br/>explainability<br/>[xai_engine.py]"]
  node_xai_renderer["Overlay renderer<br/>visualization<br/>[xai_renderer.py]"]
  node_camera["Threaded camera capture<br/>frame source<br/>[camera.py]"]
end

subgraph group_ui["Streamlit UI"]
  node_dashboard["Dashboard + dispatch<br/>Streamlit entry<br/>[app.py]"]
  node_ui_state["Session state<br/>UI state<br/>[state_manager.py]"]
  node_ui_components["Dashboard components<br/>UI controls<br/>[components.py]"]
  node_image_view["Image upload view<br/>input view<br/>[image_view.py]"]
  node_webcam_view["Webcam view<br/>live input view<br/>[webcam_view.py]"]
end

node_shared_config["Typed configuration<br/>shared contract<br/>[schema.py]"]

node_dataset_download -->|"supplies dataset"| node_training_cli
node_training_cli -.->|"reads defaults"| node_shared_config
node_training_cli -->|"runs"| node_trainer
node_trainer -->|"creates backbone"| node_model_factory
node_trainer -->|"saves evaluated weights"| node_checkpoints
node_shared_config -.->|"defines transforms"| node_preprocessor
node_checkpoints -->|"exports"| node_onnx_export
node_onnx_export -->|"produces ONNX + INT8"| node_onnx_models
node_checkpoints -->|"loads PyTorch weights"| node_model_factory
node_onnx_models -->|"loads ONNX variants"| node_inference_engine
node_model_factory -->|"provides PyTorch model"| node_inference_engine
node_benchmark -->|"measures"| node_inference_engine
node_dashboard -->|"owns session"| node_ui_state
node_dashboard -->|"renders controls"| node_ui_components
node_dashboard -->|"dispatches upload mode"| node_image_view
node_dashboard -->|"dispatches webcam mode"| node_webcam_view
node_webcam_view -->|"reads frames"| node_camera
node_image_view -->|"prepares image"| node_preprocessor
node_camera -->|"prepares frames"| node_preprocessor
node_preprocessor -->|"model input"| node_inference_engine
node_inference_engine -->|"prediction + activations"| node_xai_engine
node_model_factory -->|"CAM target layer"| node_xai_engine
node_xai_engine -->|"activation map"| node_xai_renderer
node_xai_renderer -->|"overlay result"| node_image_view
node_xai_renderer -->|"live overlay"| node_webcam_view

click node_dataset_download "https://github.com/sreejithpa21112006/opticbin/blob/main/download_dataset.py"
click node_training_cli "https://github.com/sreejithpa21112006/opticbin/blob/main/train.py"
click node_trainer "https://github.com/sreejithpa21112006/opticbin/blob/main/src/trainer.py"
click node_model_factory "https://github.com/sreejithpa21112006/opticbin/blob/main/src/model_factory.py"
click node_checkpoints "https://github.com/sreejithpa21112006/opticbin/blob/main/models/weights/.gitkeep"
click node_onnx_export "https://github.com/sreejithpa21112006/opticbin/blob/main/models/export_onnx.py"
click node_benchmark "https://github.com/sreejithpa21112006/opticbin/blob/main/models/benchmark.py"
click node_shared_config "https://github.com/sreejithpa21112006/opticbin/blob/main/config/schema.py"
click node_preprocessor "https://github.com/sreejithpa21112006/opticbin/blob/main/src/preprocessor.py"
click node_inference_engine "https://github.com/sreejithpa21112006/opticbin/blob/main/src/inference_engine.py"
click node_xai_engine "https://github.com/sreejithpa21112006/opticbin/blob/main/src/xai_engine.py"
click node_xai_renderer "https://github.com/sreejithpa21112006/opticbin/blob/main/src/xai_renderer.py"
click node_dashboard "https://github.com/sreejithpa21112006/opticbin/blob/main/app.py"
click node_ui_state "https://github.com/sreejithpa21112006/opticbin/blob/main/ui/state_manager.py"
click node_ui_components "https://github.com/sreejithpa21112006/opticbin/blob/main/ui/components.py"
click node_image_view "https://github.com/sreejithpa21112006/opticbin/blob/main/ui/image_view.py"
click node_webcam_view "https://github.com/sreejithpa21112006/opticbin/blob/main/ui/webcam_view.py"
click node_camera "https://github.com/sreejithpa21112006/opticbin/blob/main/src/camera.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_dataset_download,node_training_cli,node_trainer,node_checkpoints,node_onnx_export,node_onnx_models toneBlue
class node_model_factory,node_benchmark,node_preprocessor,node_inference_engine,node_xai_engine,node_xai_renderer,node_camera toneAmber
class node_dashboard,node_ui_state,node_ui_components,node_image_view,node_webcam_view toneMint
class node_shared_config toneNeutral
```

## Components

### Model lifecycle

Training produces interchangeable local model artifacts.

- [Dataset acquisition](https://github.com/sreejithpa21112006/opticbin/blob/main/download_dataset.py): dataset CLI · `download_dataset.py`
- [Training CLI](https://github.com/sreejithpa21112006/opticbin/blob/main/train.py): training entry · `train.py`
- [Trainer](https://github.com/sreejithpa21112006/opticbin/blob/main/src/trainer.py): training loop · Fine-tunes and evaluates the selected five-class model. · `src/trainer.py`
- [PyTorch checkpoints](https://github.com/sreejithpa21112006/opticbin/blob/main/models/weights/.gitkeep): model artifacts · `models/weights/.gitkeep`
- [ONNX export + INT8](https://github.com/sreejithpa21112006/opticbin/blob/main/models/export_onnx.py): artifact pipeline · Exports checkpoints and creates dynamically quantized variants. · `models/export_onnx.py`
- **ONNX model artifacts**: deployable artifacts

### Inference runtime

Offline prediction and explanation pipeline.

- [Model factory](https://github.com/sreejithpa21112006/opticbin/blob/main/src/model_factory.py): model abstraction · Builds backbones, loads weights, and exposes CAM target layers. · `src/model_factory.py`
- [Performance benchmark](https://github.com/sreejithpa21112006/opticbin/blob/main/models/benchmark.py): measurement CLI · `models/benchmark.py`
- [Preprocessor](https://github.com/sreejithpa21112006/opticbin/blob/main/src/preprocessor.py): input pipeline · Applies shared shape and normalization transforms. · `src/preprocessor.py`
- [Inference engine](https://github.com/sreejithpa21112006/opticbin/blob/main/src/inference_engine.py): execution boundary · Unifies PyTorch and ONNX Runtime execution providers. · `src/inference_engine.py`
- [XAI engine](https://github.com/sreejithpa21112006/opticbin/blob/main/src/xai_engine.py): explainability · Generates Grad-CAM or attention-oriented maps. · `src/xai_engine.py`
- [Overlay renderer](https://github.com/sreejithpa21112006/opticbin/blob/main/src/xai_renderer.py): visualization · Blends activation maps with source images. · `src/xai_renderer.py`
- [Threaded camera capture](https://github.com/sreejithpa21112006/opticbin/blob/main/src/camera.py): frame source · `src/camera.py`

### Streamlit UI

Dashboard, interaction state, and input modes.

- [Dashboard + dispatch](https://github.com/sreejithpa21112006/opticbin/blob/main/app.py): Streamlit entry · Selects model and routes image or webcam mode. · `app.py`
- [Session state](https://github.com/sreejithpa21112006/opticbin/blob/main/ui/state_manager.py): UI state · Coordinates model selection, mode, camera lifecycle, and results. · `ui/state_manager.py`
- [Dashboard components](https://github.com/sreejithpa21112006/opticbin/blob/main/ui/components.py): UI controls · `ui/components.py`
- [Image upload view](https://github.com/sreejithpa21112006/opticbin/blob/main/ui/image_view.py): input view · `ui/image_view.py`
- [Webcam view](https://github.com/sreejithpa21112006/opticbin/blob/main/ui/webcam_view.py): live input view · `ui/webcam_view.py`

### Other components

- [Typed configuration](https://github.com/sreejithpa21112006/opticbin/blob/main/config/schema.py): shared contract · Aligns model, preprocessing, runtime, and app defaults. · `config/schema.py`

## Connections

- Dataset acquisition → Training CLI: supplies dataset
- Training CLI → Typed configuration: reads defaults
- Training CLI → Trainer: runs
- Trainer → Model factory: creates backbone
- Trainer → PyTorch checkpoints: saves evaluated weights
- Typed configuration → Preprocessor: defines transforms
- PyTorch checkpoints → ONNX export + INT8: exports
- ONNX export + INT8 → ONNX model artifacts: produces ONNX + INT8
- PyTorch checkpoints → Model factory: loads PyTorch weights
- ONNX model artifacts → Inference engine: loads ONNX variants
- Model factory → Inference engine: provides PyTorch model
- Performance benchmark → Inference engine: measures
- Dashboard + dispatch → Session state: owns session
- Dashboard + dispatch → Dashboard components: renders controls
- Dashboard + dispatch → Image upload view: dispatches upload mode
- Dashboard + dispatch → Webcam view: dispatches webcam mode
- Webcam view → Threaded camera capture: reads frames
- Image upload view → Preprocessor: prepares image
- Threaded camera capture → Preprocessor: prepares frames
- Preprocessor → Inference engine: model input
- Inference engine → XAI engine: prediction + activations
- Model factory → XAI engine: CAM target layer
- XAI engine → Overlay renderer: activation map
- Overlay renderer → Image upload view: overlay result
- Overlay renderer → Webcam view: live overlay
