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

@@ -26,6 +26,18 @@ class FakeSession:
class UpscaleOnnxTest(unittest.TestCase):
def test_wider_context_preserves_pixels_across_odd_and_small_images(self):
for width, height in [(1, 1), (7, 5), (321, 163)]:
pixels = np.random.default_rng(7).integers(0, 256, (height, width, 3), dtype=np.uint8)
result = onnx_upscale.infer(FakeSession(), Image.fromarray(pixels), tile_border=48)
expected = np.repeat(np.repeat(pixels, 4, axis=0), 4, axis=1)
np.testing.assert_array_equal(np.asarray(result), expected)
def test_nonfinite_pixels_fail_before_conversion(self):
with patch.object(onnx_upscale, "run_session", return_value=[np.full((1, 3, 1024, 1024), np.nan)]):
with self.assertRaisesRegex(RuntimeError, "non-finite"):
onnx_upscale.infer(FakeSession(), Image.new("RGB", (7, 5)))
def test_preprocess_matches_dynamic_rgb_layout(self):
image = Image.new("RGB", (7, 5), (64, 128, 192))