Ejemplo n.º 1
0
    def testSummaryControlFlowIfWithAutoOutsideCompilation(
            self, take_true_branch):
        strategy = get_tpu_strategy()

        @def_function.function
        def step():
            def computation(x):
                x = x + 1.0
                if x < 5:
                    scalar_summary_v2.scalar("x", x, step=0)
                    x = x * 2.0
                return x + 1.0

            if take_true_branch:
                return strategy.run(computation, args=(2.0, ))
            else:
                return strategy.run(computation, args=(10.0, ))

        logdir = tempfile.mkdtemp()
        summary_writer = summary.create_file_writer(logdir, flush_millis=10000)
        output_value = 12.
        if take_true_branch:
            output_value = 7.
        with summary_writer.as_default(), summary.always_record_summaries():
            self.assertAllEqual(
                strategy.experimental_local_results(step()),
                constant_op.constant(output_value,
                                     shape=(strategy.num_replicas_in_sync)))
        if take_true_branch:
            events = _events_from_logdir(self, logdir)
            # There will be 2 entries: 1 summary file header entry, and 1 entry
            # written by host.
            #
            self.assertLen(events, 2)
            self.assertEqual(events[1].summary.value[0].tag, "cond/x")
Ejemplo n.º 2
0
  def _test_summary_for_replica_zero_only(self, d):
    logdir = tempfile.mkdtemp()

    def run_fn():
      """Function executed for each replica."""
      with summary_writer.as_default():
        replica_id = ds_context.get_replica_context().replica_id_in_sync_group
        return summary_ops.write("a", replica_id)

    with self.cached_session() as sess, d.scope(), \
        summary_ops.always_record_summaries():
      # We need global_step because summary writing op *always* has global_step
      # as input, even when we always record summary or never record summary.
      global_step = training_util.get_or_create_global_step()
      if not context.executing_eagerly():
        # When executing eagerly, variables are initialized immediately after
        # creation, and its initializer will be None.
        global_step.initializer.run()
      summary_ops.set_step(0)
      summary_writer = summary_ops.create_file_writer(logdir)
      output = d.extended.call_for_each_replica(run_fn)
      unwrapped = d.unwrap(output)
      if not context.executing_eagerly():
        sess.run(summary_writer.init())
        sess.run(unwrapped)
        sess.run(summary_writer.close())

      events = _events_from_logdir(self, logdir)
      # There will be 2 entries: 1 summary file header entry, and 1 entry
      # written by replica 0.
      self.assertLen(events, 2)
      self.assertEqual(events[1].summary.value[0].tag, "a")
      self.assertEqual(events[1].summary.value[0].simple_value, 0.0)
Ejemplo n.º 3
0
  def _write_custom_summaries(self, step, logs=None):
    """Writes metrics out as custom scalar summaries.

    Arguments:
        step: the global step to use for TensorBoard.
        logs: dict. Keys are scalar summary names, values are
            NumPy scalars.

    """
    logs = logs or {}
    if context.executing_eagerly():
      # use v2 summary ops
      with self.writer.as_default(), summary_ops_v2.always_record_summaries():
        for name, value in logs.items():
          if isinstance(value, np.ndarray):
            value = value.item()
          summary_ops_v2.scalar(name, value, step=step)
    else:
      # use FileWriter from v1 summary
      for name, value in logs.items():
        if isinstance(value, np.ndarray):
          value = value.item()
        summary = tf_summary.Summary()
        summary_value = summary.value.add()
        summary_value.simple_value = value
        summary_value.tag = name
        self.writer.add_summary(summary, step)
    self.writer.flush()
Ejemplo n.º 4
0
    def construct(self, args):
        with self.session.graph.as_default():
            # Inputs
            self.images = tf.placeholder(tf.float32,
                                         [None, MNIST.H, MNIST.W, MNIST.C],
                                         name="images")
            self.labels = tf.placeholder(tf.int64, [None], name="labels")

            # Computation
            hidden = tf.keras.layers.Flatten()(self.images)
            # TODO: Add `args.layers` number of hidden layers with size `args.hidden_layer`,
            # using activation from `args.activation`, allowing "none", "relu", "tanh", "sigmoid".
            # Store the results back to `hidden` variable.
            output_layer = tf.keras.layers.Dense(MNIST.LABELS)(hidden)
            self.predictions = tf.argmax(output_layer, axis=1)

            # Training
            loss = tf.keras.losses.sparse_categorical_crossentropy(
                self.labels, output_layer, from_logits=True)
            global_step = tf.train.create_global_step()
            self.training = tf.train.AdamOptimizer().minimize(
                loss, global_step=global_step, name="training")

            # Summaries
            accuracy = tf.math.reduce_mean(
                tf.cast(tf.equal(self.labels, self.predictions), tf.float32))
            confusion_matrix = tf.reshape(
                tf.confusion_matrix(self.labels,
                                    self.predictions,
                                    weights=tf.not_equal(
                                        self.labels, self.predictions),
                                    dtype=tf.float32),
                [1, MNIST.LABELS, MNIST.LABELS, 1])

            summary_writer = tf_summary.create_file_writer(args.logdir,
                                                           flush_millis=10 *
                                                           1000)
            self.summaries = {}
            with summary_writer.as_default(
            ), tf_summary.record_summaries_every_n_global_steps(100):
                self.summaries["train"] = [
                    tf_summary.scalar("train/loss", loss),
                    tf_summary.scalar("train/accuracy", accuracy)
                ]
            with summary_writer.as_default(
            ), tf_summary.always_record_summaries():
                for dataset in ["dev", "test"]:
                    self.summaries[dataset] = [
                        tf_summary.scalar(dataset + "/accuracy", accuracy),
                        tf_summary.image(dataset + "/confusion_matrix",
                                         confusion_matrix)
                    ]
                    with tf.control_dependencies(self.summaries[dataset]):
                        self.summaries[dataset].append(summary_writer.flush())

            # Initialize variables
            self.session.run(tf.global_variables_initializer())
            with summary_writer.as_default():
                tf_summary.initialize(session=self.session,
                                      graph=self.session.graph)
Ejemplo n.º 5
0
    def _log_metrics(self, logs, prefix, step):
        """Writes metrics out as custom scalar summaries.

        Arguments:
            logs: Dict. Keys are scalar summary names, values are NumPy scalars.
            prefix: String. The prefix to apply to the scalar summary names.
            step: Int. The global step to use for TensorBoard.
        """
        if logs is None:
            logs = {}

        with context.eager_mode():
            with summary_ops_v2.always_record_summaries():
                train_writer = self._get_writer(self._train_run_name)
                with train_writer.as_default():
                    self._train_summaries(train_writer,
                                          logs,
                                          prefix=prefix,
                                          step=step)

                eval_writer = self._get_writer(self._validation_run_name)
                with eval_writer.as_default():
                    self._eval_summaries(eval_writer,
                                         logs,
                                         prefix=prefix,
                                         step=step)
