Beispiel #1
0
def eval_epoch(data_loader, model, writer, epoch):
    metrics = {
        "loss": Mean(),
    }

    for images in tqdm(data_loader, desc="epoch {} evaluation".format(epoch)):
        images_1, targets, images_3 = [image.to(DEVICE) for image in images]

        preds, etc = model(images_1, images_3)

        loss = compute_loss(input=preds, target=targets)

        metrics["loss"].update(loss.data.cpu().numpy())

    writer.add_scalar("loss",
                      metrics["loss"].compute_and_reset(),
                      global_step=epoch)
    writer.add_image("target",
                     utils.make_grid(denormalize(targets)),
                     global_step=epoch)
    writer.add_image("pred",
                     utils.make_grid(denormalize(preds)),
                     global_step=epoch)
    with torch.no_grad():
        video = make_video(model, images_1, images_3)
    writer.add_video("video/interp", video, fps=5, global_step=epoch)
    for k in etc:
        if k.startswith("flow"):
            writer.add_image(k,
                             utils.make_grid(flow_to_rgb(etc[k])),
                             global_step=epoch)
        else:
            writer.add_image(k, utils.make_grid(etc[k]), global_step=epoch)
    writer.flush()
Beispiel #2
0
    def on_epoch_end(self, epoch, logs):
        # Initialize GRADCam Class
        cam = GradCAM(self.model, self.layer_name)
        count = 0
        foldername = self.save_dir + '/epoch{}'.format(epoch)
        if not os.path.exists(foldername):
            os.makedirs(foldername)
        for data in self.activation_data:
            image = data[0]
            image = np.expand_dims(image, 0)
            pred = model.predict(image)
            classIDx = np.argmax(pred[0])

            # Compute Heatmap
            heatmap = cam.compute_heatmap(image, classIDx)
            image = image.reshape(image.shape[1:])
            image = image * 255
            image = image.astype(np.uint8)

            # Overlay heatmap on original image
            heatmap = cv2.resize(heatmap, (image.shape[1], image.shape[0]))
            plt.imshow(np.squeeze(image))
            plt.imshow(heatmap, alpha=.6, cmap='inferno')
            plt.axis('off')
            plt.savefig(self.save_dir +
                        '/epoch{}/out{}.png'.format(epoch, count),
                        bbox_inches='tight',
                        transparent=True,
                        pad_inches=0)
            plt.clf()
            count += 1
        make_grid(foldername)
Beispiel #3
0
def test_make_grid():
    'Makes 2D grid from list using F or C order'
    mk_array = lambda ar: np.array(ar, np.int32)

    def ar_eq_(ar1, ar2):
        'Asssert equality of two arrays'
        for pair in zip(ar1.flatten(), ar2.flatten()):
            eq_(*pair)

    pairs = ((([0, 3], [1, 4], [2, 5]),
              make_grid(3, 2, top0=True, lst=range(6),
                        order='F')), (([1, 3], [0, 2]),
                                      make_grid(2,
                                                2,
                                                top0=False,
                                                lst=range(4),
                                                order='F')),
             (([0, 1], [2,
                        3]), make_grid(2,
                                       2,
                                       top0=True,
                                       lst=range(4),
                                       order='C')))

    for ar, grid in pairs:
        ar_eq_(mk_array(ar), grid)
Beispiel #4
0
def main():
    logging.basicConfig(level=logging.INFO)
    args = build_parser().parse_args()
    logging.info(args_to_string(args))
    fix_seed(args.seed)

    data_loader = torch.utils.data.DataLoader(
        Dataset(args.dataset_path),
        batch_size=args.batch_size,
        shuffle=True,
        num_workers=os.cpu_count(),
        drop_last=True,
    )

    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    encoder = Encoder(args.model_size, args.latent_size)
    decoder = Decoder(args.model_size, args.latent_size)
    encoder.to(device)
    decoder.to(device)

    opt = torch.optim.Adam(list(encoder.parameters()) +
                           list(decoder.parameters()),
                           lr=args.learning_rate)
    noise_dist = torch.distributions.Normal(0, 1)

    writer = SummaryWriter(args.experiment_path)
    metrics = {"loss": Mean()}

    for epoch in range(args.epochs):
        encoder.train()
        decoder.train()
        for real, _ in tqdm(data_loader,
                            desc="epoch {} training".format(epoch)):
            real = real.to(device)

            mean, log_var = encoder(real)
            latent = noise_dist.sample(
                (args.batch_size, args.latent_size)).to(device)
            latent = mean + latent * torch.exp(log_var / 2)
            fake = decoder(latent)

            # TODO: loss (reconstruction, summing, mean)
            mse = F.mse_loss(input=fake, target=real)
            kld = -0.5 * (1 + log_var - mean**2 - log_var.exp()).sum(-1)
            loss = mse.mean() + kld.mean()
            metrics["loss"].update(loss.data.cpu().numpy())

            opt.zero_grad()
            loss.mean().backward()
            opt.step()

        writer.add_scalar("loss",
                          metrics["loss"].compute_and_reset(),
                          global_step=epoch)
        writer.add_image("real",
                         utils.make_grid((real + 1) / 2),
                         global_step=epoch)
        writer.add_image("fake",
                         utils.make_grid((fake + 1) / 2),
                         global_step=epoch)
