Exemplo n.º 1
0
        default=0,
        help='if 1, projects predicted correspondences point on target mesh')
    parser.add_argument(
        '--randomize',
        type=int,
        default=0,
        help='if 1, projects predicted correspondences point on target mesh')

    opt = parser.parse_args()

    opt.HR = my_utils.int_2_boolean(opt.HR)
    opt.LR_input = my_utils.int_2_boolean(opt.LR_input)
    opt.clean = my_utils.int_2_boolean(opt.clean)
    opt.scale = my_utils.int_2_boolean(opt.scale)
    opt.project_on_target = my_utils.int_2_boolean(opt.project_on_target)
    opt.randomize = my_utils.int_2_boolean(opt.randomize)

    my_utils.plant_seeds(randomized_seed=opt.randomize)
    inf = Inference(HR=opt.HR,
                    nepoch=opt.nepoch,
                    model_path=opt.model_path,
                    num_points=opt.num_points,
                    num_angles=opt.num_angles,
                    clean=opt.clean,
                    scale=opt.scale,
                    project_on_target=opt.project_on_target,
                    LR_input=opt.LR_input)
    # inf.reconstruct((opt.inputA))
    inf.forward(opt.inputA, opt.inputB)
    # inf.forward(opt.inputA, opt.inputA)
Exemplo n.º 2
0
from __future__ import print_function
import sys
sys.path.append('./auxiliary/')
sys.path.append('/app/python/')
sys.path.append('./')
import my_utils
my_utils.plant_seeds(randomized_seed=False)
print("fixed seed")
import argparse
import random
import numpy as np
import torch
import torch.optim as optim
import pointcloud_processor
from dataset import *
from model import *
from ply import *
import os
import json
import datetime
import visdom

# =============PARAMETERS======================================== #
parser = argparse.ArgumentParser()
parser.add_argument('--batchSize',
                    type=int,
                    default=32,
                    help='input batch size')
