How wagur1/pre_processing_simulate works
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 train.py · dataset.py dataset.py · model.py model.py · best_model.pt best_model.pt
Components 18
Data ingestion
Dataset adapters and frame sampling boundary
- dataset.py · Data loading and dataset-specific sampling
dataset.py - Video frames · Shared video/sequence sample interface
- GOT-10k · Filesystem-backed sequence loading
- Kinetics-400 · HuggingFace-backed remote dataset path
Trainable core
Preprocessing and differentiable compression modules
- model.py · Preprocessor and virtual codec implementation
model.py - Preprocessor · Combines spatial and temporal cues into enhanced frames
- Spatial branch · Current-frame feature extraction
- Temporal branch · Neighbor-frame feature extraction
- System wrapper · Composes preprocessing and compression into one trainable path
- VirtualCodec · Compression surrogate for bitrate-aware training
- STE quantizer · Straight-through estimator based discretization
- Entropy model · Laplacian bitrate estimation
Training orchestration
Optimization, validation, and frozen task evaluation
- train.py · Optimization loop, validation, and task loss
train.py - Frozen analyzer · Pretrained downstream task model used only for feedback
- Losses · MSE, bitrate, and task loss aggregation
- Adam loop · Backpropagation, validation, and early stopping
Artifacts
Checkpoint outputs
- best_model.pt · Best validation checkpoint
checkpoints/best_model.pt - final_model.pt · Final epoch checkpoint
checkpoints/final_model.pt
Connections 22
- 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