Beispiel #5
0
def pathfinder(algorithm, name, win, win_rows, win_width, line_height):
    pygame.display.set_caption(name)
    grid = utils.make_grid(win_rows, line_height)

    start = None
    end = None

    run = True
    started = False
    while run:
        utils.draw(win, grid, win_rows, win_width, line_height=line_height)
        for event in pygame.event.get():
            if utils.is_quit(event):
                run = False

            if started:
                continue

            if pygame.mouse.get_pressed()[0]:
                pos = pygame.mouse.get_pos()
                row, col = utils.get_clicked_pos(pos, win_rows, win_width,
                                                 line_height)
                if row >= 0 and col >= 0:
                    spot = grid[row][col]
                    if not start and spot != end:
                        start = spot
                        start.make_start()
                    elif not end and spot != start:
                        end = spot
                        end.make_end()
                    elif spot != end and spot != start:
                        spot.make_barrier()
            elif pygame.mouse.get_pressed()[2]:
                pos = pygame.mouse.get_pos()
                row, col = utils.get_clicked_pos(pos, win_rows, win_width,
                                                 line_height)
                if row >= 0 and col >= 0:
                    spot = grid[row][col]
                    spot.reset()
                    if spot == start:
                        start = None
                    elif spot == end:
                        end = None

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE and start and end:
                    for row in grid:
                        for spot in row:
                            spot.update_neighbors(grid)
                    algorithm(
                        lambda: utils.draw(win, grid, win_rows, win_width,
                                           line_height), grid, start, end)

                if event.key == pygame.K_c:
                    start = None
                    end = None
                    grid = utils.make_grid(win_rows, line_height)
def make_sprite(label_img, save_path):
    import math
    nrow = int(math.floor(math.sqrt(label_img.shape[0])))
    xx = utils.make_grid(np.zeros((1, 3, 32, 32)), padding=0)
    if xx.shape[2] == 33:  # https://github.com/pytorch/vision/issues/206
        sprite = utils.make_grid(label_img, nrow=nrow, padding=0)
        sprite = sprite[:, 1:, 1:]
        utils.save_image(sprite, os.path.join(save_path, 'sprite.png'))
    else:
        utils.save_image(label_img,
                         os.path.join(save_path, 'sprite.png'),
                         nrow=nrow,
                         padding=0)
Beispiel #7
0
    def generate_data_walk(self, real, number):
        self.model.eval()
        if not os.path.exists('interpolated_images/'):
            os.makedirs('interpolated_images/')

        print('interpolating')

        # Interpolate between the noise(z1, z2) with number_int steps between
        number_int = 10
        img1 = real[0]
        img2 = real[1]

        images = []
        alpha_step = 1.0 / float(number_int + 1)
        print(alpha_step)
        alpha = 0

        for i in range(1, number_int + 1):
            img = img1 * alpha + img2 * (1.0 - alpha)
            alpha += alpha_step
            im = img.mul(0.5).add(0.5)  #denormalize
            images.append(im.view(3, 32, 32).data.cpu())

        grid = utils.make_grid(images, nrow=number_int)
        utils.save_image(grid,
                         'interpolated_images/data_interpolated_{}.png'.format(
                             str(number).zfill(3)),
                         nrow=8)
        print(
            "Saved data interpolated images to interpolated_images/interpolated_{}."
            .format(str(number).zfill(3)))
Beispiel #8
0
    def generate_latent_walk_disentangled(self, number):
        self.model.eval()
        if not os.path.exists('interpolated_images/'):
            os.makedirs('interpolated_images/')
        z = torch.randn(1, 100)

        for i in range(10, 90):
            z1 = torch.clone(z).cuda()
            images = []
            lower_lim = -1.0
            step_size = 0.2
            for j in range(10):

                z1[0, i] = lower_lim + step_size
                lower_lim += step_size

                fake_im = self.model.generator(z1)
                fake_im = fake_im.mul(0.5).add(0.5)  #denormalize
                images.append(fake_im.view(3, 32, 32).data.cpu())

            grid = utils.make_grid(images, nrow=10)
            utils.save_image(
                grid,
                'interpolated_images/varying_dim-{}_{}_epoch{}.png'.format(
                    str(i), str(j),
                    str(number).zfill(3)))
            print(
                "Saved disentangled interpolated images to interpolated_images/interpolated_{}."
                .format(str(number).zfill(3)))
    def fit(self, X, num_epoch=50, seed=0, f_resolution=10, init='random'):
        N, D = X.shape
        if init != 'random':
            Z = init.copy()
        else:
            width = (max(self.clipping) - min(self.clipping))
            mid = sum(self.clipping) / 2
            low, high = mid - self.scale * width, mid + self.scale * width
            np.random.seed(seed)
            Z = np.random.uniform(low=low, high=high, size=(N, self.L))
        X, Z = jnp.array(X), jnp.array(Z)

        history = dict(E=np.zeros((num_epoch, )),
                       Y=np.zeros((num_epoch, N, D)),
                       f=np.zeros((num_epoch, f_resolution**self.L, D)),
                       Z=np.zeros((num_epoch, N, self.L)))

        for epoch in tqdm(range(num_epoch)):
            Y = estimate_f(Z, Z, X, self.sigma)
            Z = estimate_z(X, Z, self.sigma, self.eta, self.clipping)
            Z_new = make_grid(f_resolution, (Z.min(), Z.max()))
            Z_new = jnp.array(Z_new)
            Y_new = estimate_f(Z_new, Z, X, self.sigma)

            history['E'][epoch] = np.sum((Y - X)**2) / N
            history['Y'][epoch] = np.array(Y)
            history['Z'][epoch] = np.array(Z)
            history['f'][epoch] = np.array(Y_new)
        return history
