Files

76 lines
3.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Bounded job contract and phase identity; no model dependencies."""
import hashlib
import json
import math
import re
from pathlib import PurePosixPath
def fingerprint(value):
return hashlib.sha256(json.dumps(value, sort_keys=True, allow_nan=False).encode()).hexdigest()
def filename(value):
if not isinstance(value, str) or not value or PurePosixPath(value).name != value or "\\" in value or value in {".", ".."}:
raise ValueError('Asset names must be relative filenames')
def validate_job(job):
if job.get('schema_version') != 'missioncore.urban-mapping-job/v1':
raise ValueError('Unsupported mapping job')
source = job['source']
filename(source['ply'])
for name in ('sha256', 'world_sha256', 'collider_sha256'):
if not re.fullmatch('[0-9a-f]{64}', source[name]):
raise ValueError('A mapping job requires immutable source identities')
model = job['model']
if model['repository'] != 'facebook/mask2former-swin-large-mapillary-vistas-semantic':
raise ValueError('This provider requires Mapillary Vistas labels')
for name, sha in model['files'].items():
filename(name)
if not re.fullmatch('[0-9a-f]{64}', sha):
raise ValueError('Invalid model asset identity')
if not {'config.json', 'preprocessor_config.json', 'pytorch_model.bin'} <= model['files'].keys():
raise ValueError('Incomplete model manifest')
def vector(value, length):
if not isinstance(value, list) or len(value) != length or any(type(v) not in (int, float) or not math.isfinite(v) for v in value):
raise ValueError('Invalid finite coordinate vector')
bounds = job['bounds_xy']
if len(bounds) != 2:
raise ValueError('Invalid atlas bounds')
for point in bounds:
vector(point, 2)
cell = job.get('cell_m', .1)
if not .02 <= cell <= 1 or any(b <= a for a, b in zip(*bounds)):
raise ValueError('Invalid atlas extent or cell size')
if math.prod(math.ceil((b-a)/cell) for a, b in zip(*bounds)) > 1_000_000:
raise ValueError('Split the scene into bounded tiles (maximum 1 million cells)')
if not 0 <= job.get('halo_m', 15) <= 50 or not 0 <= job.get('min_confidence', .65) <= 1:
raise ValueError('Invalid mapping thresholds')
views = job['views']
if not 2 <= len(views) <= 32 or sum(v.get('projection', 'pinhole') == 'ortho' for v in views) != 1:
raise ValueError('Use one orthographic reference and 1–31 perspective views')
identifiers = set()
for v in views:
if not re.fullmatch('[a-zA-Z0-9_-]{1,64}', v['id']) or v['id'] in identifiers:
raise ValueError('View identifiers must be unique safe names')
identifiers.add(v['id'])
if len(v['size']) != 2 or any(type(n) is not int or not 64 <= n <= 2048 for n in v['size']):
raise ValueError('Unsupported render dimensions')
for name in ('eye', 'target'):
vector(v[name], 3)
vector(v.get('up', [0, 0, 1]), 3)
projection = v.get('projection', 'pinhole')
if projection == 'ortho':
if not 0 < v['width_m'] <= 200:
raise ValueError('Invalid orthographic width')
elif projection != 'pinhole' or not 10 <= v['fov_degrees'] <= 130:
raise ValueError('Invalid perspective camera')
if type(job.get('min_views', 1)) is not int or not 1 <= job.get('min_views', 1) < len(views):
raise ValueError('Invalid observation quorum')
return job
def phase_inputs(job, spec):
return fingerprint({'source': job['source'], 'bounds': job['bounds_xy'], 'halo': job.get('halo_m', 15), 'view': spec})