File size: 2,615 Bytes
ebeed67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Pseudo ground truth from two teacher detectors — the 2026-10-07 method.

"Two large reference detectors (YOLO26-X at 1280 px and RT-DETR-X at 640 px)
are run on every frame; an object counts when both agree (or one is very
confident). Uncertain boxes are don't care." The original scripts were lost
with a session scratchpad; the thresholds below are the documented SHAPE of
the rule with defaults chosen here, not the exact values of that run.
Detections must already be in macro classes (`classes.to_macro`).
"""
from __future__ import annotations

from dataclasses import dataclass
from typing import Sequence

from .metrics import MATCH_IOU, Detection, GroundTruthBox, iou


@dataclass(frozen=True)
class PseudoGtPolicy:
    #: Both teachers at or above this, same class, IoU >= 0.5 ⇒ positive.
    agree_score: float = 0.3
    #: One teacher alone at or above this ⇒ positive.
    solo_score: float = 0.8
    #: A box a teacher holds at or above this that is not a positive ⇒ don't care.
    care_score: float = 0.3


def pseudo_ground_truth(
    teacher_a: Sequence[Detection],
    teacher_b: Sequence[Detection],
    policy: PseudoGtPolicy = PseudoGtPolicy(),
) -> tuple[list[GroundTruthBox], list[GroundTruthBox]]:
    positives: list[GroundTruthBox] = []
    used_b: set[int] = set()
    leftovers: list[Detection] = []
    for a in sorted(teacher_a, key=lambda d: -d.score):
        partner = -1
        if a.score >= policy.agree_score:
            for j, b in enumerate(teacher_b):
                if j in used_b or b.cls != a.cls or b.score < policy.agree_score:
                    continue
                if iou(a.bbox, b.bbox) >= MATCH_IOU:
                    partner = j
                    break
        if partner >= 0:
            used_b.add(partner)
            positives.append(GroundTruthBox(a.cls, a.bbox))
        elif a.score >= policy.solo_score:
            positives.append(GroundTruthBox(a.cls, a.bbox))
        else:
            leftovers.append(a)
    for j, b in enumerate(teacher_b):
        if j in used_b:
            continue
        duplicate = any(p.cls == b.cls and iou(p.bbox, b.bbox) >= MATCH_IOU for p in positives)
        if duplicate:
            continue
        if b.score >= policy.solo_score:
            positives.append(GroundTruthBox(b.cls, b.bbox))
        else:
            leftovers.append(b)
    dont_care = [
        GroundTruthBox(d.cls, d.bbox)
        for d in leftovers
        if d.score >= policy.care_score
        and not any(p.cls == d.cls and iou(p.bbox, d.bbox) >= MATCH_IOU for p in positives)
    ]
    return positives, dont_care