fix: suppress normal pseudo boxes in classifier exports

This commit is contained in:
Shaun Zhou
2026-10-14 15:31:52 +08:00
parent 219cf23abc
commit 63bab36147
3 changed files with 58 additions and 3 deletions
+35
View File
@@ -37,6 +37,41 @@ def test_class_sets_must_match():
class_order(["normal", "normal"], ["normal"])
def test_background_suppression_uses_reordered_output_index():
# Source class 1 wins, but it is output class 2 after reordering.
adapter = YoloClassifierAdapter(FixedClassifier(), 128, 336, [2, 0, 1], background_index=2)
result = adapter(torch.rand(1, 3, 128, 128))
assert result.shape == (1, 7, 336)
assert torch.count_nonzero(result[:, 4:, :]) == 0
# The existing C++ decoder returns zero candidates at any positive threshold.
assert not (result[0, 4:, :].max(dim=0).values >= 0.25).any()
def test_foreground_class_indices_and_scores_are_preserved():
foreground = YoloClassifierAdapter(FixedClassifier(), 128, 336, [0, 1, 2],
background_index=0)
legacy = YoloClassifierAdapter(FixedClassifier(), 128, 336, [0, 1, 2])
x = torch.rand(1, 3, 128, 128)
torch.testing.assert_close(foreground(x), legacy(x))
assert foreground(x)[0, 4:, 0].argmax() == 1
def test_background_gate_is_per_image_in_batch():
class BatchClassifier(nn.Module):
def forward(self, x):
return x[:, :, 0, 0]
x = torch.zeros(2, 3, 4, 4)
x[0, 0, 0, 0] = 3 # normal
x[1, 1, 0, 0] = 3 # blackdot
adapter = YoloClassifierAdapter(BatchClassifier(), 4, 10, [0, 1, 2],
eps=None, background_index=0)
result = adapter(x)
assert not torch.count_nonzero(result[0, 4:, :])
assert result[1, 4:, 0].argmax() == 1
assert result[1, 5, 0] > 0.9
def test_refuses_resampling_and_unknown_preprocessing():
config = {"data": {"val_transforms": [
{"name": "Resize", "size": [160, 160]}, {"name": "ToTensor"},