Exemplo n.º 1
0
def sample(network, save_manager, latent_size, name="", nb_samples=10):
    mp.offline()

    example = torch.ones(latent_size)

    samples = []
    for i in range(nb_samples):
        samples.append(torch.rand_like(example))
    samples = torch.stack(samples, dim=0)

    out_points_list, out_batch_list = network.decode(samples)
    out_points = out_points_list[0]
    out_batch = out_batch_list[0]

    plot = None
    for i in range(nb_samples):
        cloud_out = out_points[out_batch == i].detach().numpy()

        if plot is None:
            plot = mp.subplot(cloud_out,
                              c=cloud_out[:, 0],
                              s=[nb_samples, 1, i],
                              shading={"point_size": 0.2})
        else:
            mp.subplot(cloud_out,
                       c=cloud_out[:, 0],
                       data=plot,
                       s=[nb_samples, 1, i],
                       shading={"point_size": 0.2})

    save_manager.save_mesh_plot(plot, "sample_{}".format(name))
def sample_dataset(save_manager, data_generator, nb_files=10):
    mp.offline()
    train_loader, val_loader = data_generator.generate_loaders()

    i = 0
    for batch_obj in train_loader:
        if i == nb_files:
            break

        points = batch_obj.pos
        batch = batch_obj.batch

        plot = None
        nb_clouds = max(batch) + 1
        for j in range(nb_clouds):
            current_points = points[batch == j].detach().numpy()
            if plot is None:
                plot = mp.subplot(
                    current_points, c=current_points[:, 0],
                    s=[nb_clouds, 1, j], shading={"point_size": 0.2}
                )
            else:
                mp.subplot(
                    current_points, c=current_points[:, 0],
                    data=plot, s=[nb_clouds, 1, j], shading={"point_size": 0.2}
                )

        i += 1

        save_manager.save_mesh_plot(plot, "dataset_samples{}".format(i))
Exemplo n.º 3
0
def morph(network, data_generator, save_manager, name="", nb_steps=5):
    mp.offline()
    train_loader, val_loader = data_generator.generate_loaders()

    for batch in val_loader:
        in_points_list, in_batch_list, mean, variance = network.encode(batch)

        print(mean.size(0))
        for i in range(mean.size(0) - 1):
            mean1 = mean[i]
            for j in range(mean.size(0) - 1 - i):
                mean2 = mean[i + 1 + j]

                interpol_means = []
                for alpha in np.arange(0.0, 1.0 + 1 / nb_steps, 1 / nb_steps):
                    if alpha >= 1.001:
                        break

                    interpol_means.append((1 - alpha) * mean1 + alpha * mean2)

                interpol_stack = torch.stack(interpol_means, dim=0)

                out_points_list, out_batch_list = network.decode(
                    interpol_stack)
                batch_size = torch.max(out_batch_list[0]) + 1
                print(batch_size)

                plot = None
                for plot_nb in range(batch_size):
                    cloud_out = out_points_list[0][out_batch_list[0] ==
                                                   plot_nb].detach().numpy()

                    if plot is None:
                        plot = mp.subplot(cloud_out,
                                          c=cloud_out[:, 0],
                                          s=[batch_size, 1, plot_nb],
                                          shading={"point_size": 0.2})
                    else:
                        mp.subplot(cloud_out,
                                   c=cloud_out[:, 0],
                                   data=plot,
                                   s=[batch_size, 1, plot_nb],
                                   shading={"point_size": 0.2})

                print(i)
                print(j)
                nb = (mean.size(0) - 1) * i + j
                print(nb)
                save_manager.save_mesh_plot(plot,
                                            "morph_{}_{}".format(name, nb))

        break
Exemplo n.º 4
0
def vae_clouds(network, data_generator, save_manager, name=""):
    mp.offline()
    train_loader, val_loader = data_generator.generate_loaders()

    for batch in val_loader:
        in_points_list, in_batch_list, mean, variance = network.encode(batch)
        out_points_list, out_batch_list = network.decode(mean)

        batch_size = torch.max(in_batch_list[0]) + 1

        plot = None
        for j in range(batch_size):
            cloud_in = in_points_list[0][in_batch_list[0] == j].detach().numpy()
            cloud_out = out_points_list[0][out_batch_list[0] == j].detach().numpy()

            if plot is None:
                plot = mp.subplot(
                    cloud_in, c=cloud_in[:, 0],
                    s=[6*batch_size, 1, 6*j], shading={"point_size": 0.2}
                )
            else:
                mp.subplot(
                    cloud_in, c=cloud_in[:, 0],
                    data=plot, s=[6*batch_size, 1, 6*j], shading={"point_size": 0.2}
                )
            mp.subplot(
                cloud_out, c=cloud_out[:, 0],
                data=plot, s=[6*batch_size, 1, 6*j+1], shading={"point_size": 0.2}
            )

            cloud_in = in_points_list[1][in_batch_list[1] == j].detach().numpy()
            cloud_out = out_points_list[1][out_batch_list[1] == j].detach().numpy()

            mp.subplot(
                cloud_in, c=cloud_in[:, 0],
                data=plot, s=[6 * batch_size, 1, 6 * j + 2], shading={"point_size": 0.2}
            )
            mp.subplot(
                cloud_out, c=cloud_out[:, 0],
                data=plot, s=[6 * batch_size, 1, 6 * j + 3], shading={"point_size": 0.2}
            )

            cloud_in = in_points_list[2][in_batch_list[2] == j].detach().numpy()
            cloud_out = out_points_list[2][out_batch_list[2] == j].detach().numpy()

            mp.subplot(
                cloud_in, c=cloud_in[:, 0],
                data=plot, s=[6 * batch_size, 1, 6 * j + 4], shading={"point_size": 0.2}
            )
            mp.subplot(
                cloud_out, c=cloud_out[:, 0],
                data=plot, s=[6 * batch_size, 1, 6 * j + 5], shading={"point_size": 0.2}
            )

        save_manager.save_mesh_plot(plot, "auto_encode_clouds_{}".format(name))