-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfod_runtime.py
More file actions
184 lines (164 loc) · 6.42 KB
/
dfod_runtime.py
File metadata and controls
184 lines (164 loc) · 6.42 KB
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
from __future__ import annotations
import copy
from typing import Any, Dict
import numpy as np
from dfod_config import normalize_config
from Baseline_Perception.Main_perception import run_perception
from Baseline_Perception.embedder import finetune_adapter_from_bundle
from Baseline_Reasoning.calibration import resolve_calibration
from Baseline_Reasoning.Main_reasoning import run_reasoning_bundle_detailed
from Baseline_Reasoning.prototype_update import apply_memory_updates, select_pseudo_updates
from Baseline_Reasoning.serialization import load_memory_bundle, save_memory
DEFAULT_RUNTIME_CONFIG: Dict[str, Any] = {
"perception": {
"objectness_enabled": True,
},
"reasoning": {
"metrics": ["mahalanobis", "cosine", "euclidean", "manhattan"],
"primary_metric": "mahalanobis",
"enable_metric_fusion": True,
"score_threshold": -5000.0,
"margin_threshold": 0.5,
},
"memory": {
"max_prototypes_per_class": 2,
"cluster_min_supports": 6,
"min_supports_per_mode": 3,
"variance_alpha": 0.3,
"variance_floor": 1e-4,
},
"updates": {
"enabled": True,
"score_threshold_min": -5000.0,
"margin_threshold_min": 0.5,
"update_percentile": 90,
"margin_percentile": 75,
"det_conf_threshold": 0.70,
"objectness_threshold": 0.20,
"quality_threshold": 0.20,
"max_updates_per_image": 3,
"max_memory_per_class": 20,
"memory_keep_policy": "recent",
"persist_updated_memory": True,
},
"adapter": {
"enabled": True,
"train_on_support_build": True,
"online_enabled": True,
"online_min_examples": 3,
"weights_path": "adapter.pt",
},
}
def _deep_merge(base: Dict[str, Any], override: Dict[str, Any]) -> Dict[str, Any]:
merged = copy.deepcopy(base)
for key, value in override.items():
if isinstance(value, dict) and isinstance(merged.get(key), dict):
merged[key] = _deep_merge(merged[key], value)
else:
merged[key] = value
return merged
def build_runtime_config(config: Dict[str, Any] | None = None) -> Dict[str, Any]:
merged = copy.deepcopy(DEFAULT_RUNTIME_CONFIG)
if config:
merged = _deep_merge(merged, config)
return normalize_config(merged)
def _count_supports(memory_bundle: Dict[str, Any]) -> Dict[str, int]:
classes = memory_bundle.get("classes", {})
if not isinstance(classes, dict):
return {}
return {
str(class_name): int(np.asarray(entry.get("raw_embeddings", []), dtype=np.float32).shape[0])
for class_name, entry in sorted(classes.items())
}
def run_dfod_inference(
image_path: str,
memory_path: str = "memory.npz",
memory_bundle: Dict[str, Any] | None = None,
config: Dict[str, Any] | None = None,
device: str = "cpu",
conf_thresh: float = 0.25,
max_detections: int = 20,
apply_updates_enabled: bool | None = None,
updated_memory_path: str | None = None,
) -> Dict[str, Any]:
cfg = build_runtime_config(config)
updates_enabled = cfg["updates"].get("enabled", True) if apply_updates_enabled is None else bool(apply_updates_enabled)
current_memory = memory_bundle if memory_bundle is not None else load_memory_bundle(memory_path)
memory_before = _count_supports(current_memory)
calibration = resolve_calibration(config=cfg, memory_bundle=current_memory)
detections, embeddings = run_perception(
image_path=image_path,
device=device,
conf_thresh=conf_thresh,
max_detections=max_detections,
config=cfg,
)
if embeddings.shape[0] == 0:
accepted_updates = []
updated_memory = current_memory
memory_after = _count_supports(updated_memory)
return {
"detections": detections,
"embeddings": embeddings,
"results": [],
"decisions": [],
"scores": np.zeros((0, 0), dtype=np.float32),
"class_names": [],
"accepted_updates": accepted_updates,
"memory_bundle": updated_memory,
"memory_class_counts_before": memory_before,
"memory_class_counts_after": memory_after,
"memory_support_total_before": sum(memory_before.values()),
"memory_support_total_after": sum(memory_after.values()),
}
results, decisions, scores, class_names = run_reasoning_bundle_detailed(
embeddings=embeddings,
memory_bundle=current_memory,
config=cfg,
)
for idx in range(min(len(detections), len(results))):
detections[idx]["pred_label"] = results[idx]["label"]
detections[idx]["pred_unknown"] = results[idx]["unknown"]
detections[idx]["pred_score"] = results[idx]["similarity"]
detections[idx]["pred_margin"] = float(decisions[idx].get("margin", 0.0))
detections[idx]["rejection_reason"] = decisions[idx].get("rejection_reason", "accepted")
accepted_updates = []
updated_memory = current_memory
if updates_enabled:
accepted_updates = select_pseudo_updates(
detections=detections,
embeddings=embeddings,
decisions=decisions,
config=cfg,
calibration=calibration,
)
if accepted_updates:
updated_memory = apply_memory_updates(
memory=current_memory,
accepted_updates=accepted_updates,
config=cfg,
)
updated_memory = finetune_adapter_from_bundle(
memory_bundle=updated_memory,
accepted_updates=accepted_updates,
device=device,
config=cfg,
)
if cfg["updates"].get("persist_updated_memory", False):
save_path = updated_memory_path or memory_path
save_memory(updated_memory, save_path)
memory_after = _count_supports(updated_memory)
return {
"detections": detections,
"embeddings": embeddings,
"results": results,
"decisions": decisions,
"scores": scores,
"class_names": class_names,
"accepted_updates": accepted_updates,
"memory_bundle": updated_memory,
"memory_class_counts_before": memory_before,
"memory_class_counts_after": memory_after,
"memory_support_total_before": sum(memory_before.values()),
"memory_support_total_after": sum(memory_after.values()),
}