# wagur1/pre_processing_simulate architecture

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

- Interactive diagram: https://gitdiagram.com/wagur1/pre_processing_simulate
- Repository: https://github.com/wagur1/pre_processing_simulate

## Overview

1. **Project purpose**
   - This repo is a PyTorch training pipeline for learning a video-frame preprocessor optimized for downstream machine vision under compression, not for standalone playback or serving.
   - The core idea is to jointly optimize compression quality, bitrate, and task accuracy.

2. **Top-level structure**
   - `dataset.py` handles data loading and dataset-specific frame sampling.
   - `model.py` contains the learnable preprocessing/compression modules and the wrapper system that combines them.
   - `train.py` owns optimization, validation, checkpointing, and the frozen downstream task evaluator.
   - `checkpoints/` stores trained model weights (`best_model.pt`, `final_model.pt`).

3. **Data ingestion boundary**
   - The input side supports two mutually distinct dataset paths:
     - **GOT-10k local sequences** via filesystem-backed image folders.
     - **Kinetics-400** via HuggingFace `datasets`.
   - This creates a clean boundary between local sequence loading and remote dataset streaming/download behavior.

4. **Dataset subsystem**
   - `VideoFrameDataset`, `GOT10kDataset`, and `HFKinetics400Dataset` abstract the two data modes.
   - Their role is to provide frame-level or short-sequence samples suitable for learning temporal preprocessing.
   - OpenCV (`cv2`) is used for image/frame IO and decoding work where needed.

5. **Model subsystem: preprocessor**
   - `Preprocessor` is a multi-branch CNN designed to fuse:
     - **spatial/intra-frame cues** from the current frame
     - **temporal/inter-frame cues** from neighboring frames
   - Its output is an enhanced frame intended to survive compression better for machine vision tasks.

6. **Model subsystem: virtual codec**
   - `VirtualCodec` acts as a differentiable stand-in for a real video encoder.
   - It includes:
     - Straight-Through Estimator-based quantization
     - a Laplacian entropy model for bitrate estimation
   - This is the architecture’s compression surrogate, allowing bitrate to be optimized during backpropagation.

7. **End-to-end training wrapper**
   - `PreprocessingSystem` likely composes the preprocessor and virtual codec into a single forward path.
   - The data flow is: input frames/sequences → preprocessor → reconstructed/compressed representation → losses.
   - This wrapper is the main trainable boundary exposed to the training loop.

8. **Frozen task evaluator**
   - `train.py` includes a downstream analyzer, described as a frozen pretrained 3D CNN such as R3D-18 or SlowFast.
   - It is not trained here; it serves only to compute task loss/accuracy loss (`L_Acc`).
   - This separates representation optimization from task-model optimization.

9. **Objective function**
   - Training minimizes a composite objective:
     - distortion loss: MSE between original and reconstructed frame
     - rate loss: estimated bpp from the virtual codec
     - task loss: cross-entropy from the frozen analyzer
   - The reported weighting is `L = α·(L_D + λ·L_R) + L_Acc`, with `α=10.0` and `λ=0.001`.

10. **Training and lifecycle**
   - `train.py` manages the full loop: forward pass, loss computation, backpropagation, validation, and early stopping.
   - Adam with `1e-4` learning rate is the optimizer.
   - The training process writes two lifecycle artifacts:
     - `best_model.pt` for the best validation result
     - `final_model.pt` for the last epoch state

11. **External dependencies that matter architecturally**
   - PyTorch/Torchvision provide the model, tensor, and pretrained-video-model backbone ecosystem.
   - HuggingFace `datasets` is only relevant for the Kinetics-400 data path.
   - These dependencies shape the runtime: GPU-oriented training, not an inference-only or lightweight deployment stack.

12. **Diagramming takeaway**
   - The main architectural separation is:
     - **data adapters** in `dataset.py`
     - **trainable preprocessing/compression core** in `model.py`
     - **training orchestration and frozen task loss** in `train.py`
   - A good architecture diagram should show the dataset feeding a preprocessing/compression system, with a frozen downstream analyzer providing task feedback, and checkpoint outputs written to disk.

