Example #1
0
    def task5(self, args, path='.'):
        """
        Use as:
        -task 5 --layers # --hashes # --k # --imageId #
        """
        if args.layers == None or args.hashes == None or \
                args.k == None or args.imageId == None:
            raise ValueError(
                'Layers, Hashes, Vectors, K, and IMG must all be defined for task 5.'
            )

        layers = int(args.layers)
        hashes = int(args.hashes)
        t = int(args.k)
        imageId = args.imageId
        if args.vectors:
            vectors = str(args.vectors)

        # YOUR CODE HERE
        lsh = LSH()
        nearest = lsh.main(layers,
                           hashes,
                           imageId,
                           vectors=(),
                           t=t,
                           database=self.__database__)
        show_images(nearest, self.__database__)
        save_images(nearest, self.__database__, join(path, 'out'))
Example #2
0
    def task3(self, args, path='.'):
        """
        -task 3 --k # 
        """
        if args.k == None:
            raise ValueError('K must be defined for task 3.')
        k = int(args.k)

        # YOUR CODE HERE.
        G = self.__graph__.get_adjacency()
        images = self.__graph__.get_images()
        n = G.shape[0]
        s = 0.86
        maxerr = 0.01

        # transform G into markov matrix A
        A = csc_matrix(G, dtype=np.float)
        rsums = np.array(A.sum(1))[:, 0]
        ri, ci = A.nonzero()
        A.data /= rsums[ri]

        # bool array of sink states
        sink = rsums == 0

        # Compute pagerank r until we converge
        ro, r = np.zeros(n), np.ones(n)
        # account for sink states
        Di = sink / float(n)
        # account for teleportation to state i
        Ei = np.ones(n) / float(n)
        # while np.sum(np.abs(r - ro)) > maxerr:
        for _ in range(150):

            if np.sum(np.abs(r - ro)) <= maxerr:
                break
            ro = r.copy()
            # calculate each pagerank at a time
            for i in range(0, n):
                # in-links of state i
                Ai = np.array(A[:, i].todense())[:, 0]

                r[i] = ro.dot(Ai * s + Di * s + Ei * (1 - s))

        weights = r / float(sum(r))
        orderedWeights = np.argsort(weights)
        ReorderedWeights = np.flipud(orderedWeights)
        # m = max(weights)
        # ind = np.argmax(weights)
        listOfImages = list()
        for xx in range(k):
            listOfImages.append(images[ReorderedWeights[xx]])
        # weightDict = {}
        # for xx in range(len(weights)):
        #     weightDict[xx] = weights[xx]
        print(listOfImages)
        show_images(listOfImages, self.__database__)
        save_images(listOfImages, self.__database__, join(path, 'out'))
Example #3
0
    def forward(self,
                cam2world,  # pose
                phi,
                uv,
                intrinsics):
        batch_size, num_samples, _ = uv.shape
        log = list()

        ray_dirs = geometry.get_ray_directions(uv,
                                               cam2world=cam2world,
                                               intrinsics=intrinsics)

        initial_depth = torch.zeros((batch_size, num_samples, 1)).normal_(mean=0.05, std=5e-4).cuda()
        init_world_coords = geometry.world_from_xy_depth(uv,
                                                         initial_depth,
                                                         intrinsics=intrinsics,
                                                         cam2world=cam2world)


        world_coords = [init_world_coords]
        depths = [initial_depth]
        states = [None]

        for step in range(self.steps):

            v = phi(world_coords[-1])

            state = self.lstm(v.view(-1, self.n_feature_channels), states[-1])

            if state[0].requires_grad:
                state[0].register_hook(lambda x: x.clamp(min=-20, max=20))

            signed_distance = self.out_layer(state[0]).view(batch_size, num_samples, 1)
            new_world_coords = world_coords[-1] + ray_dirs * signed_distance

            states.append(state)
            world_coords.append(new_world_coords)

            depth = geometry.depth_from_world(world_coords[-1], cam2world)

            # if self.training:
            #     print("Raymarch step %d/%d: Min depth %0.6f, max depth %0.6f" %
            #           (step, self.steps, depths[-1].min().detach().cpu().numpy(), depths[-1].max().detach().cpu().numpy()))
            depths.append(depth)

        if not self.counter % 100:
            # Write tensorboard summary for each step of ray-marcher.
            drawing_depths = torch.stack(depths, dim=0)[:, 0, :, :]
            drawing_depths = util.lin2img(drawing_depths).repeat(1, 3, 1, 1)
            log.append(('image', 'raycast_progress',
                        torch.clamp(torchvision.utils.make_grid(drawing_depths, scale_each=False, normalize=True), 0.0,
                                    5),
                        100))

            # Visualize residual step distance (i.e., the size of the final step)
            fig = util.show_images([util.lin2img(signed_distance)[i, :, :, :].detach().cpu().numpy().squeeze()
                                    for i in range(batch_size)])
            log.append(('figure', 'stopping_distances', fig, 100))
        self.counter += 1

        return world_coords[-1], depths[-1], log
