import sys
sys.path.append("../")
import argparse
import open3d
import torch
import torch.nn as nn

import matplotlib.pyplot as plt
import numpy as np

import point_cloud_utils as pcu
from fml.nn import SinkhornLoss
from ODEOT.ANODE import InjAugNODE
from ODEOT.utils import load_mesh_by_file_extension, plot_flow, embed_3d, animate_flow, precond, seed_everything

argparser = argparse.ArgumentParser()
argparser.add_argument("--mesh_filename0", "-f0", type=str, help="Point cloud to reconstruct")
argparser.add_argument("--mesh_filename1", "-f1", type=str, help="Point cloud to reconstruct")
argparser.add_argument("--max-sinkhorn-iters", "-si", type=int, default=64,
                       help="Maximum number of Sinkhorn iterations")
argparser.add_argument("--sinkhorn-eps", "-se", type=float, default=1e-3,
                       help="Maximum number of Sinkhorn iterations")
argparser.add_argument("--learning-rate", "-lr", type=float, default=1e-3, help="Step size for gradient descent")
argparser.add_argument("--num-epochs", "-n", type=int, default=250, help="Number of training epochs")
argparser.add_argument("--device", "-d", type=str, default="cuda")
# argparser.add_argument("--adjoint", "-adj", type=eval, default=False, choices=[True,False])
argparser.add_argument("--tol", "-tol", type=float, default=1e-3)
args = argparser.parse_args()


