Example #1
0
    def save_labeled(self, batch, src2tgt, tgt2src):
        """Save a batch of labelled examples."""
        src, src2tgt = un_normalize(batch['src']), un_normalize(src2tgt)
        src_paths = batch['src_path']

        tgt, tgt2src = un_normalize(batch['tgt']), un_normalize(tgt2src)
        tgt_paths = batch['tgt_path']

        # Make grid of images
        i = 0
        for s, s2t, sp, t, t2s, tp in zip(src, src2tgt, src_paths, tgt,
                                          tgt2src, tgt_paths):
            # Save image
            if self.summary_writer is None:
                self._write(s2t, os.path.basename(sp), subdir='src2tgt')
                self._write(t2s, os.path.basename(tp), subdir='tgt2src')
            else:
                images_concat = make_grid([s, s2t],
                                          nrow=2,
                                          padding=4,
                                          pad_value=(243, 124, 42))
                self.summary_writer.add_image('src/src_src2tgt_{}'.format(i),
                                              images_concat, self.global_step)

                images_concat = make_grid([t, t2s],
                                          nrow=2,
                                          padding=4,
                                          pad_value=(243, 124, 42))
                self.summary_writer.add_image('tgt/tgt_tgt2src_{}'.format(i),
                                              images_concat, self.global_step)

            i += 1
            self.global_step += 1
Example #2
0
    def save(self, src, src2tgt, tgt, tgt2src):
        # Un-normalize
        src, src2tgt = un_normalize(src), un_normalize(src2tgt)
        tgt, tgt2src = un_normalize(tgt), un_normalize(tgt2src)

        # Make grid of images
        i = 0
        for s, s2t, t, t2s in zip(src, src2tgt, tgt, tgt2src):
            # Save image
            if self.summary_writer is None:
                images_concat = make_grid([s, s2t], nrow=2, padding=0)
                file_name = 'src2tgt_{:04d}.png'.format(self.global_step)
                self._write(images_concat, file_name)

                images_concat = make_grid([t, t2s], nrow=2, padding=0)
                file_name = 'tgt2src_{:04d}.png'.format(self.global_step)
                self._write(images_concat, file_name)
            else:
                images_concat = make_grid([s, s2t],
                                          nrow=2,
                                          padding=4,
                                          pad_value=(243, 124, 42))
                self.summary_writer.add_image('src/src_src2tgt_{}'.format(i),
                                              images_concat, self.global_step)

                images_concat = make_grid([t, t2s],
                                          nrow=2,
                                          padding=4,
                                          pad_value=(243, 124, 42))
                self.summary_writer.add_image('tgt/tgt_tgt2src_{}'.format(i),
                                              images_concat, self.global_step)

            i += 1
            self.global_step += 1
Example #3
0
def test():
    screen = util.make_grid(3, 7, fill='.')

    screen = run(screen, 'rect 3x2')
    assert util.grid_to_text(screen) == r"""
###....
###....
.......""".strip()

    screen = run(screen, 'rotate column x=1 by 1')
    assert util.grid_to_text(screen) == r"""
#.#....
###....
.#.....""".strip()

    screen = run(screen, 'rotate row y=0 by 4')
    assert util.grid_to_text(screen) == r"""
....#.#
###....
.#.....""".strip()

    screen = run(screen, 'rotate column x=1 by 1')
    assert util.grid_to_text(screen) == r"""
.#..#.#
#.#....
.#.....""".strip()
Example #4
0
 def render(self, fname):
     self.batch_imgs = np.stack(self.batch_imgs)
     img_width = self.batch_imgs.shape[2]
     
     self.batch_imgs = util.make_grid(self.batch_imgs, len(self.batch_imgs), 2)
     print(self.uncs,'\n')
     tick_labels = [f'{angle:.02f}\n{unc:.04f}\n{label_hat}'
                    for (angle, unc, label_hat) 
                    in zip(self.del_angles, self.uncs, self.label_hats)]
     util.save_batch_fig(fname, self.batch_imgs, img_width, tick_labels)
Example #5
0
    def render(self, fname):
        self.batch_imgs = np.stack(self.batch_imgs)
        img_width = self.batch_imgs.shape[2]

        self.batch_imgs = util.make_grid(self.batch_imgs, len(self.batch_imgs),
                                         2)
        # print(self.uncs, '\n')
        # print(self.accs, '\n')
        if self.action_size == 1:
            tick_labels = [
                str([float(f'{p:.01f}') for p in param]) +
                f'\n{unc:.04f}\n{label_hat}\n{reward:.04f}'
                for (param, unc, label_hat, reward)
                # in zip(self.del_params, self.uncs, self.label_hats, self.rewards)]
                in zip(self.del_params, self.accs, self.label_hats,
                       self.rewards)
            ]
        elif self.action_size == 3:
            tick_labels = [
                str([float(f'{p:.01f}') for p in param[:1]]) + '\n' +
                str([float(f'{p:.03f}') for p in param[1:3]]) +
                f'\n{unc:.04f}\n{label_hat}\n{reward:.04f}'
                for (param, unc, label_hat, reward)
                # in zip(self.del_params, self.uncs, self.label_hats, self.rewards)]
                in zip(self.del_params, self.accs, self.label_hats,
                       self.rewards)
            ]
        elif self.action_size == 5:
            tick_labels = [
                str([float(f'{p:.01f}') for p in param[:1]]) + '\n' +
                str([float(f'{p:.03f}') for p in param[1:3]]) + '\n' +
                str([float(f'{p:.03f}') for p in param[3:5]]) +
                f'\n{unc:.04f}\n{label_hat}\n{reward:.04f}'
                for (param, unc, label_hat, reward)
                # in zip(self.del_params, self.uncs, self.label_hats, self.rewards)]
                in zip(self.del_params, self.accs, self.label_hats,
                       self.rewards)
            ]
        else:  # self.action_size == 7
            tick_labels = [
                str([float(f'{p:.01f}') for p in param[:1]]) + '\n' +
                str([float(f'{p:.03f}') for p in param[1:3]]) + '\n' +
                str([float(f'{p:.03f}') for p in param[3:5]]) + '\n' +
                str([float(f'{p:.01f}') for p in param[5:7]]) +
                f'\n{unc:.04f}\n{label_hat}\n{reward:.04f}'
                for (param, unc, label_hat, reward)
                # in zip(self.del_params, self.uncs, self.label_hats, self.rewards)]
                in zip(self.del_params, self.accs, self.label_hats,
                       self.rewards)
            ]
        util.save_batch_fig(fname, self.batch_imgs, img_width, tick_labels)