Beispiel #10
0
    def get_keyboard(self):
        ntf = self.next_to_fill()

        try:
            return self.basic_keyboards[ntf](self)
        except KeyError:
            pass

        try:
            hint = self.hints[ntf](self)
        except KeyError:
            return None

        if self.more:
            hint = territories - hint

        keyboard = make_grid(sorted(hint, key=str.casefold))

        keyboard.append(["More..."])

        if ntf == "TERR" and self.terrs:
            if not self.terr_remove:
                keyboard.append(["Remove"])
            else:
                keyboard.append(["Add"])

            keyboard.append(["Done"])

        keyboard.append(["Back"])

        return RKM(keyboard)
    def fit(self, X, num_epoch=50, seed=0, f_resolution=10, init='random'):
        N, D = X.shape

        if init != 'random':
            Z = init.copy()
        else:
            np.random.seed(seed)
            Z = np.random.normal(scale=self.scale, size=(N, self.L))
        history = dict(E=np.zeros((num_epoch, )),
                       Y=np.zeros((num_epoch, N, D)),
                       f=np.zeros((num_epoch, f_resolution**self.L, D)),
                       Z=np.zeros((num_epoch, N, self.L)))

        for epoch in tqdm(range(num_epoch)):
            Y, R = self.estimate_f(X, Z)
            Z = self.estimate_e(X, Y, Z, R)

            Z_new = make_grid(f_resolution,
                              bounds=(np.min(Z), np.max(Z)),
                              dim=self.L)
            f, _ = self.estimate_f(X, Z_new, Z)

            history['Y'][epoch] = Y
            history['f'][epoch] = f
            history['Z'][epoch] = Z
            history['E'][epoch] = np.sum(
                (Y - X)**2) / N + self.λ * np.sum(Z**2)

        return history
Beispiel #12
0
    def show_retreats_prompt(self, bot, game, player):
        try:
            t = next(t1 for t1, k, t2 in player.retreat_choices if t2 is None)
        except StopIteration:
            message = "The following retreats will be attempted:\n\n"

            for t1, k, t2 in player.retreat_choices:
                message += "{}-{}\n".format(t1, t2)

            message += "\nIs this correct"

            keyboard = [
                ["Yes", "No"],
            ]

            bot.send_message(player.id, message, reply_markup=RKM(keyboard))

            return

        keyboard = make_grid(sorted(player.retreats[t], key=str.casefold))

        keyboard.append(["Disband"])

        if next(t2 for t1, k, t2 in player.retreat_choices) is not None:
            keyboard.append(["Back"])

        bot.send_message(player.id,
                         "Where should {} retreat to?".format(t),
                         reply_markup=RKM(keyboard))
    def fit(self, X, num_epoch=50, seed=0, f_resolution=10, init='random'):
        N, D = X.shape

        if init != 'random':
            Z = init.copy()
        else:
            width = (max(self.clipping) - min(self.clipping))
            mid = sum(self.clipping) / 2
            low, high = mid - self.scale*width, mid + self.scale* width
            np.random.seed(seed)
            Z = np.random.uniform(low=low, high=high, size=(N, self.L))
        history = dict(
            E=np.zeros((num_epoch,)),
            Y=np.zeros((num_epoch, N, D)),
            f=np.zeros((num_epoch, f_resolution**self.L, D)),
            Z=np.zeros((num_epoch, N, self.L)))

        for epoch in tqdm(range(num_epoch)):
            Y, R = self.estimate_f(X, Z)
            Z = self.estimate_e(X, Y, Z, R)

            Z_new = make_grid(f_resolution,
                              bounds=(np.min(Z), np.max(Z)),
                              dim=self.L)
            f, _ = self.estimate_f(X, Z_new, Z)

            history['Y'][epoch] = Y
            history['f'][epoch] = f
            history['Z'][epoch] = Z
            history['E'][epoch] = np.sum((Y - X)**2) / N
        return history
Beispiel #14
0
    def generate_latent_walk(self, number):
        self.model.eval()
        if not os.path.exists('interpolated_images/'):
            os.makedirs('interpolated_images/')

        print('interpolating')

        # Interpolate between the noise(z1, z2) with number_int steps between
        number_int = 10
        z_intp = torch.FloatTensor(1, 100)
        z1 = torch.randn(1, 100)
        z2 = torch.randn(1, 100)
        z_intp = z_intp.cuda()
        z1 = z1.cuda()
        z2 = z2.cuda()

        z_intp = Variable(z_intp)
        images = []
        alpha_step = 1.0 / float(number_int + 1)
        print(alpha_step)
        alpha = 0
        for i in range(1, number_int + 1):
            z_intp.data = z1 * alpha + z2 * (1.0 - alpha)
            alpha += alpha_step
            fake_im = self.model.generator(z_intp)
            fake_im = fake_im.mul(0.5).add(0.5)  #denormalize
            images.append(fake_im.view(3, 32, 32).data.cpu())

        grid = utils.make_grid(images, nrow=number_int)
        utils.save_image(
            grid, 'interpolated_images/interpolated_{}.png'.format(
                str(number).zfill(3)))
        print(
            "Saved latent interpolated images to interpolated_images/interpolated_{}."
            .format(str(number).zfill(3)))
Beispiel #15
0
def test_ind_to_ij():
  '''Consistency of make_grid (which maps list to 2D grid) and ind_to_ij
     which maps list index to ij position on grid'''
  height, width = 3,2
  grid = make_grid(height,width,lst=range(height*width),order='F')
  for ind in range(height*width):
    ij = tuple( ind_to_ij(height,width,ind,'F') )
    eq_( grid[ij], ind )
Beispiel #16
0
def test_ind_to_ij():
    '''Consistency of make_grid (which maps list to 2D grid) and ind_to_ij
     which maps list index to ij position on grid'''
    height, width = 3, 2
    grid = make_grid(height, width, lst=range(height * width), order='F')
    for ind in range(height * width):
        ij = tuple(ind_to_ij(height, width, ind, 'F'))
        eq_(grid[ij], ind)
