1 from __future__
import absolute_import
2 from __future__
import division
3 from __future__
import print_function
4 from __future__
import unicode_literals
8 import onnx.backend.test
11 from onnx
import numpy_helper
14 def load_tensor_as_numpy_array(f):
15 tensor = onnx.TensorProto()
16 with open(f,
'rb')
as file:
17 tensor.ParseFromString(file.read())
21 def assert_similar(ref, real):
22 np.testing.assert_equal(len(ref), len(real))
23 for i
in range(len(ref)):
24 np.testing.assert_allclose(ref[i], real[i], rtol=1e-3)
27 def run_generated_test(model_file, data_dir, device='CPU'):
28 model = onnx.load(model_file)
29 input_num = len(glob.glob(os.path.join(data_dir,
"input_*.pb")))
31 for i
in range(input_num):
32 inputs.append(numpy_helper.to_array(load_tensor_as_numpy_array(
33 os.path.join(data_dir,
"input_{}.pb".format(i)))))
34 output_num = len(glob.glob(os.path.join(data_dir,
"output_*.pb")))
36 for i
in range(output_num):
37 outputs.append(numpy_helper.to_array(load_tensor_as_numpy_array(
38 os.path.join(data_dir,
"output_{}.pb".format(i)))))
39 prepared = c2.prepare(model, device=device)
40 c2_outputs = prepared.run(inputs)
41 assert_similar(outputs, c2_outputs)