diff options
author | A. Unique TensorFlower <gardener@tensorflow.org> | 2017-07-05 14:47:12 -0700 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2017-07-05 14:51:59 -0700 |
commit | 1857e187c98b4863669b62a469acc1251e1c1f04 (patch) | |
tree | 68d598418db99746dd3357111091c006358c6bb9 /tensorflow/tools/mlpbtxt | |
parent | eccd162119675d0bf5bc6f8e6a93dcda7ab6db4a (diff) |
Add some utility functions for supporting an alternate pbtxt format
that supports multi-line text without uncomfortable escaping. So:
description: "A `SparseTensor` ... `sparse_indices`,\n`sparse_values`, and `sparse_shape`, where\n\n```sparse_indices.shape[1] == sparse_shape.shape[0] == R```\n\nAn `N`-minibatch ..."
would become:
description: <<END
A `SparseTensor` ... `sparse_indices`,
`sparse_values`, and `sparse_shape`, where
```sparse_indices.shape[1] == sparse_shape.shape[0] == R```
An `N`-minibatch ...
END
PiperOrigin-RevId: 161008382
Diffstat (limited to 'tensorflow/tools/mlpbtxt')
-rw-r--r-- | tensorflow/tools/mlpbtxt/BUILD | 44 | ||||
-rw-r--r-- | tensorflow/tools/mlpbtxt/frommlpbtxt.cc | 70 | ||||
-rw-r--r-- | tensorflow/tools/mlpbtxt/tomlpbtxt.cc | 81 |
3 files changed, 195 insertions, 0 deletions
diff --git a/tensorflow/tools/mlpbtxt/BUILD b/tensorflow/tools/mlpbtxt/BUILD new file mode 100644 index 0000000000..fc63e9a0b7 --- /dev/null +++ b/tensorflow/tools/mlpbtxt/BUILD @@ -0,0 +1,44 @@ +# Description: +# This package provides binaries that convert between multi-line and standard +# pbtxt (text-serialization of protocol message) files. + +package(default_visibility = ["//visibility:private"]) + +licenses(["notice"]) # Apache 2.0 + +exports_files([ + "LICENSE", + "placeholder.txt", +]) + +cc_binary( + name = "tomlpbtxt", + srcs = ["tomlpbtxt.cc"], + deps = [ + "//tensorflow/core:framework_internal", + "//tensorflow/core:lib", + "//tensorflow/core:op_gen_lib", + ], +) + +cc_binary( + name = "frommlpbtxt", + srcs = ["frommlpbtxt.cc"], + deps = [ + "//tensorflow/core:framework_internal", + "//tensorflow/core:lib", + "//tensorflow/core:op_gen_lib", + ], +) + +filegroup( + name = "all_files", + srcs = glob( + ["**/*"], + exclude = [ + "**/METADATA", + "**/OWNERS", + ], + ), + visibility = ["//tensorflow:__subpackages__"], +) diff --git a/tensorflow/tools/mlpbtxt/frommlpbtxt.cc b/tensorflow/tools/mlpbtxt/frommlpbtxt.cc new file mode 100644 index 0000000000..643924b318 --- /dev/null +++ b/tensorflow/tools/mlpbtxt/frommlpbtxt.cc @@ -0,0 +1,70 @@ +/* Copyright 2017 The TensorFlow Authors All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#include <stdio.h> + +#include "tensorflow/core/framework/op_gen_lib.h" +#include "tensorflow/core/lib/core/errors.h" +#include "tensorflow/core/lib/strings/str_util.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/init_main.h" +#include "tensorflow/core/util/command_line_flags.h" + +namespace tensorflow { +namespace { + +int Run(int argc, char** argv) { + string FLAGS_in = ""; + string FLAGS_out = ""; + + std::vector<Flag> flag_list = { + Flag("in", &FLAGS_in, "Input multi-line proto text (.mlpbtxt) file name"), + Flag("out", &FLAGS_out, "Output proto text (.pbtxt) file name")}; + + // Parse the command-line. + const string usage = Flags::Usage(argv[0], flag_list); + const bool parse_ok = Flags::Parse(&argc, argv, flag_list); + if (argc != 1 || !parse_ok) { + printf("%s", usage.c_str()); + return 2; + } + + port::InitMain(argv[0], &argc, &argv); + + // Read the input file --in. + string in_contents; + Status s = ReadFileToString(Env::Default(), FLAGS_in, &in_contents); + if (!s.ok()) { + printf("Error reading file %s: %s\n", FLAGS_in.c_str(), + s.ToString().c_str()); + return 1; + } + + // Write the output file --out. + const string out_contents = PBTxtFromMultiline(in_contents); + s = WriteStringToFile(Env::Default(), FLAGS_out, out_contents); + if (!s.ok()) { + printf("Error writing file %s: %s\n", FLAGS_out.c_str(), + s.ToString().c_str()); + return 1; + } + + return 0; +} + +} // namespace +} // namespace tensorflow + +int main(int argc, char** argv) { return tensorflow::Run(argc, argv); } diff --git a/tensorflow/tools/mlpbtxt/tomlpbtxt.cc b/tensorflow/tools/mlpbtxt/tomlpbtxt.cc new file mode 100644 index 0000000000..469be49ed3 --- /dev/null +++ b/tensorflow/tools/mlpbtxt/tomlpbtxt.cc @@ -0,0 +1,81 @@ +/* Copyright 2017 The TensorFlow Authors All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#include <stdio.h> + +#include "tensorflow/core/framework/op_gen_lib.h" +#include "tensorflow/core/lib/core/errors.h" +#include "tensorflow/core/lib/strings/str_util.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/init_main.h" +#include "tensorflow/core/util/command_line_flags.h" + +namespace tensorflow { +namespace { + +int Run(int argc, char** argv) { + string FLAGS_in = ""; + string FLAGS_out = ""; + string FLAGS_fields = "description"; + + std::vector<Flag> flag_list = { + Flag("in", &FLAGS_in, "Input proto text (.pbtxt) file name"), + Flag("out", &FLAGS_out, + "Output multi-line proto text (.mlpbtxt) file name"), + Flag("fields", &FLAGS_fields, "Comma-separated list of field names")}; + + // Parse the command-line. + const string usage = Flags::Usage(argv[0], flag_list); + const bool parse_ok = Flags::Parse(&argc, argv, flag_list); + if (argc != 1 || !parse_ok) { + printf("%s", usage.c_str()); + return 2; + } + + // Parse the --fields option. + std::vector<string> fields = + str_util::Split(FLAGS_fields, ',', str_util::SkipEmpty()); + if (fields.empty()) { + printf("--fields must be non-empty.\n%s", usage.c_str()); + return 2; + } + + port::InitMain(argv[0], &argc, &argv); + + // Read the input file --in. + string in_contents; + Status s = ReadFileToString(Env::Default(), FLAGS_in, &in_contents); + if (!s.ok()) { + printf("Error reading file %s: %s\n", FLAGS_in.c_str(), + s.ToString().c_str()); + return 1; + } + + // Write the output file --out. + const string out_contents = PBTxtToMultiline(in_contents, fields); + s = WriteStringToFile(Env::Default(), FLAGS_out, out_contents); + if (!s.ok()) { + printf("Error writing file %s: %s\n", FLAGS_out.c_str(), + s.ToString().c_str()); + return 1; + } + + return 0; +} + +} // namespace +} // namespace tensorflow + +int main(int argc, char** argv) { return tensorflow::Run(argc, argv); } |