コード例 #1
0
class Logger(object):
    def __init__(self, log_dir):
        """Create a summary writer logging to log_dir."""
        self.writer = SummaryWriter(log_dir=log_dir)

    def scalar_summary(self, tag, value, step):
        """Log a scalar variable."""
        self.writer.add_summary(tag, value, step)

    def image_summary(self, tag, images, step):
        """Log a list of images."""
        self.writer.add_image(tag, images, step)

    def graph_summary(self, model, dummy_input):
        self.writer.add_graph(model, (dummy_input, ))
コード例 #2
0
ファイル: logger.py プロジェクト: yhu9/RCAN
class Logger(object):
    def __init__(self, log_dir, step=0):
        """Create a summary writer logging to log_dir."""
        log_dir = os.path.join('log', log_dir)
        print('logging at: ', log_dir)
        self.writer = SummaryWriter(log_dir)
        self.step = step

    def incstep(self):
        self.step += 1

    def scalar_summary(self, data):
        """Log a scalar variable."""

        for tag, value in data.items():
            self.writer.add_scalar(tag, value, self.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 hist_summary(self, tag, values):
        """Log a histogram of the tensor of values."""

        self.writer.add_histogram(tag, values, self.step, bins="auto")
コード例 #3
0
class Logger(object):
    def __init__(self, log_dir):
        """Create a summary writer logging to log_dir."""
        self.writer = SummaryWriter(log_dir=log_dir)

    def add_scalars(self, tag, value_dict, step):
        """Log a scalar variable."""
        self.writer.add_scalars(tag, value_dict, 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 add_hist(self, tag, values, step, bins=1000):
        """Log a histogram of the tensor of values."""
        # self.writer.add_histogram('hist', array, iteration)
        pass
コード例 #4
0
ファイル: logger.py プロジェクト: yhu9/ESRGAN
class Logger(object):

    def __init__(self, log_dir):
        """Create a summary writer logging to log_dir."""
        log_dir = os.path.join('log',log_dir)
        if not os.path.exists(log_dir):
            print('log dir', log_dir)
            #self.writer = tf.compat.v1.summary.FileWriter(log_dir)
            self.writer = SummaryWriter(log_dir)
        else:
            print('This training session name already exists. Please use a different name or delete it')
        self.step = 0

    def incstep(self): self.step += 1

    def scalar_summary(self, data):
        """Log a scalar variable."""

        for tag, value in data.items():
            self.writer.add_scalar(tag,value,self.step)
            #summary = tf.compat.v1.Summary(value=[tf.compat.v1.Summary.Value(tag=tag, simple_value=value)])
            #self.writer.add_summary(summary, self.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 hist_summary(self, tag, values, bins=5):
        """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.compat.v1.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
        print('hello')
        self.writer.add_histogram(tag,values.clone().cpu().data.numpy(),self.step)
        print('hello')
        '''
コード例 #5
0
class Visualizer:
    def __init__(self, opt):
        self.opt = opt
        self.tf_log = opt.isTrain and opt.tf_log

        self.tensorboard_log = opt.tensorboard_log

        self.win_size = opt.display_winsize
        self.name = opt.name
        if self.tensorboard_log:

            if self.opt.isTrain:
                self.log_dir = os.path.join(opt.checkpoints_dir, opt.name, "logs")
                if not os.path.exists(self.log_dir):
                    os.makedirs(self.log_dir)
                self.writer = SummaryWriter(log_dir=self.log_dir)
            else:
                print("hi :)")
                self.log_dir = os.path.join(opt.checkpoints_dir, opt.name, opt.results_dir)
                if not os.path.exists(self.log_dir):
                    os.makedirs(self.log_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):

        all_tensor = []
        if self.tensorboard_log:

            for key, tensor in visuals.items():
                all_tensor.append((tensor.data.cpu() + 1) / 2)

            output = torch.cat(all_tensor, 0)
            img_grid = vutils.make_grid(output, nrow=self.opt.batchSize, padding=0, normalize=False)

            if self.opt.isTrain:
                self.writer.add_image("Face_SPADE/training_samples", img_grid, step)
            else:
                vutils.save_image(
                    output,
                    os.path.join(self.log_dir, str(step) + ".png"),
                    nrow=self.opt.batchSize,
                    padding=0,
                    normalize=False,
                )

    # 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_log:

            self.writer.add_scalar("Loss/GAN_Feat", errors["GAN_Feat"].mean().float(), step)
            self.writer.add_scalar("Loss/VGG", errors["VGG"].mean().float(), step)
            self.writer.add_scalars(
                "Loss/GAN",
                {
                    "G": errors["GAN"].mean().float(),
                    "D": (errors["D_Fake"].mean().float() + errors["D_real"].mean().float()) / 2,
                },
                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():
            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)  ## B*H*W*C 0-255 numpy
            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)
コード例 #6
0
class Visualizer():
    def __init__(self, opt):
        self.opt = opt
        self.tf_log = opt.isTrain and opt.tf_log

        self.tensorboard_log = opt.tensorboard_log

        self.use_html = opt.isTrain and not opt.no_html
        self.win_size = opt.display_winsize
        self.name = opt.name
        if self.tensorboard_log:

            if self.opt.isTrain:
                self.log_dir = os.path.join(opt.checkpoints_dir, opt.name,
                                            'logs')
                if not os.path.exists(self.log_dir):
                    os.makedirs(self.log_dir)
                self.writer = SummaryWriter(log_dir=self.log_dir)
            else:
                print("hi :)")
                self.log_dir = os.path.join(opt.checkpoints_dir, opt.name,
                                            opt.results_dir)
                if not os.path.exists(self.log_dir):
                    os.makedirs(self.log_dir)
            # 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 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):

        all_tensor = []
        if self.tensorboard_log:

            for key, tensor in visuals.items():
                if key == 'input_label':
                    tile = self.opt.batchSize > 1
                    t = util.tensor2label(tensor,
                                          self.opt.label_nc + 2,
                                          tile=tile)  ## B*H*W*3 0-255 numpy
                    t = np.transpose(t, (0, 3, 1, 2))
                    all_tensor.append(torch.tensor(t).float() / 255)
                else:
                    all_tensor.append((tensor.data.cpu() + 1) / 2)

            output = torch.cat(all_tensor, 0)
            img_grid = vutils.make_grid(output,
                                        nrow=self.opt.batchSize,
                                        padding=0,
                                        normalize=False)

            if self.opt.isTrain:
                self.writer.add_image('Face_SPADE/training_samples', img_grid,
                                      step)
            else:
                # self.writer.add_image('Face_SPADE/test_samples',img_grid,step)
                vutils.save_image(output,
                                  os.path.join(self.log_dir,
                                               str(step) + '.png'),
                                  nrow=self.opt.batchSize,
                                  padding=0,
                                  normalize=False)

        ## convert tensors to numpy arrays
        # visuals = self.convert_visuals_to_numpy(visuals)

        # if self.tf_log: # 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
        #         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)

    # 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_log:

            #    self.writer.add_scalar('G',errors['GAN'].item(),step)
            self.writer.add_scalar('Loss/GAN_Feat',
                                   errors['GAN_Feat'].mean().float(), step)
            self.writer.add_scalar('Loss/VGG', errors['VGG'].mean().float(),
                                   step)
            #    self.writer.add_scalar('D',(errors['D_Fake'].item()+errors['D_real'].item())/2,step)
            self.writer.add_scalars(
                'Loss/GAN', {
                    'G':
                    errors['GAN'].mean().float(),
                    'D': (errors['D_Fake'].mean().float() +
                          errors['D_real'].mean().float()) / 2
                }, 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)  ## B*H*W*C 0-255 numpy
            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)