ONE - On-device Neural Engine
Loading...
Searching...
No Matches
infer.py
Go to the documentation of this file.
1import numpy as np
2import os
3import shutil
4
5from .native import libnnfw_api_pybind
6
7
8def num_elems(tensor_info):
9 """Get the total number of elements in nnfw_tensorinfo.dims."""
10 n = 1
11 for x in range(tensor_info.rank):
12 n *= tensor_info.dims[x]
13 return n
14
15
16class session(libnnfw_api_pybind.nnfw_session):
17 """Class inherited nnfw_session for easily processing input/output"""
18 def __init__(self, nnpackage_path, backends="cpu"):
19 super().__init__(nnpackage_path, backends)
20 self.inputs = []
21 self.outputs = []
22 self.set_outputs(self.output_size())
23
24 def set_inputs(self, size, inputs_array=[]):
25 """Set inputs for each index"""
26 for i in range(size):
27 input_tensorinfo = self.input_tensorinfo(i)
28
29 if len(inputs_array) > i:
30 input_array = np.array(inputs_array[i], dtype=input_tensorinfo.dtype)
31 else:
32 print(
33 f"model's input size is {size} but given inputs_array size is {len(inputs_array)}.\n{i}-th index input is replaced by an array filled with 0."
34 )
35 input_array = np.zeros((num_elems(input_tensorinfo)),
36 dtype=input_tensorinfo.dtype)
37
38 self.set_input(i, input_array)
39 self.inputs.append(input_array)
40
41 def set_outputs(self, size):
42 """Set outputs for each index"""
43 for i in range(size):
44 output_tensorinfo = self.output_tensorinfo(i)
45 output_array = np.zeros((num_elems(output_tensorinfo)),
46 dtype=output_tensorinfo.dtype)
47 self.set_output(i, output_array)
48 self.outputs.append(output_array)
49
50 def inference(self):
51 """Inference model and get outputs"""
52 self.run()
53
54 return self.outputs
55
56
58 return libnnfw_api_pybind.nnfw_tensorinfo()
set_outputs(self, size)
Definition infer.py:41
__init__(self, nnpackage_path, backends="cpu")
Definition infer.py:18
set_inputs(self, size, inputs_array=[])
Definition infer.py:24
num_elems(tensor_info)
Definition infer.py:8