How sreejithpa21112006/opticbin works
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: inference_engine.py Inference engine · app.py Dashboard + dispatch · model_factory.py Model factory · preprocessor.py Preprocessor
Components 19
Model lifecycle
Training produces interchangeable local model artifacts.
- Dataset acquisition
download_dataset.py - Training CLI
train.py - Trainer · Fine-tunes and evaluates the selected five-class model.
src/trainer.py - PyTorch checkpoints
models/weights/.gitkeep - ONNX export + INT8 · Exports checkpoints and creates dynamically quantized variants.
models/export_onnx.py - ONNX model artifacts
Inference runtime
Offline prediction and explanation pipeline.
- Model factory · Builds backbones, loads weights, and exposes CAM target layers.
src/model_factory.py - Performance benchmark
models/benchmark.py - Preprocessor · Applies shared shape and normalization transforms.
src/preprocessor.py - Inference engine · Unifies PyTorch and ONNX Runtime execution providers.
src/inference_engine.py - XAI engine · Generates Grad-CAM or attention-oriented maps.
src/xai_engine.py - Overlay renderer · Blends activation maps with source images.
src/xai_renderer.py - Threaded camera capture
src/camera.py
Streamlit UI
Dashboard, interaction state, and input modes.
- Dashboard + dispatch · Selects model and routes image or webcam mode.
app.py - Session state · Coordinates model selection, mode, camera lifecycle, and results.
ui/state_manager.py - Dashboard components
ui/components.py - Image upload view
ui/image_view.py - Webcam view
ui/webcam_view.py
Other components
- Typed configuration · Aligns model, preprocessing, runtime, and app defaults.
config/schema.py
Connections 25
- 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