what.utils.logger

  1import logging
  2
  3import tensorflow as tf
  4from io import StringIO
  5import matplotlib.pyplot as plt
  6import numpy as np
  7
  8def get_logger(name, level=logging.INFO):
  9    logging.basicConfig()
 10    logger = logging.getLogger(name)
 11    logger.handlers = [] # This is the key thing for the question!
 12
 13    # Start defining and assigning your handlers here
 14    handler = logging.StreamHandler()
 15    handler.setFormatter(logging.Formatter("[%(levelname)s] %(asctime)s %(name)s: %(message)s"))
 16    handler.setLevel(level)
 17    logger.handlers = [handler]
 18    logger.propagate = False
 19    logger.setLevel(level)
 20
 21    return logger
 22
 23"""Simple example on how to log scalars and images to tensorboard without tensor ops.
 24
 25License: BSD License 2.0
 26"""
 27__author__ = "Michael Gygli"
 28
 29class TensorBoardLogger(object):
 30    """Logging in tensorboard without tensorflow ops."""
 31
 32    def __init__(self, log_dir):
 33        """Creates a summary writer logging to log_dir."""
 34        self.writer = tf.compat.v1.summary.FileWriter(log_dir)
 35
 36    def log_scalar(self, tag, value, step):
 37        """Log a scalar variable.
 38
 39        Parameter
 40        ----------
 41        tag : basestring
 42            Name of the scalar
 43        value
 44        step : int
 45            training iteration
 46        """
 47        summary = tf.compat.v1.Summary(value=[tf.compat.v1.Summary.Value(tag=tag,
 48                                                     simple_value=value)])
 49        self.writer.add_summary(summary, step)
 50
 51    def log_images(self, tag, images, step):
 52        """Logs a list of images."""
 53
 54        im_summaries = []
 55        for nr, img in enumerate(images):
 56            # Write the image to a string
 57            s = StringIO()
 58            plt.imsave(s, img, format='png')
 59
 60            # Create an Image object
 61            img_sum = tf.Summary.Image(encoded_image_string=s.getvalue(),
 62                                       height=img.shape[0],
 63                                       width=img.shape[1])
 64            # Create a Summary value
 65            im_summaries.append(tf.Summary.Value(tag='%s/%d' % (tag, nr),
 66                                                 image=img_sum))
 67
 68        # Create and write Summary
 69        summary = tf.Summary(value=im_summaries)
 70        self.writer.add_summary(summary, step)
 71        
 72
 73    def log_histogram(self, tag, values, step, bins=1000):
 74        """Logs the histogram of a list/vector of values."""
 75        # Convert to a numpy array
 76        values = np.array(values)
 77        
 78        # Create histogram using numpy        
 79        counts, bin_edges = np.histogram(values, bins=bins)
 80
 81        # Fill fields of histogram proto
 82        hist = tf.HistogramProto()
 83        hist.min = float(np.min(values))
 84        hist.max = float(np.max(values))
 85        hist.num = int(np.prod(values.shape))
 86        hist.sum = float(np.sum(values))
 87        hist.sum_squares = float(np.sum(values**2))
 88
 89        # Requires equal number as bins, where the first goes from -DBL_MAX to bin_edges[1]
 90        # See https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/framework/summary.proto#L30
 91        # Thus, we drop the start of the first bin
 92        bin_edges = bin_edges[1:]
 93
 94        # Add bin edges and counts
 95        for edge in bin_edges:
 96            hist.bucket_limit.append(edge)
 97        for c in counts:
 98            hist.bucket.append(c)
 99
100        # Create and write Summary
101        summary = tf.Summary(value=[tf.Summary.Value(tag=tag, histo=hist)])
102        self.writer.add_summary(summary, step)
103        self.writer.flush()
def get_logger(name, level=20):
 9def get_logger(name, level=logging.INFO):