Beispiel #17
0
    def make_image(trainer):
        fake = G(inputv, test=True)
        fake = chainer.cuda.to_cpu(fake.data)
        img = make_grid(fake)
        img = np.asarray(np.transpose(np.clip((img + 1) * 127.5, 0, 255),
                                      (1, 2, 0)),
                         dtype=np.uint8)

        imsave(os.path.join(dst, name.format(trainer.updater.iteration)), img)
Beispiel #18
0
def log(phase):
    writer.add_scalar(f'{phase}_loss', loss.item(), global_step)

    if i % args.display_freq == 0:
        recon = torch.cat((x[0], x_[0]), dim=2)
        latent = make_grid(z[0].unsqueeze(1), 4, 4)
        if args.display:
            view_in.render(recon)
            view_z.render(latent)
        writer.add_image(f'{phase}_recon', recon, global_step)
        writer.add_image(f'{phase}_latent', latent.squeeze(0), global_step)
Beispiel #19
0
def test_make_grid():
  'Makes 2D grid from list using F or C order'
  mk_array = lambda ar: np.array(ar,np.int32)

  def ar_eq_(ar1,ar2):
    'Asssert equality of two arrays'
    for pair in zip( ar1.flatten(), ar2.flatten() ):
      eq_(*pair)
  
  pairs = ( ( ([0,3],[1,4],[2,5]), 
              make_grid(3,2, top0=True, lst = range(6), order='F') ),
            
            ( ([1,3],[0,2]),
              make_grid( 2, 2,top0=False, lst = range(4), order='F') ),
            
            ( ([0,1],[2,3]),
              make_grid( 2, 2, top0=True, lst = range(4), order='C') ) )
  
  for ar,grid in pairs:
    ar_eq_( mk_array(ar), grid)
Beispiel #20
0
    def sample(self, padding=2):

        dpi = 100

        try:
            fake_X = self.generator(self.fixed_z, self.sample_y).detach().cpu()

            images = make_grid(fake_X,
                               samples_per_class=self.samples_per_class,
                               num_classes=self.num_classes,
                               padding=padding,
                               im_size=self.sample_im_size)

            if (self.current_epoch % self.checkpoint_every) == 0:

                y_labels = [
                    'AK', 'BCC', 'BKL', 'DF', 'MEL', 'NV', 'SCC', 'VASC'
                ]
                y_ptr = self.sample_im_size // 2
                y_locs = []

                while y_ptr < images.shape[0]:
                    y_locs.append(y_ptr)
                    y_ptr += (self.sample_im_size + padding)

                assert len(y_labels) == len(y_locs)

                fig, ax = plt.subplots(figsize=(images.shape[1] / dpi,
                                                images.shape[0] / dpi),
                                       dpi=dpi)
                ax.imshow(images, interpolation='nearest')
                ax.set_xticks([])
                plt.yticks(ticks=y_locs, labels=y_labels)
                plt.savefig(self.checkpoint_path +
                            'samples/{}.png'.format(self.current_epoch),
                            bbox_inches='tight',
                            dpi=dpi)

                # CLOSE THE FIGURE.
                plt.close(fig)

            #if (self.current_epoch % self.show_every) == 0:
            #    plt.show()

        except AttributeError:
            n_samples = self.num_classes * self.samples_per_class
            sample_y = np.repeat(np.arange(self.num_classes),
                                 self.samples_per_class)
            self.sample_y = torch.LongTensor(sample_y).to(self.device)
            self.fixed_z = torch.FloatTensor(size=(n_samples,
                                                   self.z_dim)).normal_(
                                                       0., 1.).to(self.device)
            self.sample()
Beispiel #21
0
 def make_image(trainer):
     with chainer.using_config("Train", False):
         with chainer.no_backprop_mode():
             fake = G(inputv)
             fake = chainer.cuda.to_cpu(fake.data)
             img = make_grid(fake)
             img = np.asarray(np.transpose(
                 np.clip((img + 1) * 127.5, 0, 255), (1, 2, 0)),
                              dtype=np.uint8)
             imsave(
                 os.path.join(dst, name.format(trainer.updater.iteration)),
                 img)
    def _make_images_board(self, model):
        model.eval()
        num_imgs = 64
        fuseTrans = self.cfg.fuseTrans

        batch = next(iter(self.data_loaders[1]))
        input_images, renderTrans, depthGT, maskGT = utils.unpack_batch_novel(batch, self.cfg.device)

        with torch.set_grad_enabled(False):
            XYZ, maskLogit = model(input_images)
            # ------ build transformer ------
            XYZid, ML = transform.fuse3D(
                self.cfg, XYZ, maskLogit, fuseTrans) # [B,3,VHW],[B,1,VHW]
            newDepth, newMaskLogit, collision = transform.render2D(
                self.cfg, XYZid, ML, renderTrans)  # [B,N,1,H,W]

        return {'RGB': utils.make_grid( input_images[:num_imgs]),
                'depth': utils.make_grid(
                    ((1-newDepth)*(collision==1).float())[:num_imgs, 0, 0:1, :, :]),
                'depthGT': utils.make_grid(
                    1-depthGT[:num_imgs, 0, 0:1, :, :]),
                'mask': utils.make_grid(
                    torch.sigmoid(maskLogit[:num_imgs, 0:1,:, :])),
                'mask_rendered': utils.make_grid(
                    torch.sigmoid(newMaskLogit[:num_imgs, 0, 0:1, :, :])),
                'maskGT': utils.make_grid(
                    maskGT[:num_imgs, 0, 0:1, :, :]),
                }
