--- license: gpl-3.0 license_link: LICENSE base_model: - fushh7/WeDetect library_name: onnx pipeline_tag: zero-shot-object-detection --- # WeDetect-Large (ONNX) ONNX export of `wedetect_large.pth` and `wedetect_large_uni.pth` from [fushh7/WeDetect](https://huggingface.co/fushh7/WeDetect). ## Usage ```python import numpy as np import onnxruntime as ort from PIL import Image from tokenizers import Tokenizer providers = ["CUDAExecutionProvider", "CPUExecutionProvider"] text = ort.InferenceSession("onnx/text_model_fp16.onnx", providers=providers) vision = ort.InferenceSession("onnx/vision_model_fp16.onnx", providers=providers) tokenizer = Tokenizer.from_file("tokenizer.json") tokenizer.enable_padding(pad_id=1, pad_token="") labels = ["猫", "狗", "汽车"] enc = tokenizer.encode_batch(labels) txt_feats = text.run(["txt_feats"], {"input_ids": np.array([e.ids for e in enc], np.int64), "attention_mask": np.array([e.attention_mask for e in enc], np.int64)})[0] image = Image.open("image.jpg").convert("RGB") r = 1280 / max(image.size) w, h = round(image.width * r), round(image.height * r) canvas = Image.new("RGB", (1280, 1280), (114, 114, 114)) dx, dy = (1280 - w) // 2, (1280 - h) // 2 canvas.paste(image.resize((w, h), Image.BILINEAR), (dx, dy)) x = (np.asarray(canvas, np.float32) / 255).transpose(2, 0, 1)[None] boxes, scores = vision.run(["bboxes", "scores"], {"image": x, "txt_feats": txt_feats}) for i in np.argsort(-scores[0].max(-1))[:5]: box = (boxes[0, i] - [dx, dy, dx, dy]) / r print(labels[scores[0, i].argmax()], round(float(scores[0, i].max()), 3), box.round(1)) proposal = ort.InferenceSession("onnx/proposal_model_fp16.onnx", providers=providers) boxes, scores, region_embeddings = proposal.run(None, {"image": x}) top = np.argsort(-scores[0].max(-1))[:5] print(((boxes[0, top] - [dx, dy, dx, dy]) / r).round(1), region_embeddings[0, top].shape) ``` ## Notes - NMS is not included in the graph; the Usage above skips it. - `proposal_model` is WeDetect-Large-Uni. - Files: `*.onnx` (fp32), `*_fp16` (fp16 weights, float32 I/O), `*_int8` (8-bit weight-only `MatMulNBits`, requires ONNX Runtime; lower accuracy than fp16). Opset 23 (ONNX Runtime 1.22+). - Changes from the original: exported the text encoder and the detector as separate graphs and the Uni model as `proposal_model`; NMS is left out; the computation is unchanged.