import numpy as np import os import ntpath import time from . import util from . import html # from scipy.misc import imresize # save image to the disk def save_images(webpage, visuals, image_path, aspect_ratio=1.0, width=256): image_dir = webpage.get_image_dir() if isinstance(image_path,list): image_path = image_path[0] short_path = ntpath.basename(image_path) name = os.path.splitext(short_path)[0] webpage.add_header(name) ims, txts, links = [], [], [] for label, im_data in visuals.items(): im = util.tensor2im(im_data) # image_name = '%s_%s.png' % (name, label) # save_path = os.path.join(image_dir, image_name) image_name = '%s.png' % (name) util.mkdirs(os.path.join(image_dir, label)) save_path = os.path.join(image_dir, label, image_name) h, w, _ = im.shape # if aspect_ratio > 1.0: # im = imresize(im, (h, int(w * aspect_ratio)), interp='bicubic') # if aspect_ratio < 1.0: # im = imresize(im, (int(h / aspect_ratio), w), interp='bicubic') util.save_image(im, save_path) link_name = os.path.join(label,image_name) ims.append(link_name) txts.append(label) links.append(link_name) webpage.add_images(ims, txts, links, width=width) def save_crop(visuals, save_path): im_data = visuals['fake_A'] im = util.tensor2im(im_data) util.save_image(im, save_path) # save image to the disk def save_images_test(webpage, visuals, image_dir, image_path, aspect_ratio=1.0, width=256): # image_dir = webpage.get_image_dir() if isinstance(image_path,list): image_path = image_path[0] short_path = ntpath.basename(image_path) name = os.path.splitext(short_path)[0] webpage.add_header(name) ims, txts, links = [], [], [] for label, im_data in visuals.items(): im = util.tensor2im(im_data) # image_name = '%s_%s.png' % (name, label) # save_path = os.path.join(image_dir, image_name) image_name = '%s.png' % (name) util.mkdirs(os.path.join(image_dir, label)) save_path = os.path.join(image_dir, label, image_name) exit('wwwwwwwwwwwww') h, w, _ = im.shape # if aspect_ratio > 1.0: # im = imresize(im, (h, int(w * aspect_ratio)), interp='bicubic') # if aspect_ratio < 1.0: # im = imresize(im, (int(h / aspect_ratio), w), interp='bicubic') util.save_image(im, save_path) link_name = os.path.join(label,image_name) ims.append(link_name) txts.append(label) links.append(link_name) webpage.add_images(ims, txts, links, width=width) # save image to the disk # Original Version # def save_images(webpage, visuals, image_path, aspect_ratio=1.0, width=256): # 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, im_data in visuals.items(): # im = util.tensor2im(im_data) # image_name = '%s_%s.png' % (name, label) # save_path = os.path.join(image_dir, image_name) # h, w, _ = im.shape # if aspect_ratio > 1.0: # im = imresize(im, (h, int(w * aspect_ratio)), interp='bicubic') # if aspect_ratio < 1.0: # im = imresize(im, (int(h / aspect_ratio), w), interp='bicubic') # util.save_image(im, save_path) # ims.append(image_name) # txts.append(label) # links.append(image_name) # webpage.add_images(ims, txts, links, width=width) class Visualizer(): def __init__(self, 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 self.opt = opt self.saved = False if self.display_id > 0: import visdom self.ncols = opt.display_ncols self.vis = visdom.Visdom(server=opt.display_server, port=opt.display_port, env=opt.display_env, raise_exceptions=True) 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) def reset(self): self.saved = False def throw_visdom_connection_error(self): print('\n\nCould not connect to Visdom server (https://github.com/facebookresearch/visdom) for displaying training progress.\nYou can suppress connection to Visdom using the option --display_id -1. To install visdom, run \n$ pip install visdom\n, and start the server by \n$ python -m visdom.server.\n\n') exit(1) # |visuals|: dictionary of images to display or save def display_current_results(self, visuals, epoch, save_result): if self.display_id > 0: # show images in the browser ncols = self.ncols if ncols > 0: ncols = min(ncols, len(visuals)) h, w = next(iter(visuals.values())).shape[:2] table_css = """""" % (w, h) title = self.name label_html = '' label_html_row = '' images = [] idx = 0 for label, image in visuals.items(): image_numpy = util.tensor2im(image) label_html_row += '