Ejemplo n.º 6
0
 def write_weights(self, mode: str, models: Iterable[Model], step: int,
                   visualize: bool) -> None:
     # Similar to TF implementation, but multiple models
     with self.tf_summary_writers[mode].as_default(
     ), summary_ops_v2.always_record_summaries():
         for model in models:
             for layer in model.layers:
                 for weight in layer.weights:
                     weight_name = weight.name.replace(':', '_')
                     weight_name = "{}_{}".format(model.model_name,
                                                  weight_name)
                     with tfops.init_scope():
                         weight = backend.get_value(weight)
                     summary_ops_v2.histogram(weight_name,
                                              weight,
                                              step=step)
                     if visualize:
                         weight = self._weight_to_image(
                             weight=weight, kernel_channels_last=True)
                         if weight is not None:
                             summary_ops_v2.image(
                                 weight_name,
                                 weight,
                                 step=step,
                                 max_images=weight.shape[0])
 def testWriterInitAndClose(self):
   logdir = self.get_temp_dir()
   get_total = lambda: len(summary_test_util.events_from_logdir(logdir))
   with summary_ops.always_record_summaries():
     writer = summary_ops.create_file_writer(
         logdir, max_queue=100, flush_millis=1000000)
     self.assertEqual(1, get_total())  # file_version Event
     # Calling init() again while writer is open has no effect
     writer.init()
     self.assertEqual(1, get_total())
     try:
       # Not using .as_default() to avoid implicit flush when exiting
       writer.set_as_default()
       summary_ops.scalar('one', 1.0, step=1)
       self.assertEqual(1, get_total())
       # Calling .close() should do an implicit flush
       writer.close()
       self.assertEqual(2, get_total())
       # Calling init() on a closed writer should start a new file
       time.sleep(1.1)  # Ensure filename has a different timestamp
       writer.init()
       files = sorted(gfile.Glob(os.path.join(logdir, '*tfevents*')))
       self.assertEqual(2, len(files))
       get_total = lambda: len(summary_test_util.events_from_file(files[1]))
       self.assertEqual(1, get_total())  # file_version Event
       summary_ops.scalar('two', 2.0, step=2)
       writer.close()
       self.assertEqual(2, get_total())
     finally:
       # Clean up by resetting default writer
       summary_ops.create_file_writer(None).set_as_default()
Ejemplo n.º 8
0
    def _write_custom_summaries(self, step, logs=None):
        """Writes metrics out as custom scalar summaries.

    Arguments:
        step: the global step to use for TensorBoard.
        logs: dict. Keys are scalar summary names, values are
            NumPy scalars.

    """
        logs = logs or {}
        if context.executing_eagerly():
            # use v2 summary ops
            with self.writer.as_default(
            ), summary_ops_v2.always_record_summaries():
                for name, value in logs.items():
                    if isinstance(value, np.ndarray):
                        value = value.item()
                    summary_ops_v2.scalar(name, value, step=step)
        else:
            # use FileWriter from v1 summary
            for name, value in logs.items():
                if isinstance(value, np.ndarray):
                    value = value.item()
                summary = tf_summary.Summary()
                summary_value = summary.value.add()
                summary_value.simple_value = value
                summary_value.tag = name
                self.writer.add_summary(summary, step)
        self.writer.flush()
Ejemplo n.º 9
0
    def start(self):
        """Starts the evaluation loop."""
        optimizer_checkpoint = tracking_util.Checkpoint(iter=self._iterations)
        checkpoint = tracking_util.Checkpoint(model=self.model,
                                              optimizer=optimizer_checkpoint)

        for latest_checkpoint in checkpoint_utils.checkpoints_iterator(
                self.checkpoint_dir):
            try:
                # `expect_partial` because the checkpoint can have other `Trackable`s
                # such as `optimizer`.
                checkpoint.restore(latest_checkpoint).expect_partial()
            except (errors_impl.OpError, ) as e:
                # A couple errors can happen here with the coordinator racing to write
                # checkpoint:
                # 1) OpError: open failed for <file path>: No such file or directory
                # 2) NotFoundError (subclass of OpError): Unsuccessful
                # TensorSliceReader constructor.
                # TODO(rchao): Remove this except block once b/150954027 is resolved.
                logging.info(
                    'SidecarEvaluator has an error loading '
                    'checkpoint: %s. Retrying. Error: %s: %s',
                    latest_checkpoint, e.__class__.__name__, e)
                continue

            if self._iterations.numpy() == _ITERATIONS_UNINITIALIZED:
                raise RuntimeError(
                    '`iterations` cannot be loaded from the '
                    'checkpoint file. Please ensure `iterations` is '
                    'tracked in the `checkpoint` saved by the coordinator.')

            logging.info(
                'Evaluation starts: Model weights loaded from latest '
                'checkpoint file: %s.', latest_checkpoint)

            # TODO(rchao): Support arbitrary callback for extensibility.
            self.model.evaluate(self.data, steps=self.steps)

            logging.info('End of evaluation. Accuracy: %r', [
                metric.result().numpy()
                for metric in self.model.compiled_metrics.metrics
            ])

            if self._summary_writer:
                with summary_ops_v2.always_record_summaries(
                ), self._summary_writer.as_default():
                    for metric in self.model.compiled_metrics.metrics:
                        summary_ops_v2.scalar(
                            metric.name,
                            metric.result(),
                            step=self._iterations.read_value())

            # TODO(rchao): Make the max evaluation robust in case users save the
            # checkpoints with epoch format {epoch:03d}.
            if (self.max_evaluations and latest_checkpoint.endswith(
                    '-{}'.format(self.max_evaluations))):
                # Exit the loop because we have evaluated the final checkpoint file.
                logging.info(
                    'Last checkpoint evaluated. SidecarEvaluator stops.')
                return
 def testFlushFunction(self):
     logdir = self.get_temp_dir()
     writer = summary_ops.create_file_writer(logdir,
                                             max_queue=999999,
                                             flush_millis=999999)
     with writer.as_default(), summary_ops.always_record_summaries():
         summary_ops.scalar('scalar', 2.0, step=1)
         flush_op = summary_ops.flush()
     with self.cached_session() as sess:
         sess.run(summary_ops.summary_writer_initializer_op())
         get_total = lambda: len(
             summary_test_util.events_from_logdir(logdir))
         # Note: First tf.Event is always file_version.
         self.assertEqual(1, get_total())
         sess.run(summary_ops.all_summary_ops())
         self.assertEqual(1, get_total())
         sess.run(flush_op)
         self.assertEqual(2, get_total())
         # Test "writer" parameter
         sess.run(summary_ops.all_summary_ops())
         sess.run(summary_ops.flush(writer=writer))
         self.assertEqual(3, get_total())
         sess.run(summary_ops.all_summary_ops())
         sess.run(summary_ops.flush(writer=writer._resource))  # pylint:disable=protected-access
         self.assertEqual(4, get_total())
Ejemplo n.º 11
0
 def testWriterInitAndClose(self):
   logdir = self.get_temp_dir()
   get_total = lambda: len(summary_test_util.events_from_logdir(logdir))
   with summary_ops.always_record_summaries():
     writer = summary_ops.create_file_writer(
         logdir, max_queue=100, flush_millis=1000000)
     self.assertEqual(1, get_total())  # file_version Event
     # Calling init() again while writer is open has no effect
     writer.init()
     self.assertEqual(1, get_total())
     try:
       # Not using .as_default() to avoid implicit flush when exiting
       writer.set_as_default()
       summary_ops.scalar('one', 1.0, step=1)
       self.assertEqual(1, get_total())
       # Calling .close() should do an implicit flush
       writer.close()
       self.assertEqual(2, get_total())
       # Calling init() on a closed writer should start a new file
       time.sleep(1.1)  # Ensure filename has a different timestamp
       writer.init()
       files = sorted(gfile.Glob(os.path.join(logdir, '*tfevents*')))
       self.assertEqual(2, len(files))
       get_total = lambda: len(summary_test_util.events_from_file(files[1]))
       self.assertEqual(1, get_total())  # file_version Event
       summary_ops.scalar('two', 2.0, step=2)
       writer.close()
       self.assertEqual(2, get_total())
     finally:
       # Clean up by resetting default writer
       summary_ops.create_file_writer(None).set_as_default()
