Views
No views yet
1import onnxruntime as ort
2from transformers import BlipProcessor
3import numpy as np
4from PIL import Image
5
6# load the ONNX model
7onnx_model_path = "models/rgb_language_vqa_onnx/model.onnx"
8ort_session = ort.InferenceSession(onnx_model_path,providers=["CPUExecutionProvider"])
9
10# load the processor
11model_id = "models/rgb_language_vqa_onnx"
12processor = BlipProcessor.from_pretrained(model_id)
13
14# prepare the input image and question
15raw_image = Image.open("img1.jpg")
16question = "Where is the person?"
17
18# process the inputs using the processor
19inputs = processor(raw_image, question, return_tensors="np")
20
21# the input tensors for ONNX
22pixel_values = inputs["pixel_values"]
23input_ids = inputs["input_ids"]
24
25# run inference on the ONNX model
26outputs = ort_session.run(
27 None,
28 {
29 "pixel_values": pixel_values,
30 "input_ids": input_ids,
31 }
32)
33
34# decode the output
35output_ids = outputs[0] # Extract the output (token IDs)
36decoded_output = processor.tokenizer.decode(output_ids[0], skip_special_tokens=True)
37
38print(decoded_output)