Beispiel #23
0
    def _make_images_board(self, model):
        model.eval()
        num_imgs = 64

        batch = next(iter(self.data_loaders[1]))
        input_images, depthGT, maskGT = utils.unpack_batch_fixed(
            batch, self.cfg.device)

        with torch.set_grad_enabled(False):
            XYZ, maskLogit = model(input_images)
            XY = XYZ[:, :self.cfg.outViewN * 2, :, :]
            depth = XYZ[:, self.cfg.outViewN * 2:self.cfg.outViewN * 3, :, :]
            mask = (maskLogit > 0).float()

        return {
            'RGB':
            utils.make_grid(input_images[:num_imgs]),
            'depth':
            utils.make_grid(1 - depth[:num_imgs, 0:1, :, :]),
            'depth_mask':
            utils.make_grid(((1 - depth) * mask)[:num_imgs, 0:1, :, :]),
            'depthGT':
            utils.make_grid(1 - depthGT[:num_imgs, 0:1, :, :]),
            'mask':
            utils.make_grid(torch.sigmoid(maskLogit[:num_imgs, 0:1, :, :])),
            'maskGT':
            utils.make_grid(maskGT[:num_imgs, 0:1, :, :]),
        }
Beispiel #24
0
    def _make_images_board(self, model):
        model.eval()
        num_imgs = 64
        fuseTrans = self.cfg.fuseTrans

        batch = next(iter(self.data_loaders[1]))
        input_images, renderTrans, depthGT, maskGT = utils.unpack_batch_novel(
            batch, self.cfg.device)

        with torch.set_grad_enabled(False):
            XYZ, maskLogit = model(input_images)
            # ------ build transformer ------
            XYZid, ML = transform.fuse3D(self.cfg, XYZ, maskLogit,
                                         fuseTrans)  # [B,3,VHW],[B,1,VHW]
            newDepth, newMaskLogit, collision = transform.render2D(
                self.cfg, XYZid, ML, renderTrans)  # [B,N,1,H,W]

        return {
            'RGB':
            utils.make_grid(input_images[:num_imgs]),
            'depth':
            utils.make_grid(
                ((1 - newDepth) * (collision == 1).float())[:num_imgs, 0,
                                                            0:1, :, :]),
            'depthGT':
            utils.make_grid(1 - depthGT[:num_imgs, 0, 0:1, :, :]),
            'mask':
            utils.make_grid(torch.sigmoid(maskLogit[:num_imgs, 0:1, :, :])),
            'mask_rendered':
            utils.make_grid(
                torch.sigmoid(newMaskLogit[:num_imgs, 0, 0:1, :, :])),
            'maskGT':
            utils.make_grid(maskGT[:num_imgs, 0, 0:1, :, :]),
        }
Beispiel #25
0
    def show_build_prompt(self, bot, game, player):
        try:
            t, k, c = player.units_choices[-1]
        except IndexError:
            t, k, c = True, True, True

        if not k:
            message = "What kind of unit do you want to build?"
            keyboard = [["Army", "Fleet"], ["Back"]]

        elif not c and t in split_coasts and k == "F":
            message = "On which coast?"
            keyboard = [["North", "South"], ["Back"]]

        elif not player.units_delta or player.units_done:
            if not player.units_choices:
                message = "No new units will be built."

            elif len(player.units_choices) == 1:
                message = ("This new unit will be built:\n\n" +
                           self.format_build(player.units_choices))

            else:
                message = ("These new units will be built:\n\n" +
                           self.format_build(player.units_choices))

            message += "\nAre you sure?"

            keyboard = [["Yes", "No"]]

        else:
            keyboard = make_grid(
                sorted(player.units_options -
                       {t
                        for t, k, c in player.units_choices},
                       key=str.casefold))

            keyboard.append(["Done"])

            if not player.units_choices:
                message = "Where do you want to build?"

            else:
                message = ("Currently building:\n\n" +
                           self.format_build(player.units_choices) +
                           "\nWhat else?")

                keyboard.append(["Back"])

        bot.send_message(player.id, message, reply_markup=RKM(keyboard))
Beispiel #26
0
def make_video(model, images_1, images_3):
    interp_video = []

    interp_video.append(torch.zeros_like(images_1))
    interp_video.append(denormalize(images_1))
    for t in np.arange(0.1, 1.0, 0.1):
        preds, etc = model(images_1, images_3, t=t)
        interp_video.append(denormalize(preds))
    interp_video.append(denormalize(images_3))
    interp_video.append(torch.zeros_like(images_3))

    interp_video = [utils.make_grid(images) for images in interp_video]
    interp_video = torch.stack(interp_video, 0).unsqueeze(0)

    return interp_video
Beispiel #27
0
    def show_disband_prompt(self, bot, game, player):
        if not player.units_delta:
            message = ("These units will be disbanded: {}\n"
                       "Are you sure?".format(", ".join(player.units_choices)))

            keyboard = [["Yes", "No"]]

        else:
            keyboard = make_grid(
                sorted(player.units_options.difference(player.units_choices),
                       key=str.casefold))

            if not player.units_choices:
                message = "Which unit should be disbanded{}?".format(
                    " first" if len(player.units_options) > 1 else "")

            else:
                message = "Disbanding {}. Who else?".format(", ".join(
                    player.units_choices))

                keyboard.append(["Back"])

        bot.send_message(player.id, message, reply_markup=RKM(keyboard))
    def _make_images_board(self, model):
        model.eval()
        num_imgs = 64
        fuseTrans = self.cfg.fuseTrans

        batch = next(iter(self.data_loaders[1]))
        input_images, renderTrans, depthGT, maskGT = utils.unpack_batch_novel(batch, self.cfg.device)

        with torch.set_grad_enabled(False):
            XYZ, maskLogit = model(input_images)
            ##################################
            tmp_1 = torch.cat([maskLogit[:,0:1,:,:],maskLogit[:,2:3,:,:],maskLogit[:,4:5,:,:],
                               maskLogit[:,6:7,:,:],maskLogit[:,7:8,:,:],maskLogit[:,9:10,:,:],
                               maskLogit[:,11:12,:,:],maskLogit[:,13:14,:,:]],1)

            #print(tmp_1.size())
            tmp_2 = torch.cat([maskLogit[:,1:2,:,:],maskLogit[:,3:4,:,:],maskLogit[:,5:6,:,:],
                               maskLogit[:,7:8,:,:],maskLogit[:,9:10,:,:],maskLogit[:,11:12,:,:],
                               maskLogit[:,13:14,:,:],maskLogit[:,15:16,:,:]],1)
            #mask = (maskLogit > 0).byte()
            maskLogit = (maskLogit[:,8:16,:,:] > 0).byte()
            maskLogit = maskLogit.float()
            #mask = (tmp_2 > 0).byte()