Ejemplo n.º 12
0
 def _scatter(self, epoch, logs={}):
     x = self.scatter_x_model.predict_generator(self.eval_seq)
     y = self.scatter_y_model.predict_generator(self.eval_seq)
     # =====================================================================
     writer = self._get_writer(self._train_run_name)
     with context.eager_mode(), writer.as_default(), \
         summary_ops_v2.always_record_summaries():
         # ------------------------------------------------------------------
         for i, name in self.scatter:
             fig, ax = plt.subplots()
             ax.hist2d(x[i],
                       y[i],
                       bins=50,
                       density=True,
                       norm=colors.SymLogNorm(linthresh=0.01,
                                              linscale=2,
                                              vmin=-1.0,
                                              vmax=2.0))
             ax.set_xlabel(name[0])
             ax.set_ylabel(name[1])
             fig.canvas.draw()
             # ------------------------------------------------------------------
             plot = np.array(fig.canvas.renderer.buffer_rgba())
             plot = np.expand_dims(plot, 0)
             summary_ops_v2.image("2DHistogram", plot, step=step)
             # ------------------------------------------------------------------
             plt.close()
             writer.flush()
    def _log_metrics(self, logs, prefix, step):
        """Writes metrics out as custom scalar summaries.
        Arguments:
            logs: Dict. Keys are scalar summary names, values are NumPy scalars.
            prefix: String. The prefix to apply to the scalar summary names.
            step: Int. The global step to use for TensorBoard.
        """
        if logs is None:
            logs = {}

        # Group metrics by the name of their associated file writer. Values
        # are lists of metrics, as (name, scalar_value) pairs.
        validation_prefix = 'val_'
        logs_by_writer = []
        for (name, value) in logs.items():
            if name in ('batch', 'size', 'num_steps'):
                # Scrub non-metric items.
                continue
            name = prefix + name  # assign batch or epoch prefix
            logs_by_writer.append((name, value))

        with context.eager_mode():
            with summary_ops_v2.always_record_summaries():
                if not logs_by_writer:
                    # Don't create a "validation" events file if we don't
                    # actually have any validation data.
                    pass
                with self.writer.as_default():
                    for (name, value) in logs_by_writer:
                        summary_ops_v2.scalar(name, value, step=step)
 def testWriterInitAndClose(self):
     logdir = self.get_temp_dir()
     with summary_ops.always_record_summaries():
         writer = summary_ops.create_file_writer(logdir,
                                                 max_queue=100,
                                                 flush_millis=1000000)
         with writer.as_default():
             summary_ops.scalar('one', 1.0, step=1)
     with self.cached_session() as sess:
         sess.run(summary_ops.summary_writer_initializer_op())
         get_total = lambda: len(
             summary_test_util.events_from_logdir(logdir))
         self.assertEqual(1, get_total())  # file_version Event
         # Running init() again while writer is open has no effect
         sess.run(writer.init())
         self.assertEqual(1, get_total())
         sess.run(summary_ops.all_summary_ops())
         self.assertEqual(1, get_total())
         # Running close() should do an implicit flush
         sess.run(writer.close())
         self.assertEqual(2, get_total())
         # Running init() on a closed writer should start a new file
         time.sleep(1.1)  # Ensure filename has a different timestamp
         sess.run(writer.init())
         sess.run(summary_ops.all_summary_ops())
         sess.run(writer.close())
         files = sorted(gfile.Glob(os.path.join(logdir, '*tfevents*')))
         self.assertEqual(2, len(files))
         self.assertEqual(2,
                          len(summary_test_util.events_from_file(files[1])))
Ejemplo n.º 15
0
    def __init__(self,
                 session,
                 logdir,
                 max_queue=10,
                 flush_secs=120,
                 filename_suffix=''):
        """Creates an `EventFileWriterV2` and an event file to write to.

    On construction, this calls `tf.contrib.summary.create_file_writer` within
    the graph from `session.graph` to look up a shared summary writer resource
    for `logdir` if one exists, and create one if not. Creating the summary
    writer resource in turn creates a new event file in `logdir` to be filled
    with `Event` protocol buffers passed to `add_event`. Graph ops to control
    this writer resource are added to `session.graph` during this init call;
    stateful methods on this class will call `session.run()` on these ops.

    Note that because the underlying resource is shared, it is possible that
    other parts of the code using the same session may interact independently
    with the resource, e.g. by flushing or even closing it. It is the caller's
    responsibility to avoid any undesirable sharing in this regard.

    The remaining arguments to the constructor (`flush_secs`, `max_queue`, and
    `filename_suffix`) control the construction of the shared writer resource
    if one is created. If an existing resource is reused, these arguments have
    no effect.  See `tf.contrib.summary.create_file_writer` for details.

    Args:
      session: A `tf.Session`. Session that will hold shared writer resource.
        The writer ops will be added to session.graph during this init call.
      logdir: A string. Directory where event file will be written.
      max_queue: Integer. Size of the queue for pending events and summaries.
      flush_secs: Number. How often, in seconds, to flush the
        pending events and summaries to disk.
      filename_suffix: A string. Every event file's name is suffixed with
        `filename_suffix`.
    """
        self._session = session
        self._logdir = logdir
        self._closed = False
        if not gfile.IsDirectory(self._logdir):
            gfile.MakeDirs(self._logdir)

        with self._session.graph.as_default():
            with ops.name_scope('filewriter'):
                file_writer = summary_ops_v2.create_file_writer(
                    logdir=self._logdir,
                    max_queue=max_queue,
                    flush_millis=flush_secs * 1000,
                    filename_suffix=filename_suffix)
                with summary_ops_v2.always_record_summaries(
                ), file_writer.as_default():
                    self._event_placeholder = array_ops.placeholder_with_default(
                        constant_op.constant('unused', dtypes.string),
                        shape=[])
                    self._add_event_op = summary_ops_v2.import_event(
                        self._event_placeholder)
                self._init_op = file_writer.init()
                self._flush_op = file_writer.flush()
                self._close_op = file_writer.close()
            self._session.run(self._init_op)
Ejemplo n.º 16
0
    def test_summaries_in_tf_function(self):
        if not context.executing_eagerly():
            return

        class MyLayer(keras.layers.Layer):
            def call(self, inputs):
                summary_ops_v2.scalar('mean', math_ops.reduce_mean(inputs))
                return inputs

        tmp_dir = self.get_temp_dir()
        writer = summary_ops_v2.create_file_writer_v2(tmp_dir)
        with writer.as_default(), summary_ops_v2.always_record_summaries():
            my_layer = MyLayer()
            x = array_ops.ones((10, 10))

            def my_fn(x):
                return my_layer(x)

            _ = my_fn(x)

        event_file = gfile.Glob(os.path.join(tmp_dir, 'events*'))
        self.assertLen(event_file, 1)
        event_file = event_file[0]
        tags = set()
        for e in summary_iterator.summary_iterator(event_file):
            for val in e.summary.value:
                tags.add(val.tag)
        self.assertEqual(set(['my_layer/mean']), tags)
    def testSummaryWithAutoOutsideCompilation(self):
        strategy = get_tpu_strategy()

        def host_computation(x):
            summary.scalar("x", x, step=0)
            return x * 2.0

        @def_function.function
        def step():
            def computation(x):
                x = x + 1.0
                y = host_computation(x)
                return y + 1.0

            return strategy.run(computation, args=(2.0, ))

        logdir = tempfile.mkdtemp()
        summary_writer = summary.create_file_writer(logdir, flush_millis=10000)
        with summary_writer.as_default(), summary.always_record_summaries():
            self.assertAllEqual(
                strategy.experimental_local_results(step()),
                constant_op.constant(7.,
                                     shape=(strategy.num_replicas_in_sync)))
        events = _events_from_logdir(self, logdir)
        # There will be 2 entries: 1 summary file header entry, and 1 entry
        # written by host.
        self.assertLen(events, 2)
        self.assertEqual(events[1].summary.value[0].tag, "x")
        self.assertEqual(events[1].summary.value[0].simple_value, 3.0)
Ejemplo n.º 18
0
    def testSummaryInWhile(self):
        strategy = get_tpu_strategy()

        def host_computation(x):
            summary.scalar("x", x, step=0)
            return x * 2.0

        @def_function.function
        def step():
            def computation(x):
                n = 0
                while n < 3:
                    x = x + 1.0
                    y = tpu.outside_compilation(host_computation, x)
                    y = tpu.outside_compilation(host_computation, x)
                    x = y
                    n = n + 1
                return y + 1.0

            return strategy.run(computation, args=(2.0, ))

        summary_writer = summary.create_file_writer(os.path.join(
            os.getenv("TEST_TMPDIR", "/tmp")),
                                                    flush_millis=10000)
        with summary_writer.as_default(), summary.always_record_summaries():
            self.assertAllEqual(
                strategy.experimental_local_results(step()),
                constant_op.constant(31.,
                                     shape=(strategy.num_replicas_in_sync)))