Example #4
0
def train_CycleGan(train_step,
                   loss,
                   reconstruction,
                   show_every=opts.show_every,
                   print_every=opts.print_every,
                   batch_size=128,
                   num_epoch=10):
    """
    function that trains VAE.
    :param train_step: an op that defines what to do with the loss function (minimize or maximize)
    :param loss: an op that defines the loss function to be minimized
    :param reconstruction: an op that defines how to reconstruct a target image
    :param show_every: how often to show an image to gauge training progress
    :param print_every: how often to print loss
    :param batch_size: batch size of training samples
    :param num_epoch: how many times to iterate over the training samples
    :return:
    """

    image_dir = '/home/youngwook/Downloads/edges2shoes'
    folder_names = get_folders(image_dir)

    train_folder = folder_names[2]
    val_folder = folder_names[1]

    train_data = AB_Combined_ImageLoader(train_folder,
                                         size=opts.resize,
                                         num_images=opts.num_images,
                                         randomcrop=opts.image_shape)
    train_loader = DataLoader(train_data,
                              batch_size=opts.batch,
                              shuffle=True,
                              num_workers=12)

    step = 0
    target_pred_list = []
    input_pred_list = []
    input_true_list = []
    target_true_list = []
    last_100_loss_dq = deque(maxlen=100)
    last_100_loss = []

    checkpoint_dir = './model'
    saver = tf.train.Saver()

    if opts.resume:
        #print('Loading Saved Checkpoint')
        tf_util.load_session(checkpoint_dir,
                             saver,
                             session,
                             model_name=opts.model_name)

    for epoch in range(num_epoch):
        # every show often, show a sample result

        lr = util.linear_LR(epoch, opts)

        for (minibatch, minbatch_y) in train_loader:
            # run a batch of data through the network
            # logits= sess.run(logits_real, feed_dict={x:minibatch})

            target_pred, input_pred = session.run(
                [target_image_prediction, input_image_prediction],
                feed_dict={
                    input_image: minibatch,
                    target_image: minbatch_y,
                    adaptive_lr: lr
                })

            input_memory.append(input_pred)
            target_memory.append(target_pred)

            target_replay_images = np.vstack(target_memory)
            input_replay_images = np.vstack(input_memory)

            #train the Generator
            _, G_loss_curr = session.run(
                [train_step[2], loss[1]],
                feed_dict={
                    input_image: minibatch,
                    target_image: minbatch_y,
                    input_replay: input_replay_images,
                    target_replay: target_replay_images,
                    input_image_pred: input_pred,
                    target_image_pred: target_pred,
                    adaptive_lr: lr
                })

            #train the discriminator
            _, D_loss_curr = session.run(
                [train_step[0], loss[0][0]],
                feed_dict={
                    input_image: minibatch,
                    input_replay: input_replay_images,
                    adaptive_lr: lr
                })
            _, D_loss_curr = session.run(
                [train_step[1], loss[0][1]],
                feed_dict={
                    target_image: minbatch_y,
                    target_replay: target_replay_images,
                    adaptive_lr: lr
                })

            last_100_loss_dq.append(G_loss_curr)
            last_100_loss.append(np.mean(last_100_loss_dq))

            step += 1
            if step % show_every == 0:
                '''for every show_every step, show reconstructed images from the training iteration'''

                target_name = './img/target_pred_%s.png' % step
                input_name = './img/input_pred_%s.png' % step
                input_true_name = './img/true_input_%s.png' % step
                target_true_name = './img/true_target_%s.png' % step

                #translate the image
                target_pred, input_pred = session.run(
                    [target_image_prediction, input_image_prediction],
                    feed_dict={
                        input_image: minibatch,
                        target_image: minbatch_y
                    })

                target_pred_list.append(target_name)
                input_pred_list.append(input_name)
                input_true_list.append(input_true_name)
                target_true_list.append(target_true_name)

                util.show_images(target_pred[:opts.batch], opts, target_name)
                util.plt.show()
                util.show_images(minbatch_y[:opts.batch], opts,
                                 target_true_name)
                util.plt.show()

                util.show_images(input_pred[:opts.batch], opts, input_name)
                util.plt.show()
                util.show_images(minibatch[:opts.batch], opts, input_true_name)
                util.plt.show()

            if step % print_every == 0:
                print('Epoch: {}, D: {:.4}'.format(epoch, G_loss_curr))
                util.raw_score_plotter(last_100_loss)

        #save the model after every epoch
        if opts.save_progress:
            tf_util.save_session(saver,
                                 session,
                                 checkpoint_dir,
                                 epoch,
                                 model_name=opts.model_name)

    util.raw_score_plotter(last_100_loss)

    image_to_gif('', target_pred_list, duration=0.5, gifname='target_pred')
    image_to_gif('', input_pred_list, duration=0.5, gifname='input_pred')
    image_to_gif('', input_true_list, duration=0.5, gifname='input_true')
    image_to_gif('', target_true_list, duration=0.5, gifname='target_true')
    def write_updates(self,
                      writer,
                      predictions,
                      ground_truth,
                      iter,
                      prefix=""):
        """Writes tensorboard summaries using tensorboardx api.

        :param writer: tensorboardx writer object.
        :param predictions: Output of forward pass.
        :param ground_truth: Ground truth.
        :param iter: Iteration number.
        :param prefix: Every summary will be prefixed with this string.
        """
        predictions, depth_maps = predictions
        trgt_imgs = ground_truth['rgb']

        trgt_imgs = trgt_imgs.cuda()

        batch_size, num_samples, _ = predictions.shape

        # Module"s own log
        for type, name, content, every_n in self.logs:
            name = prefix + name

            if not iter % every_n:
                if type == "image":
                    writer.add_image(name,
                                     content.detach().cpu().numpy(), iter)
                    writer.add_scalar(name + "_min", content.min(), iter)
                    writer.add_scalar(name + "_max", content.max(), iter)
                elif type == "figure":
                    writer.add_figure(name, content, iter, close=True)
                elif type == "histogram":
                    writer.add_histogram(name,
                                         content.detach().cpu().numpy(), iter)
                elif type == "scalar":
                    writer.add_scalar(name,
                                      content.detach().cpu().numpy(), iter)
                elif type == "embedding":
                    writer.add_embedding(mat=content, global_step=iter)

        if not iter % 100:
            output_vs_gt = torch.cat((predictions, trgt_imgs), dim=0)
            output_vs_gt = util.lin2img(output_vs_gt)
            writer.add_image(
                prefix + "Output_vs_gt",
                torchvision.utils.make_grid(
                    output_vs_gt, scale_each=False,
                    normalize=True).cpu().detach().numpy(), iter)

            rgb_loss = ((predictions.float().cuda() -
                         trgt_imgs.float().cuda())**2).mean(dim=2,
                                                            keepdim=True)
            rgb_loss = util.lin2img(rgb_loss)

            fig = util.show_images([
                rgb_loss[i].detach().cpu().numpy().squeeze()
                for i in range(batch_size)
            ])
            writer.add_figure(prefix + "rgb_error_fig", fig, iter, close=True)

            depth_maps_plot = util.lin2img(depth_maps)
            writer.add_image(
                prefix + "pred_depth",
                torchvision.utils.make_grid(
                    depth_maps_plot.repeat(1, 3, 1, 1),
                    scale_each=True,
                    normalize=True).cpu().detach().numpy(), iter)

        writer.add_scalar(prefix + "out_min", predictions.min(), iter)
        writer.add_scalar(prefix + "out_max", predictions.max(), iter)

        writer.add_scalar(prefix + "trgt_min", trgt_imgs.min(), iter)
        writer.add_scalar(prefix + "trgt_max", trgt_imgs.max(), iter)

        if iter:
            writer.add_scalar(prefix + "latent_reg_loss", self.latent_reg_loss,
                              iter)
