import numpy as np import os import ntpath import time from . import util from . import html class Visualizer(): def __init__(self, opt): # self.opt = opt self.display_id = opt.display_id self.use_html = opt.isTrain and not opt.no_html self.win_size = opt.display_winsize self.name = opt.name if self.display_id > 0: import visdom self.vis = visdom.Visdom(port = opt.display_port) self.display_single_pane_ncols = opt.display_single_pane_ncols 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]) 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): if self.display_id > 0: # show images in the browser if self.display_single_pane_ncols > 0: ncols = self.display_single_pane_ncols title = self.name label_html = '' label_html_row = '' nrows = int(np.ceil(len(visuals.items()) / ncols)) images = [] idx = 0 for label, image_numpy in visuals.items(): label_html_row += '%s' % label images.append(image_numpy.transpose([2, 0, 1])) idx += 1 if idx % ncols == 0: label_html += '%s' % label_html_row label_html_row = '' while idx % ncols != 0: white_image = np.ones_like(image_numpy.transpose([2, 0, 1]))*255 images.append(white_image) label_html_row += '' idx += 1 if label_html_row != '': label_html += '%s' % label_html_row self.vis.images(images, nrow=ncols, win=self.display_id + 1, opts=dict(title=title + ' images')) # pane col = image row label_html = '%s