Ejemplo n.º 19
0
    def testSummaryInCond(self, take_true_branch):
        strategy = get_tpu_strategy()

        def host_computation(x):
            summary.scalar("x", x, step=0)
            return x * 2.0

        @def_function.function
        def step(take_true_branch):
            def computation(x):
                x = x + 1.0
                if x < 5.0:
                    y = tpu.outside_compilation(host_computation, x)
                    y = tpu.outside_compilation(host_computation, x)
                    x = y
                return x + 1.0

            if take_true_branch:
                return strategy.run(computation, args=(2.0, ))
            else:
                return strategy.run(computation, args=(10.0, ))

        summary_writer = summary.create_file_writer(os.path.join(
            os.getenv("TEST_TMPDIR", "/tmp")),
                                                    flush_millis=10000)

        output_value = 12.
        if take_true_branch:
            output_value = 7.
        with summary_writer.as_default(), summary.always_record_summaries():
            self.assertAllEqual(
                strategy.experimental_local_results(step(take_true_branch)),
                constant_op.constant(output_value,
                                     shape=(strategy.num_replicas_in_sync)))
Ejemplo n.º 20
0
 def testWriterInitAndClose(self):
   logdir = self.get_temp_dir()
   with summary_ops.always_record_summaries():
     writer = summary_ops.create_file_writer(
         logdir, max_queue=100, flush_millis=1000000)
     with writer.as_default():
       summary_ops.scalar('one', 1.0, step=1)
   with self.cached_session() as sess:
     sess.run(summary_ops.summary_writer_initializer_op())
     get_total = lambda: len(summary_test_util.events_from_logdir(logdir))
     self.assertEqual(1, get_total())  # file_version Event
     # Running init() again while writer is open has no effect
     sess.run(writer.init())
     self.assertEqual(1, get_total())
     sess.run(summary_ops.all_summary_ops())
     self.assertEqual(1, get_total())
     # Running close() should do an implicit flush
     sess.run(writer.close())
     self.assertEqual(2, get_total())
     # Running init() on a closed writer should start a new file
     time.sleep(1.1)  # Ensure filename has a different timestamp
     sess.run(writer.init())
     sess.run(summary_ops.all_summary_ops())
     sess.run(writer.close())
     files = sorted(gfile.Glob(os.path.join(logdir, '*tfevents*')))
     self.assertEqual(2, len(files))
     self.assertEqual(2, len(summary_test_util.events_from_file(files[1])))
Ejemplo n.º 21
0
    def _test_summary_for_replica_zero_only(self, d):
        logdir = tempfile.mkdtemp()

        def run_fn():
            """Function executed for each replica."""
            with summary_writer.as_default():
                replica_id = ds_context.get_replica_context(
                ).replica_id_in_sync_group
                return summary_ops.write("a", replica_id)

        with self.cached_session() as sess, d.scope(), \
            summary_ops.always_record_summaries():
            # We need global_step because summary writing op *always* has global_step
            # as input, even when we always record summary or never record summary.
            global_step = training_util.get_or_create_global_step()
            if not context.executing_eagerly():
                # When executing eagerly, variables are initialized immediately after
                # creation, and its initializer will be None.
                global_step.initializer.run()
            summary_ops.set_step(0)
            summary_writer = summary_ops.create_file_writer(logdir)
            output = d.extended.call_for_each_replica(run_fn)
            unwrapped = d.unwrap(output)
            if not context.executing_eagerly():
                sess.run(summary_writer.init())
                sess.run(unwrapped)
                sess.run(summary_writer.close())

            events = _events_from_logdir(self, logdir)
            # There will be 2 entries: 1 summary file header entry, and 1 entry
            # written by replica 0.
            self.assertLen(events, 2)
            self.assertEqual(events[1].summary.value[0].tag, "a")
            self.assertEqual(events[1].summary.value[0].simple_value, 0.0)
Ejemplo n.º 22
0
    def on_epoch_end(self, epoch, logs={}):

        if epoch % self._period == 0 and self._period != 0:
            _map, average_precisions = self.evaluate_map()
            print('\n')
            for label, average_precision in average_precisions.items():
                print(self._yolo.labels[label],
                      '{:.4f}'.format(average_precision))
            print('mAP: {:.4f}'.format(_map))

            if self._save_best and self._save_name is not None and _map > self.bestMap:
                print("mAP improved from {} to {}, saving model to {}.".format(
                    self.bestMap, _map, self._save_name))
                self.bestMap = _map
                self.model.save(self._save_name)
            else:
                print("mAP did not improve from {}.".format(self.bestMap))

            if self._tensorboard is not None:
                with summary_ops_v2.always_record_summaries():
                    with self._tensorboard._val_writer.as_default():
                        name = "mAP"  # Remove 'val_' prefix.
                        summary_ops_v2.scalar('epoch_' + name,
                                              _map,
                                              step=epoch)
Ejemplo n.º 23
0
    def _log_epoch_metrics(self, epoch, logs):
        """Writes epoch metrics out as scalar summaries.

        Arguments:
            epoch: Int. The global step to use for TensorBoard.
            logs: Dict. Keys are scalar summary names, values are scalars.
        """
        if not logs:
            return

        train_logs = {
            k: v
            for k, v in logs.items() if not k.startswith('val_')
        }
        val_logs = {k: v for k, v in logs.items() if k.startswith('val_')}
        train_logs = self._collect_learning_rate(train_logs)

        with summary_ops_v2.always_record_summaries():
            if train_logs:
                with self._train_writer.as_default():
                    for name, value in train_logs.items():
                        summary_ops_v2.scalar(name, value, step=epoch)
            if val_logs:
                with self._val_writer.as_default():
                    for name, value in val_logs.items():
                        name = name[4:]  # Remove 'val_' prefix.
                        summary_ops_v2.scalar(name, value, step=epoch)
Ejemplo n.º 24
0
 def save_trace(self):
   """Saves tensorboard profile to event file."""
   step = self.current_step()
   with self.writer.as_default(), summary_ops_v2.always_record_summaries():
     tf.summary.trace_export(
         name='profile_batch', step=step, profiler_outdir=self.full_write_dir)
   self.is_tracing = False