10    logging.basicConfig()
11    logger = logging.getLogger(name)
12    logger.handlers = [] # This is the key thing for the question!
13
14    # Start defining and assigning your handlers here
15    handler = logging.StreamHandler()
16    handler.setFormatter(logging.Formatter("[%(levelname)s] %(asctime)s %(name)s: %(message)s"))
17    handler.setLevel(level)
18    logger.handlers = [handler]
19    logger.propagate = False
20    logger.setLevel(level)
21
22    return logger
class TensorBoardLogger:
 30class TensorBoardLogger(object):
 31    """Logging in tensorboard without tensorflow ops."""
 32
 33    def __init__(self, log_dir):
 34        """Creates a summary writer logging to log_dir."""
 35        self.writer = tf.compat.v1.summary.FileWriter(log_dir)
 36
 37    def log_scalar(self, tag, value, step):
 38        """Log a scalar variable.
 39
 40        Parameter
 41        ----------
 42        tag : basestring
 43            Name of the scalar
 44        value
 45        step : int
 46            training iteration
 47        """
 48        summary = tf.compat.v1.Summary(value=[tf.compat.v1.Summary.Value(tag=tag,
 49                                                     simple_value=value)])
 50        self.writer.add_summary(summary, step)
 51
 52    def log_images(self, tag, images, step):
 53        """Logs a list of images."""
 54
 55        im_summaries = []
 56        for nr, img in enumerate(images):
 57            # Write the image to a string
 58            s = StringIO()
 59            plt.imsave(s, img, format='png')
 60
 61            # Create an Image object
 62            img_sum = tf.Summary.Image(encoded_image_string=s.getvalue(),
 63                                       height=img.shape[0],
 64                                       width=img.shape[1])
 65            # Create a Summary value
 66            im_summaries.append(tf.Summary.Value(tag='%s/%d' % (tag, nr),
 67                                                 image=img_sum))
 68
 69        # Create and write Summary
 70        summary = tf.Summary(value=im_summaries)
 71        self.writer.add_summary(summary, step)
 72        
 73
 74    def log_histogram(self, tag, values, step, bins=1000):
 75        """Logs the histogram of a list/vector of values."""
 76        # Convert to a numpy array
 77        values = np.array(values)
 78        
 79        # Create histogram using numpy        
 80        counts, bin_edges = np.histogram(values, bins=bins)
 81
 82        # Fill fields of histogram proto
 83        hist = tf.HistogramProto()
 84        hist.min = float(np.min(values))
 85        hist.max = float(np.max(values))
 86        hist.num = int(np.prod(values.shape))
 87        hist.sum = float(np.sum(values))
 88        hist.sum_squares = float(np.sum(values**2))
 89
 90        # Requires equal number as bins, where the first goes from -DBL_MAX to bin_edges[1]
 91        # See https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/framework/summary.proto#L30
 92        # Thus, we drop the start of the first bin
 93        bin_edges = bin_edges[1:]
 94
 95        # Add bin edges and counts
 96        for edge in bin_edges:
 97            hist.bucket_limit.append(edge)
 98        for c in counts:
 99            hist.bucket.append(c)
100
101        # Create and write Summary
102        summary = tf.Summary(value=[tf.Summary.Value(tag=tag, histo=hist)])
103        self.writer.add_summary(summary, step)
104        self.writer.flush()

Logging in tensorboard without tensorflow ops.

TensorBoardLogger(log_dir)
33    def __init__(self, log_dir):
34        """Creates a summary writer logging to log_dir."""
35        self.writer = tf.compat.v1.summary.FileWriter(log_dir)

Creates a summary writer logging to log_dir.

def log_scalar(self, tag, value, step):
37    def log_scalar(self, tag, value, step):
38        """Log a scalar variable.
39
40        Parameter
41        ----------
42        tag : basestring
43            Name of the scalar
44        value
45        step : int
46            training iteration
47        """
48        summary = tf.compat.v1.Summary(value=[tf.compat.v1.Summary.Value(tag=tag,
49                                                     simple_value=value)])
50        self.writer.add_summary(summary, step)

Log a scalar variable.

Parameter

tag : basestring Name of the scalar value step : int training iteration

def log_images(self, tag, images, step):
52    def log_images(self, tag, images, step):
53        """Logs a list of images."""
54
55        im_summaries = []
56        for nr, img in enumerate(images):
57            # Write the image to a string
58            s = StringIO()
59            plt.imsave(s, img, format='png')
60
61            # Create an Image object
62            img_sum = tf.Summary.Image(encoded_image_string=s.getvalue(),
63                                       height=img.shape[0],
64                                       width=img.shape[1])
65            # Create a Summary value
66            im_summaries.append(tf.Summary.Value(tag='%s/%d' % (tag, nr),
67                                                 image=img_sum))
68
69        # Create and write Summary
70        summary = tf.Summary(value=im_summaries)
71        self.writer.add_summary(summary, step)

Logs a list of images.

def log_histogram(self, tag, values, step, bins=1000):
 74    def log_histogram(self, tag, values, step, bins=1000):
 75        """Logs the histogram of a list/vector of values."""
 76        # Convert to a numpy array
 77        values = np.array(values)
 78        
 79        # Create histogram using numpy        
 80        counts, bin_edges = np.histogram(values, bins=bins)
 81
 82        # Fill fields of histogram proto
 83        hist = tf.HistogramProto()
 84        hist.min = float(np.min(values))
 85        hist.max = float(np.max(values))
 86        hist.num = int(np.prod(values.shape))
 87        hist.sum = float(np.sum(values))
 88        hist.sum_squares = float(np.sum(values**2))
 89
 90        # Requires equal number as bins, where the first goes from -DBL_MAX to bin_edges[1]
 91        # See https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/framework/summary.proto#L30
 92        # Thus, we drop the start of the first bin
 93        bin_edges = bin_edges[1:]
 94
 95        # Add bin edges and counts
 96        for edge in bin_edges:
 97            hist.bucket_limit.append(edge)
 98        for c in counts:
 99            hist.bucket.append(c)
100
101        # Create and write Summary
102        summary = tf.Summary(value=[tf.Summary.Value(tag=tag, histo=hist)])
103        self.writer.add_summary(summary, step)
104        self.writer.flush()

Logs the histogram of a list/vector of values.