Preserve installed operator workspace before consolidation
Local preservation snapshot, not a new deployment or simulation acceptance.
This commit is contained in:
@@ -14,6 +14,26 @@ import numpy as np
|
||||
from PIL import Image
|
||||
|
||||
|
||||
def encode_surface(mask, confidence):
|
||||
"""Retain reference labels; low confidence is neither permission nor ban.
|
||||
|
||||
0.55 is an explicit engineering gate, shared with the ADE adapter, not a
|
||||
claim of calibrated probabilities or a scene-dependent tuning parameter.
|
||||
"""
|
||||
if (
|
||||
mask.shape != (512, 512)
|
||||
or not np.issubdtype(mask.dtype, np.integer)
|
||||
or np.any(mask < 0)
|
||||
or np.any(mask >= 64)
|
||||
or confidence.shape != mask.shape
|
||||
or not np.isfinite(confidence).all()
|
||||
or np.any((confidence < 0) | (confidence > 1))
|
||||
):
|
||||
raise ValueError("DDRNet surface certainty contract changed")
|
||||
known = (confidence >= 0.55) & ~np.isin(mask, (0, 56))
|
||||
return mask.astype(np.uint8).tobytes() + known.astype(np.uint8).tobytes()
|
||||
|
||||
|
||||
def checked(path, expected):
|
||||
value = Path(path)
|
||||
if hashlib.sha256(value.read_bytes()).hexdigest() != expected:
|
||||
@@ -22,6 +42,8 @@ def checked(path, expected):
|
||||
|
||||
|
||||
def main():
|
||||
import torch
|
||||
|
||||
checkpoint = checked(
|
||||
"/assets/ddrnet-checkpoint.pth",
|
||||
"b99c2838051bcd7b092fd3970aa62a77d5c0bbb809c9b9afb2ff4b0ebdaa4ee6",
|
||||
@@ -34,8 +56,18 @@ def main():
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
model, _, _ = module.load_model("ddrnet", checkpoint)
|
||||
tensor, _ = module.preprocess(Image.fromarray(np.zeros((600, 800, 3), np.uint8)))
|
||||
module.infer(model, tensor)
|
||||
|
||||
def infer(rgb):
|
||||
tensor, _ = module.preprocess(Image.fromarray(rgb))
|
||||
with torch.inference_mode():
|
||||
logits = module.logits_from_output(model(tensor.cuda(non_blocking=True)))
|
||||
# Preserve the pinned reference's sigmoid/first-index tie labels.
|
||||
# Measure certainty of that exact selected class, including ties.
|
||||
labels = logits.sigmoid().argmax(1)
|
||||
confidence = logits.softmax(1).gather(1, labels[:, None])[:, 0]
|
||||
return encode_surface(labels[0].cpu().numpy(), confidence[0].cpu().numpy())
|
||||
|
||||
infer(np.zeros((600, 800, 3), np.uint8))
|
||||
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
def setup(self):
|
||||
@@ -53,7 +85,16 @@ def main():
|
||||
if self.path != "/ready":
|
||||
self.reply(404, b"")
|
||||
return
|
||||
self.reply(200, json.dumps({"model": "ddrnet-goose-pytorch-reference"}).encode())
|
||||
self.reply(
|
||||
200,
|
||||
json.dumps(
|
||||
{
|
||||
"model": "ddrnet-goose-pytorch-reference",
|
||||
"output": "goose64-labels-and-known-square512-v2",
|
||||
"minimum_class_confidence": 0.55,
|
||||
}
|
||||
).encode(),
|
||||
)
|
||||
|
||||
def do_POST(self):
|
||||
if self.path != "/infer" or self.headers.get("Content-Length") != "1440000":
|
||||
@@ -64,11 +105,7 @@ def main():
|
||||
if len(raw) != 1440000:
|
||||
raise ValueError("Incomplete camera frame")
|
||||
rgb = np.frombuffer(raw, np.uint8).reshape(600, 800, 3)
|
||||
tensor, _ = module.preprocess(Image.fromarray(rgb))
|
||||
mask, _ = module.infer(model, tensor)
|
||||
if mask.shape != (512, 512) or np.any(mask < 0) or np.any(mask >= 64):
|
||||
raise ValueError("DDRNet output contract changed")
|
||||
self.reply(200, mask.astype(np.uint8).tobytes())
|
||||
self.reply(200, infer(rgb))
|
||||
except (TimeoutError, ValueError, RuntimeError):
|
||||
self.reply(500, b"DDRNet inference failed")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user