Main entry points: [`train.py`](https://github.com/wagur1/pre_processing_simulate/blob/main/train.py) (train.py), [`dataset.py`](https://github.com/wagur1/pre_processing_simulate/blob/main/dataset.py) (dataset.py), [`model.py`](https://github.com/wagur1/pre_processing_simulate/blob/main/model.py) (model.py), [`checkpoints/best_model.pt`](https://github.com/wagur1/pre_processing_simulate/blob/main/checkpoints/best_model.pt) (best_model.pt)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Data ingestion"]
  node_dataset_py["dataset.py<br/>[dataset.py]"]
  node_video_frame_dataset["Video frames<br/>dataset base"]
  node_got10k_dataset["GOT-10k<br/>local dataset"]
  node_hf_kinetics_dataset["Kinetics-400<br/>hf dataset"]
end

subgraph group_model["Trainable core"]
  node_model_py["model.py<br/>[model.py]"]
  node_preprocessor["Preprocessor<br/>fusion cnn"]
  node_spatial_branch["Spatial branch<br/>intra-frame path"]
  node_temporal_branch["Temporal branch<br/>inter-frame path"]
  node_preprocessing_system["System wrapper<br/>end-to-end wrapper"]
  node_virtual_codec["VirtualCodec<br/>differentiable codec"]
  node_ste_quantizer["STE quantizer<br/>quantization"]
  node_entropy_model["Entropy model<br/>rate model"]
end

subgraph group_train["Training orchestration"]
  node_train_py["train.py<br/>training module<br/>[train.py]"]
  node_frozen_analyzer["Frozen analyzer<br/>3d cnn evaluator"]
  node_loss_composer["Losses<br/>objective"]
  node_optimizer_loop["Adam loop<br/>trainer"]
end

subgraph group_artifacts["Artifacts"]
  node_best_ckpt[("best_model.pt<br/>checkpoint<br/>[best_model.pt]")]
  node_final_ckpt[("final_model.pt<br/>checkpoint<br/>[final_model.pt]")]
end

node_dataset_py -->|"defines"| node_video_frame_dataset
node_dataset_py -->|"loads"| node_got10k_dataset
node_dataset_py -->|"loads"| node_hf_kinetics_dataset
node_got10k_dataset -->|"frames"| node_preprocessing_system
node_hf_kinetics_dataset -->|"frames"| node_preprocessing_system
node_model_py -->|"defines"| node_preprocessor
node_model_py -->|"defines"| node_virtual_codec
node_preprocessor -->|"uses"| node_spatial_branch
node_preprocessor -->|"uses"| node_temporal_branch
node_preprocessor -->|"feeds"| node_virtual_codec
node_virtual_codec -->|"includes"| node_ste_quantizer
node_virtual_codec -->|"includes"| node_entropy_model
node_preprocessing_system -->|"wraps"| node_preprocessor
node_preprocessing_system -->|"wraps"| node_virtual_codec
node_train_py -->|"optimizes"| node_preprocessing_system
node_train_py -->|"evaluates"| node_frozen_analyzer
node_train_py -->|"builds"| node_loss_composer
node_loss_composer -->|"supervises"| node_preprocessing_system
node_loss_composer -->|"supervises"| node_frozen_analyzer
node_optimizer_loop -->|"writes"| node_best_ckpt
node_optimizer_loop -->|"writes"| node_final_ckpt
node_train_py -->|"runs"| node_optimizer_loop

click node_dataset_py "https://github.com/wagur1/pre_processing_simulate/blob/main/dataset.py"
click node_model_py "https://github.com/wagur1/pre_processing_simulate/blob/main/model.py"
click node_train_py "https://github.com/wagur1/pre_processing_simulate/blob/main/train.py"
click node_best_ckpt "https://github.com/wagur1/pre_processing_simulate/blob/main/checkpoints/best_model.pt"
click node_final_ckpt "https://github.com/wagur1/pre_processing_simulate/blob/main/checkpoints/final_model.pt"

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_py,node_video_frame_dataset,node_got10k_dataset,node_hf_kinetics_dataset toneBlue
class node_model_py,node_preprocessor,node_spatial_branch,node_temporal_branch,node_preprocessing_system,node_virtual_codec,node_ste_quantizer,node_entropy_model toneAmber
class node_train_py,node_frozen_analyzer,node_loss_composer,node_optimizer_loop toneMint
class node_best_ckpt,node_final_ckpt toneRose
```

## Components

### Data ingestion

Dataset adapters and frame sampling boundary

- [dataset.py](https://github.com/wagur1/pre_processing_simulate/blob/main/dataset.py): module · Data loading and dataset-specific sampling · `dataset.py`
- **Video frames**: dataset base · Shared video/sequence sample interface
- **GOT-10k**: local dataset · Filesystem-backed sequence loading
- **Kinetics-400**: hf dataset · HuggingFace-backed remote dataset path

### Trainable core

Preprocessing and differentiable compression modules

- [model.py](https://github.com/wagur1/pre_processing_simulate/blob/main/model.py): module · Preprocessor and virtual codec implementation · `model.py`
- **Preprocessor**: fusion cnn · Combines spatial and temporal cues into enhanced frames
- **Spatial branch**: intra-frame path · Current-frame feature extraction
- **Temporal branch**: inter-frame path · Neighbor-frame feature extraction
- **System wrapper**: end-to-end wrapper · Composes preprocessing and compression into one trainable path
- **VirtualCodec**: differentiable codec · Compression surrogate for bitrate-aware training
- **STE quantizer**: quantization · Straight-through estimator based discretization
- **Entropy model**: rate model · Laplacian bitrate estimation

### Training orchestration

Optimization, validation, and frozen task evaluation

- [train.py](https://github.com/wagur1/pre_processing_simulate/blob/main/train.py): training module · Optimization loop, validation, and task loss · `train.py`
- **Frozen analyzer**: 3d cnn evaluator · Pretrained downstream task model used only for feedback
- **Losses**: objective · MSE, bitrate, and task loss aggregation
- **Adam loop**: trainer · Backpropagation, validation, and early stopping

### Artifacts

Checkpoint outputs

- [best_model.pt](https://github.com/wagur1/pre_processing_simulate/blob/main/checkpoints/best_model.pt): checkpoint · Best validation checkpoint · `checkpoints/best_model.pt`
- [final_model.pt](https://github.com/wagur1/pre_processing_simulate/blob/main/checkpoints/final_model.pt): checkpoint · Final epoch checkpoint · `checkpoints/final_model.pt`

## Connections

- dataset.py → Video frames: defines · Shared sampling/data interface
- dataset.py → GOT-10k: loads · Filesystem-backed GOT-10k sequences
- dataset.py → Kinetics-400: loads · HuggingFace Kinetics-400 path
- GOT-10k → System wrapper: frames · Local sequence samples into training path
- Kinetics-400 → System wrapper: frames · Remote dataset samples into training path
- model.py → Preprocessor: defines · Model components
- model.py → VirtualCodec: defines · Model components
- Preprocessor → Spatial branch: uses · Intra-frame features
- Preprocessor → Temporal branch: uses · Inter-frame features
- Preprocessor → VirtualCodec: feeds · Enhanced frames enter compression surrogate
- VirtualCodec → STE quantizer: includes · Differentiable quantization
- VirtualCodec → Entropy model: includes · Bitrate estimation
- System wrapper → Preprocessor: wraps · End-to-end trainable preprocessing path
- System wrapper → VirtualCodec: wraps · Compression surrogate path
- train.py → System wrapper: optimizes · Main trainable forward path
- train.py → Frozen analyzer: evaluates · Frozen downstream task feedback
- train.py → Losses: builds · Composite objective
- Losses → System wrapper: supervises · Distortion and rate terms
- Losses → Frozen analyzer: supervises · Task accuracy term
- Adam loop → best_model.pt: writes · Best validation state
- Adam loop → final_model.pt: writes · Final epoch state
- train.py → Adam loop: runs · Training, validation, checkpointing