Example #6
0
            loss.backward()

            # puede darse que la imagen haya acabado con pĂ­xeles fuera de [0,1], esto lo arregla
            input_image.data.clamp_(0, 1)
            i[0] += 1

            if i[0] % 50 == 0:
                print("iteraciones: {}:".format(i[0]))
                print(
                    'Diferencia de estilo : {:4f} Diferencia de contenido: {:4f}'
                    .format(style_score.item(), content_score.item()))
                if path != "":
                    save(input_image, path.format(i[0]))
                print()

            return style_score + content_score

        optimizer.step(closure)

    return input_image


output = run_style_transfer(content_image,
                            style_image,
                            input_image,
                            num_steps=500,
                            style_weight=1000000,
                            path=out_path)

show_images([output], [])
Example #7
0
    def task4(self, args, path='.'):
        """
        -task 4 --k # --imgs id1 id2 id3
        """
        if args.k == None or args.imgs == None:
            raise ValueError('K and Imgs must be defined for task 4.')
        k = int(args.k)
        imgs = list(args.imgs)
        # 6 2976167 83 38391649 299 135049429
        # YOUR CODE HERE.
        G = self.__graph__.get_adjacency()
        images = self.__graph__.get_images()
        indexes = list()
        for x in imgs:
            indexes.append(images.index(x))
        n = G.shape[0]
        s = 0.6
        maxerr = 0.1

        # transform G into markov matrix A
        A = csc_matrix(G, dtype=np.float)
        rsums = np.array(A.sum(1))[:, 0]
        ri, ci = A.nonzero()
        A.data /= rsums[ri]

        # bool array of sink states
        sink = rsums == 0

        Ei = np.zeros(n)
        for ii in indexes:
            Ei[ii] = 1 / len(imgs)
        # Compute pagerank r until we converge
        ro, r = np.zeros(n), np.ones(n)
        # while np.sum(np.abs(r - ro)) > maxerr:
        for _ in range(100):

            if np.sum(np.abs(r - ro)) <= maxerr:
                break

            ro = r.copy()
            # calculate each pagerank at a time
            for i in range(0, n):
                # in-links of state i
                Ai = np.array(A[:, i].todense())[:, 0]
                # account for sink states
                Di = sink / float(n)
                # account for teleportation to state i

                r[i] = ro.dot(Ai * s + Di * s + Ei * (1 - s))

        weights = r / float(sum(r))
        orderedWeights = np.argsort(weights)
        ReorderedWeights = np.flipud(orderedWeights)
        # m = max(weights)
        # ind = np.argmax(weights)
        listOfImages = list()
        for xx in range(k):
            listOfImages.append(images[ReorderedWeights[xx]])
        print(listOfImages)
        show_images(listOfImages, self.__database__)
        save_images(listOfImages, self.__database__, join(path, 'out'))