Ejemplo n.º 25
0
  def testIntegerSummaries(self):
    step = training_util.create_global_step()
    writer = self.create_db_writer()

    def adder(x, y):
      state_ops.assign_add(step, 1)
      summary_ops.generic('x', x)
      summary_ops.generic('y', y)
      sum_ = x + y
      summary_ops.generic('sum', sum_)
      return sum_

    with summary_ops.always_record_summaries():
      with writer.as_default():
        self.assertEqual(5, adder(int64(2), int64(3)).numpy())

    six.assertCountEqual(
        self, [1, 1, 1],
        get_all(self.db, 'SELECT step FROM Tensors WHERE dtype IS NOT NULL'))
    six.assertCountEqual(self, ['x', 'y', 'sum'],
                         get_all(self.db, 'SELECT tag_name FROM Tags'))
    x_id = get_one(self.db, 'SELECT tag_id FROM Tags WHERE tag_name = "x"')
    y_id = get_one(self.db, 'SELECT tag_id FROM Tags WHERE tag_name = "y"')
    sum_id = get_one(self.db, 'SELECT tag_id FROM Tags WHERE tag_name = "sum"')

    with summary_ops.always_record_summaries():
      with writer.as_default():
        self.assertEqual(9, adder(int64(4), int64(5)).numpy())

    six.assertCountEqual(
        self, [1, 1, 1, 2, 2, 2],
        get_all(self.db, 'SELECT step FROM Tensors WHERE dtype IS NOT NULL'))
    six.assertCountEqual(self, [x_id, y_id, sum_id],
                         get_all(self.db, 'SELECT tag_id FROM Tags'))
    self.assertEqual(2, get_tensor(self.db, x_id, 1))
    self.assertEqual(3, get_tensor(self.db, y_id, 1))
    self.assertEqual(5, get_tensor(self.db, sum_id, 1))
    self.assertEqual(4, get_tensor(self.db, x_id, 2))
    self.assertEqual(5, get_tensor(self.db, y_id, 2))
    self.assertEqual(9, get_tensor(self.db, sum_id, 2))
    six.assertCountEqual(
        self, ['experiment'],
        get_all(self.db, 'SELECT experiment_name FROM Experiments'))
    six.assertCountEqual(self, ['run'],
                         get_all(self.db, 'SELECT run_name FROM Runs'))
    six.assertCountEqual(self, ['user'],
                         get_all(self.db, 'SELECT user_name FROM Users'))
  def testIntegerSummaries(self):
    step = training_util.create_global_step()
    writer = self.create_db_writer()

    def adder(x, y):
      state_ops.assign_add(step, 1)
      summary_ops.generic('x', x)
      summary_ops.generic('y', y)
      sum_ = x + y
      summary_ops.generic('sum', sum_)
      return sum_

    with summary_ops.always_record_summaries():
      with writer.as_default():
        self.assertEqual(5, adder(int64(2), int64(3)).numpy())

    six.assertCountEqual(
        self, [1, 1, 1],
        get_all(self.db, 'SELECT step FROM Tensors WHERE dtype IS NOT NULL'))
    six.assertCountEqual(self, ['x', 'y', 'sum'],
                         get_all(self.db, 'SELECT tag_name FROM Tags'))
    x_id = get_one(self.db, 'SELECT tag_id FROM Tags WHERE tag_name = "x"')
    y_id = get_one(self.db, 'SELECT tag_id FROM Tags WHERE tag_name = "y"')
    sum_id = get_one(self.db, 'SELECT tag_id FROM Tags WHERE tag_name = "sum"')

    with summary_ops.always_record_summaries():
      with writer.as_default():
        self.assertEqual(9, adder(int64(4), int64(5)).numpy())

    six.assertCountEqual(
        self, [1, 1, 1, 2, 2, 2],
        get_all(self.db, 'SELECT step FROM Tensors WHERE dtype IS NOT NULL'))
    six.assertCountEqual(self, [x_id, y_id, sum_id],
                         get_all(self.db, 'SELECT tag_id FROM Tags'))
    self.assertEqual(2, get_tensor(self.db, x_id, 1))
    self.assertEqual(3, get_tensor(self.db, y_id, 1))
    self.assertEqual(5, get_tensor(self.db, sum_id, 1))
    self.assertEqual(4, get_tensor(self.db, x_id, 2))
    self.assertEqual(5, get_tensor(self.db, y_id, 2))
    self.assertEqual(9, get_tensor(self.db, sum_id, 2))
    six.assertCountEqual(
        self, ['experiment'],
        get_all(self.db, 'SELECT experiment_name FROM Experiments'))
    six.assertCountEqual(self, ['run'],
                         get_all(self.db, 'SELECT run_name FROM Runs'))
    six.assertCountEqual(self, ['user'],
                         get_all(self.db, 'SELECT user_name FROM Users'))
Ejemplo n.º 27
0
    def _log_raw_confusion_matrix(self, logs, prefix, step):
        """
        Logs the confusion matrix values as raw text.

        Arguments:
            logs: Training log dictionary with metric nams as keys, <dict>.
            prefix: The prefix to apply to the summary names, <str>.
            step: The global step to use for TensorBoard, <int>.
        """
        if logs is None:
            logs = {}

        # Group metrics by the name of their associated file writer. Values
        # are lists of metrics, as (name, scalar_value) pairs.
        logs_by_writer = {
            self._train_run_name: [],
            self._validation_run_name: [],
        }

        # Get confusion matrix values
        for (name, value) in logs.items():
            if name.endswith('confusion_matrix'):
                # Assign writer
                if name.startswith(self._validation_prefix):
                    name = name[len(self._validation_prefix):]
                    writer_name = self._validation_run_name
                else:
                    writer_name = self._train_run_name

                # Add prefix and suffix
                name = prefix + name + '_values'

                # Convert to text
                value = tf.identity(value)
                value = tf.expand_dims(tf.keras.backend.flatten(value), axis=0)
                value = tf.strings.as_string(
                    value,
                    precision=tf.keras.backend.epsilon(),
                    scientific=False)

                # Add to writer list
                logs_by_writer[writer_name].append((name, value))

        # Iterate over writers (train, val)
        with context.eager_mode():
            with summary_ops_v2.always_record_summaries():
                for writer_name in logs_by_writer:
                    these_logs = logs_by_writer[writer_name]
                    if not these_logs:
                        # Skip if empts (no validation metric)
                        continue

                    # Write logs
                    writer = self._get_writer(writer_name)
                    with writer.as_default():
                        for (name, value) in these_logs:
                            tf.summary.text(name, value, step=step)

                    writer.flush()
Ejemplo n.º 28
0
 def _log_trace(self, global_batch_idx):
     with self.summary_writers['train'].as_default(
     ), summary_ops_v2.always_record_summaries():
         summary_ops_v2.trace_export(
             name='batch_{}'.format(global_batch_idx),
             step=global_batch_idx,
             profiler_outdir=self.train_log_dir)
     self.is_tracing = False
Ejemplo n.º 29
0
    def graph(self, model):
        from tensorflow.python.ops import summary_ops_v2
        from tensorflow.python.keras import backend as K

        with self.train_writer.as_default():
            with summary_ops_v2.always_record_summaries():
                if not model.run_eagerly:
                    summary_ops_v2.graph(K.get_graph(), step=0)
Ejemplo n.º 30
0
 def write_epoch_models(self, mode: str) -> None:
     with self.tf_summary_writers[mode].as_default(), summary_ops_v2.always_record_summaries():
         summary_ops_v2.graph(backend.get_graph(), step=0)
         for model in self.network.epoch_models:
             summary_writable = (model.__class__.__name__ == 'Sequential'
                                 or (hasattr(model, '_is_graph_network') and model._is_graph_network))
             if summary_writable:
                 summary_ops_v2.keras_model(model.model_name, model, step=0)
Ejemplo n.º 31
0
 def make_summary(self, step, tensor, type):
     with context.eager_mode(), self.writer.as_default(
     ), summary_ops_v2.always_record_summaries():
         for i in range(self.n_channels):
             summary_ops_v2.image("%s image dim %d" % (type, i),
                                  tensor[:, :, :, i, tf.newaxis],
                                  max_images=3,
                                  step=step)
Ejemplo n.º 32
0
 def f(tag_prefix):
   with summary_ops.create_file_writer(logdir).as_default():
     default_output = summary_ops.write(tag_prefix + '_default', 1, step=0)
     with summary_ops.always_record_summaries():
       on_output = summary_ops.write(tag_prefix + '_on', 1, step=0)
     with summary_ops.never_record_summaries():
       off_output = summary_ops.write(tag_prefix + '_off', 1, step=0)
     return [default_output, on_output, off_output]
 def _custom_step(features, labels):
     del labels
     logits = model(features)
     with summary_ops_v2.always_record_summaries(
     ), writer.as_default():
         summary_ops_v2.scalar('logits',
                               logits,
                               step=model.optimizer.iterations)
     return logits
Ejemplo n.º 34
0
 def testGraphSummary(self):
   training_util.get_or_create_global_step()
   name = 'hi'
   graph = graph_pb2.GraphDef(node=(node_def_pb2.NodeDef(name=name),))
   with summary_ops.always_record_summaries():
     with self.create_db_writer().as_default():
       summary_ops.graph(graph)
   six.assertCountEqual(self, [name],
                        get_all(self.db, 'SELECT node_name FROM Nodes'))
 def testGraphSummary(self):
   training_util.get_or_create_global_step()
   name = 'hi'
   graph = graph_pb2.GraphDef(node=(node_def_pb2.NodeDef(name=name),))
   with summary_ops.always_record_summaries():
     with self.create_db_writer().as_default():
       summary_ops.graph(graph)
   six.assertCountEqual(self, [name],
                        get_all(self.db, 'SELECT node_name FROM Nodes'))
