litert-models / conversion /convert_dfine_final.py
unicorn who dev
Complete FireViewer Android learning SDK, source and verification records
dee7f43 verified
Raw History Blame Contribute Delete
1.93 kB
"""Rebuild the verified D-FINE component from the published upstream ONNX.
Run with .venv-onnx Python; the recorded onnx2tf version and protection manifest
are required. No custom runtime operators or modified weights are introduced.
"""
import subprocess,shutil
from pathlib import Path
import onnx,onnxsim
from onnxruntime.tools.symbolic_shape_infer import SymbolicShapeInference
r=Path('/workspace/vds-litert-batch2');d=r/'learning-backbones/fireviewer_dfine_m_strict_v1';work=d/'raw-onnx';work.mkdir(parents=True,exist_ok=True)
model=onnx.load(r/'models/upstream/fireviewer_dfine_m_strict_v1/model.onnx')
names=['/model/decoder/Gather_7_output_0','/model/decoder/Gather_8_output_0']
for n,c in zip(names,[2,4]):model.graph.value_info.append(onnx.helper.make_tensor_value_info(n,onnx.TensorProto.FLOAT,['batch',300,c]))
model=onnx.utils.Extractor(model).extract_model(['images'],names)
model.graph.input[0].type.tensor_type.shape.dim[0].dim_value=1
for value in model.graph.output:value.type.tensor_type.shape.dim[0].dim_value=1
model=SymbolicShapeInference.infer_shapes(model,auto_merge=True,guess_output_rank=True,verbose=0);onnx.save(model,work/'raw-original.onnx')
simplified,valid=onnxsim.simplify(model,overwrite_input_shapes={'images':[1,3,704,704]});assert valid
onnx.save(simplified,work/'simplified.onnx')
subprocess.run([str(r/'.venv-onnx/bin/python'),str(r/'patch_onnx2tf_gather_signature.py')],check=True)
subprocess.run([str(r/'.venv-onnx/bin/onnx2tf'),'-i',str(work/'simplified.onnx'),'-o',str(d/'simplified-converted'),'-b','1','-k','images','-nuo'],check=True)
if not (d/'arena-protection.json').exists():shutil.copy2(r/'dfine-arena-protection.json',d/'arena-protection.json')
for script in ['crop_dfine_outputs.py','protect_dfine_arena.py']:
subprocess.run(['/opt/vds-tf/bin/python',str(r/script)],check=True)
subprocess.run([str(r/'.venv-onnx/bin/python'),str(r/'check_dfine_final.py')],check=True)