File size: 9,493 Bytes
a693d8d
 
 
 
 
8e42393
a693d8d
 
 
 
 
 
 
 
9c0b09e
a693d8d
 
 
 
 
9c0b09e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a693d8d
 
 
 
 
9c0b09e
a693d8d
 
 
 
 
9c0b09e
 
 
 
 
 
 
 
a693d8d
 
 
 
9c0b09e
 
 
a693d8d
9c0b09e
 
a693d8d
 
 
9c0b09e
a693d8d
 
 
 
 
 
9c0b09e
a693d8d
 
 
 
 
 
9c0b09e
a693d8d
9c0b09e
a693d8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c0b09e
a693d8d
 
 
 
9c0b09e
 
a693d8d
 
 
9c0b09e
a693d8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c0b09e
 
 
a693d8d
9c0b09e
 
 
 
 
a693d8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c0b09e
 
 
 
 
 
a693d8d
 
 
 
 
 
 
8e42393
a693d8d
 
9c0b09e
a693d8d
 
 
 
9c0b09e
a693d8d
 
8e42393
 
a693d8d
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import os
import time
from dataclasses import dataclass
from functools import lru_cache

import gradio as gr
import numpy as np
from huggingface_hub import hf_hub_download
from openvino import Core
from PIL import Image, ImageOps


MODEL_REPO_ID = os.getenv("MODEL_REPO_ID", "ibrhr/BiRefNet-openvino-xeon-w2145")
DEVICE = os.getenv("OPENVINO_DEVICE", "CPU")
DEFAULT_MODEL_VARIANT_KEY = os.getenv("MODEL_VARIANT", "fp32_1024x1024")

IMAGENET_MEAN = np.array([0.485, 0.456, 0.406], dtype=np.float32)
IMAGENET_STD = np.array([0.229, 0.224, 0.225], dtype=np.float32)


@dataclass(frozen=True)
class ModelVariant:
    key: str
    label: str
    xml: str
    precision: str
    input_size: int
    benchmark_ms: float
    benchmark_fps: float
    notes: str

    @property
    def bin(self) -> str:
        return self.xml.replace(".xml", ".bin")


MODEL_VARIANTS = (
    ModelVariant(
        key="fp32_1024x1024",
        label="FP32 - 1024x1024 - 4853 ms / 0.21 FPS",
        xml="fp32_1024x1024.xml",
        precision="FP32",
        input_size=1024,
        benchmark_ms=4853.2,
        benchmark_fps=0.21,
        notes="Reference OpenVINO FP32 model at full input resolution.",
    ),
    ModelVariant(
        key="fp32_512x512",
        label="FP32 - 512x512 - 1262 ms / 0.79 FPS",
        xml="fp32_512x512.xml",
        precision="FP32",
        input_size=512,
        benchmark_ms=1261.7,
        benchmark_fps=0.79,
        notes="Reference OpenVINO FP32 model at lower input resolution.",
    ),
    ModelVariant(
        key="fp16_1024x1024",
        label="FP16 - 1024x1024 - 4838 ms / 0.21 FPS",
        xml="openvino_fp16/fp16_1024x1024.xml",
        precision="FP16",
        input_size=1024,
        benchmark_ms=4838.1,
        benchmark_fps=0.21,
        notes="Smaller OpenVINO weights than FP32 at full input resolution.",
    ),
    ModelVariant(
        key="fp16_512x512",
        label="FP16 - 512x512 - 1252 ms / 0.80 FPS",
        xml="openvino_fp16/fp16_512x512.xml",
        precision="FP16",
        input_size=512,
        benchmark_ms=1252.5,
        benchmark_fps=0.80,
        notes="Fastest benchmarked OpenVINO option in this repo.",
    ),
    ModelVariant(
        key="int8_1024x1024",
        label="INT8 NNCF - 1024x1024 - 4923 ms / 0.20 FPS",
        xml="openvino_int8/int8_1024x1024.xml",
        precision="INT8 NNCF",
        input_size=1024,
        benchmark_ms=4923.4,
        benchmark_fps=0.20,
        notes="NNCF quantized OpenVINO model at full input resolution.",
    ),
    ModelVariant(
        key="int8_512x512",
        label="INT8 NNCF - 512x512 - 1257 ms / 0.80 FPS",
        xml="openvino_int8/int8_512x512.xml",
        precision="INT8 NNCF",
        input_size=512,
        benchmark_ms=1256.7,
        benchmark_fps=0.80,
        notes="NNCF quantized OpenVINO model at lower input resolution.",
    ),
    ModelVariant(
        key="int8wo_1024x1024",
        label="INT8 weight-only - 1024x1024 - 4973 ms / 0.20 FPS",
        xml="openvino_int8wo/int8wo_1024x1024.xml",
        precision="INT8 weight-only",
        input_size=1024,
        benchmark_ms=4972.6,
        benchmark_fps=0.20,
        notes="Weight-only quantized OpenVINO model at full input resolution.",
    ),
    ModelVariant(
        key="int8wo_512x512",
        label="INT8 weight-only - 512x512 - 1283 ms / 0.78 FPS",
        xml="openvino_int8wo/int8wo_512x512.xml",
        precision="INT8 weight-only",
        input_size=512,
        benchmark_ms=1283.3,
        benchmark_fps=0.78,
        notes="Weight-only quantized OpenVINO model at lower input resolution.",
    ),
)
MODEL_VARIANTS_BY_KEY = {variant.key: variant for variant in MODEL_VARIANTS}


