def vis(q):
     while True:
         model, resolution = q.get()
         sdf, _ = generate_volume(gdf(*model), resolution=resolution)
         sdf = np.reshape(sdf, (resolution, resolution, resolution))
         print(sdf.shape)
         visualize_sdf_as_mesh(sdf, resolution)
Exemple #2
0
def optimize_to_point_cloud(tree, pointcloud):
    ps = decompose_tree(tree)
    vs, p = tree
    model = gdf(vs, p)(Function('pos', 3))
    optimizer = torch.optim.Adam(ps)
    # LBFGS
    loss_fn = torch.nn.MSELoss(size_average=False)
    for epoch in range(100):
        for row_index in range(pointcloud.shape[0]):

            xyz = pointcloud[row_index][:3]
            d = pointcloud[row_index][3]
            # prediction = model(torch.tensor(xyz, dtype=torch.float32))
            # prediction = model(Function(xyz.tolist()))
            prediction = model.generate_model(
                {'pos': torch.tensor(xyz, dtype=torch.float32, device=torch.cuda.current_device())})
            expected = torch.tensor(d).unsqueeze(0)
            loss = loss_fn(prediction, expected)

            #print("Prediction {}; expected: {}".format(prediction, expected))
            # print(ps)

            optimizer.zero_grad()
            loss.backward(retain_graph=True)
            optimizer.step()
            model.update()

        with open("model.glsl", "w") as f:
            f.write(model.generate_shader())

        print(epoch, loss.item())
    return model
Exemple #3
0
def main():
    #header, sdf = load_sdf_from_file(argv[1])
    # visualizer.visualize_sdf_as_mesh(sdf)
    # visualizer.start_visualization()
    #pc = sdf_to_point_cloud(sdf, header)
    '''def vec3(a, b, c): return Function((a, b, c), 'requires_grad')
    tree = ([vec3(0.577, 0.577, 0.577),
             vec3(-0.577, 0.577, 0.577),
             vec3(0.577, -0.577, 0.577),
             vec3(0.577, 0.577, -0.577)],
            Function([9], 'requires_grad'))'''

    torch.backends.cudnn.deterministic = True
    torch.backends.mkl.deterministic = True
    tree = build_tree(2, 4)

    def vec3(a, b, c): return Function((a, b, c), 'requires_grad')
    sdf = ([vec3(0.577, 0.577, 0.577),
            vec3(-0.577, 0.577, 0.577),
            vec3(0.577, -0.577, 0.577),
            vec3(0.577, 0.577, -0.577)],
           Function([9], 'requires_grad'))

    #fitted = optimize_to_point_cloud(tree, pc)
    fitted = optimize_to_sdf(tree, gdf(*sdf)(Function('pos', 3)))
Exemple #4
0
def optimize_to_sdf(tree, sdf):
    ps = decompose_tree(tree)
    vs, p = tree
    model = gdf(vs, p)(Function('pos', 3))
    optimizer = torch.optim.Adam(ps)

    loss_fn = torch.nn.MSELoss(size_average=False)
    for epoch in range(100):

        #print_tree(tree, 0)

        for i in range(1000):

            start = time.time()
            xyz = torch.randn(BATCH_SIZE, 3, device=device)

            d = sdf.generate_model(
                {'pos': torch.tensor(xyz, dtype=torch.float32, device=device)})

            prediction = model.generate_model(
                {'pos': torch.tensor(xyz, dtype=torch.float32, device=device)})
            expected = d
            end = time.time()

            loss = loss_fn(prediction, expected)
            # print_tree(tree)
            print("Loss: {}".format(loss))

            optimizer.zero_grad()
            loss.backward(retain_graph=True)
            optimizer.step()
            model.update()
            print(end - start)

            if loss < 1e-14:
                break
        print(loss)

        with open("model.glsl", "w") as f:
            f.write(model.generate_shader())

    return model
def main():
    a = Function([3.0, 5.0])
    b = Function([4.0, 6.0])
    print((a + b).generate_shader())
    print((a + b).generate_model())
    c = a + b
    d = Function([5.0, 7.0])
    print(c.pow(d).generate_shader())

    b.model = torch.tensor([0.0, 0.0])
    b.update()
    print(c.generate_model())
    print(c.generate_shader())

    print(Function("a", 3).generate_shader())

    from model import gdf
    g = gdf([Function((0.577, 0.577, 0.577)),
             Function((0.577, 0.577, 0.577))], Function(9.0, 'requires_grad'))
    at_pos = g(Function((0.0, 0.0, 0.0)))
    print(at_pos.generate_shader())
def sdf_visualization(model, resolution=20):
    #queue.put((model, resolution))
    sdf, _ = generate_volume(gdf(*model), resolution=resolution)
    sdf = np.reshape(sdf, (resolution, resolution, resolution))
    visualize_sdf_as_mesh(sdf, resolution)