Ejemplo n.º 36
0
    def _log_confusion_matrix(self, logs, prefix, step):
        """
        Logs the confusion matrix as image.

        Arguments:
            logs: Training log dictionary with metric nams as keys, <dict>.
            prefix: The prefix to apply to the summary names, <str>.
            step: The global step to use for TensorBoard, <int>.
        """
        if logs is None:
            logs = {}

        # Group metrics by the name of their associated file writer. Values
        # are lists of metrics, as (name, scalar_value) pairs.
        logs_by_writer = {
            self._train_run_name: [],
            self._validation_run_name: [],
        }

        # Get confusion matrix values
        for (name, value) in logs.items():
            if name.endswith('confusion_matrix'):
                # Assign writer
                if name.startswith(self._validation_prefix):
                    name = name[len(self._validation_prefix):]
                    writer_name = self._validation_run_name
                else:
                    writer_name = self._train_run_name

                # Add prefix
                name = prefix + name

                # Plot confusion matrix and decode figure
                value = tf.identity(value)
                value = plot_confusion_matrix(value,
                                              class_names=self.class_names,
                                              norm=True)
                value = decode_figure(value)

                # Add to writer list
                logs_by_writer[writer_name].append((name, value))

        # Iterate over writers (train, val)
        with context.eager_mode():
            with summary_ops_v2.always_record_summaries():
                for writer_name in logs_by_writer:
                    these_logs = logs_by_writer[writer_name]
                    if not these_logs:
                        # Skip if empts (no validation metric)
                        continue

                    # Write logs
                    writer = self._get_writer(writer_name)
                    with writer.as_default():
                        for (name, value) in these_logs:
                            summary_ops_v2.image(name, value, step=step)
Ejemplo n.º 37
0
  def __init__(self, session, logdir, max_queue=10, flush_secs=120,
               filename_suffix=''):
    """Creates an `EventFileWriterV2` and an event file to write to.

    On construction, this calls `tf.contrib.summary.create_file_writer` within
    the graph from `session.graph` to look up a shared summary writer resource
    for `logdir` if one exists, and create one if not. Creating the summary
    writer resource in turn creates a new event file in `logdir` to be filled
    with `Event` protocol buffers passed to `add_event`. Graph ops to control
    this writer resource are added to `session.graph` during this init call;
    stateful methods on this class will call `session.run()` on these ops.

    Note that because the underlying resource is shared, it is possible that
    other parts of the code using the same session may interact independently
    with the resource, e.g. by flushing or even closing it. It is the caller's
    responsibility to avoid any undesirable sharing in this regard.

    The remaining arguments to the constructor (`flush_secs`, `max_queue`, and
    `filename_suffix`) control the construction of the shared writer resource
    if one is created. If an existing resource is reused, these arguments have
    no effect.  See `tf.contrib.summary.create_file_writer` for details.

    Args:
      session: A `tf.compat.v1.Session`. Session that will hold shared writer
        resource. The writer ops will be added to session.graph during this
        init call.
      logdir: A string. Directory where event file will be written.
      max_queue: Integer. Size of the queue for pending events and summaries.
      flush_secs: Number. How often, in seconds, to flush the
        pending events and summaries to disk.
      filename_suffix: A string. Every event file's name is suffixed with
        `filename_suffix`.
    """
    self._session = session
    self._logdir = logdir
    self._closed = False
    if not gfile.IsDirectory(self._logdir):
      gfile.MakeDirs(self._logdir)

    with self._session.graph.as_default():
      with ops.name_scope('filewriter'):
        file_writer = summary_ops_v2.create_file_writer(
            logdir=self._logdir,
            max_queue=max_queue,
            flush_millis=flush_secs * 1000,
            filename_suffix=filename_suffix)
        with summary_ops_v2.always_record_summaries(), file_writer.as_default():
          self._event_placeholder = array_ops.placeholder_with_default(
              constant_op.constant('unused', dtypes.string),
              shape=[])
          self._add_event_op = summary_ops_v2.import_event(
              self._event_placeholder)
        self._init_op = file_writer.init()
        self._flush_op = file_writer.flush()
        self._close_op = file_writer.close()
      self._session.run(self._init_op)
Ejemplo n.º 38
0
 def testSummaryName(self):
   logdir = self.get_temp_dir()
   writer = summary_ops.create_file_writer(logdir, max_queue=0)
   with writer.as_default(), summary_ops.always_record_summaries():
     summary_ops.scalar('scalar', 2.0, step=1)
   with self.cached_session() as sess:
     sess.run(summary_ops.summary_writer_initializer_op())
     sess.run(summary_ops.all_summary_ops())
   events = summary_test_util.events_from_logdir(logdir)
   self.assertEqual(2, len(events))
   self.assertEqual('scalar', events[1].summary.value[0].tag)
Ejemplo n.º 39
0
 def testEagerMemory(self):
   training_util.get_or_create_global_step()
   logdir = self.get_temp_dir()
   with summary_ops.create_file_writer(
       logdir, max_queue=0,
       name='t0').as_default(), summary_ops.always_record_summaries():
     summary_ops.generic('tensor', 1, '')
     summary_ops.scalar('scalar', 2.0)
     summary_ops.histogram('histogram', [1.0])
     summary_ops.image('image', [[[[1.0]]]])
     summary_ops.audio('audio', [[1.0]], 1.0, 1)
Ejemplo n.º 40
0
  def testSummaryGlobalStep(self):
    step = training_util.get_or_create_global_step()
    logdir = tempfile.mkdtemp()
    with summary_ops.create_file_writer(
        logdir, max_queue=0,
        name='t2').as_default(), summary_ops.always_record_summaries():

      summary_ops.scalar('scalar', 2.0, step=step)

      events = summary_test_util.events_from_logdir(logdir)
      self.assertEqual(len(events), 2)
      self.assertEqual(events[1].summary.value[0].tag, 'scalar')
Ejemplo n.º 41
0
  def testWriteSummaries(self):
    m = metrics.Mean()
    m([1, 10, 100])
    training_util.get_or_create_global_step()
    logdir = tempfile.mkdtemp()
    with summary_ops.create_file_writer(
        logdir, max_queue=0,
        name="t0").as_default(), summary_ops.always_record_summaries():
      m.result()  # As a side-effect will write summaries.

    events = summary_test_util.events_from_logdir(logdir)
    self.assertEqual(len(events), 2)
    self.assertEqual(events[1].summary.value[0].simple_value, 37.0)
Ejemplo n.º 42
0
 def testMaxQueue(self):
   logs = tempfile.mkdtemp()
   with summary_ops.create_file_writer(
       logs, max_queue=1, flush_millis=999999,
       name='lol').as_default(), summary_ops.always_record_summaries():
     get_total = lambda: len(summary_test_util.events_from_logdir(logs))
     # Note: First tf.Event is always file_version.
     self.assertEqual(1, get_total())
     summary_ops.scalar('scalar', 2.0, step=1)
     self.assertEqual(1, get_total())
     # Should flush after second summary since max_queue = 1
     summary_ops.scalar('scalar', 2.0, step=2)
     self.assertEqual(3, get_total())
Ejemplo n.º 43
0
 def testSummaryGlobalStep(self):
   training_util.get_or_create_global_step()
   logdir = self.get_temp_dir()
   writer = summary_ops.create_file_writer(logdir, max_queue=0)
   with writer.as_default(), summary_ops.always_record_summaries():
     summary_ops.scalar('scalar', 2.0)
   with self.cached_session() as sess:
     sess.run(variables.global_variables_initializer())
     sess.run(summary_ops.summary_writer_initializer_op())
     step, _ = sess.run(
         [training_util.get_global_step(), summary_ops.all_summary_ops()])
   events = summary_test_util.events_from_logdir(logdir)
   self.assertEqual(2, len(events))
   self.assertEqual(step, events[1].step)
