File size: 7,378 Bytes
e8055cf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | """Tests for inference/adapters.py -- inference backend registry and adapter dispatch."""
import numpy as np
import pytest
from inference import adapters
def test_sam3_adapter_accepts_tracking_modes():
assert adapters.get_adapter("SAM3", tracking="tracking").tracking == "tracking"
assert (
adapters.get_adapter("SAM3", tracking="no tracking").tracking == "no tracking"
)
def test_metric_adapter_is_registered():
assert adapters._ADAPTERS["DA3NESTED-GIANT-LARGE-1.1"].depth_variant == "metric"
def test_relative_adapter_is_registered():
assert adapters._ADAPTERS["DA3-LARGE-1.1"].depth_variant == "relative"
@pytest.mark.parametrize("algorithm", ["1", "2", "3", "4", "5"])
def test_selector_algorithm_dispatch_is_registered(algorithm):
assert algorithm in adapters._SELECTOR_ALGORITHMS
assert callable(adapters._SELECTOR_ALGORITHMS[algorithm])
def test_selector_algorithm_defaults_to_five(monkeypatch):
import importlib
monkeypatch.delenv("VSI_SELECTOR_ALGORITHM", raising=False)
reloaded = importlib.reload(adapters)
try:
assert reloaded.SELECTOR_ALGORITHM == "5"
finally:
importlib.reload(adapters)
def test_select_video_frame_indices_rejects_unknown_algorithm(monkeypatch, tmp_path):
monkeypatch.setattr(adapters, "SELECTOR_ALGORITHM", "99")
monkeypatch.setattr(adapters, "SELECTED_FRAMES_CACHE", tmp_path)
video = tmp_path / "scene.mp4"
video.write_bytes(b"fake video bytes")
with pytest.raises(ValueError, match="unknown selector algorithm"):
adapters.select_video_frame_indices(str(video))
# Every constant that affects an algorithm's behavior must be part of
# _selector_config()'s fingerprint, or a cache built before a constant change gets
# silently served as "fresh" after the change -- this bit us twice while tuning
# algorithm 3 (GLITCH_THUMBNAIL_WIDTH, then GLITCH_MINIMUM_OWN_KEYPOINTS were both
# added to the algorithm without being added to the fingerprint).
_ALGORITHM_TUNABLE_CONSTANTS = {
"1": [
"REDUNDANCY_SSIM_THRESHOLD",
"MINIMUM_ALIGNMENT_MATCHES",
"MINIMUM_ALIGNMENT_INLIER_RATIO",
"MINIMUM_VALID_OVERLAP_FRACTION",
],
"2": [
"BLUR_RELATIVE_MEDIAN_FRACTION",
"BLUR_ABSOLUTE_FLOOR",
"BLUR_CANONICAL_WIDTH",
"DARK_MEAN_THRESHOLD",
"BRIGHT_MEAN_THRESHOLD",
"LOW_CONTRAST_STD_THRESHOLD",
],
"3": [
"BLACK_PIXEL_LUMINANCE_THRESHOLD",
"BLACK_FRAME_PIXEL_RATIO_THRESHOLD",
"GLITCH_MAX_NEIGHBOR_COVISIBILITY",
"GLITCH_THUMBNAIL_WIDTH",
"GLITCH_MINIMUM_OWN_KEYPOINTS",
"MINIMUM_ALIGNMENT_MATCHES",
],
"4": [
"COVISIBILITY_OVERLAP_THRESHOLD",
"MINIMUM_ALIGNMENT_MATCHES",
"REDUNDANCY_SSIM_THRESHOLD",
],
"5": [
"BLUR_RELATIVE_MEDIAN_FRACTION",
"BLUR_ABSOLUTE_FLOOR",
"BLUR_CANONICAL_WIDTH",
"DARK_MEAN_THRESHOLD",
"BRIGHT_MEAN_THRESHOLD",
"LOW_CONTRAST_STD_THRESHOLD",
"COVISIBILITY_OVERLAP_THRESHOLD",
"MINIMUM_ALIGNMENT_MATCHES",
"REDUNDANCY_SSIM_THRESHOLD",
],
}
@pytest.mark.parametrize(
("algorithm", "constant_name"),
[
(algorithm, name)
for algorithm, names in _ALGORITHM_TUNABLE_CONSTANTS.items()
for name in names
],
)
def test_selector_config_reflects_every_tunable_constant(
monkeypatch, algorithm, constant_name
):
monkeypatch.setattr(adapters, "SELECTOR_ALGORITHM", algorithm)
before = adapters._selector_config()
original_value = getattr(adapters, constant_name)
monkeypatch.setattr(adapters, constant_name, original_value * 2 + 1)
after = adapters._selector_config()
assert after != before
def test_cache_is_invalidated_when_selector_config_changes(tmp_path, monkeypatch):
monkeypatch.setattr(adapters, "SELECTED_FRAMES_CACHE", tmp_path)
monkeypatch.setattr(adapters, "SELECTOR_ALGORITHM", "2")
video = tmp_path / "scene.mp4"
video.write_bytes(b"fake video bytes")
adapters._cache_selected_frame_indices(str(video), [1, 2, 3])
assert adapters._load_selected_frame_indices(str(video)) == [1, 2, 3]
monkeypatch.setattr(
adapters, "BLUR_ABSOLUTE_FLOOR", adapters.BLUR_ABSOLUTE_FLOOR + 5
)
assert adapters._load_selected_frame_indices(str(video)) is None
def _write_synthetic_video(path, frames, fps=10):
cv2 = pytest.importorskip("cv2")
height, width = frames[0].shape[:2]
writer = cv2.VideoWriter(
str(path), cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height)
)
for frame in frames:
writer.write(frame)
writer.release()
def _textured_frame(shape=(90, 160, 3), seed=0, circles=40):
cv2 = pytest.importorskip("cv2")
rng = np.random.default_rng(seed)
frame = np.zeros(shape, np.uint8)
for _ in range(circles):
x, y = int(rng.integers(0, shape[1])), int(rng.integers(0, shape[0]))
radius = int(rng.integers(3, 8))
color = tuple(int(value) for value in rng.integers(50, 255, 3))
cv2.circle(frame, (x, y), radius, color, -1)
return frame
def test_algorithm_3_catches_injected_corruption_without_false_positives(tmp_path):
cv2 = pytest.importorskip("cv2")
if not hasattr(cv2, "VideoWriter"):
pytest.skip("real OpenCV video I/O is not installed")
base = _textured_frame(seed=0)
rng = np.random.default_rng(1)
corrupt_frames = {20, 45}
frames = [
(
rng.integers(0, 255, base.shape, dtype=np.uint8)
if i in corrupt_frames
else base
)
for i in range(60)
]
path = tmp_path / "synthetic_corrupt.mp4"
_write_synthetic_video(path, frames)
kept = set(adapters._select_indices_algorithm_3(str(path)))
discarded = set(range(len(frames))) - kept
assert corrupt_frames.issubset(discarded)
assert discarded - corrupt_frames == set()
def test_algorithm_4_compresses_static_redundancy_at_least_as_well_as_algorithm_1(
tmp_path,
):
cv2 = pytest.importorskip("cv2")
if not hasattr(cv2, "VideoWriter"):
pytest.skip("real OpenCV video I/O is not installed")
base = _textured_frame(seed=2)
rng = np.random.default_rng(3)
# A static camera with tiny per-frame sensor noise -- exactly the appearance-level
# jitter that fragmented algorithm 1's SSIM-based groups on real static footage.
frames = [
np.clip(base.astype(np.int16) + rng.integers(-3, 3, base.shape), 0, 255).astype(
np.uint8
)
for _ in range(80)
]
path = tmp_path / "static_scene.mp4"
_write_synthetic_video(path, frames)
kept1 = adapters._select_indices_algorithm_1(str(path))
kept4 = adapters._select_indices_algorithm_4(str(path))
assert len(kept4) <= len(kept1)
def test_algorithm_5_output_is_subset_of_algorithm_2_output(tmp_path):
cv2 = pytest.importorskip("cv2")
if not hasattr(cv2, "VideoWriter"):
pytest.skip("real OpenCV video I/O is not installed")
base = _textured_frame(seed=4)
frames = [base for _ in range(60)]
path = tmp_path / "scene.mp4"
_write_synthetic_video(path, frames)
kept2 = set(adapters._select_indices_algorithm_2(str(path)))
kept5 = set(adapters._select_indices_algorithm_5(str(path)))
assert kept5.issubset(kept2)
|