Skip to content

Commit

Permalink
Remove dependency on contrib.learn in inspect_checkpoint binary.
Browse files Browse the repository at this point in the history
Add inspect checkpoint binary to contrib.learn with additional functionality:
 - supports sorted output.
 - inspect checkpoints folder (finds latest checkpoint).
Change: 122306430
  • Loading branch information
ilblackdragon authored and tensorflower-gardener committed May 13, 2016
1 parent febd091 commit dcef59f
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 6 deletions.
11 changes: 11 additions & 0 deletions tensorflow/contrib/learn/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,17 @@ py_test(
],
)

py_binary(
name = "inspect_checkpoint",
srcs = [
"python/learn/utils/inspect_checkpoint.py",
],
srcs_version = "PY2AND3",
deps = [
"//tensorflow:tensorflow_py",
],
)

filegroup(
name = "all_files",
srcs = glob(
Expand Down
68 changes: 68 additions & 0 deletions tensorflow/contrib/learn/python/learn/utils/inspect_checkpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Copyright 2016 Google Inc. 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.
# ==============================================================================

"""A simple script for inspect checkpoint files."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import sys

import tensorflow as tf

from tensorflow.contrib.learn.python.learn.utils import checkpoints

FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_string("file_name", "", "Checkpoint filename")
tf.app.flags.DEFINE_string("tensor_name", "", "Name of the tensor to inspect")


def print_tensors_in_checkpoint_file(file_name, tensor_name):
"""Prints tensors in a checkpoint file.
If no `tensor_name` is provided, prints the tensor names and shapes
in the checkpoint file.
If `tensor_name` is provided, prints the content of the tensor.
Args:
file_name: Name of the checkpoint file.
tensor_name: Name of the tensor in the checkpoint file to print.
"""
try:
if not tensor_name:
variables = checkpoints.list_variables(file_name)
for name, shape in variables:
print("%s\t%s" % (name, str(shape)))
else:
print("tensor_name: ", tensor_name)
print(checkpoints.load_variable(file_name, tensor_name))
except Exception as e: # pylint: disable=broad-except
print(str(e))
if "corrupted compressed block contents" in str(e):
print("It's likely that your checkpoint file has been compressed "
"with SNAPPY.")


def main(unused_argv):
if not FLAGS.file_name:
print("Usage: inspect_checkpoint --file_name=<checkpoint_file_name "
"or directory> [--tensor_name=tensor_to_print]")
sys.exit(1)
else:
print_tensors_in_checkpoint_file(FLAGS.file_name, FLAGS.tensor_name)

if __name__ == "__main__":
tf.app.run()
9 changes: 3 additions & 6 deletions tensorflow/python/tools/inspect_checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

import tensorflow as tf

from tensorflow.contrib.learn.python.learn.utils import checkpoints

FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_string("file_name", "", "Checkpoint filename")
tf.app.flags.DEFINE_string("tensor_name", "", "Name of the tensor to inspect")
Expand All @@ -42,13 +40,12 @@ def print_tensors_in_checkpoint_file(file_name, tensor_name):
tensor_name: Name of the tensor in the checkpoint file to print.
"""
try:
reader = tf.train.NewCheckpointReader(file_name)
if not tensor_name:
variables = checkpoints.list_variables(file_name)
for name, shape in variables:
print("%s\t%s" % (name, str(shape)))
print(reader.debug_string().decode("utf-8"))
else:
print("tensor_name: ", tensor_name)
print(checkpoints.load_variable(file_name, tensor_name))
print(reader.get_tensor(tensor_name))
except Exception as e: # pylint: disable=broad-except
print(str(e))
if "corrupted compressed block contents" in str(e):
Expand Down

0 comments on commit dcef59f

Please sign in to comment.