Example #6
0
                    pred_map_batch > 0.5)  # visualize the first in the batch
                vis_cands.append(res)

                if ti % 10 == 0:
                    print(
                        'TEST [iter %d/%d, epoch %.3f]: loss=%f, mean_IU=%f' %
                        (ti, test_iter, test_epoch, loss, score))
                    sys.stdout.flush()

            vis.compute_scores(suffix=it)
            tot_save = 0
            if opt.eval:
                tot_save = len(vis_cands[0])
            for tt in range(tot_save):
                save_list = [a[tt] for a in vis_cands]
                vis_grid = make_grid(save_list, ncols=6)
                vis_grid = misc.imresize(vis_grid, 0.5)
                misc.imsave(
                    os.path.join(
                        opt.checkpoint_path,
                        'img/test_results_{}_{}.png'.format(
                            str(test_epoch), str(tt))), vis_grid)
            vis.reset()
            epoch_iter = 0
            if opt.eval: break

        start = time.time()

        # 1.进入data_gen的imerge函数
        # 2.调用image中Iterator类的iter函数
        # 3.
Example #7
0
    assert util.grid_to_text(screen) == r"""
#.#....
###....
.#.....""".strip()

    screen = run(screen, 'rotate row y=0 by 4')
    assert util.grid_to_text(screen) == r"""
....#.#
###....
.#.....""".strip()

    screen = run(screen, 'rotate column x=1 by 1')
    assert util.grid_to_text(screen) == r"""
.#..#.#
#.#....
.#.....""".strip()


test()

with open('input.txt', 'r') as f:
    input = f.read()
    screen = util.make_grid(6, 50, fill='.')
    screen = run(screen, input)
    cnt = util.grid_count(screen, '#')
    print('Part 1 count:', cnt)

    render = util.grid_to_text(screen, {'.': util.PIXEL_WHITE})
    #FIXME - PIXEL_BLACK render size looks bad
    print(render)
Example #8
0
    def end_iter(self):
        """Log info for end of an iteration."""
        self.iter += self.batch_size
        self.global_step += self.batch_size

        # Periodically write to the log and TensorBoard
        if self.global_step % self.iters_per_print == 0:
            # Write a header for the log entry
            avg_time = (time() - self.iter_start_time) / self.batch_size
            message = '[epoch: {}, iter: {} / {}, time: {:.2f}] '\
                      .format(self.epoch, self.iter, self.dataset_len, avg_time)

            # Write the current error report
            loss_dict = self.model.get_loss_dict()
            loss_keys = ['loss_g', 'loss_d'
                         ]  # Can add other losses here (e.g., 'loss_g_l1').
            loss_strings = [
                '{}: {:.3g}'.format(k, loss_dict[k]) for k in loss_keys
            ]
            message += ', '.join(loss_strings)

            # Write all errors as scalars to the graph
            for k, v in loss_dict.items():
                # Group generator and discriminator losses
                if k.startswith('loss_d'):
                    k = 'd/' + k
                else:
                    k = 'g/' + k
                self.summary_writer.add_scalar(k, v, self.global_step)

            self.write(message)

        # Periodically visualize training examples
        if self.global_step % self.iters_per_visual == 0:
            image_dict = self.model.get_image_dict()
            if len(image_dict) % 3 == 0:
                # Image dict contains full cycle (e.g., in CycleGAN)
                plot_groups = ('src', 'tgt')
                image_keys = {
                    'src': ('src', 'src2tgt', 'src2tgt2src'),
                    'tgt': ('tgt', 'tgt2src', 'tgt2src2tgt')
                }
                nrow = 3
            elif len(image_dict) > 2:
                # Image dict contains src2tgt, tgt2src (e.g., in CycleFlow/Flow2Flow)
                plot_groups = ('src', 'tgt')
                image_keys = {
                    'src': ('src', 'src2tgt'),
                    'tgt': ('tgt', 'tgt2src')
                }
                nrow = 2
            else:
                # Image dict just contains src -> tgt (e.g., in Pix2Pix)
                plot_groups = ('example', )
                image_keys = {'example': ('src', 'src2tgt')}
                nrow = 2

            # Concatenate images into triples
            for plot_group in plot_groups:
                image_batches = [image_dict[k] for k in image_keys[plot_group]]
                for i in range(self.num_visuals):
                    images = []
                    for image_batch in image_batches:
                        if len(image_batch) > i:
                            images.append(image_batch[i])
                    if len(images) == 0:
                        continue
                    images_title = '{}/{}_{}'.format(
                        plot_group, '_'.join(image_keys[plot_group]), i + 1)
                    images_concat = util.make_grid(images,
                                                   nrow=nrow,
                                                   padding=4,
                                                   pad_value=(243, 124, 42))
                    self.summary_writer.add_image(images_title, images_concat,
                                                  self.global_step)