Beispiel #1
0
def test(config):
    config[
        'num_threads'] = 1  # only <num_threads = 1> supported when testing_usr
    config['flip'] = False  # not allowed to flip image
    config['add_colorjit'] = False  # not allowed to use color jitting

    dataset = create_dataset(config)
    model = create_model(config)
    model.setup(config)

    result_root_path = os.path.join(config['checkpoints_dir'], config['name'],
                                    config['results_dir'])
    util.mkdir(result_root_path)
    print(" create testing_usr folder: " + result_root_path)

    # set module to testing_usr mode
    model.eval()

    for i, data in enumerate(dataset):
        model.set_input(data)  # push test datasets to module
        model.test()  # forward module

        for k in range(len(model.test_result)):
            img = util.tensor2im(model.test_result[k][1])
            img_path = os.path.join(result_root_path, data['PATH'][0])
            util.save_image(img, img_path)

        print("Testing forward-- complete:" + str(i + 1) + "  total:" +
              str(dataset.__len__()))
Beispiel #2
0
def save_images(webpage, visuals, image_path, width=256):
    """"""
    """ this function save images in "visuals" to html file.

    args:
        webpage (the HTML class)   -- the html class to save the images (see html.py for details)
        visuals (OrderedDict)      -- OrderedDict to save name/iamges
        image_path (str)           -- iamge path
        aspect_ratio (float)       -- the aspect ratio for saving image (default 1.0)
        width (int)                -- set iamge width, the image will be scaled width x width size (default 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 = tools.tensor2im(im_data)
        image_name = '%s_%s.png' % (name, label)
        save_path = os.path.join(image_dir, image_name)
        h, w, _ = im.shape
        tools.save_image(im, save_path)

        ims.append(image_name)
        txts.append(label)
        links.append(image_name)
    webpage.add_images(ims, txts, links, width=width)
Beispiel #3
0
def test(config):
    config[
        'num_threads'] = 1  # only <num_threads = 1> supported when testing_usr
    config['flip'] = False  # not allowed to flip image
    config['status'] = 'test'
    config['crop_scale'] = 1.0

    dataset = create_dataset(config)
    model = create_model(config)
    model.setup(config)

    result_root_path = os.path.join(config['checkpoints_dir'], config['name'],
                                    'evaluation')
    util.mkdir(result_root_path)
    util.mkdir(os.path.join(result_root_path, 'prediction_distance'))
    util.mkdir(os.path.join(result_root_path, 'prediction_heatmap'))
    print(" create evaluate folder: " + result_root_path)

    # set module to testing_usr mode
    model.eval()

    save_npy = np.ndarray(shape=(dataset.__len__() + 1, 2), dtype=np.float)
    save_npy[0][0], save_npy[0][1] = -1, -1

    for i, data in enumerate(dataset):
        model.set_input(data)  # push test datasets to module
        model.test()  # forward module

        datapoints = (model.test_result[0][1]).cpu().data.numpy()
        index = data["PATH"].cpu().data.numpy()[0]
        save_npy[index][0], save_npy[index][1] = datapoints[0][0], datapoints[
            0][1]

        dist_img = model.test_result[1][1]
        util.save_image(
            util.tensor2im(dist_img),
            os.path.join(result_root_path, 'prediction_distance',
                         str(index) + ".png"))

        heatmap_img = model.test_result[2][1]
        util.save_image(
            util.tensor2im(heatmap_img),
            os.path.join(result_root_path, 'prediction_heatmap',
                         str(index) + ".png"))

        print("Evaluate forward-- complete:" + str(i + 1) + "  total:" +
              str(dataset.__len__()))

    np.save(os.path.join(result_root_path, 'regression.npy'), save_npy)
    l2_dist, easy_dist, hard_dist = evaluation.evaluate_detailed(save_npy)
    print("Testing npy result have been saved! Evaluation distance: " +
          str(round(l2_dist)) + "(" + str(round(easy_dist)) + "," +
          str(round(hard_dist)) + ")")
Beispiel #4
0
def save_images(webpage, visuals, image_path, 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 = tools.tensor2im(im_data)
        image_name = '%s_%s.png' % (name, label)
        save_path = os.path.join(image_dir, image_name)
        h, w, _ = im.shape
        tools.save_image(im, save_path)

        ims.append(image_name)
        txts.append(label)
        links.append(image_name)
    webpage.add_images(ims, txts, links, width=width)
Beispiel #5
0
def val(config, epoch, dataset, model):

    result_root_path = os.path.join(config['checkpoints_dir'], config['name'],
                                    config['results_dir'],
                                    "epoch" + str(epoch))
    util.mkdir(result_root_path)

    model.eval()
    save_npy = np.ndarray(shape=(dataset.__len__() + 1, 2), dtype=np.float)
    save_npy[0][0], save_npy[0][1] = -1, -1
    print("Start evaluating epoch " + str(epoch) + "...")

    for i, data in enumerate(dataset):
        model.set_input(data)  # push test datasets to module
        model.test()  # forward module

        datapoints = (model.test_result[0][1]).cpu().data.numpy()
        index = data["PATH"].cpu().data.numpy()[0]
        save_npy[index][0], save_npy[index][1] = datapoints[0][0], datapoints[
            0][1]

        dist_img = model.test_result[1][1]
        util.save_image(util.tensor2im(dist_img),
                        os.path.join(result_root_path,
                                     str(index) + ".png"))

    model.train()

    np.save(os.path.join(result_root_path, 'regression.npy'), save_npy)
    l2_dist, easy_dist, hard_dist = evaluation.evaluate_detailed(save_npy)
    text = open(
        os.path.join(config['checkpoints_dir'], config['name'],
                     config['results_dir'], "evaluation.txt"), "a+")
    text.writelines("EPOCH " + str(epoch) + ": " + str(round(l2_dist, 4)) +
                    "   " + str(round(easy_dist, 4)) + '   ' +
                    str(round(hard_dist, 4)) + "\n")
    text.close()
    print("Testing npy result have been saved! Evaluation distance: " +
          str(round(l2_dist)) + "(" + str(round(easy_dist)) + "," +
          str(round(hard_dist)) + ")")
Beispiel #6
0
    def display_current_results(self, visuals, epoch, save_result):
        """"""
        """ diaplay current result in visdom and save to html

        args:
            visuals (OrderedDict) - -     image OrderedDict to visulize of save
            epoch (int) - -               current epoch
            save_result (bool) - -        save current result to html or not
        """

        # show image in browser using visdom
        if self.display_id > 0:
            ncols = self.ncols
            # show all the iamge in one visdom web pannel
            if ncols > 0:
                ncols = min(ncols, len(visuals))
                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)  # create CSS of the tabel

                title = self.name
                label_html = ''
                label_html_row = ''
                images = []
                idx = 0
                for label, image in visuals.items():
                    image_numpy = tools.tensor2im(image)
                    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
                try:
                    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'))
                except VisdomExceptionBase:
                    self.create_visdom_connections()

            else:
                idx = 1
                try:
                    for label, image in visuals.items():
                        image_numpy = tools.tensor2im(image)
                        self.vis.image(image_numpy.transpose([2, 0, 1]),
                                       opts=dict(title=label),
                                       win=self.display_id + idx)
                        idx += 1
                except VisdomExceptionBase:
                    self.create_visdom_connections()

        if self.use_html and (save_result or not self.saved):
            self.saved = True

            for label, image in visuals.items():
                image_numpy = tools.tensor2im(image)
                img_path = os.path.join(self.img_dir,
                                        'epoch%.3d_%s.png' % (epoch, label))
                tools.save_image(image_numpy, img_path)

            # refresh html
            webpage = html.HTML(self.web_dir,
                                'Experiment name = %s' % self.name)
            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()