Instructions to use moonbox-dev/BiRefNet_lite-TensorRT with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- TensorRT
How to use moonbox-dev/BiRefNet_lite-TensorRT with TensorRT:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- BiRefNet
How to use moonbox-dev/BiRefNet_lite-TensorRT with BiRefNet:
# Option 1: use with transformers from transformers import AutoModelForImageSegmentation birefnet = AutoModelForImageSegmentation.from_pretrained("moonbox-dev/BiRefNet_lite-TensorRT", trust_remote_code=True)# Option 2: use with BiRefNet # Install from https://github.com/ZhengPeng7/BiRefNet from models.birefnet import BiRefNet model = BiRefNet.from_pretrained("moonbox-dev/BiRefNet_lite-TensorRT") - Notebooks
- Google Colab
- Kaggle
File size: 2,229 Bytes
65b8e2d | 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 | """A minimal TensorRT engine runner: numpy in, numpy out (tensorrt-cu12 10.x and polygraphy; no torch).
from trt_runner import Engine
e = Engine("model.engine")
out = e({"input": x}) # dict of output name -> numpy array
"""
from __future__ import annotations
from pathlib import Path
import numpy as np
def lean_dlls():
"""Version-compatible engines are built against and run on TensorRT's lean runtime (pip tensorrt-lean-cu12):
put its DLL folder on the search path (Windows)."""
import importlib.util
import os
spec = importlib.util.find_spec("tensorrt_lean_libs")
if spec and spec.submodule_search_locations:
d = list(spec.submodule_search_locations)[0]
os.environ["PATH"] = d + os.pathsep + os.environ.get("PATH", "")
if hasattr(os, "add_dll_directory"):
os.add_dll_directory(d)
class Engine:
def __init__(self, path):
lean_dlls()
import tensorrt as trt
from polygraphy.backend.trt import TrtRunner
self.trt = trt
logger = trt.Logger(trt.Logger.WARNING)
runtime = trt.Runtime(logger)
runtime.engine_host_code_allowed = True # version-compatible engines carry their own lean runtime
engine = runtime.deserialize_cuda_engine(Path(path).read_bytes())
if engine is None:
raise RuntimeError(f"could not load {path} (TensorRT {trt.__version__}; the engine needs TensorRT 10.x "
"and an RTX 30-series or newer GPU)")
self.engine = engine
self.runner = TrtRunner(engine)
self.runner.activate()
self.inputs = [engine.get_tensor_name(i) for i in range(engine.num_io_tensors)
if engine.get_tensor_mode(engine.get_tensor_name(i)) == trt.TensorIOMode.INPUT]
def __call__(self, feed: dict) -> dict:
feed = {k: np.ascontiguousarray(v) for k, v in feed.items() if k in self.inputs}
out = self.runner.infer(feed, check_inputs=True)
return {k: np.array(v) for k, v in out.items()}
def close(self):
self.runner.deactivate()
def __del__(self):
try:
self.close()
except Exception:
pass
|