def main():
    # NOTE: We're doing everything in float32 and numpy defaults to float64, so you need to make sure you cast
    # everything to the right type

    # x is a tensor of shape [n, 3] containing the positions of the points we are trying to fit
    seed_everything(1024)
    mesh0 = pcu.read_obj(args.mesh_filename0)
    mesh1 = pcu.read_obj(args.mesh_filename1)
    t = torch.from_numpy(pcu.sample_mesh_lloyd(mesh0[0],mesh0[1][:,0:3],2000).astype(np.float32)).to(args.device)
    x = torch.from_numpy(pcu.sample_mesh_lloyd(mesh1[0],mesh1[1][:,0:3],2000).astype(np.float32)).to(args.device)

    t=precond(t,0)
    print("haha",mesh0[0])
    precond(torch.from_numpy(mesh0[0]),0)
    x=precond(x,1)
    vardim = 3
    phi = InjAugNODE(in_dim=3, out_dim=3, var_dim=vardim, ker_dims=[1024, 1024, 1024, 1024], device=args.device).to(
        args.device)
    # phi.load_state_dict(torch.load("../models/phi_itpllionhead.pt"))
    # Eps is 1/lambda and max_iters is the maximum number of Sinkhorn iterations to do
    loss_fun = SinkhornLoss(eps=args.sinkhorn_eps, max_iters=args.max_sinkhorn_iters)
    dummy = torch.ones(x.shape[0], vardim).to(args.device)
    dummy[:, 0:x.shape[1]] = x
    x = dummy

    # Here I'm using the Adam optimizer just as an example, you'll need to replace this with your thing
    optimizer = torch.optim.Adam(phi.parameters(), lr=args.learning_rate)
    # optimizer.add_param_group({"params": phi.augment_part})
    # print("Number of Parameters=", count_parameters(phi))

    for epoch in range(1, args.num_epochs + 1):
        optimizer.zero_grad()

        # Do the forward pass of the neural net, evaluating the function at the parametric points
        y = phi(t)

        loss = loss_fun(y.unsqueeze(0), x.unsqueeze(0))

        loss.backward()
        optimizer.step()
        print("Epoch %d, loss = %f" % (epoch, loss.item()))
        # b = x[0:1,:]
        # print(phi.invert(x))

    print(phi.invert(x)[:, -1])
    # print(x)
    torch.save(phi.state_dict(), "../models/phi_itpllionhead.pt")
    phi.load_state_dict(torch.load("../models/phi_itpllionhead.pt"))
    # plot_flow(x[:,0:3], t, phi, 128,  t.shape[0]//100)
    animate_flow(x[:, 0:3], t, phi, 128, t.shape[0] // 100, mesh0=mesh0)


if __name__ == "__main__":
    main()
a1aba4b854c80f7563dbabd57924dd719914b5af
<mask token>


def main():
    seed_everything(1024)
    mesh0 = pcu.read_obj(args.mesh_filename0)
    mesh1 = pcu.read_obj(args.mesh_filename1)
    t = torch.from_numpy(pcu.sample_mesh_lloyd(mesh0[0], mesh0[1][:, 0:3], 
        2000).astype(np.float32)).to(args.device)
    x = torch.from_numpy(pcu.sample_mesh_lloyd(mesh1[0], mesh1[1][:, 0:3], 
        2000).astype(np.float32)).to(args.device)
    t = precond(t, 0)
    print('haha', mesh0[0])
    precond(torch.from_numpy(mesh0[0]), 0)
    x = precond(x, 1)
    vardim = 3
    phi = InjAugNODE(in_dim=3, out_dim=3, var_dim=vardim, ker_dims=[1024, 
        1024, 1024, 1024], device=args.device).to(args.device)
    loss_fun = SinkhornLoss(eps=args.sinkhorn_eps, max_iters=args.
        max_sinkhorn_iters)
    dummy = torch.ones(x.shape[0], vardim).to(args.device)
    dummy[:, 0:x.shape[1]] = x
    x = dummy
    optimizer = torch.optim.Adam(phi.parameters(), lr=args.learning_rate)
    for epoch in range(1, args.num_epochs + 1):
        optimizer.zero_grad()
        y = phi(t)
        loss = loss_fun(y.unsqueeze(0), x.unsqueeze(0))
        loss.backward()
        optimizer.step()
        print('Epoch %d, loss = %f' % (epoch, loss.item()))
    print(phi.invert(x)[:, -1])
    torch.save(phi.state_dict(), '../models/phi_itpllionhead.pt')
    phi.load_state_dict(torch.load('../models/phi_itpllionhead.pt'))
    animate_flow(x[:, 0:3], t, phi, 128, t.shape[0] // 100, mesh0=mesh0)


<mask token>

<mask token>
sys.path.append('../')
<mask token>
argparser.add_argument('--mesh_filename0', '-f0', type=str, help=
    'Point cloud to reconstruct')
argparser.add_argument('--mesh_filename1', '-f1', type=str, help=
    'Point cloud to reconstruct')
argparser.add_argument('--max-sinkhorn-iters', '-si', type=int, default=64,
    help='Maximum number of Sinkhorn iterations')
argparser.add_argument('--sinkhorn-eps', '-se', type=float, default=0.001,
    help='Maximum number of Sinkhorn iterations')
argparser.add_argument('--learning-rate', '-lr', type=float, default=0.001,
    help='Step size for gradient descent')
argparser.add_argument('--num-epochs', '-n', type=int, default=250, help=
    'Number of training epochs')
argparser.add_argument('--device', '-d', type=str, default='cuda')
argparser.add_argument('--tol', '-tol', type=float, default=0.001)
<mask token>


def main():
    seed_everything(1024)
    mesh0 = pcu.read_obj(args.mesh_filename0)
    mesh1 = pcu.read_obj(args.mesh_filename1)
    t = torch.from_numpy(pcu.sample_mesh_lloyd(mesh0[0], mesh0[1][:, 0:3], 
        2000).astype(np.float32)).to(args.device)
    x = torch.from_numpy(pcu.sample_mesh_lloyd(mesh1[0], mesh1[1][:, 0:3], 
        2000).astype(np.float32)).to(args.device)
    t = precond(t, 0)
    print('haha', mesh0[0])
    precond(torch.from_numpy(mesh0[0]), 0)
    x = precond(x, 1)
    vardim = 3
    phi = InjAugNODE(in_dim=3, out_dim=3, var_dim=vardim, ker_dims=[1024, 
        1024, 1024, 1024], device=args.device).to(args.device)
    loss_fun = SinkhornLoss(eps=args.sinkhorn_eps, max_iters=args.
        max_sinkhorn_iters)
    dummy = torch.ones(x.shape[0], vardim).to(args.device)
    dummy[:, 0:x.shape[1]] = x
    x = dummy
    optimizer = torch.optim.Adam(phi.parameters(), lr=args.learning_rate)
    for epoch in range(1, args.num_epochs + 1):
        optimizer.zero_grad()
        y = phi(t)
        loss = loss_fun(y.unsqueeze(0), x.unsqueeze(0))
        loss.backward()
        optimizer.step()
        print('Epoch %d, loss = %f' % (epoch, loss.item()))
    print(phi.invert(x)[:, -1])
    torch.save(phi.state_dict(), '../models/phi_itpllionhead.pt')
    phi.load_state_dict(torch.load('../models/phi_itpllionhead.pt'))
    animate_flow(x[:, 0:3], t, phi, 128, t.shape[0] // 100, mesh0=mesh0)


if __name__ == '__main__':
    main()

<mask token>
sys.path.append('../')
<mask token>
argparser = argparse.ArgumentParser()
argparser.add_argument('--mesh_filename0', '-f0', type=str, help=
    'Point cloud to reconstruct')
argparser.add_argument('--mesh_filename1', '-f1', type=str, help=
    'Point cloud to reconstruct')
argparser.add_argument('--max-sinkhorn-iters', '-si', type=int, default=64,
    help='Maximum number of Sinkhorn iterations')
argparser.add_argument('--sinkhorn-eps', '-se', type=float, default=0.001,
    help='Maximum number of Sinkhorn iterations')
argparser.add_argument('--learning-rate', '-lr', type=float, default=0.001,
    help='Step size for gradient descent')
argparser.add_argument('--num-epochs', '-n', type=int, default=250, help=
    'Number of training epochs')
argparser.add_argument('--device', '-d', type=str, default='cuda')
argparser.add_argument('--tol', '-tol', type=float, default=0.001)
args = argparser.parse_args()


def main():
    seed_everything(1024)
    mesh0 = pcu.read_obj(args.mesh_filename0)
    mesh1 = pcu.read_obj(args.mesh_filename1)
    t = torch.from_numpy(pcu.sample_mesh_lloyd(mesh0[0], mesh0[1][:, 0:3], 
        2000).astype(np.float32)).to(args.device)
    x = torch.from_numpy(pcu.sample_mesh_lloyd(mesh1[0], mesh1[1][:, 0:3], 
        2000).astype(np.float32)).to(args.device)
    t = precond(t, 0)
    print('haha', mesh0[0])
    precond(torch.from_numpy(mesh0[0]), 0)
    x = precond(x, 1)
    vardim = 3
    phi = InjAugNODE(in_dim=3, out_dim=3, var_dim=vardim, ker_dims=[1024, 
        1024, 1024, 1024], device=args.device).to(args.device)
    loss_fun = SinkhornLoss(eps=args.sinkhorn_eps, max_iters=args.
        max_sinkhorn_iters)
    dummy = torch.ones(x.shape[0], vardim).to(args.device)
    dummy[:, 0:x.shape[1]] = x
    x = dummy
    optimizer = torch.optim.Adam(phi.parameters(), lr=args.learning_rate)
    for epoch in range(1, args.num_epochs + 1):
        optimizer.zero_grad()
        y = phi(t)
        loss = loss_fun(y.unsqueeze(0), x.unsqueeze(0))
        loss.backward()
        optimizer.step()
        print('Epoch %d, loss = %f' % (epoch, loss.item()))
    print(phi.invert(x)[:, -1])
    torch.save(phi.state_dict(), '../models/phi_itpllionhead.pt')
    phi.load_state_dict(torch.load('../models/phi_itpllionhead.pt'))
    animate_flow(x[:, 0:3], t, phi, 128, t.shape[0] // 100, mesh0=mesh0)


if __name__ == '__main__':
    main()

import sys
sys.path.append('../')
import argparse
import open3d
import torch
import torch.nn as nn
import matplotlib.pyplot as plt
import numpy as np
import point_cloud_utils as pcu
from fml.nn import SinkhornLoss
from ODEOT.ANODE import InjAugNODE
from ODEOT.utils import load_mesh_by_file_extension, plot_flow, embed_3d, animate_flow, precond, seed_everything
argparser = argparse.ArgumentParser()
argparser.add_argument('--mesh_filename0', '-f0', type=str, help=
    'Point cloud to reconstruct')
argparser.add_argument('--mesh_filename1', '-f1', type=str, help=
    'Point cloud to reconstruct')
argparser.add_argument('--max-sinkhorn-iters', '-si', type=int, default=64,
    help='Maximum number of Sinkhorn iterations')
argparser.add_argument('--sinkhorn-eps', '-se', type=float, default=0.001,
    help='Maximum number of Sinkhorn iterations')
argparser.add_argument('--learning-rate', '-lr', type=float, default=0.001,
    help='Step size for gradient descent')
argparser.add_argument('--num-epochs', '-n', type=int, default=250, help=
    'Number of training epochs')
argparser.add_argument('--device', '-d', type=str, default='cuda')
argparser.add_argument('--tol', '-tol', type=float, default=0.001)
args = argparser.parse_args()


def main():
    seed_everything(1024)
    mesh0 = pcu.read_obj(args.mesh_filename0)
    mesh1 = pcu.read_obj(args.mesh_filename1)
    t = torch.from_numpy(pcu.sample_mesh_lloyd(mesh0[0], mesh0[1][:, 0:3], 
        2000).astype(np.float32)).to(args.device)
    x = torch.from_numpy(pcu.sample_mesh_lloyd(mesh1[0], mesh1[1][:, 0:3], 
        2000).astype(np.float32)).to(args.device)
    t = precond(t, 0)
    print('haha', mesh0[0])
    precond(torch.from_numpy(mesh0[0]), 0)
    x = precond(x, 1)
    vardim = 3
    phi = InjAugNODE(in_dim=3, out_dim=3, var_dim=vardim, ker_dims=[1024, 
        1024, 1024, 1024], device=args.device).to(args.device)
    loss_fun = SinkhornLoss(eps=args.sinkhorn_eps, max_iters=args.
        max_sinkhorn_iters)
    dummy = torch.ones(x.shape[0], vardim).to(args.device)
    dummy[:, 0:x.shape[1]] = x
    x = dummy
    optimizer = torch.optim.Adam(phi.parameters(), lr=args.learning_rate)
    for epoch in range(1, args.num_epochs + 1):
        optimizer.zero_grad()
        y = phi(t)
        loss = loss_fun(y.unsqueeze(0), x.unsqueeze(0))
        loss.backward()
        optimizer.step()
        print('Epoch %d, loss = %f' % (epoch, loss.item()))
    print(phi.invert(x)[:, -1])
    torch.save(phi.state_dict(), '../models/phi_itpllionhead.pt')
    phi.load_state_dict(torch.load('../models/phi_itpllionhead.pt'))
    animate_flow(x[:, 0:3], t, phi, 128, t.shape[0] // 100, mesh0=mesh0)


if __name__ == '__main__':
    main()

import sys
sys.path.append("../")
import argparse
import open3d
import torch
import torch.nn as nn

import matplotlib.pyplot as plt
import numpy as np

import point_cloud_utils as pcu
from fml.nn import SinkhornLoss
from ODEOT.ANODE import InjAugNODE
from ODEOT.utils import load_mesh_by_file_extension, plot_flow, embed_3d, animate_flow, precond, seed_everything

argparser = argparse.ArgumentParser()
argparser.add_argument("--mesh_filename0", "-f0", type=str, help="Point cloud to reconstruct")
argparser.add_argument("--mesh_filename1", "-f1", type=str, help="Point cloud to reconstruct")
argparser.add_argument("--max-sinkhorn-iters", "-si", type=int, default=64,
                       help="Maximum number of Sinkhorn iterations")
argparser.add_argument("--sinkhorn-eps", "-se", type=float, default=1e-3,
                       help="Maximum number of Sinkhorn iterations")
argparser.add_argument("--learning-rate", "-lr", type=float, default=1e-3, help="Step size for gradient descent")
argparser.add_argument("--num-epochs", "-n", type=int, default=250, help="Number of training epochs")
argparser.add_argument("--device", "-d", type=str, default="cuda")
# argparser.add_argument("--adjoint", "-adj", type=eval, default=False, choices=[True,False])
argparser.add_argument("--tol", "-tol", type=float, default=1e-3)
args = argparser.parse_args()


def main():
    # NOTE: We're doing everything in float32 and numpy defaults to float64, so you need to make sure you cast
    # everything to the right type

    # x is a tensor of shape [n, 3] containing the positions of the points we are trying to fit
    seed_everything(1024)
    mesh0 = pcu.read_obj(args.mesh_filename0)
    mesh1 = pcu.read_obj(args.mesh_filename1)
    t = torch.from_numpy(pcu.sample_mesh_lloyd(mesh0[0],mesh0[1][:,0:3],2000).astype(np.float32)).to(args.device)
    x = torch.from_numpy(pcu.sample_mesh_lloyd(mesh1[0],mesh1[1][:,0:3],2000).astype(np.float32)).to(args.device)

    t=precond(t,0)
    print("haha",mesh0[0])
    precond(torch.from_numpy(mesh0[0]),0)
    x=precond(x,1)
    vardim = 3
    phi = InjAugNODE(in_dim=3, out_dim=3, var_dim=vardim, ker_dims=[1024, 1024, 1024, 1024], device=args.device).to(
        args.device)
    # phi.load_state_dict(torch.load("../models/phi_itpllionhead.pt"))
    # Eps is 1/lambda and max_iters is the maximum number of Sinkhorn iterations to do
    loss_fun = SinkhornLoss(eps=args.sinkhorn_eps, max_iters=args.max_sinkhorn_iters)
    dummy = torch.ones(x.shape[0], vardim).to(args.device)
    dummy[:, 0:x.shape[1]] = x
    x = dummy

    # Here I'm using the Adam optimizer just as an example, you'll need to replace this with your thing
    optimizer = torch.optim.Adam(phi.parameters(), lr=args.learning_rate)
    # optimizer.add_param_group({"params": phi.augment_part})
    # print("Number of Parameters=", count_parameters(phi))

    for epoch in range(1, args.num_epochs + 1):
        optimizer.zero_grad()

        # Do the forward pass of the neural net, evaluating the function at the parametric points
        y = phi(t)

        loss = loss_fun(y.unsqueeze(0), x.unsqueeze(0))

        loss.backward()
        optimizer.step()
        print("Epoch %d, loss = %f" % (epoch, loss.item()))
        # b = x[0:1,:]
        # print(phi.invert(x))

    print(phi.invert(x)[:, -1])
    # print(x)
    torch.save(phi.state_dict(), "../models/phi_itpllionhead.pt")
    phi.load_state_dict(torch.load("../models/phi_itpllionhead.pt"))
    # plot_flow(x[:,0:3], t, phi, 128,  t.shape[0]//100)
    animate_flow(x[:, 0:3], t, phi, 128, t.shape[0] // 100, mesh0=mesh0)


if __name__ == "__main__":
    main()