def test_opt_on_model(self): val = np.asarray([[[[1.0, 2.0, 3.0], [1.1, 2.1, 3.1]]]], np.float32) nodes = [] nodes[0:] = \ [helper.make_node('Constant', [], ['const1'], value=helper.make_tensor( name='const0', data_type=onnx_proto.TensorProto.FLOAT, dims=val.shape, vals=val.flatten().astype(float)), name="0")] nodes[1:] = [helper.make_node('Identity', ['const1'], ['identity1'], name="1")] nodes[2:] = [helper.make_node('Identity', ['identity1'], ['identity2'], name="2")] nodes[3:] = [helper.make_node('Max', ['input1', 'identity2'], ['max0'], name="3")] nodes[4:] = [helper.make_node('LeakyRelu', ['max0'], ['leak0'], name="4")] nodes[5:] = [helper.make_node('LeakyRelu', ['leak0'], ['leak1'], name="5")] nodes[6:] = [helper.make_node('LeakyRelu', ['leak0'], ['leak2'], name="6")] nodes[7:] = [helper.make_node('Transpose', ['leak1'], ['tranpose0'], perm=[0, 2, 3, 1], name="7")] nodes[8:] = [helper.make_node('Transpose', ['leak2'], ['tranpose1'], perm=[0, 2, 3, 1], name="8")] nodes[9:] = [helper.make_node('Add', ['tranpose0', 'tranpose1'], ['add0'], name="9")] nodes[10:] = [helper.make_node('Transpose', ['add0'], ['tranpose2'], perm=[0, 3, 1, 2], name="10")] nodes[11:] = [helper.make_node('Conv', ['tranpose2'], ['output0'], name="11")] input0 = helper.make_tensor_value_info('input1', onnx_proto.TensorProto.FLOAT, [1, 1, 2, 3]) output0 = helper.make_tensor_value_info('output0', onnx_proto.TensorProto.FLOAT, [1, 1, 2, 3]) graph = helper.make_graph(nodes, 'test0', [input0], [output0]) model = helper.make_model(graph) self.assertIsNotNone(model) optd_model = optimize_onnx_model(model) self.assertEqual(len(optd_model.graph.node), 5)
def test_onnx_models(self): model_names = ['mobile_segnet_no_opt.onnx', 'srgan_no_opt.onnx', 'test_model_0_no_opt.onnx'] dir_path = os.path.dirname(os.path.realpath(__file__)) for model_name_ in model_names: model_dir = dir_path + '/data/' + model_name_ origin_model = onnx.load_model(model_dir) opt_model = optimize_onnx_model(origin_model) self.assertIsNotNone(opt_model)
def test_onnx_models(self): model_names = [ 'mobile_segnet_no_opt.onnx', 'srgan_no_opt.onnx', 'test_model_0_no_opt.onnx', 'test_model_1_no_opt.onnx' ] num_transpose_list = [2, 3, 11, 6] dir_path = os.path.dirname(os.path.realpath(__file__)) for idx_, model_name_ in enumerate(model_names): model_dir = dir_path + '/data/' + model_name_ origin_model = onnx.load_model(model_dir) opt_model = optimize_onnx_model(origin_model) self.assertIsNotNone(opt_model) num_transpose = sum([ 1 if n_.op_type == 'Transpose' else 0 for n_ in opt_model.graph.node ]) self.assertEqual(num_transpose, num_transpose_list[idx_])
def test_matmul_opt(self): @onnx_function(outputs=['z'], input_types=(_Ty.F([1, 1, 6, 1])), output_types=[_Ty.f]) def transpose_n_matmul(x): ox = x.ox # type: OnnxOperatorBuilderX wm = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]).astype(np.float32).reshape([2, 6]) b = ox.constant(value=wm) a = ox.transpose(x, perm=[0, 1, 3, 2]) c = ox.transpose(b, perm=[1, 0]) return ox.matmul([a, c]) m1 = np.array([[2, 3], [4, 5], [6, 7]]).astype(np.float32).reshape([1, 1, 6, 1]) expected = transpose_n_matmul(m1) opted = optimize_onnx_model(transpose_n_matmul.to_model()) actual = _ort_inference(opted, {'x': m1}) self.assertTrue(np.allclose(expected, actual), "The result mismatch")