1 | /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. |
2 | |
3 | Licensed under the Apache License, Version 2.0 (the "License"); |
4 | you may not use this file except in compliance with the License. |
5 | You may obtain a copy of the License at |
6 | |
7 | http://www.apache.org/licenses/LICENSE-2.0 |
8 | |
9 | Unless required by applicable law or agreed to in writing, software |
10 | distributed under the License is distributed on an "AS IS" BASIS, |
11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | See the License for the specific language governing permissions and |
13 | limitations under the License. |
14 | ==============================================================================*/ |
15 | |
16 | #include <string> |
17 | #include <vector> |
18 | |
19 | #include "pybind11/pybind11.h" |
20 | #include "tensorflow/core/lib/strings/str_util.h" |
21 | #include "tensorflow/core/platform/types.h" |
22 | #include "tensorflow/python/lib/core/pybind11_status.h" |
23 | #include "tensorflow/tools/graph_transforms/transform_graph.h" |
24 | |
25 | namespace py = pybind11; |
26 | |
27 | namespace tensorflow { |
28 | |
29 | string TransformGraphWithStringInputs(string graph_def_string, |
30 | string inputs_string, |
31 | string outputs_string, |
32 | string transforms_string) { |
33 | GraphDef graph_def; |
34 | if (!graph_def.ParseFromString(graph_def_string)) { |
35 | MaybeRaiseFromStatus( |
36 | errors::InvalidArgument("Couldn't interpret input as a GraphDef" )); |
37 | } |
38 | |
39 | graph_transforms::TransformParameters params_list; |
40 | Status parse_status = graph_transforms::ParseTransformParameters( |
41 | transforms_string, ¶ms_list); |
42 | if (!parse_status.ok()) { |
43 | MaybeRaiseFromStatus(parse_status); |
44 | } |
45 | std::vector<string> inputs = str_util::Split(inputs_string, ','); |
46 | std::vector<string> outputs = str_util::Split(outputs_string, ','); |
47 | |
48 | Status transform_status = graph_transforms::TransformGraph( |
49 | inputs, outputs, params_list, &graph_def); |
50 | if (!transform_status.ok()) { |
51 | MaybeRaiseFromStatus(transform_status); |
52 | } |
53 | string result; |
54 | if (!graph_def.SerializeToString(&result)) { |
55 | MaybeRaiseFromStatus( |
56 | errors::InvalidArgument("Couldn't serialize output as a GraphDef" )); |
57 | } |
58 | return result; |
59 | } |
60 | |
61 | } // namespace tensorflow |
62 | |
63 | PYBIND11_MODULE(_pywrap_transform_graph, m) { |
64 | m.def( |
65 | "TransformGraphWithStringInputs" , |
66 | [](const py::object graph_def_string, const py::object inputs_string, |
67 | const py::object outputs_string, const py::object transforms_string) { |
68 | return py::bytes(tensorflow::TransformGraphWithStringInputs( |
69 | graph_def_string.cast<std::string>(), |
70 | inputs_string.cast<std::string>(), |
71 | outputs_string.cast<std::string>(), |
72 | transforms_string.cast<std::string>())); |
73 | }); |
74 | }; |
75 | |