Hsyshasuzh/longmemory-geo / project /evaluation /phase2_precap_capture.py
Hsyshasuzh's picture
download
raw
2.63 kB
"""Lossless, pre-selection capture of decoded YOLO oriented predictions."""
import numpy as np
import torch
def capture_decoded(decoded, class_logits, class_names, tile_origin, valid_size, *, regression_logits=None):
"""Return every tile-local xywhr prediction and class logit, before selection.
``decoded`` is the model head's one-branch output: B x (4 + classes + 1)
x anchors, with class probabilities and the original angle channel. Input
batch size must be one. Tile origin and valid size are recorded separately;
coordinates are not clipped or transformed.
"""
if decoded.ndim != 3 or class_logits.ndim != 3 or decoded.shape[0] != 1 or class_logits.shape[0] != 1:
raise ValueError('expected single-image, three-dimensional tensors')
count = class_logits.shape[1]
if count != len(class_names) or decoded.shape[1] != count + 5 or decoded.shape[2] != class_logits.shape[2] or not decoded.shape[2]:
raise ValueError('decoded, logit and class dimensions do not agree')
if len(set(class_names)) != count or any(not isinstance(n,str) or not n for n in class_names):
raise ValueError('class names must be nonempty and unique')
if (len(tile_origin) != 2 or len(valid_size) != 2 or
any(type(v) is not int or v < 0 for v in tile_origin) or
any(type(v) is not int or not 0 < v <= 1024 for v in valid_size)):
raise ValueError('invalid tile origin or valid size')
if not torch.isfinite(decoded).all() or not torch.isfinite(class_logits).all():
raise ValueError('nonfinite raw model output')
if not torch.allclose(decoded[:,4:4+count], class_logits.sigmoid(), rtol=1e-5, atol=1e-6):
raise ValueError('decoded probabilities do not match class logits')
boxes = torch.cat((decoded[:, :4], decoded[:, -1:]), dim=1)[0].transpose(0,1)
if regression_logits is not None:
if (regression_logits.ndim != 3 or regression_logits.shape[0] != 1 or
regression_logits.shape[2] != decoded.shape[2] or not torch.isfinite(regression_logits).all()):
raise ValueError('invalid raw box regression output')
result = {'xywhr': boxes.detach().cpu().numpy().copy(),
'class_logits': class_logits[0].transpose(0,1).detach().cpu().numpy().copy(),
'class_names': np.asarray(class_names), 'tile_origin': np.asarray(tile_origin,dtype=np.int64),
'valid_size': np.asarray(valid_size,dtype=np.int64)}
if regression_logits is not None:
result['regression_logits'] = regression_logits[0].transpose(0,1).detach().cpu().numpy().copy()
return result

Xet Storage Details

Size:
2.63 kB
·
Xet hash:
01d19a5e01d4eeb0fee8a493433cfc52c903144faf2291dce39fe861e26fd68b

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.