2''''export SCRIPT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # '''
3''''export PY_PATH=${SCRIPT_PATH}/../bin/venv/bin/python # '''
4''''test -f ${PY_PATH} && exec ${PY_PATH} "$0" "$@" # '''
5''''echo "Error: Virtual environment not found. Please run 'one-prepare-venv' command." # '''
31import onnxruntime
as ort
33parser = argparse.ArgumentParser()
34parser.add_argument(
'--driver', type=str, required=
True)
35parser.add_argument(
'--onnx', type=str, required=
True)
36parser.add_argument(
'--circle', type=str, required=
True)
37args = parser.parse_args()
40onnx_filepath = args.onnx
41circle_filepath = args.circle
45 return tensor.cpu().numpy()
49 if (tensor.ndim == 4):
50 return np.transpose(tensor, (0, 2, 3, 1))
66 onnx.checker.check_model(model)
67 options = ort.SessionOptions()
70 options.intra_op_num_threads = 4
72 providers = ort.get_available_providers()
83 input_shape = self.
inputs[in_idx].shape
84 input_type = self.
inputs[in_idx].type
85 if input_type ==
'tensor(float)':
86 torch_type = torch.float32
89 raise SystemExit(
"Unsupported input dtype")
91 x = torch.randn(input_shape, dtype=torch_type)
96 input_npa_nhwc =
to_nhwc(input_npa)
97 input_npa_nhwc.tofile(circle_filepath +
".input" +
str(in_idx))
111onnx_runner.feed_random_inputs()
113onnx_runner.get_outputs()
116print(
"Run luci-interpreter...")
117process = subprocess.run([
118 driver, circle_filepath,
119 str(onnx_runner.inputs_size), circle_filepath +
".input", circle_filepath +
".output"
127for idx
in range(onnx_runner.outputs_size):
128 output_shape = onnx_runner.outputs[idx].shape
129 output_type = onnx_runner.outputs[idx].type
130 if output_type ==
'tensor(float)':
131 output_np_type = np.float32
134 raise SystemExit(
"Unsupported output dtype")
137 output_data = np.fromfile(circle_filepath +
".output" +
str(idx), output_np_type)
138 shape_file = open(circle_filepath +
".output" +
str(idx) +
".shape",
'r')
139 output_shape = [int(i)
for i
in shape_file.read().split(
',')]
140 luci_output_data = np.reshape(output_data, output_shape)
143 output_nchw = onnx_runner.outs[idx]
147 diff = np.isclose(output_nhwc, luci_output_data, rtol=rtolerance, atol=atolerance)
149 result_compare_one = np.all(diff)
150 print(
"Compare", idx, result_compare_one)
151 if (
not result_compare_one):
152 diff_val = np.subtract(output_nhwc, luci_output_data)
153 print(
"ONNX Result", output_nhwc)
154 print(
"Diff", diff_val)
155 print(
"Diff Max", np.ndarray.max(diff_val))
157 result_compare = result_compare
and result_compare_one
159if (
not result_compare):