#                 print(mask.size())
            ###################################
            # ------ build transformer ------
            XYZid, ML = transform.fuse3D(
                self.cfg, XYZ, maskLogit, fuseTrans) # [B,3,VHW],[B,1,VHW]
            newDepth, newMaskLogit, collision = transform.render2D(
                self.cfg, XYZid, ML, renderTrans)  # [B,N,1,H,W]

        return {'RGB': utils.make_grid( input_images[:num_imgs]),
                'depth': utils.make_grid(
                    ((1-newDepth)*(collision==1).float())[:num_imgs, 0, 0:1, :, :]),
                'depthGT': utils.make_grid(
                    1-depthGT[:num_imgs, 0, 0:1, :, :]),
                'mask': utils.make_grid(
                    torch.sigmoid(maskLogit[:num_imgs, 0:1,:, :])),
                'mask_rendered': utils.make_grid(
                    torch.sigmoid(newMaskLogit[:num_imgs, 0, 0:1, :, :])),
                'maskGT': utils.make_grid(
                    maskGT[:num_imgs, 0, 0:1, :, :]),
                }
    def _make_images_board(self, model):
        model.eval()
        num_imgs = 64

        batch = next(iter(self.data_loaders[1]))
        input_images, depthGT, maskGT = utils.unpack_batch_fixed(batch, self.cfg.device)

        with torch.set_grad_enabled(False):
            XYZ, maskLogit = model(input_images)
            XY = XYZ[:, :self.cfg.outViewN * 2, :, :]
            depth = XYZ[:, self.cfg.outViewN * 2:self.cfg.outViewN * 3, :,  :]
            mask = (maskLogit > 0).float()

        return {'RGB': utils.make_grid(input_images[:num_imgs]),
                'depth': utils.make_grid(1-depth[:num_imgs, 0:1, :, :]),
                'depth_mask': utils.make_grid(
                    ((1-depth)*mask)[:num_imgs, 0:1, :, :]),
                'depthGT': utils.make_grid(
                    1-depthGT[:num_imgs, 0:1, :, :]),
                'mask': utils.make_grid(
                    torch.sigmoid(maskLogit[:num_imgs, 0:1,:, :])),
                'maskGT': utils.make_grid(maskGT[:num_imgs, 0:1, :, :]),
                }
