Implement BiRefNet and HAT Sharper models with background removal capabilities

- Added validation documentation for AI model tiers.
- Introduced `background.py` for background removal logic, including inference functions for alpha and foreground extraction.
- Created `onnx_birefnet.py` to handle BiRefNet model sessions and alpha inference.
- Developed tests for background models, ensuring proper dispatch and session reuse.
- Added tests for BiRefNet ONNX model to validate alpha output and error handling for invalid values.
- Implemented model export scripts for BiRefNet and HAT Sharper, ensuring proper weight compression and licensing.
- Created `deform_sampling.py` for deformable convolution operations used in BiRefNet.
- Added utility functions for compressing ONNX weights without altering graph arithmetic.
- Introduced a reference plane node for depth field adjustments in geometry processing.
This commit is contained in:
2026-09-10 20:50:44 +08:00
parent 8ae3c1d5b7
commit e8129fe495
70 changed files with 1890 additions and 1208 deletions

View File

@@ -0,0 +1,49 @@
"""Compress stored ONNX weights without changing graph arithmetic."""
import numpy as np
from onnx import TensorProto, helper, numpy_helper
def compress_weights(model):
casts = []
for initializer in model.graph.initializer:
if initializer.data_type != TensorProto.FLOAT:
continue
name = initializer.name
values = numpy_helper.to_array(initializer)
if not np.isfinite(values).all() or np.max(np.abs(values), initial=0) > np.finfo(np.float16).max:
continue
array = values.astype(np.float16)
stored_name = name + "__fp16_storage"
initializer.CopyFrom(numpy_helper.from_array(array, stored_name))
casts.append(helper.make_node(
"Cast", [stored_name], [name], to=TensorProto.FLOAT,
name=stored_name + "_restore",
))
nodes = list(model.graph.node)
del model.graph.node[:]
model.graph.node.extend(casts + nodes)
def compress_exact_constants(model):
"""Compress only constant tensors that round-trip without any value change."""
nodes = []
for node in model.graph.node:
nodes.append(node)
if node.op_type != "Constant":
continue
attribute = next((item for item in node.attribute if item.name == "value"), None)
if attribute is None or attribute.t.data_type != TensorProto.FLOAT:
continue
values = numpy_helper.to_array(attribute.t)
if not np.isfinite(values).all() or np.max(np.abs(values), initial=0) > np.finfo(np.float16).max:
continue
stored = values.astype(np.float16)
if values.nbytes < 64 or not np.array_equal(values, stored.astype(np.float32)):
continue
name = node.output[0]
node.output[0] = name + "__fp16_constant"
attribute.t.CopyFrom(numpy_helper.from_array(stored))
nodes.append(helper.make_node("Cast", [node.output[0]], [name], to=TensorProto.FLOAT))
del model.graph.node[:]
model.graph.node.extend(nodes)