Ejemplo n.º 44
0
 def testSummaryOps(self):
   training_util.get_or_create_global_step()
   logdir = tempfile.mkdtemp()
   with summary_ops.create_file_writer(
       logdir, max_queue=0,
       name='t0').as_default(), summary_ops.always_record_summaries():
     summary_ops.generic('tensor', 1, '')
     summary_ops.scalar('scalar', 2.0)
     summary_ops.histogram('histogram', [1.0])
     summary_ops.image('image', [[[[1.0]]]])
     summary_ops.audio('audio', [[1.0]], 1.0, 1)
     # The working condition of the ops is tested in the C++ test so we just
     # test here that we're calling them correctly.
     self.assertTrue(gfile.Exists(logdir))
Ejemplo n.º 45
0
 def testDbURIOpen(self):
   tmpdb_path = os.path.join(self.get_temp_dir(), 'tmpDbURITest.sqlite')
   tmpdb_uri = six.moves.urllib_parse.urljoin("file:", tmpdb_path)
   tmpdb_writer = summary_ops.create_db_writer(
       tmpdb_uri,
       "experimentA",
       "run1",
       "user1")
   with summary_ops.always_record_summaries():
     with tmpdb_writer.as_default():
       summary_ops.scalar('t1', 2.0)
   tmpdb = sqlite3.connect(tmpdb_path)
   num = get_one(tmpdb, 'SELECT count(*) FROM Tags WHERE tag_name = "t1"')
   self.assertEqual(num, 1)
   tmpdb.close()
Ejemplo n.º 46
0
 def testWriterFlush(self):
   logdir = self.get_temp_dir()
   get_total = lambda: len(summary_test_util.events_from_logdir(logdir))
   with summary_ops.always_record_summaries():
     writer = summary_ops.create_file_writer(
         logdir, max_queue=100, flush_millis=1000000)
     self.assertEqual(1, get_total())  # file_version Event
     with writer.as_default():
       summary_ops.scalar('one', 1.0, step=1)
       self.assertEqual(1, get_total())
       writer.flush()
       self.assertEqual(2, get_total())
       summary_ops.scalar('two', 2.0, step=2)
     # Exiting the "as_default()" should do an implicit flush of the "two" tag
     self.assertEqual(3, get_total())
Ejemplo n.º 47
0
 def testSummaryOps(self):
   logdir = self.get_temp_dir()
   writer = summary_ops.create_file_writer(logdir, max_queue=0)
   with writer.as_default(), summary_ops.always_record_summaries():
     summary_ops.generic('tensor', 1, step=1)
     summary_ops.scalar('scalar', 2.0, step=1)
     summary_ops.histogram('histogram', [1.0], step=1)
     summary_ops.image('image', [[[[1.0]]]], step=1)
     summary_ops.audio('audio', [[1.0]], 1.0, 1, step=1)
   with self.cached_session() as sess:
     sess.run(summary_ops.summary_writer_initializer_op())
     sess.run(summary_ops.all_summary_ops())
   # The working condition of the ops is tested in the C++ test so we just
   # test here that we're calling them correctly.
   self.assertTrue(gfile.Exists(logdir))
Ejemplo n.º 48
0
  def testDefunSummarys(self):
    training_util.get_or_create_global_step()
    logdir = tempfile.mkdtemp()
    with summary_ops.create_file_writer(
        logdir, max_queue=0,
        name='t1').as_default(), summary_ops.always_record_summaries():

      @function.defun
      def write():
        summary_ops.scalar('scalar', 2.0)

      write()
      events = summary_test_util.events_from_logdir(logdir)
      self.assertEqual(len(events), 2)
      self.assertEqual(events[1].summary.value[0].simple_value, 2.0)
Ejemplo n.º 49
0
 def testWriterFlush(self):
   logdir = self.get_temp_dir()
   with summary_ops.always_record_summaries():
     writer = summary_ops.create_file_writer(
         logdir, max_queue=100, flush_millis=1000000)
     with writer.as_default():
       summary_ops.scalar('one', 1.0, step=1)
   with self.cached_session() as sess:
     sess.run(summary_ops.summary_writer_initializer_op())
     get_total = lambda: len(summary_test_util.events_from_logdir(logdir))
     self.assertEqual(1, get_total())  # file_version Event
     sess.run(summary_ops.all_summary_ops())
     self.assertEqual(1, get_total())
     sess.run(writer.flush())
     self.assertEqual(2, get_total())
Ejemplo n.º 50
0
 def testMaxQueue(self):
   logdir = self.get_temp_dir()
   writer = summary_ops.create_file_writer(
       logdir, max_queue=1, flush_millis=999999)
   with writer.as_default(), summary_ops.always_record_summaries():
     summary_ops.scalar('scalar', 2.0, step=1)
   with self.cached_session() as sess:
     sess.run(summary_ops.summary_writer_initializer_op())
     get_total = lambda: len(summary_test_util.events_from_logdir(logdir))
     # Note: First tf.Event is always file_version.
     self.assertEqual(1, get_total())
     sess.run(summary_ops.all_summary_ops())
     self.assertEqual(1, get_total())
     # Should flush after second summary since max_queue = 1
     sess.run(summary_ops.all_summary_ops())
     self.assertEqual(3, get_total())
Ejemplo n.º 51
0
  def testSharedName(self):
    logdir = self.get_temp_dir()
    with summary_ops.always_record_summaries():
      # Create with default shared name (should match logdir)
      writer1 = summary_ops.create_file_writer(logdir)
      with writer1.as_default():
        summary_ops.scalar('one', 1.0, step=1)
      # Create with explicit logdir shared name (should be same resource/file)
      shared_name = 'logdir:' + logdir
      writer2 = summary_ops.create_file_writer(logdir, name=shared_name)
      with writer2.as_default():
        summary_ops.scalar('two', 2.0, step=2)
      # Create with different shared name (should be separate resource/file)
      writer3 = summary_ops.create_file_writer(logdir, name='other')
      with writer3.as_default():
        summary_ops.scalar('three', 3.0, step=3)

    with self.cached_session() as sess:
      # Run init ops across writers sequentially to avoid race condition.
      # TODO(nickfelt): fix race condition in resource manager lookup or create
      sess.run(writer1.init())
      sess.run(writer2.init())
      time.sleep(1.1)  # Ensure filename has a different timestamp
      sess.run(writer3.init())
      sess.run(summary_ops.all_summary_ops())
      sess.run([writer1.flush(), writer2.flush(), writer3.flush()])

    event_files = iter(sorted(gfile.Glob(os.path.join(logdir, '*tfevents*'))))

    # First file has tags "one" and "two"
    events = summary_test_util.events_from_file(next(event_files))
    self.assertEqual('brain.Event:2', events[0].file_version)
    tags = [e.summary.value[0].tag for e in events[1:]]
    self.assertItemsEqual(['one', 'two'], tags)

    # Second file has tag "three"
    events = summary_test_util.events_from_file(next(event_files))
    self.assertEqual('brain.Event:2', events[0].file_version)
    tags = [e.summary.value[0].tag for e in events[1:]]
    self.assertItemsEqual(['three'], tags)

    # No more files
    self.assertRaises(StopIteration, lambda: next(event_files))
Ejemplo n.º 52
0
 def testFlushFunction(self):
   logs = tempfile.mkdtemp()
   writer = summary_ops.create_file_writer(
       logs, max_queue=999999, flush_millis=999999, name='lol')
   with writer.as_default(), summary_ops.always_record_summaries():
     get_total = lambda: len(summary_test_util.events_from_logdir(logs))
     # Note: First tf.Event is always file_version.
     self.assertEqual(1, get_total())
     summary_ops.scalar('scalar', 2.0, step=1)
     summary_ops.scalar('scalar', 2.0, step=2)
     self.assertEqual(1, get_total())
     summary_ops.flush()
     self.assertEqual(3, get_total())
     # Test "writer" parameter
     summary_ops.scalar('scalar', 2.0, step=3)
     summary_ops.flush(writer=writer)
     self.assertEqual(4, get_total())
     summary_ops.scalar('scalar', 2.0, step=4)
     summary_ops.flush(writer=writer._resource)  # pylint:disable=protected-access
     self.assertEqual(5, get_total())