@dataclass(frozen=True)
class Runtime:
    compiled_model: object
    input_node: object
    output_node: object
    variant: ModelVariant
    model_path: str
    load_seconds: float
    device: str


def get_model_variant(variant_key: str | None) -> ModelVariant:
    key = variant_key or DEFAULT_MODEL_VARIANT_KEY
    if key not in MODEL_VARIANTS_BY_KEY:
        valid_keys = ", ".join(MODEL_VARIANTS_BY_KEY)
        raise gr.Error(f"Unknown model variant '{key}'. Valid variants: {valid_keys}")
    return MODEL_VARIANTS_BY_KEY[key]


def _resampling(name: str) -> int:
    return getattr(Image.Resampling, name)


@lru_cache(maxsize=len(MODEL_VARIANTS))
def get_runtime(variant_key: str) -> Runtime:
    variant = get_model_variant(variant_key)
    started = time.perf_counter()
    model_path = hf_hub_download(repo_id=MODEL_REPO_ID, filename=variant.xml)
    weights_path = hf_hub_download(repo_id=MODEL_REPO_ID, filename=variant.bin)

    core = Core()
    model = core.read_model(model=model_path, weights=weights_path)
    model.reshape({model.input(0): [1, 3, variant.input_size, variant.input_size]})
    compiled_model = core.compile_model(model, DEVICE)

    return Runtime(
        compiled_model=compiled_model,
        input_node=compiled_model.input(0),
        output_node=compiled_model.output(0),
        variant=variant,
        model_path=model_path,
        load_seconds=time.perf_counter() - started,
        device=DEVICE,
    )


def preprocess(image: Image.Image, model_size: int) -> np.ndarray:
    rgb_image = ImageOps.exif_transpose(image).convert("RGB")
    resized = rgb_image.resize((model_size, model_size), _resampling("BICUBIC"))
    array = np.asarray(resized, dtype=np.float32) / 255.0
    array = (array - IMAGENET_MEAN) / IMAGENET_STD
    array = np.transpose(array, (2, 0, 1))[None, ...]
    return np.ascontiguousarray(array, dtype=np.float32)


def sigmoid(array: np.ndarray) -> np.ndarray:
    clipped = np.clip(array, -50.0, 50.0)
    return 1.0 / (1.0 + np.exp(-clipped))


