Beispiel #1
0
    def display_current_results(self, visuals, epoch, save_result):
        if self.display_id > 0:  # show images in the browser
            ncols = self.display_single_pane_ncols
            if ncols > 0:
                h, w = next(iter(visuals.values())).shape[:2]
                table_css = """<style>
                        table {border-collapse: separate; border-spacing:4px; white-space:nowrap; text-align:center}
                        table td {width: %dpx; height: %dpx; padding: 4px; outline: 4px solid black}
                        </style>""" % (w, h)
                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 += '<td>%s</td>' % label
                    images.append(image_numpy.transpose([2, 0, 1]))
                    idx += 1
                    if idx % ncols == 0:
                        label_html += '<tr>%s</tr>' % label_html_row
                        label_html_row = ''
                white_image = np.ones_like(image_numpy.transpose([2, 0, 1
                                                                  ])) * 255
                while idx % ncols != 0:
                    images.append(white_image)
                    label_html_row += '<td></td>'
                    idx += 1
                if label_html_row != '':
                    label_html += '<tr>%s</tr>' % label_html_row
                # pane col = image row
                self.vis.images(images,
                                nrow=ncols,
                                win=self.display_id + 1,
                                padding=2,
                                opts=dict(title=title + ' images'))
                label_html = '<table>%s</table>' % label_html
                self.vis.text(table_css + label_html,
                              win=self.display_id + 2,
                              opts=dict(title=title + ' labels'))
            else:
                idx = 1
                for label, image_numpy in visuals.items():
                    self.vis.image(image_numpy.transpose([2, 0, 1]),
                                   opts=dict(title=label),
                                   win=self.display_id + idx)
                    idx += 1

        if self.use_html and (save_result
                              or not self.saved):  # save images to a html file
            self.saved = True
            for label, image_numpy in visuals.items():
                img_path = os.path.join(self.img_dir,
                                        'epoch%.3d_%s.png' % (epoch, label))
                util.save_image(image_numpy, img_path)
            # update website
            webpage = html.HTML(self.web_dir,
                                'Experiment name = %s' % self.name,
                                reflesh=1)
            for n in range(epoch, 0, -1):
                webpage.add_header('epoch [%d]' % n)
                ims = []
                txts = []
                links = []

                for label, image_numpy in visuals.items():
                    img_path = 'epoch%.3d_%s.png' % (n, label)
                    ims.append(img_path)
                    txts.append(label)
                    links.append(img_path)
                webpage.add_images(ims, txts, links, width=self.win_size)
            webpage.save()
Beispiel #2
0
 opt.no_flip = True  # no flip; comment this line if results on flipped images are needed.
 opt.display_id = -1  # no visdom display; the test code saves the results to a HTML file.
 dataset = create_dataset(
     opt)  # create a dataset given opt.dataset_mode and other options
 model = create_model(
     opt)  # create a model given opt.model and other options
 model.setup(
     opt)  # regular setup: load and print networks; create schedulers
 # create a website
 web_dir = os.path.join(opt.results_dir, opt.name, '{}_{}'.format(
     opt.phase, model.epoch_count - 1))  # define the website directory
 if opt.load_iter > 0:  # load_iter is 0 by default
     web_dir = '{:s}_iter{:d}'.format(web_dir, opt.load_iter)
 print('creating web directory', web_dir)
 webpage = html.HTML(
     web_dir, 'Experiment = %s, Phase = %s, Epoch = %s' %
     (opt.name, opt.phase, model.epoch_count - 1))
 # test with eval mode. This only affects layers like batchnorm and dropout.
 # For [pix2pix]: we use batchnorm and dropout in the original pix2pix. You can experiment it with and without eval() mode.
 # For [CycleGAN]: It should not affect CycleGAN as CycleGAN uses instancenorm without dropout.
 if opt.eval:
     model.eval()
 for i, data in enumerate(dataset):
     if i >= opt.num_test:  # only apply our model to opt.num_test images.
         break
     model.set_input(data)  # unpack data from data loader
     model.test()  # run inference
     visuals = model.get_current_visuals()  # get image results
     img_path = model.get_image_paths()  # get image paths
     if i % 5 == 0:  # save images to an HTML file
         print('processing (%04d)-th image... %s' % (i, img_path))