Ejemplo n.º 53
0
  def testSummaryGraphModeCond(self):
    with ops.Graph().as_default(), self.cached_session():
      training_util.get_or_create_global_step()
      logdir = tempfile.mkdtemp()
      with summary_ops.create_file_writer(
          logdir, max_queue=0,
          name='t2').as_default(), summary_ops.always_record_summaries():
        summary_ops.initialize()
        training_util.get_or_create_global_step().initializer.run()
        def f():
          summary_ops.scalar('scalar', 2.0)
          return constant_op.constant(True)
        pred = array_ops.placeholder(dtypes.bool)
        x = control_flow_ops.cond(pred, f,
                                  lambda: constant_op.constant(False))
        x.eval(feed_dict={pred: True})

      events = summary_test_util.events_from_logdir(logdir)
      self.assertEqual(len(events), 2)
      self.assertEqual(events[1].summary.value[0].tag, 'cond/scalar')
Ejemplo n.º 54
0
  def testSharedName(self):
    logdir = self.get_temp_dir()
    with summary_ops.always_record_summaries():
      # Create with default shared name (should match logdir)
      writer1 = summary_ops.create_file_writer(logdir)
      with writer1.as_default():
        summary_ops.scalar('one', 1.0, step=1)
        summary_ops.flush()
      # Create with explicit logdir shared name (should be same resource/file)
      shared_name = 'logdir:' + logdir
      writer2 = summary_ops.create_file_writer(logdir, name=shared_name)
      with writer2.as_default():
        summary_ops.scalar('two', 2.0, step=2)
        summary_ops.flush()
      # Create with different shared name (should be separate resource/file)
      time.sleep(1.1)  # Ensure filename has a different timestamp
      writer3 = summary_ops.create_file_writer(logdir, name='other')
      with writer3.as_default():
        summary_ops.scalar('three', 3.0, step=3)
        summary_ops.flush()

    event_files = iter(sorted(gfile.Glob(os.path.join(logdir, '*tfevents*'))))

    # First file has tags "one" and "two"
    events = iter(summary_test_util.events_from_file(next(event_files)))
    self.assertEqual('brain.Event:2', next(events).file_version)
    self.assertEqual('one', next(events).summary.value[0].tag)
    self.assertEqual('two', next(events).summary.value[0].tag)
    self.assertRaises(StopIteration, lambda: next(events))

    # Second file has tag "three"
    events = iter(summary_test_util.events_from_file(next(event_files)))
    self.assertEqual('brain.Event:2', next(events).file_version)
    self.assertEqual('three', next(events).summary.value[0].tag)
    self.assertRaises(StopIteration, lambda: next(events))

    # No more files
    self.assertRaises(StopIteration, lambda: next(event_files))
Ejemplo n.º 55
0
 def testFlushFunction(self):
   logdir = self.get_temp_dir()
   writer = summary_ops.create_file_writer(
       logdir, max_queue=999999, flush_millis=999999)
   with writer.as_default(), summary_ops.always_record_summaries():
     summary_ops.scalar('scalar', 2.0, step=1)
     flush_op = summary_ops.flush()
   with self.cached_session() as sess:
     sess.run(summary_ops.summary_writer_initializer_op())
     get_total = lambda: len(summary_test_util.events_from_logdir(logdir))
     # Note: First tf.Event is always file_version.
     self.assertEqual(1, get_total())
     sess.run(summary_ops.all_summary_ops())
     self.assertEqual(1, get_total())
     sess.run(flush_op)
     self.assertEqual(2, get_total())
     # Test "writer" parameter
     sess.run(summary_ops.all_summary_ops())
     sess.run(summary_ops.flush(writer=writer))
     self.assertEqual(3, get_total())
     sess.run(summary_ops.all_summary_ops())
     sess.run(summary_ops.flush(writer=writer._resource))  # pylint:disable=protected-access
     self.assertEqual(4, get_total())
Ejemplo n.º 56
0
 def f():
   with summary_ops.create_file_writer(
       summary_logdir).as_default(), summary_ops.always_record_summaries():
     return self._all_metric_results()
Ejemplo n.º 57
0
  def testSharing_withExplicitSummaryFileWriters(self):
    logdir = self.get_temp_dir()
    with session.Session() as sess:
      # Initial file writer via FileWriter(session=?)
      writer1 = writer.FileWriter(session=sess, logdir=logdir)
      writer1.add_summary(self._createTaggedSummary("one"), 1)
      writer1.flush()

      # Next one via create_file_writer(), should use same file
      writer2 = summary_ops_v2.create_file_writer(logdir=logdir)
      with summary_ops_v2.always_record_summaries(), writer2.as_default():
        summary2 = summary_ops_v2.scalar("two", 2.0, step=2)
      sess.run(writer2.init())
      sess.run(summary2)
      sess.run(writer2.flush())

      # Next has different shared name, should be in separate file
      time.sleep(1.1)  # Ensure filename has a different timestamp
      writer3 = summary_ops_v2.create_file_writer(logdir=logdir, name="other")
      with summary_ops_v2.always_record_summaries(), writer3.as_default():
        summary3 = summary_ops_v2.scalar("three", 3.0, step=3)
      sess.run(writer3.init())
      sess.run(summary3)
      sess.run(writer3.flush())

      # Next uses a second session, should be in separate file
      time.sleep(1.1)  # Ensure filename has a different timestamp
      with session.Session() as other_sess:
        writer4 = summary_ops_v2.create_file_writer(logdir=logdir)
        with summary_ops_v2.always_record_summaries(), writer4.as_default():
          summary4 = summary_ops_v2.scalar("four", 4.0, step=4)
        other_sess.run(writer4.init())
        other_sess.run(summary4)
        other_sess.run(writer4.flush())

        # Next via FileWriter(session=?) uses same second session, should be in
        # same separate file. (This checks sharing in the other direction)
        writer5 = writer.FileWriter(session=other_sess, logdir=logdir)
        writer5.add_summary(self._createTaggedSummary("five"), 5)
        writer5.flush()

      # One more via create_file_writer(), should use same file
      writer6 = summary_ops_v2.create_file_writer(logdir=logdir)
      with summary_ops_v2.always_record_summaries(), writer6.as_default():
        summary6 = summary_ops_v2.scalar("six", 6.0, step=6)
      sess.run(writer6.init())
      sess.run(summary6)
      sess.run(writer6.flush())

    event_paths = iter(sorted(glob.glob(os.path.join(logdir, "event*"))))

    # First file should have tags "one", "two", and "six"
    events = summary_iterator.summary_iterator(next(event_paths))
    self.assertEqual("brain.Event:2", next(events).file_version)
    self.assertEqual("one", next(events).summary.value[0].tag)
    self.assertEqual("two", next(events).summary.value[0].tag)
    self.assertEqual("six", next(events).summary.value[0].tag)
    self.assertRaises(StopIteration, lambda: next(events))

    # Second file should have just "three"
    events = summary_iterator.summary_iterator(next(event_paths))
    self.assertEqual("brain.Event:2", next(events).file_version)
    self.assertEqual("three", next(events).summary.value[0].tag)
    self.assertRaises(StopIteration, lambda: next(events))

    # Third file should have "four" and "five"
    events = summary_iterator.summary_iterator(next(event_paths))
    self.assertEqual("brain.Event:2", next(events).file_version)
    self.assertEqual("four", next(events).summary.value[0].tag)
    self.assertEqual("five", next(events).summary.value[0].tag)
    self.assertRaises(StopIteration, lambda: next(events))

    # No more files
    self.assertRaises(StopIteration, lambda: next(event_paths))
Ejemplo n.º 58
0
 def testShouldRecordSummary(self):
   self.assertFalse(summary_ops.should_record_summaries())
   with summary_ops.always_record_summaries():
     self.assertTrue(summary_ops.should_record_summaries())