-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_resnet18_fp32.py
62 lines (53 loc) · 1.73 KB
/
test_resnet18_fp32.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
import torch
import cv2
import numpy as np
from scipy.special import softmax
np.set_printoptions(precision=3)
from tinyinfer.runtime import import_model, build_network
from tinyinfer.config import Config
from tinyinfer.utils import ytensor_2_numpy
import tinyinfer
from image_utils import *
from cuda import cudart
import time
if __name__ == "__main__":
tinyinfer.get_gpu_info()
img_name = "data/eagle.jpg"
img = imagenet_preprocess(img_name)
onnx_name = "data/resnet18.onnx"
config = Config()
config.log_verbose = False
config.fp32 = True
config.fp16 = False
config.use_cpu = False
config.use_gpu = True
model_data = import_model(onnx_name, config)
network = build_network(model_data, config)
inputs_zeros = {"images": np.zeros((1, 3, 224, 224)).astype(np.float32)}
inputs_max_shape = {"images": [1, 3, 224, 224]}
network.prepare(inputs_zeros, inputs_max_shape)
inputs = {"images": img.numpy()}
# warup
for i in range(5):
results = network.run(inputs)
cudart.cudaDeviceSynchronize()
start = time.time()
# forward
loops = 50
for i in range(loops):
results = network.run(inputs)
cudart.cudaDeviceSynchronize()
end = time.time()
fps = float(loops) / (end - start)
out_ytensor = results["output"]
# out_ytensor.print(10)
outdata = ytensor_2_numpy(out_ytensor)
out_tensor = softmax(outdata, axis=1)
# print(out_tensor[0][:10])
print("out shape:", out_tensor.shape)
print("detect confidence:", np.max(out_tensor[0]))
print("max index:", np.argmax(out_tensor[0]))
print("imagenet label:", get_imagenet_labels(np.argmax(out_tensor[0])))
print("fps:", fps)
del network
assert np.argmax(out_tensor[0]) == 22