Batch Range Inference
Overview
Batch range inference runs a model independently on every frame in a user-specified range [start_frame, end_frame]. Unlike recurrent inference (where each frame’s output feeds into the next), batch range inference treats each frame as a standalone prediction — no state is carried between frames.
Runs execute on a background QThread so the UI stays responsive during long jobs. Decoded geometry (masks, points, lines) appears progressively in the view as frames complete; feature vectors are flushed to TensorData when the run finishes.
Orchestration lives in InferenceController. Model batch-size constraints are documented in Batch Mode Guide.
User workflow
- Configure model and slot bindings as for single-frame inference.
- Click ▶▶ Run Batch in the Properties panel.
- Choose a mode in the dialog:
- Manual range — enter start and end frame indices.
- From interval series — select a
DigitalIntervalSerieskey; each interval is processed sequentially with unified progress.
- The View widget progress bar shows per-frame progress.
- On completion or cancellation, results are written to DataManager at their respective frame indices.
While a batch runs:
- Run Batch becomes Cancel Batch.
- Run Single and Run Recurrent are disabled.
- The Qt event loop stays free (no
processEvents()hacks).
Architecture
DeepLearningPropertiesWidget::_onRunBatch()
→ InferenceController::runBatch() / runBatchIntervals()
→ BatchInferenceWorker / MultiIntervalBatchWorker (QThread)
→ SlotAssembler::runBatchRangeOffline()
→ ResultCallback → WriteReservation::push()
→ ResultProcessor (QTimer 200 ms) → WriteReservation::drain()
→ acceptResults() → DataManager::addAtTime()
| Component | Header | Role |
|---|---|---|
InferenceController |
Inference/InferenceController.hpp |
Owns worker lifecycle, media clones, cancel flag |
BatchInferenceWorker |
Inference/BatchInferenceWorker.hpp |
Single contiguous frame range |
MultiIntervalBatchWorker |
Inference/MultiIntervalBatchWorker.hpp |
Multiple intervals, cumulative progress |
WriteReservation |
Inference/WriteReservation.hpp |
Thread-safe buffer between worker and main thread |
ResultProcessor |
Inference/ResultProcessor.hpp |
Timer-driven merge into DataManager |
BatchInferenceResult |
Inference/BatchInferenceResult.hpp |
FrameResult, DecodedOutputVariant, error status |
SlotAssembler |
Core/SlotAssembler.hpp |
runBatchRangeOffline() inference loop |
MediaOverrides |
Core/MediaOverrides.hpp |
data_key → MediaData map for thread-safe reads |
Workers are dedicated header/.cpp pairs (not anonymous namespaces) so AUTOMOC can generate meta-object code without .moc includes in .cpp files.
SlotAssembler offline API
SlotAssembler::runBatchRangeOffline() is the core per-frame loop. It is identical to the synchronous runBatchRange() except:
MediaOverrides— worker reads media through clonedVideoDatainstances keyed bydata_key, avoiding FFmpeg decoder contention with the UI thread.- No direct DataManager writes — decoded outputs are returned as
FrameResultentries (DecodedOutputVariantgeometry or feature vector) instead of callingaddAtTime()on the worker thread. - Cancellation — polls
std::atomic<bool> const & cancel_requestedbefore each batch chunk; returns partial success on early exit. batch_size— number of frames per forward pass (last chunk may be smaller). See Batch Mode Guide.
Optional callbacks:
using ProgressCallback = std::function<void(int current_frame, int total_frames)>;
using ResultCallback = std::function<void(std::vector<FrameResult>)>;When a ResultCallback is provided, results are pushed per chunk via the callback and not accumulated in BatchInferenceResult::results. Workers always pass a callback that forwards to WriteReservation::push().
runBatchRange() (synchronous, writes directly to DataManager) remains in SlotAssembler for tests and programmatic use; the widget always uses the offline path through InferenceController.
Background threading pattern
The implementation follows the thread confinement + merge model from the Concurrency Architecture:
- DataManager stays single-threaded. All writes happen on the main (Qt) thread.
- The worker uses private media. Cloned
VideoDatagives the worker its own FFmpeg decoder. Non-videoMediaDatais shared (stateless reads). Static memory-frame sources that reference video are cloned the same way. - Results merge on the main thread.
ResultProcessorruns aQTimer(200 ms) that drainsWriteReservationand writes geometry viaaddAtTime(..., NotifyObservers::No), then callsnotifyObservers()once per affected key. - Qt signals cross threads.
progressChangedand completion signals useQt::QueuedConnectionautomatically.
Worker thread Main thread
───────────── ───────────
runBatchRangeOffline()
decode chunk ──→ push(results)
QTimer (200 ms) ──→ drain()
decode chunk ──→ push(results) ├─ acceptResults()
│ └─ addAtTime(...)
└─ notifyObservers()
QThread::finished ──────────────→ stopMergeTimer()
├─ final drain()
└─ flushFeatureVectors()
WriteReservation
Thread-safe accumulation buffer. The worker calls push() after each decoded chunk; ResultProcessor calls drain() on timer ticks and on worker completion. A std::mutex protects only the pending vector — DataManager is never touched under the lock.
ResultProcessor
Owns the merge timer and writes drained results into DataManager:
- Geometry (
Mask2D,Point2D<float>,Line2D) — written immediately viaaddAtTime(..., NotifyObservers::No), thennotifyObservers()per key. - Feature vectors (
std::vector<float>) — accumulated in_pending_feature_rowsand flushed toTensorDataviaupsertRows()influshFeatureVectors()when the worker finishes.
InferenceController calls clear() at batch start, startMergeTimer() when the worker launches, and stopMergeTimer() + flushFeatureVectors() in the QThread::finished handler.
Why a separate VideoData?
VideoData wraps a stateful FFmpeg decoder (seek position, codec context, frame buffer). Two threads cannot read from the same instance without corrupting each other’s state. InferenceController clones VideoData at the same file path for every image-encoder binding and static memory-frame video source.
This aligns with ConcurrencyTraits<VideoData>::supports_cheap_clone = true.
Cancellation
InferenceController::cancel() sets a shared std::atomic<bool> flag read by runBatchRangeOffline() between batch chunks. Clicking Cancel Batch (or starting a new run while one is active) triggers this path. Partial results already pushed to WriteReservation are merged on the next timer tick and in the final drain.
The destructor also sets the cancel flag and waits for the worker to exit cleanly.
Progress reporting
| Path | Signal chain |
|---|---|
| Single range | BatchInferenceWorker::progressChanged → InferenceController::batchProgressChanged → DeepLearningPropertiesWidget → DeepLearningViewWidget |
| Multi-interval | MultiIntervalBatchWorker reports cumulative progress across all intervals |
Recurrent inference runs synchronously on the GUI thread and uses recurrentProgressChanged instead; see InferenceController.
Generalizing to other components
The same write-reservation + timer merge pattern applies to any long-running computation that must keep DataManager on the main thread:
- Share a
WriteReservationbetween worker and main thread. - Push results from the worker via callback.
- Drain periodically with
QTimeron the main thread. - Perform a final drain on
QThread::finished.
See Concurrency Architecture for the full design.
See also
- InferenceController — public API, signals, recurrent path
- Batch Mode Guide — model batch-size constraints
- Concurrency Architecture — merge pattern rationale