Beispiel #30
0
def main(win, width):
    ROWS = 10
    grid = make_grid(ROWS, width)

    start = None
    end = None
    target = None
    boxes = []
    targets = []

    # default algorithm
    algorithms = itertools.cycle([astar, bfs])
    algorithm = next(algorithms)
    print(f'Current Algorithm {algorithm.__name__}')

    run = True
    while run:
        draw(win, grid, ROWS, width)
        for event in pygame.event.get():
            # quitting the window
            if event.type == pygame.QUIT:
                run = False

            # keyboard events
            if event.type == pygame.KEYDOWN:
                # the algorithm
                if event.key == pygame.K_SPACE and start and len(boxes):
                    # automated process
                    while len(targets) > 0 and len(boxes) > 0:
                        # clearing the grid except for start, boxes and barriers
                        start, grid = clear_grid(grid, all=False)

                        for row in grid:
                            for spot in row:
                                spot.update_neighbors(grid)

                        path, end = algorithm(
                            lambda: draw(win, grid, ROWS, width), grid, start,
                            end, boxes, targets, 'box')
                        # if the algorithm found a path
                        if path:
                            print('got the path')
                            # clear the opened and colosed spots
                            start, grid = clear_grid(grid,
                                                     all=False,
                                                     path=False)
                            end.make_thebox()
                            # Walking the robot
                            start, boxes, targets = walking_robot(
                                lambda: draw(win, grid, ROWS, width),
                                start,
                                path,
                                end,
                                boxes,
                                targets,
                                task='pickup')
                            # re-coloring the targets
                            for t in targets:
                                t.make_target()
                            for b in boxes:
                                b.make_end()

                            # switch to searching for target locations
                            path, end = algorithm(
                                lambda: draw(win, grid, ROWS, width), grid,
                                start, end, boxes, targets, 'target')
                            if path:
                                print('got the targets path')
                                # clear the opened and colosed spots
                                start, grid = clear_grid(grid,
                                                         all=False,
                                                         path=False)
                                end.make_thebox()
                                # Walking the robot
                                start, boxes, targets = walking_robot(
                                    lambda: draw(win, grid, ROWS, width),
                                    start,
                                    path,
                                    end,
                                    boxes,
                                    targets,
                                    task='putdown')
                                # re-coloring the targets
                                print(
                                    f'Number of targets after the movement: {len(targets)}'
                                )
                                for t in targets:
                                    t.make_target()
                                for b in boxes:
                                    b.make_end()
                            else:
                                print('Stuck')
                                break

                        else:
                            print('Stuck')
                            break

                # Changing algorithm
                if event.key == pygame.K_LCTRL:
                    algorithm = next(algorithms)
                    print(f'Current Algorithm {algorithm.__name__}')
                    # clearing the grid except for start, boxes and barriers
                    time.sleep(0.4)
                    start, grid = clear_grid(grid, all=False)

                # clearing the grid
                if event.key == pygame.K_c:
                    start, grid = clear_grid(grid)
                    boxes.clear()
                    targets.clear()

                # Adding A Robot
                if event.key == pygame.K_r:
                    # if pygame.mouse.get_pressed()[0]: # left mouse click
                    pos = pygame.mouse.get_pos()
                    row, col = get_clicked_pos(pos, ROWS, width)
                    spot = grid[row][col]

                    if not start and spot not in boxes and spot not in targets:
                        start = spot
                        start.make_start()

                # Adding boxes
                if event.key == pygame.K_b:
                    # if pygame.mouse.get_pressed()[0]: # left mouse click
                    pos = pygame.mouse.get_pos()
                    row, col = get_clicked_pos(pos, ROWS, width)
                    spot = grid[row][col]

                    if spot != start and spot != end and spot not in boxes and spot not in targets:
                        end = spot
                        end.make_end()
                        boxes.append(end)
                        print(f'Number of Boxes: {len(boxes)}')

                # Adding Targets
                if event.key == pygame.K_t:
                    pos = pygame.mouse.get_pos()
                    row, col = get_clicked_pos(pos, ROWS, width)
                    spot = grid[row][col]

                    if spot != start and spot != end and spot not in boxes and spot not in targets:
                        target = spot
                        target.make_target()
                        targets.append(target)
                        print(f'Number of Targets {len(targets)}')

                # Adding Barriers
                if event.key == pygame.K_p:
                    pos = pygame.mouse.get_pos()
                    row, col = get_clicked_pos(pos, ROWS, width)
                    spot = grid[row][col]

                    if spot != start and spot not in boxes and spot not in targets:
                        spot.make_barrier()

            # clearning spots
            if pygame.mouse.get_pressed()[2]:  # right mouse click
                pos = pygame.mouse.get_pos()
                row, col = get_clicked_pos(pos, ROWS, width)
                spot = grid[row][col]
                spot.reset()
                if spot == start:
                    start = None
                elif spot in boxes:
                    boxes.remove(spot)
                    end = None
                elif spot in targets:
                    targets.remove(spot)
                    target = None

                print(f'Number of Boxes  : {len(boxes)}')
                print(f'Number of Targets: {len(targets)}')

    # quit the window if it exits the while loop
    pygame.quit()
Beispiel #31
0
    "west": True,
}
if stretch["west"]:
    maxdx = stretchfactorx * dx
    nstrx = int(Lstretchx / dx)
    dxvec[:nstrx] = maxdx - (maxdx - dx) / nstrx * np.arange(nstrx)
    assert dxvec[0] == maxdx
    assert dxvec[nstrx] == dx

x = np.insert(np.cumsum(dxvec), 0, 0)
y = np.insert(np.cumsum(dyvec), 0, 0)


print("Making grid...")
grid = utils.process_cdl_output("./grd_spherical.nc", nx, ny, nz, spherical)
grid = utils.make_grid(grid, x, y)

# sponges
Lsponge = 7e3
sponges = {
    "east": False,
    "west": True,
    "north": False,
    "south": False,
}

nspx = int(Lsponge / dx)
nspy = int(Lsponge / dy)
visc_factor = xr.zeros_like(grid.visc_factor)

if sponges["west"]:
Beispiel #32
0
def main():
    toTensor = transforms.Compose([transforms.ToTensor()])
    toPILImg = transforms.ToPILImage()

    # Build model
    print('Loading model ...\n')
    net = FeaturesBlockDualNet(channels=1)
    device_ids = [0]

    model = nn.DataParallel(net, device_ids=device_ids).cuda()
    model.load_state_dict(
        torch.load(os.path.join(
            opt.logdir, 'FBDN_nl25_30.6378.pth')))  # Input model's path files.
    model.eval()
    # load data info
    print('Loading data info ...\n')
    files_source = glob.glob(os.path.join('data', opt.test_data, '*.png'))
    files_source.sort()
    # process data
    psnr_test = 0
    for f in files_source:
        # image
        Img = cv2.imread(f)
        Img = normalize(np.float32(Img[:, :, 0]))
        Img = np.expand_dims(Img, 0)
        Img = np.expand_dims(Img, 1)
        ISource = torch.Tensor(Img)

        # noise
        noise = torch.FloatTensor(ISource.size()).normal_(mean=0,
                                                          std=opt.test_noiseL /
                                                          255.)
        # noisy image
        INoisy = ISource + noise

        ISource, INoisy = Variable(ISource.cuda()), Variable(INoisy.cuda())
        with torch.no_grad():  # this can save much memory
            Out = torch.clamp(model(INoisy), 0., 1.)
        ## if you are using older version of PyTorch, torch.no_grad() may not be supported
        # ISource, INoisy = Variable(ISource.cuda(),volatile=True), Variable(INoisy.cuda(),volatile=True)
        # Out = torch.clamp(INoisy-model(INoisy), 0., 1.)
        psnr = batch_PSNR(Out, ISource, 1.)
        psnr_test += psnr
        print("%s PSNR %f" % (f, psnr))

        # Tensor to Image.
        clean_img = utils.make_grid(ISource.data,
                                    nrow=8,
                                    normalize=True,
                                    scale_each=True)
        denoising_img = utils.make_grid(Out.data,
                                        nrow=8,
                                        normalize=True,
                                        scale_each=True)

        # Image Plot.
        fig = plt.figure()
        rows = 1
        cols = 2

        ax1 = fig.add_subplot(rows, cols, 1)
        ax1.imshow(np.transpose(clean_img.cpu(), (1, 2, 0)), cmap="gray")
        ax1.set_title('clean image')

        ax2 = fig.add_subplot(rows, cols, 2)
        ax2.imshow(np.transpose(denoising_img.cpu(), (1, 2, 0)), cmap="gray")
        ax2.set_title('denoising image')

        plt.show()
        result_img = torch.clamp(denoising_img * 255, 0, 255)
        result_img = np.uint8(result_img.cpu())
        imwrite('./fig_result/denoising/' + f,
                np.transpose(result_img, (1, 2, 0)))

    psnr_test /= len(files_source)
    print("\nPSNR on test data %f" % psnr_test)
