class Logger: def __init__(self, log_dir): """Create a summary writer logging to log_dir.""" self.writer = SummaryWriter(log_dir) def scalar_summary(self, tag, value, step): """Log a scalar variable.""" summary = tf.Summary( value=[tf.Summary.Value(tag=tag, simple_value=value)]) self.writer.add_summary(summary, step) def list_of_scalars_summary(self, tag_value_pairs, step): """Log scalar variables.""" summary = tf.Summary(value=[ tf.Summary.Value(tag=tag, simple_value=value) for tag, value in tag_value_pairs ]) self.writer.add_summary(summary, step)
class Logger(object): def __init__(self, log_dir): """Create a summary writer logging to log_dir.""" # self.writer = tf.summary.FileWriter(log_dir) self.writer = SummaryWriter(log_dir) def scalar_summary(self, tag, value, step): """Log a scalar variable.""" self.writer.add_scalar(tag=tag, scalar_value=value, global_step=step) def image_summary(self, tag, images, step): """Log a list of images.""" img_summaries = [] for i, img in enumerate(images): # Write the image to a string try: s = StringIO() except: s = BytesIO() scipy.misc.toimage(img).save(s, format="png") # Create an Image object img_sum = tf.Summary.Image(encoded_image_string=s.getvalue(), height=img.shape[0], width=img.shape[1]) # Create a Summary value img_summaries.append(tf.Summary.Value(tag='%s/%d' % (tag, i), image=img_sum)) # Create and write Summary summary = tf.Summary(value=img_summaries) self.writer.add_summary(summary, step) def histo_summary(self, tag, values, step, bins='auto'): """Log a histogram of the tensor of values.""" self.writer.add_histogram(tag, values, step, bins) self.writer.flush() def histo_summary_old(self, tag, values, step, bins=1000): """Log a histogram of the tensor of values.""" # Create a histogram using numpy counts, bin_edges = np.histogram(values, bins=bins) # Fill the fields of the histogram proto hist = tf.HistogramProto() hist.min = float(np.min(values)) hist.max = float(np.max(values)) hist.num = int(np.prod(values.shape)) hist.sum = float(np.sum(values)) hist.sum_squares = float(np.sum(values**2)) # Drop the start of the first bin bin_edges = bin_edges[1:] # Add bin edges and counts for edge in bin_edges: hist.bucket_limit.append(edge) for c in counts: hist.bucket.append(c) # Create and write Summary summary = tf.Summary(value=[tf.Summary.Value(tag=tag, histo=hist)]) self.writer.add_summary(summary, step) self.writer.flush()
class Visualizer(): def __init__(self, opt): self.opt = opt self.tf_log = opt.isTrain and opt.tf_log self.tensorboard = opt.isTrain and opt.tensorboard self.use_html = opt.isTrain and not opt.no_html self.win_size = opt.display_winsize self.name = opt.name if self.tf_log: import tensorflow as tf self.tf = tf self.log_dir = os.path.join(opt.checkpoints_dir, opt.name, 'logs') self.writer = tf.summary.FileWriter(self.log_dir) if self.tensorboard: self.log_dir = os.path.join(opt.checkpoints_dir, opt.name, 'logs') self.writer = SummaryWriter(self.log_dir, comment=opt.name) if self.use_html: self.web_dir = os.path.join(opt.checkpoints_dir, opt.name, 'web') self.img_dir = os.path.join(self.web_dir, 'images') print('create web directory %s...' % self.web_dir) util.mkdirs([self.web_dir, self.img_dir]) if opt.isTrain: self.log_name = os.path.join(opt.checkpoints_dir, opt.name, 'loss_log.txt') with open(self.log_name, "a") as log_file: now = time.strftime("%c") log_file.write( '================ Training Loss (%s) ================\n' % now) # |visuals|: dictionary of images to display or save def display_current_results(self, visuals, epoch, step): ## convert tensors to numpy arrays if self.tf_log: # show images in tensorboard output img_summaries = [] visuals = self.convert_visuals_to_numpy(visuals) for label, image_numpy in visuals.items(): # Write the image to a string try: s = StringIO() except: s = BytesIO() if len(image_numpy.shape) >= 4: image_numpy = image_numpy[0] scipy.misc.toimage(image_numpy).save(s, format="jpeg") # Create an Image object img_sum = self.tf.Summary.Image( encoded_image_string=s.getvalue(), height=image_numpy.shape[0], width=image_numpy.shape[1]) # Create a Summary value img_summaries.append( self.tf.Summary.Value(tag=label, image=img_sum)) # Create and write Summary summary = self.tf.Summary(value=img_summaries) self.writer.add_summary(summary, step) if self.tensorboard: # show images in tensorboard output img_summaries = [] for label, image_numpy in visuals.items(): # Write the image to a string try: s = StringIO() except: s = BytesIO() # if len(image_numpy.shape) >= 4: # image_numpy = image_numpy[0] # scipy.misc.toimage(image_numpy).save(s, format="jpeg") # Create an Image object # self.writer.add_image(tag=label, img_tensor=image_numpy, global_step=step, dataformats='HWC') # Create a Summary value batch_size = image_numpy.size(0) x = vutils.make_grid(image_numpy[:min(batch_size, 16)], normalize=True, scale_each=True) self.writer.add_image(label, x, step) if self.use_html: # save images to a html file for label, image_numpy in visuals.items(): if isinstance(image_numpy, list): for i in range(len(image_numpy)): img_path = os.path.join( self.img_dir, 'epoch%.3d_iter%.3d_%s_%d.png' % (epoch, step, label, i)) util.save_image(image_numpy[i], img_path) else: img_path = os.path.join( self.img_dir, 'epoch%.3d_iter%.3d_%s.png' % (epoch, step, label)) if len(image_numpy.shape) >= 4: image_numpy = image_numpy[0] util.save_image(image_numpy, img_path) # update website webpage = html.HTML(self.web_dir, 'Experiment name = %s' % self.name, refresh=5) for n in range(epoch, 0, -1): webpage.add_header('epoch [%d]' % n) ims = [] txts = [] links = [] for label, image_numpy in visuals.items(): if isinstance(image_numpy, list): for i in range(len(image_numpy)): img_path = 'epoch%.3d_iter%.3d_%s_%d.png' % ( n, step, label, i) ims.append(img_path) txts.append(label + str(i)) links.append(img_path) else: img_path = 'epoch%.3d_iter%.3d_%s.png' % (n, step, label) ims.append(img_path) txts.append(label) links.append(img_path) if len(ims) < 10: webpage.add_images(ims, txts, links, width=self.win_size) else: num = int(round(len(ims) / 2.0)) webpage.add_images(ims[:num], txts[:num], links[:num], width=self.win_size) webpage.add_images(ims[num:], txts[num:], links[num:], width=self.win_size) webpage.save() # errors: dictionary of error labels and values def plot_current_errors(self, errors, step): if self.tf_log: for tag, value in errors.items(): value = value.mean().float() summary = self.tf.Summary( value=[self.tf.Summary.Value(tag=tag, simple_value=value)]) self.writer.add_summary(summary, step) if self.tensorboard: for tag, value in errors.items(): value = value.mean().float() self.writer.add_scalar(tag=tag, scalar_value=value, global_step=step) # errors: same format as |errors| of plotCurrentErrors def print_current_errors(self, epoch, i, errors, t): message = '(epoch: %d, iters: %d, time: %.3f) ' % (epoch, i, t) for k, v in errors.items(): #print(v) #if v != 0: v = v.mean().float() message += '%s: %.3f ' % (k, v) print(message) with open(self.log_name, "a") as log_file: log_file.write('%s\n' % message) def convert_visuals_to_numpy(self, visuals): for key, t in visuals.items(): tile = self.opt.batchSize > 8 if 'input_label' == key: t = util.tensor2label(t, self.opt.label_nc + 2, tile=tile) else: t = util.tensor2im(t, tile=tile) visuals[key] = t return visuals # save image to the disk def save_images(self, webpage, visuals, image_path): visuals = self.convert_visuals_to_numpy(visuals) image_dir = webpage.get_image_dir() short_path = ntpath.basename(image_path[0]) name = os.path.splitext(short_path)[0] webpage.add_header(name) ims = [] txts = [] links = [] for label, image_numpy in visuals.items(): image_name = os.path.join(label, '%s.png' % (name)) save_path = os.path.join(image_dir, image_name) util.save_image(image_numpy, save_path, create_dir=True) ims.append(image_name) txts.append(label) links.append(image_name) webpage.add_images(ims, txts, links, width=self.win_size)
class Logger(object): def __init__(self, log_dir, seed, create_model_dir=True, use_tf=False): """Create a summary writer logging to log_dir.""" self.seed = int(seed) self.log_dir = Path(log_dir) self.model_dir = Path(log_dir) / 'model' self.log_dir.mkdir(parents=True, exist_ok=True) # if create_model_dir: # self.model_dir.mkdir(parents=True, exist_ok=True) #self.meta_dir.mkdir(mode=0o775, parents=True, exist_ok=True) self.use_tf = bool(use_tf) self.tensorboard_dir = self.log_dir #self.tensorboard_dir = self.log_dir / ('tensorboard-{:}'.format(time.strftime( '%d-%h-at-%H:%M:%S', time.gmtime(time.time()) ))) # self.logger_path = self.log_dir / 'seed-{:}-T-{:}.log'.format(self.seed, time.strftime('%d-%h-at-%H-%M-%S', time.gmtime(time.time()))) self.logger_path = self.log_dir / 'seed-{:}.log'.format(self.seed) self.logger_file = open(self.logger_path, 'w') self.tensorboard_dir.mkdir(mode=0o775, parents=True, exist_ok=True) self.writer = SummaryWriter(str(self.tensorboard_dir)) def __repr__(self): return ( '{name}(dir={log_dir}, use-tf={use_tf}, writer={writer})'.format( name=self.__class__.__name__, **self.__dict__)) def path(self, mode): valids = ('model', 'best', 'info', 'log') if mode == 'model': return self.model_dir / 'seed-{:}-basic.pth'.format(self.seed) elif mode == 'best': return self.model_dir / 'seed-{:}-best.pth'.format(self.seed) elif mode == 'info': return self.log_dir / 'seed-{:}-last-info.pth'.format(self.seed) elif mode == 'log': return self.log_dir else: raise TypeError('Unknow mode = {:}, valid modes = {:}'.format( mode, valids)) def extract_log(self): return self.logger_file def close(self): self.logger_file.close() if self.writer is not None: self.writer.close() def log(self, string, save=True, stdout=False): if stdout: sys.stdout.write(string) sys.stdout.flush() else: print(string) if save: self.logger_file.write('{:}\n'.format(string)) self.logger_file.flush() def scalar_summary(self, tags, values, step): """Log a scalar variable.""" if not self.use_tf: warnings.warn( 'Do set use-tensorflow installed but call scalar_summary') else: assert isinstance(tags, list) == isinstance( values, list), 'Type : {:} vs {:}'.format(type(tags), type(values)) if not isinstance(tags, list): tags, values = [tags], [values] for tag, value in zip(tags, values): summary = tf.Summary( value=[tf.Summary.Value(tag=tag, simple_value=value)]) self.writer.add_summary(summary, step) self.writer.flush() def image_summary(self, tag, images, step): """Log a list of images.""" import scipy if not self.use_tf: warnings.warn( 'Do set use-tensorflow installed but call scalar_summary') return img_summaries = [] for i, img in enumerate(images): # Write the image to a string try: s = StringIO() except: s = BytesIO() scipy.misc.toimage(img).save(s, format="png") # Create an Image object img_sum = tf.Summary.Image(encoded_image_string=s.getvalue(), height=img.shape[0], width=img.shape[1]) # Create a Summary value img_summaries.append( tf.Summary.Value(tag='{}/{}'.format(tag, i), image=img_sum)) # Create and write Summary summary = tf.Summary(value=img_summaries) self.writer.add_summary(summary, step) self.writer.flush() def histo_summary(self, tag, values, step, bins=1000): """Log a histogram of the tensor of values.""" if not self.use_tf: raise ValueError('Do not have tensorflow') import tensorflow as tf # Create a histogram using numpy counts, bin_edges = np.histogram(values, bins=bins) # Fill the fields of the histogram proto hist = tf.HistogramProto() hist.min = float(np.min(values)) hist.max = float(np.max(values)) hist.num = int(np.prod(values.shape)) hist.sum = float(np.sum(values)) hist.sum_squares = float(np.sum(values**2)) # Drop the start of the first bin bin_edges = bin_edges[1:] # Add bin edges and counts for edge in bin_edges: hist.bucket_limit.append(edge) for c in counts: hist.bucket.append(c) # Create and write Summary summary = tf.Summary(value=[tf.Summary.Value(tag=tag, histo=hist)]) self.writer.add_summary(summary, step) self.writer.flush()