fix(AR-004): the TransNetV2 window stores the model's input, not the frame
The rolling window held frames as decoded — `images_.push_back(f.image)` — and
left the downscale to the backend. TransNetV2's input is 48x27, so the buffer
held roughly 590 MB at 1080p to feed a model that needs about 380 KB. The
config note for `dense_scale` says as much outright: "TransNetV2 downsamples to
48x27 regardless".
This is not a channel capacity, so no amount of tuning channel depths would
ever have found it. It is a `std::deque<cv::Mat>` member, and it is the single
largest allocation in the scene branch.
It is also redundant work. Windows overlap by `kWindow - stride`, so a frame
appears in several of them and was re-downscaled once per window it appeared
in; now it is downscaled once, on arrival.
**The risk here is the invariant, not the memory.** Every model gets the input
it was trained for — a model run off-distribution returns confident, plausible,
wrong output, and for a boundary detector that means fabricated cuts, which are
indistinguishable from real ones in the output. So this reproduces the
backends' preprocessing exactly rather than doing its own: both
ort_backend.cpp and trt_backend.cpp guard mis-sized input with
`convertTo(CV_8UC3)` and then
`cv::resize(..., {kFrameW, kFrameH}, 0, 0, cv::INTER_AREA)`, in that order, and
`to_model_input` performs the same two operations. The backend guard then sees
a correctly-sized frame and does nothing, so the tensor the model receives is
unchanged. The interface has always specified this as the caller's job — "Each
frame must already be kFrameW x kFrameH, BGR, CV_8UC3" — so the node now meets
a contract it was already given.
The tests assert equivalence, not size. They perform the backend's own two
operations independently and compare byte for byte, on a gradient rather than a
flat fill, since INTER_AREA averages and a constant image would compare equal
under almost any resize. Order is pinned too: converting a 4-channel frame
after downscaling averages alpha into the colour channels and gives different
pixels.
Verified in both directions. With INTER_LINEAR substituted for INTER_AREA —
the most plausible way to get this subtly wrong — three assertions fail. With
the backend's own operations, byte-identical at 1920x1080, 640x360 and 720x480.
149/149.
Still unmeasured on real content, as with the previous commit: the equivalence
argument says the model sees the same tensor, but a run comparing scenes.json
before and after on a real clip is what would settle it, and I could not launch
one here.
TRACES: AR-004, AR-010 | SR-002
This commit is contained in:
@@ -6,6 +6,8 @@
|
||||
#include <memory>
|
||||
#include "inference/scene_detector.hpp"
|
||||
|
||||
#include <opencv2/imgproc.hpp> // cv::resize, for to_model_input
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
@@ -76,7 +78,7 @@ struct SceneDetectorFunc {
|
||||
}
|
||||
prev_ts_ = f.timestamp_sec;
|
||||
|
||||
images_.push_back(f.image);
|
||||
images_.push_back(to_model_input(f.image));
|
||||
times_.push_back(f.timestamp_sec);
|
||||
|
||||
// Once we have a full window, score it and slide forward by `stride`.
|
||||
@@ -90,6 +92,48 @@ struct SceneDetectorFunc {
|
||||
}
|
||||
}
|
||||
|
||||
/// TRACES: AR-004, AR-010 | SR-002
|
||||
/// Reduce a decoded frame to exactly what TransNetV2 consumes, once.
|
||||
///
|
||||
/// The window used to hold the frames as decoded — full resolution — and
|
||||
/// leave the downscale to the backend. But the model's input is 48x27
|
||||
/// (`ISceneDetector::kFrameW/H`; the config note for `dense_scale` says so
|
||||
/// outright: "TransNetV2 downsamples to 48x27 regardless"), so the buffer
|
||||
/// held ~590 MB at 1080p to feed something that needs ~380 KB. That is not
|
||||
/// a channel capacity, so no amount of tuning channel depths would ever
|
||||
/// have found it.
|
||||
///
|
||||
/// It is also redundant work. Windows overlap by `kWindow - stride`, so a
|
||||
/// frame appears in several of them and was re-downscaled once per window;
|
||||
/// now it is downscaled once, when it arrives.
|
||||
///
|
||||
/// **This must reproduce the backends' preprocessing exactly**, because the
|
||||
/// project invariant is that every model gets the input it was trained for
|
||||
/// — a model run off-distribution returns confident, plausible, wrong
|
||||
/// output, and here that means fabricated shot boundaries. Both
|
||||
/// ort_backend.cpp and trt_backend.cpp guard mis-sized input with, in this
|
||||
/// order, `convertTo(CV_8UC3)` then
|
||||
/// `cv::resize(..., {kFrameW, kFrameH}, 0, 0, cv::INTER_AREA)`. The same
|
||||
/// two operations are done here, so the tensor the model receives is
|
||||
/// unchanged; the backend guard then sees a correctly-sized frame and does
|
||||
/// nothing. The interface has always specified this shape as the caller's
|
||||
/// job ("Each frame must already be kFrameW x kFrameH, BGR, CV_8UC3"), so
|
||||
/// this makes the node meet a contract it was already given.
|
||||
static cv::Mat to_model_input(const cv::Mat& src) {
|
||||
cv::Mat typed;
|
||||
if (src.type() != CV_8UC3) src.convertTo(typed, CV_8UC3);
|
||||
else typed = src;
|
||||
|
||||
if (typed.cols == ISceneDetector::kFrameW &&
|
||||
typed.rows == ISceneDetector::kFrameH)
|
||||
return typed;
|
||||
|
||||
cv::Mat small;
|
||||
cv::resize(typed, small, {ISceneDetector::kFrameW, ISceneDetector::kFrameH},
|
||||
0, 0, cv::INTER_AREA);
|
||||
return small;
|
||||
}
|
||||
|
||||
/// TRACES: AR-011 | SR-002
|
||||
// How close two boundaries have to be before they are the same boundary,
|
||||
// derived from the cadence the detector was actually fed.
|
||||
|
||||
Reference in New Issue
Block a user