Beispiel #3
0
    set_seed(opt.seed)
    if opt.config_str is not None:
        assert 'super' in opt.netG or 'sub' in opt.netG
        config = decode_config(opt.config_str)
    else:
        assert 'super' not in opt.model
        config = None

    dataloader = create_dataloader(opt)
    model = create_model(opt)
    model.setup(opt, verbose=False)

    web_dir = opt.results_dir  # define the website directory
    if opt.model == 'munit_test':
        webpage = html.HTML(
            web_dir, 'G_A_path: %s\tG_B_path: %s' %
            (opt.restore_G_A_path, opt.restore_G_B_path))
    else:
        webpage = html.HTML(web_dir, 'G_path: %s' % (opt.restore_G_path))
    fakes, names = [], []
    for i, data in enumerate(tqdm.tqdm(dataloader)):
        model.set_input(data)  # unpack data from data loader
        if i == 0 and opt.need_profile:
            model.profile(config)
        model.test(config)  # run inference
        visuals = model.get_current_visuals()  # get image results
        generated = visuals['fake_B'].cpu()
        fakes.append(generated)
        for path in model.get_image_paths():
            short_path = ntpath.basename(path)
            name = os.path.splitext(short_path)[0]
    def display_current_results(self, visuals, epoch, step):
        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()
                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.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_%s_%d.jpg' % (epoch, label, i))
                        util.save_image(image_numpy[i], img_path)
                else:
                    img_path = os.path.join(
                        self.img_dir, 'epoch%.3d_%s.jpg' % (epoch, label))
                    util.save_image(image_numpy, img_path)

            # update website
            webpage = html.HTML(self.web_dir, 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_%s_%d.jpg' % (n, label, i)
                            ims.append(img_path)
                            txts.append(label + str(i))
                            links.append(img_path)
                    else:
                        img_path = 'epoch%.3d_%s.jpg' % (n, 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()
Beispiel #5
0
if __name__ == '__main__':
    opt = TestOptions().parse()
    print(' '.join(sys.argv))
    if opt.config_str is not None:
        assert 'super' in opt.netG or 'sub' in opt.netG
        config = decode_config(opt.config_str)
    else:
        assert 'super' not in opt.model
        config = None

    dataloader = create_dataloader(opt)
    model = create_model(opt)
    model.setup(opt)

    web_dir = opt.results_dir  # define the website directory
    webpage = html.HTML(web_dir, 'restore_G_path: %s' % (opt.restore_G_path))
    fakes, names = [], []
    for i, data in enumerate(tqdm.tqdm(dataloader)):
        model.set_input(data)  # unpack data from data loader
        if i == 0 and opt.need_profile:
            model.profile(config)
        model.test(config)  # run inference
        visuals = model.get_current_visuals()  # get image results
        generated = visuals['fake_B'].cpu()
        fakes.append(generated)
        for path in model.get_image_paths():
            short_path = ntpath.basename(path)
            name = os.path.splitext(short_path)[0]
            names.append(name)
        if i < opt.num_test:
            save_images(webpage, visuals, model.get_image_paths(), opt)
Beispiel #6
0
from torch.autograd import Variable
import time

opt = TestOptions().parse(save=False)
opt.nThreads = 1
opt.batchSize = 1
opt.serial_batches = True  # no shuffle

data_loader = CreateDataLoader(opt)
_, dataset = data_loader.load_data()
model = create_model(opt, data_loader.dataset)
visualizer = Visualizer(opt)
# create website
web_dir = os.path.join(opt.results_dir, opt.name,
                       '%s_%s' % (opt.phase, opt.which_epoch))
webpage = html.HTML(web_dir, '%s: %s' % (opt.name, opt.which_epoch))

img_dir = os.path.join(web_dir, 'images')
# test

label_trues, label_preds = [], []

model.model.eval()
tic = time.time()

accs = []
for i, data in enumerate(dataset):
    if i >= opt.how_many and opt.how_many != 0:
        break
    seggt, segpred = model.forward(data, False)
    print(time.time() - tic)
# args for dataloader and display.
opts.num_threads = 1
opts.batch_size = 1
opts.serial_batches = True
opts.display_id = -1

# get dataset.
dataset = DatasetDataLoader(opts)
model = LocationAwareSIRR(opts, device)
model.setup()
model.eval()

# create a website.
web_dir = os.path.join(opts.save_dir,
                       opts.name)  # define the website directory.
webpage = html.HTML(web_dir, 'Experment={}'.format(opts.name))

# if exists ground truth transmission and the length equals to len(origin images).
gt_available = False
I_dir = os.path.join(opts.data_root, 'blend')
assert (os.path.exists(I_dir))
T_dir = os.path.join(opts.data_root, 'transmission')
if os.path.exists(T_dir) and len(os.listdir(T_dir)) == len(os.listdir(I_dir)):
    gt_available = True

# inference start.
print('Inference process start. Total images num: %s' % dataset.get_length())
# record predicted results and gt transmission.
fake_Ts = []
if gt_available:
    real_Ts = []