Beispiel #33
0
def main_loop():
    window = pygame.display.set_mode((WIDTH, WIDTH))
    pygame.display.set_caption('A* Pathfinding Algorithm Visualisation')

    rows = 50
    width = WIDTH

    grid = utils.make_grid(rows, width)

    start_spot = None
    end_spot = None

    run_loop = True

    while run_loop:        
        utils.draw(window, grid, rows, width)
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run_loop = False

            if pygame.mouse.get_pressed()[0]: #left mouse button = draw
                pos = pygame.mouse.get_pos()
                row, col = utils.get_click_pos(pos, rows, width)
                spot = grid[row][col]
                if not start_spot and spot != end_spot:
                    start_spot = spot
                    start_spot.make_start()

                elif not end_spot and spot != start_spot:
                    end_spot = spot
                    end_spot.make_end()
                
                elif spot != start_spot and spot != end_spot:
                    spot.make_wall()

            elif pygame.mouse.get_pressed()[2]: #right mouse button - erase
                pos = pygame.mouse.get_pos()
                row, col = utils.get_click_pos(pos, rows, width)
                spot = grid[row][col]
                spot.reset()
                if spot == start_spot:
                    start_spot = None

                elif spot == end_spot:
                    end_spot = None
            
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE and start_spot and end_spot:
                    for row in grid:                        
                        for spot in row:
                            spot.update_neighbors(grid)
                        
                    utils.pathfinding_algorithm(lambda: utils.draw(window, grid, rows, width), grid, start_spot, end_spot)

                if event.key == pygame.K_c:
                    start_spot = None
                    end_spot = None
                    grid = utils.make_grid(rows, width)

    pygame.quit()
Beispiel #34
0
def main(win, width, ROWS=30):
    global chosenAlgo
    # initial values are created
    ROWS = slider.getValue()
    if ROWS > 25 and ROWS % 2 == 0 and ROWS != 40:
        ROWS += 1
    grid = make_grid(ROWS, width)
    start = None
    end = None
    run = True
    if chosenAlgo == "Github":
        choseAlgo = "A*M"
    # this while run is here to make the pygame window run untill you close it
    while run:
        events = pygame.event.get()
        draw(win, grid, ROWS, width)
        pygame.display.update()
        # the event listening is all made here
        for event in pygame.event.get():
            # the quit event that reroutes you back to the main_menu function
            # NOTE THIS FUNCTION IS CALLED FROM WITHIN THE MAIN_MENU FUNCTION THAT IS WHY THIS WORKS
            if event.type == pygame.QUIT:
                run = False
            # event call for LMB, it draws either a start or an end or a block
            if pygame.mouse.get_pressed()[0]:
                try:
                    pos = pygame.mouse.get_pos()
                    row, col = get_clicked_pos(pos, ROWS, width)
                    node = grid[row][col]
                    if not start and node != end:
                        start = node
                        start.make_start()

                    elif not end and node != start:
                        end = node
                        end.make_end()

                    elif node != end and node != start:
                        node.make_blocked()
                except:
                    continue
            # event call for MMB, it clears the path found in the prev run
            elif pygame.mouse.get_pressed()[1]:
                for row in grid:
                    for node in row:
                        if node.is_path():
                            node.reset()

            # event call RMB, deleting any block clicked on
            elif pygame.mouse.get_pressed()[2]:
                pos = pygame.mouse.get_pos()
                row, col = get_clicked_pos(pos, ROWS, width)
                node = grid[row][col]
                node.reset()
                if node == start:
                    start = None
                elif node == end:
                    end = None

            # event listener for KEYBOARD press
            if event.type == pygame.KEYDOWN:
                # if SPACE is pressed than run the A* algorithm
                if event.key == pygame.K_SPACE and start and end:
                    for row in grid:
                        for node in row:
                            if node.is_closed() or node.is_open(
                            ) or node.is_path():
                                node.reset()
                    for row in grid:
                        for node in row:
                            node.update_neighbors(grid)

                    if chosenAlgo == "A*" or chosenAlgo == "A*M":
                        aStar_algorithm(lambda: draw(win, grid, ROWS, width),
                                        grid, start, end)
                    if chosenAlgo == "BFS":
                        bfs(lambda: draw(win, grid, ROWS, width), grid, start,
                            end)
                    elif chosenAlgo == "Dijkstra" or chosenAlgo == "DijkstraM":
                        Dijkstra_algorithm(
                            lambda: draw(win, grid, ROWS, width), grid, start,
                            end)

                # if c in pressed than clear the current grid
                if event.key == pygame.K_c:
                    start = None
                    end = None
                    grid = make_grid(ROWS, width)

                if event.key == pygame.K_g:
                    generate_walls(lambda: draw(win, grid, ROWS, width), grid,
                                   len(grid), start, end)