-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_vqa_dataset.py
68 lines (58 loc) · 2.18 KB
/
test_vqa_dataset.py
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
import json
import sys
import numpy as np
import tritonclient.http as httpclient
from tritonclient.utils import np_to_triton_dtype
dataset_dir = "/datasets/vqa/"
json_file = dataset_dir + "test.json"
with open(json_file) as f:
dataset = json.load(f)
model_name = "blip_vqa"
# sample_size = 128
sample_size = 16
batch_size = 16
USE_MODAL_LEVEL_BATCH = True
with httpclient.InferenceServerClient("localhost:8000") as client:
image_batch = []
question_batch = []
use_modal_level_batch = np.array([USE_MODAL_LEVEL_BATCH] * batch_size)
for data in dataset[0:sample_size]:
image_batch.append(bytes(dataset_dir + data["image"], "utf-8"))
question_batch.append(bytes(data["question"], "utf-8"))
if len(image_batch) == batch_size:
images = np.array(image_batch)
questions = np.array(question_batch)
image_batch = []
question_batch = []
inputs = [
httpclient.InferInput(
"IMAGE",
images.shape,
np_to_triton_dtype(images.dtype),
),
httpclient.InferInput(
"QUESTION",
questions.shape,
np_to_triton_dtype(questions.dtype),
),
httpclient.InferInput(
"USE_MODAL_LEVEL_BATCH",
use_modal_level_batch.shape,
np_to_triton_dtype(use_modal_level_batch.dtype),
),
]
inputs[0].set_data_from_numpy(images)
inputs[1].set_data_from_numpy(questions)
inputs[2].set_data_from_numpy(use_modal_level_batch)
outputs = [
httpclient.InferRequestedOutput("ANSWER"),
]
response = client.infer(model_name,
inputs,
request_id=str(1),
outputs=outputs)
result = response.get_response()
answers = response.as_numpy("ANSWER")
print("IMAGE ({}) + QUESTION ({}) = ANSWER ({})".format(
images, questions, answers))
sys.exit(0)