ONE - On-device Neural Engine
Loading...
Searching...
No Matches
dynamic_shape_inference.py
Go to the documentation of this file.
1import numpy as np
2import random
3import sys
4from onert import infer
5
6
7def main(nnpackage_path, backends="cpu"):
8 # Create session and load nnpackage
9 session = infer.session(nnpackage_path, backends)
10
11 # Prepare input. Here we just allocate dummy input arrays.
12 input_infos = session.get_inputs_tensorinfo()
13
14 # Call infer() 10 times
15 for i in range(10):
16 dummy_inputs = []
17 for info in input_infos:
18 # Retrieve the dimensions list from tensorinfo property.
19 dims = list(info.dims)
20 # Replace -1 with a random value between 1 and 10
21 dims = [random.randint(1, 10) if d == -1 else d for d in dims]
22 # Build the shape tuple from tensorinfo dimensions.
23 shape = tuple(dims[:info.rank])
24 # Create a dummy numpy array filled with uniform random values in [0,1).
25 dummy_inputs.append(
26 np.random.uniform(low=0.0, high=1.0, size=shape).astype(info.dtype))
27
28 outputs = session.infer(dummy_inputs)
29 print(f"Inference run {i+1}/10 completed.")
30
31 print(f"nnpackage {nnpackage_path.split('/')[-1]} runs successfully.")
32 return
33
34
35if __name__ == "__main__":
36 argv = sys.argv[1:]
37 main(*argv)
int main(void)