parser.add_argument('--workers',
                    type=int,
                    help='number of data loading workers',
Exemplo n.º 3
0
def forward(opt):
    """
    Takes an input and a target mesh. Deform input in output and propagate a
    manually defined high frequency from the oinput to the output
    :return:
    """
    my_utils.plant_seeds(randomized_seed=opt.randomize)
    os.makedirs(opt.output_dir, exist_ok=True)

    trainer = t.Trainer(opt)
    trainer.build_dataset_train_for_matching()
    trainer.build_dataset_test_for_matching()
    trainer.build_network()
    trainer.build_losses()
    trainer.network.eval()

    if opt.eval_list and os.path.isfile(opt.eval_list):
        source_target_files = np.loadtxt(opt.eval_list, dtype=str)
        source_target_files = source_target_files.tolist()
        for i, st in enumerate(source_target_files):
            source, target = st
            cat1, fname1 = source.split('/')
            fname1 = os.path.splitext(fname1)[0]
            cat2, fname2 = target.split('/')
            fname2 = os.path.splitext(fname2)[0]
            if len(opt.shapenetv1_path) > 0:
                source_target_files[i] = (os.path.join(opt.shapenetv1_path,
                                                       cat1, fname1,
                                                       "model.obj"),
                                          os.path.join(opt.shapenetv1_path,
                                                       cat2, fname2,
                                                       "model.obj"))
            elif len(opt.shapenetv2_path) > 0:
                source_target_files[i] = (os.path.join(opt.shapenetv2_path,
                                                       cat1, fname1, "models",
                                                       "model_normalized.obj"),
                                          os.path.join(opt.shapenetv2_path,
                                                       cat2, fname2, "models",
                                                       "model_normalized.obj"))
    elif (opt.eval_source != "" and opt.eval_source[-4:] == ".txt") and (
            opt.eval_target != "" and opt.eval_target[-4:] == ".txt"):
        source_target_files = [
            (figure_2_3.convert_path(opt.shapenetv1_path, opt.eval_source),
             figure_2_3.convert_path(opt.shapenetv1_path, opt.eval_target))
        ]

    rot_mat = get_3D_rot_matrix(1, np.pi / 2)
    rot_mat_rev = get_3D_rot_matrix(1, -np.pi / 2)
    isV2 = len(opt.shapenetv2_path) > 0
    for i, source_target in enumerate(source_target_files):
        basename = get_model_id(source_target[0], isV2) + "-" + get_model_id(
            source_target[1], isV2)
        path_deformed = os.path.join(opt.output_dir, basename + "-Sab.ply")
        path_source = os.path.join(opt.output_dir, basename + "-Sa.ply")
        path_target = os.path.join(opt.output_dir, basename + "-Sb.ply")

        mesh_path = source_target[0]
        print(mesh_path)
        source_mesh_edge = get_shapenet_model.link(mesh_path)

        mesh_path = source_target[1]
        target_mesh_edge = get_shapenet_model.link(mesh_path)

        print("Deforming source in target")

        source = source_mesh_edge.vertices
        target = target_mesh_edge.vertices

        pymesh.save_mesh_raw(path_source,
                             source,
                             source_mesh_edge.faces,
                             ascii=True)
        pymesh.save_mesh_raw(path_target,
                             target,
                             target_mesh_edge.faces,
                             ascii=True)

        if len(opt.shapenetv2_path) > 0:
            source = source.dot(rot_mat)
            target = target.dot(rot_mat)

        source = torch.from_numpy(source).cuda().float().unsqueeze(0)
        target = torch.from_numpy(target).cuda().float().unsqueeze(0)

        with torch.no_grad():
            source, _, _, _, _ = loss.forward_chamfer(
                trainer.network,
                source,
                target,
                local_fix=None,
                distChamfer=trainer.distChamfer)

        try:
            source = source.squeeze().cpu().detach().numpy()
            if len(opt.shapenetv2_path) > 0:
                source = source.dot(rot_mat_rev)
            P2_P1_mesh = pymesh.form_mesh(vertices=source,
                                          faces=source_mesh_edge.faces)
            pymesh.save_mesh(path_deformed, P2_P1_mesh, ascii=True)

            # print("computing signal tranfer form source to target")
            # high_frequencies.high_frequency_propagation(path_source, path_deformed, path_target)
        except Exception as e:
            print(e)
            import pdb
            pdb.set_trace()
            path_deformed = path_deformed[:-4] + ".pts"
            save_pts(path_deformed, source.squeeze().cpu().detach().numpy())
def get_criterion_shape(opt):
    return_dict = {}
    my_utils.plant_seeds(randomized_seed=opt.randomize)

    trainer = t.Trainer(opt)
    trainer.build_dataset_train_for_matching()
    trainer.build_dataset_test_for_matching()
    trainer.build_network()
    trainer.build_losses()
    trainer.network.eval()

    # Load input mesh
    exist_P2_label = True

    try:
        mesh_path = opt.eval_get_criterions_for_shape  # Ends in .txt
        points = np.loadtxt(mesh_path)
        points = torch.from_numpy(points).float()
        # Normalization is done before resampling !
        P2 = normalize_points.BoundingBox(points[:, :3])
        P2_label = points[:, 6].data.cpu().numpy()
    except:
        mesh_path = opt.eval_get_criterions_for_shape  # Ends in .obj
        source_mesh_edge = get_shapenet_model.link(mesh_path)
        P2 = torch.from_numpy(source_mesh_edge.vertices)
        exist_P2_label = False

    min_k = Min_k(opt.k_max_eval)
    max_k = Max_k(opt.k_max_eval)

    points_train_list = []
    point_train_paths = []
    labels_train_list = []
    iterator_train = trainer.dataloader_train.__iter__()

    for find_best in range(opt.num_shots_eval):
        try:
            points_train, _, _, file_path = iterator_train.next()
            points_train_list.append(
                points_train[:, :, :3].contiguous().cuda().float())
            point_train_paths.append(file_path)
            labels_train_list.append(
                points_train[:, :, 6].contiguous().cuda().float())
        except:
            break

    # ========Loop on test examples======================== #
    with torch.no_grad():
        P2 = P2[:, :3].unsqueeze(0).contiguous().cuda().float()
        P2_latent = trainer.network.encode(
            P2.transpose(1, 2).contiguous(),
            P2.transpose(1, 2).contiguous())

        # Chamfer (P0_P2)
        P0_P2_list = list(
            map(
                lambda x: loss.forward_chamfer(trainer.network,
                                               P2,
                                               x,
                                               local_fix=None,
                                               distChamfer=trainer.distChamfer
                                               ), points_train_list))

        # Compute Chamfer (P2_P0)
        P2_P0_list = list(
            map(
                lambda x: loss.forward_chamfer(trainer.network,
                                               x,
                                               P2,
                                               local_fix=None,
                                               distChamfer=trainer.distChamfer
                                               ), points_train_list))

        predicted_ours_P2_P0_list = list(
            map(lambda x, y: x.view(-1)[y[4].view(-1).data.long()].view(1, -1),
                labels_train_list, P2_P0_list))

        if exist_P2_label:
            iou_ours_list = list(
                map(
                    lambda x: miou_shape.miou_shape(x.squeeze().cpu().numpy(
                    ), P2_label, trainer.parts), predicted_ours_P2_P0_list))
            top_k_idx, top_k_values = max_k(iou_ours_list)
            return_dict["oracle"] = point_train_paths[top_k_idx[0]][0]

        predicted_ours_P2_P0_list = list(
            map(lambda x, y: x.view(-1)[y[4].view(-1).data.long()].view(1, -1),
                labels_train_list, P2_P0_list))
        predicted_ours_P2_P0_list = torch.cat(predicted_ours_P2_P0_list)

        # Compute NN
        P2_P0_NN_list = list(
            map(lambda x: loss.distChamfer(x, P2), points_train_list))
        predicted_NN_P2_P0_list = list(
            map(lambda x, y: x.view(-1)[y[3].view(-1).data.long()].view(1, -1),
                labels_train_list, P2_P0_NN_list))
        predicted_NN_P2_P0_list = torch.cat(predicted_NN_P2_P0_list)

        # NN
        NN_chamferL2_list = list(
            map(lambda x: loss.chamferL2(x[0], x[1]), P2_P0_NN_list))
        top_k_idx, top_k_values = min_k(NN_chamferL2_list)
        return_dict["NN_criterion"] = point_train_paths[top_k_idx[0]][0]

        # Chamfer ours
        chamfer_list = list(
            map(lambda x: loss.chamferL2(x[1], x[2]), P2_P0_list))
        top_k_idx, top_k_values = min_k(chamfer_list)
        return_dict["chamfer_criterion"] = point_train_paths[top_k_idx[0]][0]

        # NN in latent space
        P0_latent_list = list(
            map(
                lambda x: trainer.network.encode(
                    x.transpose(1, 2).contiguous(),
                    x.transpose(1, 2).contiguous()), points_train_list))
        cosine_list = list(
            map(lambda x: loss.cosine(x, P2_latent), P0_latent_list))

        top_k_idx, top_k_values = min_k(cosine_list)
        return_dict["cosine_criterion"] = point_train_paths[top_k_idx[0]][0]

        # Cycle 2
        P0_P2_cycle_list = list(
            map(lambda x, y: loss.batch_cycle_2(x[0], y[3], 1), P0_P2_list,
                P2_P0_list))
        P0_P2_cycle_list = list(
            map(lambda x, y: loss.L2(x, y), P0_P2_cycle_list,
                points_train_list))

        P2_P0_cycle_list = list(
            map(lambda x, y: loss.batch_cycle_2(x[0], y[3], 1), P2_P0_list,
                P0_P2_list))
        P2_P0_cycle_list = list(map(lambda x: loss.L2(x, P2),
                                    P2_P0_cycle_list))

        # Cycle 2 both sides
        both_cycle_list = list(
            map(lambda x, y: x * y, P0_P2_cycle_list, P2_P0_cycle_list))
        both_cycle_list = np.power(both_cycle_list, 1.0 / 2.0).tolist()
        top_k_cycle2_idx, top_k_values = min_k(both_cycle_list)
        return_dict["cycle_criterion"] = point_train_paths[
            top_k_cycle2_idx[0]][0]
        pprint.pprint(return_dict)
        return return_dict
Exemplo n.º 5
0
def forward(opt):
    """
    Takes an input and a target mesh. Deform input in output and propagate a
    manually defined high frequency from the oinput to the output
    :return:
    """
    my_utils.plant_seeds(randomized_seed=opt.randomize)

    pdb.set_trace()

    trainer = t.Trainer(opt)
    trainer.build_dataset_train_for_matching()
    trainer.build_dataset_test_for_matching()
    trainer.build_network()
    trainer.build_losses()
    trainer.network.eval()

    if opt.eval_source[-4:] == ".txt":
        opt.eval_source = figure_2_3.convert_path(opt.shapenetv1_path,
                                                  opt.eval_source)
    if opt.eval_target[-4:] == ".txt":
        opt.eval_target = figure_2_3.convert_path(opt.shapenetv1_path,
                                                  opt.eval_target)

    path_deformed = os.path.join("./figures/forward_input_target/",
                                 opt.eval_source[-42:-10] + "deformed.ply")
    path_source = os.path.join("./figures/forward_input_target/",
                               opt.eval_source[-42:-10] + ".ply")
    path_target = os.path.join(
        "./figures/forward_input_target/",
        opt.eval_source[-42:-10] + "_" + opt.eval_target[-42:-10] + ".ply")

    mesh_path = opt.eval_source
    print(mesh_path)
    source_mesh_edge = get_shapenet_model.link(mesh_path)

    mesh_path = opt.eval_target
    target_mesh_edge = get_shapenet_model.link(mesh_path)

    pymesh.save_mesh(path_source, source_mesh_edge, ascii=True)
    pymesh.save_mesh(path_target, target_mesh_edge, ascii=True)
    print("Deforming source in target")

    source = torch.from_numpy(
        source_mesh_edge.vertices).cuda().float().unsqueeze(0)
    target = torch.from_numpy(
        target_mesh_edge.vertices).cuda().float().unsqueeze(0)

    with torch.no_grad():
        source, _, _, _, _ = loss.forward_chamfer(
            trainer.network,
            source,
            target,
            local_fix=None,
            distChamfer=trainer.distChamfer)

    P2_P1_mesh = pymesh.form_mesh(
        vertices=source.squeeze().cpu().detach().numpy(),
        faces=source_mesh_edge.faces)
    pymesh.save_mesh(path_deformed, P2_P1_mesh, ascii=True)

    print("computing signal tranfer form source to target")
    high_frequencies.high_frequency_propagation(path_source, path_deformed,
                                                path_target)