def postprocess_mask(output: np.ndarray, size: tuple[int, int]) -> Image.Image:
    mask = np.asarray(output, dtype=np.float32)
    while mask.ndim > 2:
        mask = mask[0]

    mask = sigmoid(mask)
    mask = np.clip(mask * 255.0, 0, 255).astype(np.uint8)
    mask_image = Image.fromarray(mask, mode="L")
    return mask_image.resize(size, _resampling("LANCZOS"))


def remove_background(image: Image.Image, model_variant_key: str):
    if image is None:
        raise gr.Error("Upload an image first.")

    total_started = time.perf_counter()
    variant = get_model_variant(model_variant_key)
    runtime = get_runtime(variant.key)
    original = ImageOps.exif_transpose(image).convert("RGB")

    preprocess_started = time.perf_counter()
    tensor = preprocess(original, variant.input_size)
    preprocess_seconds = time.perf_counter() - preprocess_started

    inference_started = time.perf_counter()
    output = runtime.compiled_model({runtime.input_node: tensor})[runtime.output_node]
    inference_seconds = time.perf_counter() - inference_started

    postprocess_started = time.perf_counter()
    mask_image = postprocess_mask(output, original.size)
    cutout = original.convert("RGBA")
    cutout.putalpha(mask_image)
    postprocess_seconds = time.perf_counter() - postprocess_started

    total_seconds = time.perf_counter() - total_started
    timing = (
        f"Total: {total_seconds:.3f} s\n"
        f"Preprocess: {preprocess_seconds * 1000:.1f} ms\n"
        f"Inference: {inference_seconds * 1000:.1f} ms\n"
        f"Postprocess: {postprocess_seconds * 1000:.1f} ms"
    )

    specs = {
        "model": MODEL_REPO_ID,
        "variant": variant.key,
        "variant_label": variant.label,
        "model_xml": variant.xml,
        "device": runtime.device,
        "precision": variant.precision,
        "model_input_size": f"{variant.input_size}x{variant.input_size}",
        "benchmark_ms": variant.benchmark_ms,
        "benchmark_fps": variant.benchmark_fps,
        "variant_notes": variant.notes,
        "uploaded_image_size": f"{original.width}x{original.height}",
        "input_tensor_shape": list(tensor.shape),
        "output_tensor_shape": list(np.asarray(output).shape),
        "model_load_seconds": round(runtime.load_seconds, 3),
        "total_seconds": round(total_seconds, 3),
        "preprocess_ms": round(preprocess_seconds * 1000, 1),
        "inference_ms": round(inference_seconds * 1000, 1),
        "postprocess_ms": round(postprocess_seconds * 1000, 1),
    }
    return mask_image, cutout, timing, specs


with gr.Blocks(title="BiRefNet OpenVINO") as demo:
    gr.Markdown("# BiRefNet OpenVINO")
    with gr.Row():
        input_image = gr.Image(label="Image", type="pil")
        model_dropdown = gr.Dropdown(
            label="Model variant",
            choices=[(variant.label, variant.key) for variant in MODEL_VARIANTS],
            value=get_model_variant(DEFAULT_MODEL_VARIANT_KEY).key,
            interactive=True,
        )
    run_button = gr.Button("Run", variant="primary")
    with gr.Row():
        mask_output = gr.Image(label="Mask", type="pil")
        cutout_output = gr.Image(label="Background removed", type="pil", format="png")
    with gr.Row():
        timing_output = gr.Textbox(label="Processing time", lines=4)
        specs_output = gr.JSON(label="Specs")

    run_button.click(
        fn=remove_background,
        inputs=[input_image, model_dropdown],
        outputs=[mask_output, cutout_output, timing_output, specs_output],
    )
    input_image.upload(
        fn=remove_background,
        inputs=[input_image, model_dropdown],
        outputs=[mask_output, cutout_output, timing_output, specs_output],
    )


if __name__ == "__main__":
    demo.queue(max_size=8).launch()