From dd845147f16679033f5efe3cbde7c417b3c71a8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20Man=C3=A9?= Date: Thu, 20 Oct 2016 16:39:41 -0800 Subject: [PATCH] Modify tensorflow/python/training to use the new summary ops. Very simple migration replacing tf.scalar_summary with tf.summary.scalar, etc. It is almost entirely a namespace shift. Also, the new scalar_summary does not support array of inputs, which was only really used in test code. Change: 136782705 --- .../training/basic_session_run_hooks.py | 4 +- .../training/basic_session_run_hooks_test.py | 2 +- tensorflow/python/training/input.py | 23 +++++------ .../python/training/monitored_session.py | 8 ++-- .../python/training/monitored_session_test.py | 2 +- tensorflow/python/training/saver_test.py | 2 +- tensorflow/python/training/supervisor.py | 6 +-- tensorflow/python/training/supervisor_test.py | 41 +++++++++++++------ 8 files changed, 52 insertions(+), 36 deletions(-) diff --git a/tensorflow/python/training/basic_session_run_hooks.py b/tensorflow/python/training/basic_session_run_hooks.py index 6abc5b9e759..da930f2bdb9 100644 --- a/tensorflow/python/training/basic_session_run_hooks.py +++ b/tensorflow/python/training/basic_session_run_hooks.py @@ -372,8 +372,8 @@ class SummarySaverHook(session_run_hook.SessionRunHook): one will be created accordingly. scaffold: `Scaffold` to get summary_op if it's not provided. summary_op: `Tensor` of type `string`. A serialized `Summary` protocol - buffer, as output by TF summary methods like `scalar_summary` or - `merge_all_summaries`. + buffer, as output by TF summary methods like `tf.summary.scalar` or + `tf.summary.merge_all`. Raises: ValueError: Exactly one of scaffold or summary_op should be set. diff --git a/tensorflow/python/training/basic_session_run_hooks_test.py b/tensorflow/python/training/basic_session_run_hooks_test.py index 040164af2cf..77be27a4ff3 100644 --- a/tensorflow/python/training/basic_session_run_hooks_test.py +++ b/tensorflow/python/training/basic_session_run_hooks_test.py @@ -333,7 +333,7 @@ class SummarySaverHookTest(tf.test.TestCase): var = tf.Variable(0.0) tensor = tf.assign_add(var, 1.0) - self.summary_op = tf.scalar_summary('my_summary', tensor) + self.summary_op = tf.summary.scalar('my_summary', tensor) global_step = tf.contrib.framework.get_or_create_global_step() self.train_op = tf.assign_add(global_step, 1) diff --git a/tensorflow/python/training/input.py b/tensorflow/python/training/input.py index cc813cfaac5..2bdfb211608 100644 --- a/tensorflow/python/training/input.py +++ b/tensorflow/python/training/input.py @@ -27,6 +27,7 @@ import collections from six.moves import xrange # pylint: disable=redefined-builtin +from tensorflow.python import summary from tensorflow.python.framework import constant_op from tensorflow.python.framework import dtypes from tensorflow.python.framework import ops @@ -35,7 +36,6 @@ from tensorflow.python.ops import array_ops from tensorflow.python.ops import control_flow_ops from tensorflow.python.ops import data_flow_ops from tensorflow.python.ops import io_ops -from tensorflow.python.ops import logging_ops from tensorflow.python.ops import math_ops from tensorflow.python.ops import random_ops from tensorflow.python.ops import sparse_ops @@ -147,9 +147,8 @@ def input_producer(input_tensor, element_shape=None, num_epochs=None, enq = q.enqueue_many([input_tensor]) queue_runner.add_queue_runner(queue_runner.QueueRunner(q, [enq])) if summary_name is not None: - logging_ops.scalar_summary("queue/%s/%s" % (q.name, summary_name), - math_ops.cast(q.size(), dtypes.float32) * - (1. / capacity)) + summary.scalar("queue/%s/%s" % (q.name, summary_name), + math_ops.cast(q.size(), dtypes.float32) * (1. / capacity)) return q @@ -650,9 +649,9 @@ def batch(tensors, batch_size, num_threads=1, capacity=32, capacity=capacity, dtypes=types, shapes=shapes, shared_name=shared_name) print("Enqueueing: ", enqueue_many, tensor_list, shapes) _enqueue(queue, tensor_list, num_threads, enqueue_many) - logging_ops.scalar_summary( - "queue/%s/fraction_of_%d_full" % (queue.name, capacity), - math_ops.cast(queue.size(), dtypes.float32) * (1. / capacity)) + summary.scalar("queue/%s/fraction_of_%d_full" % (queue.name, capacity), + math_ops.cast(queue.size(), dtypes.float32) * + (1. / capacity)) if allow_smaller_final_batch: dequeued = queue.dequeue_up_to(batch_size, name=name) @@ -763,9 +762,9 @@ def batch_join(tensors_list, batch_size, capacity=32, enqueue_many=False, queue = _which_queue(dynamic_pad)( capacity=capacity, dtypes=types, shapes=shapes, shared_name=shared_name) _enqueue_join(queue, tensor_list_list, enqueue_many) - logging_ops.scalar_summary( - "queue/%s/fraction_of_%d_full" % (queue.name, capacity), - math_ops.cast(queue.size(), dtypes.float32) * (1. / capacity)) + summary.scalar("queue/%s/fraction_of_%d_full" % (queue.name, capacity), + math_ops.cast(queue.size(), dtypes.float32) * + (1. / capacity)) if allow_smaller_final_batch: dequeued = queue.dequeue_up_to(batch_size, name=name) @@ -874,7 +873,7 @@ def shuffle_batch(tensors, batch_size, capacity, min_after_dequeue, summary_name = ( "queue/%sfraction_over_%d_of_%d_full" % (name, min_after_dequeue, capacity - min_after_dequeue)) - logging_ops.scalar_summary(summary_name, full) + summary.scalar(summary_name, full) if allow_smaller_final_batch: dequeued = queue.dequeue_up_to(batch_size, name=name) @@ -978,7 +977,7 @@ def shuffle_batch_join(tensors_list, batch_size, capacity, summary_name = ( "queue/%sfraction_over_%d_of_%d_full" % (name, min_after_dequeue, capacity - min_after_dequeue)) - logging_ops.scalar_summary(summary_name, full) + summary.scalar(summary_name, full) if allow_smaller_final_batch: dequeued = queue.dequeue_up_to(batch_size, name=name) diff --git a/tensorflow/python/training/monitored_session.py b/tensorflow/python/training/monitored_session.py index 0f1fde8eb32..eb96f04fb33 100644 --- a/tensorflow/python/training/monitored_session.py +++ b/tensorflow/python/training/monitored_session.py @@ -22,12 +22,12 @@ from __future__ import print_function import abc from tensorflow.core.protobuf import saver_pb2 +from tensorflow.python import summary from tensorflow.python.framework import errors from tensorflow.python.framework import ops from tensorflow.python.ops import array_ops from tensorflow.python.ops import control_flow_ops from tensorflow.python.ops import data_flow_ops -from tensorflow.python.ops import logging_ops from tensorflow.python.ops import resources from tensorflow.python.ops import variables from tensorflow.python.training import basic_session_run_hooks @@ -148,9 +148,9 @@ class Scaffold(object): 'local_init_op', ops.GraphKeys.LOCAL_INIT_OP, Scaffold._default_local_init_op) if self._summary_op is None: - self._summary_op = Scaffold.get_or_default( - 'summary_op', ops.GraphKeys.SUMMARY_OP, - logging_ops.merge_all_summaries) + self._summary_op = Scaffold.get_or_default('summary_op', + ops.GraphKeys.SUMMARY_OP, + summary.merge_all) # pylint: disable=g-long-lambda if self._saver is None: self._saver = Scaffold.get_or_default( diff --git a/tensorflow/python/training/monitored_session_test.py b/tensorflow/python/training/monitored_session_test.py index 33b56431eb8..16245dcd10c 100644 --- a/tensorflow/python/training/monitored_session_test.py +++ b/tensorflow/python/training/monitored_session_test.py @@ -162,7 +162,7 @@ class MonitoredTrainingSessionTest(tf.test.TestCase): with tf.Graph().as_default(): gstep = tf.contrib.framework.get_or_create_global_step() new_gstep = tf.assign_add(gstep, 1) - tf.scalar_summary('my_summary_tag', new_gstep * 2) + tf.summary.scalar('my_summary_tag', new_gstep * 2) with tf.train.MonitoredTrainingSession( is_chief=True, checkpoint_dir=logdir) as session: for _ in range(101): # 100 is default summary writing steps diff --git a/tensorflow/python/training/saver_test.py b/tensorflow/python/training/saver_test.py index 0fc81d71f53..987b7164d65 100644 --- a/tensorflow/python/training/saver_test.py +++ b/tensorflow/python/training/saver_test.py @@ -1504,7 +1504,7 @@ class MetaGraphTest(tf.test.TestCase): name="xentropy") loss = tf.reduce_mean(cross_entropy, name="xentropy_mean") - tf.scalar_summary(loss.op.name, loss) + tf.summary.scalar("loss", loss) # Creates the gradient descent optimizer with the given learning rate. optimizer = tf.train.GradientDescentOptimizer(0.01) diff --git a/tensorflow/python/training/supervisor.py b/tensorflow/python/training/supervisor.py index a233fa50372..8c56907dff5 100644 --- a/tensorflow/python/training/supervisor.py +++ b/tensorflow/python/training/supervisor.py @@ -23,12 +23,12 @@ import time from tensorflow.core.framework.summary_pb2 import Summary from tensorflow.core.util.event_pb2 import SessionLog +from tensorflow.python import summary as _summary from tensorflow.python.framework import dtypes from tensorflow.python.framework import meta_graph from tensorflow.python.framework import ops from tensorflow.python.ops import control_flow_ops from tensorflow.python.ops import data_flow_ops -from tensorflow.python.ops import logging_ops from tensorflow.python.ops import variables from tensorflow.python.platform import tf_logging as logging from tensorflow.python.training import coordinator @@ -269,7 +269,7 @@ class Supervisor(object): The directory will be created if it does not exist. summary_op: An `Operation` that returns a Summary for the event logs. Used by chief supervisors if a `logdir` was specified. Defaults to the - operation returned from merge_all_summaries(). If `None`, summaries are + operation returned from summary.merge_all(). If `None`, summaries are not computed automatically. saver: A Saver object. Used by chief supervisors if a `logdir` was specified. Defaults to the saved returned by Saver(). @@ -471,7 +471,7 @@ class Supervisor(object): if summary_op is Supervisor.USE_DEFAULT: summary_op = self._get_first_op_from_collection(ops.GraphKeys.SUMMARY_OP) if summary_op is None: - summary_op = logging_ops.merge_all_summaries() + summary_op = _summary.merge_all() if summary_op is not None: ops.add_to_collection(ops.GraphKeys.SUMMARY_OP, summary_op) self._summary_op = summary_op diff --git a/tensorflow/python/training/supervisor_test.py b/tensorflow/python/training/supervisor_test.py index 6a668eba433..06f805d5f05 100644 --- a/tensorflow/python/training/supervisor_test.py +++ b/tensorflow/python/training/supervisor_test.py @@ -133,7 +133,10 @@ class SupervisorTest(tf.test.TestCase): def testManagedSessionDoNotKeepSummaryWriter(self): logdir = _test_dir("managed_not_keep_summary_writer") with tf.Graph().as_default(): - summ = tf.scalar_summary(["c1", "c2", "c3"], tf.constant([1.0, 2.0, 3.0])) + tf.summary.scalar("c1", tf.constant(1)) + tf.summary.scalar("c2", tf.constant(2)) + tf.summary.scalar("c3", tf.constant(3)) + summ = tf.summary.merge_all() sv = tf.train.Supervisor(logdir=logdir, summary_op=None) with sv.managed_session("", close_summary_writer=True, start_standard_services=False) as sess: @@ -182,7 +185,10 @@ class SupervisorTest(tf.test.TestCase): def testManagedSessionKeepSummaryWriter(self): logdir = _test_dir("managed_keep_summary_writer") with tf.Graph().as_default(): - summ = tf.scalar_summary(["c1", "c2", "c3"], tf.constant([1.0, 2.0, 3.0])) + tf.summary.scalar("c1", tf.constant(1)) + tf.summary.scalar("c2", tf.constant(2)) + tf.summary.scalar("c3", tf.constant(3)) + summ = tf.summary.merge_all() sv = tf.train.Supervisor(logdir=logdir) with sv.managed_session("", close_summary_writer=False, start_standard_services=False) as sess: @@ -311,7 +317,10 @@ class SupervisorTest(tf.test.TestCase): def testChiefCanWriteEvents(self): logdir = _test_dir("can_write") with tf.Graph().as_default(): - summ = tf.scalar_summary(["c1", "c2", "c3"], tf.constant([1.0, 2.0, 3.0])) + tf.summary.scalar("c1", tf.constant(1)) + tf.summary.scalar("c2", tf.constant(2)) + tf.summary.scalar("c3", tf.constant(3)) + summ = tf.summary.merge_all() sv = tf.train.Supervisor(is_chief=True, logdir=logdir, summary_op=None) meta_graph_def = meta_graph.create_meta_graph_def() sess = sv.prepare_or_wait_for_session("") @@ -361,7 +370,9 @@ class SupervisorTest(tf.test.TestCase): with tf.Graph().as_default(): sv = tf.train.Supervisor(is_chief=False) sess = sv.prepare_or_wait_for_session("") - summ = tf.scalar_summary(["c1", "c2"], tf.constant([1.0, 2.0])) + tf.summary.scalar("c1", tf.constant(1)) + tf.summary.scalar("c2", tf.constant(2)) + summ = tf.summary.merge_all() sv.summary_computed(sess, sess.run(summ)) def _start_standard_services(): @@ -375,8 +386,10 @@ class SupervisorTest(tf.test.TestCase): def testNoLogdirButWantSummary(self): with tf.Graph().as_default(): - const = tf.constant([1.0, 2.0, 3.0]) - summ = tf.scalar_summary(["c1", "c2", "c3"], const) + tf.summary.scalar("c1", tf.constant(1)) + tf.summary.scalar("c2", tf.constant(2)) + tf.summary.scalar("c3", tf.constant(3)) + summ = tf.summary.merge_all() sv = tf.train.Supervisor(logdir="", summary_op=None) sess = sv.prepare_or_wait_for_session("") with self.assertRaisesRegexp(RuntimeError, "requires a summary writer"): @@ -386,8 +399,10 @@ class SupervisorTest(tf.test.TestCase): logdir = _test_dir("explicit_no_summary_writer") with tf.Graph().as_default(): tf.Variable([1.0], name="foo") - const = tf.constant([1.0, 2.0, 3.0]) - summ = tf.scalar_summary(["c1", "c2", "c3"], const) + tf.summary.scalar("c1", tf.constant(1)) + tf.summary.scalar("c2", tf.constant(2)) + tf.summary.scalar("c3", tf.constant(3)) + summ = tf.summary.merge_all() sv = tf.train.Supervisor(logdir=logdir, summary_writer=None) sess = sv.prepare_or_wait_for_session("") # Check that a checkpoint is still be generated. @@ -399,8 +414,10 @@ class SupervisorTest(tf.test.TestCase): def testNoLogdirButExplicitSummaryWriter(self): logdir = _test_dir("explicit_summary_writer") with tf.Graph().as_default(): - const = tf.constant([1.0, 2.0, 3.0]) - summ = tf.scalar_summary(["c1", "c2", "c3"], const) + tf.summary.scalar("c1", tf.constant(1)) + tf.summary.scalar("c2", tf.constant(2)) + tf.summary.scalar("c3", tf.constant(3)) + summ = tf.summary.merge_all() sw = tf.train.SummaryWriter(logdir) sv = tf.train.Supervisor(logdir="", summary_op=None, summary_writer=sw) meta_graph_def = meta_graph.create_meta_graph_def() @@ -547,7 +564,7 @@ class SupervisorTest(tf.test.TestCase): with tf.Graph().as_default(): v = tf.Variable( 10.0, name="ready_for_local_init_op_restore_v_" + str(uid)) - tf.scalar_summary("ready_for_local_init_op_restore_v_" + str(uid), v) + tf.summary.scalar("ready_for_local_init_op_restore_v_" + str(uid), v) sv = tf.train.Supervisor(logdir=logdir) sv.prepare_or_wait_for_session(server.target) save_path = sv.save_path @@ -707,7 +724,7 @@ class SupervisorTest(tf.test.TestCase): # Create a checkpoint. with tf.Graph().as_default(): v = tf.Variable([1.0], name="foo") - tf.scalar_summary(["v"], v) + tf.summary.scalar("v", v[0]) sv = tf.train.Supervisor(logdir=logdir) meta_graph_def = meta_graph.create_meta_graph_def( saver_def=sv.saver.saver_def)