This class includes several functions that can display/save images and print/save logging information. It uses a Python library 'visdom' for display, and a Python library 'dominate' (wrapped in 'HTML') for creating HTML files with images.
| 45 | |
| 46 | |
| 47 | class Visualizer(): |
| 48 | """This class includes several functions that can display/save images and print/save logging information. |
| 49 | |
| 50 | It uses a Python library 'visdom' for display, and a Python library 'dominate' (wrapped in 'HTML') for creating HTML files with images. |
| 51 | """ |
| 52 | |
| 53 | def __init__(self, opt): |
| 54 | """Initialize the Visualizer class |
| 55 | |
| 56 | Parameters: |
| 57 | opt -- stores all the experiment flags; needs to be a subclass of BaseOptions |
| 58 | Step 1: Cache the training/test options |
| 59 | Step 2: connect to a visdom server |
| 60 | Step 3: create an HTML object for saveing HTML filters |
| 61 | Step 4: create a logging file to store training losses |
| 62 | """ |
| 63 | self.opt = opt # cache the option |
| 64 | self.display_id = opt.display_id |
| 65 | self.use_html = opt.isTrain and not opt.no_html |
| 66 | self.win_size = opt.display_winsize |
| 67 | self.name = opt.name |
| 68 | self.port = opt.display_port |
| 69 | self.saved = False |
| 70 | |
| 71 | if self.use_html: # create an HTML object at <checkpoints_dir>/web/; images will be saved under <checkpoints_dir>/web/images/ |
| 72 | self.web_dir = os.path.join(opt.checkpoints_dir, opt.name, 'web') |
| 73 | self.img_dir = os.path.join(self.web_dir, 'images') |
| 74 | print('create web directory %s...' % self.web_dir) |
| 75 | util.mkdirs([self.web_dir, self.img_dir]) |
| 76 | # create a logging file to store training losses |
| 77 | self.log_name = os.path.join(opt.checkpoints_dir, opt.name, 'loss_log.txt') |
| 78 | with open(self.log_name, "a") as log_file: |
| 79 | now = time.strftime("%c") |
| 80 | log_file.write('================ Training Loss (%s) ================\n' % now) |
| 81 | |
| 82 | def reset(self): |
| 83 | """Reset the self.saved status""" |
| 84 | self.saved = False |
| 85 | |
| 86 | def create_visdom_connections(self): |
| 87 | """If the program could not connect to Visdom server, this function will start a new server at port < self.port > """ |
| 88 | cmd = sys.executable + ' -m visdom.server -p %d &>/dev/null &' % self.port |
| 89 | print('\n\nCould not connect to Visdom server. \n Trying to start a server....') |
| 90 | print('Command: %s' % cmd) |
| 91 | Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE) |
| 92 | |
| 93 | def display_current_results(self, visuals, epoch, save_result): |
| 94 | """Display current results on visdom; save current results to an HTML file. |
| 95 | |
| 96 | Parameters: |
| 97 | visuals (OrderedDict) - - dictionary of images to display or save |
| 98 | epoch (int) - - the current epoch |
| 99 | save_result (bool) - - if save the current results to an HTML file |
| 100 | """ |
| 101 | if self.use_html and (save_result or not self.saved): # save images to an HTML file if they haven't been saved. |
| 102 | self.saved = True |
| 103 | # save images to the disk |
| 104 | for label, image in visuals.items(): |