Exemplo n.º 1
0
def decimate(obj: pyr.Object, verbose=False):
    """
    Decimates a mesh, reducing the number of faces by 2.
    This is EXTREMELY inefficient, and not differentiable - use it sparingly!
    Modifies the input mesh.
    """
    # Let's make a temporary directory
    intermediate_dir = tempfile.mkdtemp()

    orig_out = os.path.join(intermediate_dir, "orig.obj")
    new_out = os.path.join(intermediate_dir, "decim.obj")

    if verbose:
        print("Made temp dir:")
        print(intermediate_dir)

    # First, let's save the redner
    pyr.save_obj(obj, orig_out)
    # Now, let's load in openmesh
    mesh = openmesh.read_trimesh(orig_out)
    # Now, decimate by half
    orig_nfaces = mesh.n_faces()

    if verbose:
        print("Original # of faces:", orig_nfaces)

    decimator = openmesh.TriMeshDecimater(mesh)
    algorithm = openmesh.TriMeshModQuadricHandle()

    decimator.add(algorithm)
    decimator.initialize()
    decimator.decimate_to_faces(n_faces=round(orig_nfaces / 2))

    mesh.garbage_collection()

    if verbose:
        print("New # of faces:", mesh.n_faces())

    openmesh.write_mesh(new_out, mesh)

    # Now, we have it. Load it back into redner
    decim_obj = pyr.load_obj(new_out, return_objects=True)[0]
    # And set the faces/indices
    obj.vertices = decim_obj.vertices
    obj.indices = decim_obj.indices

    # Recompute normals - the face normals have been broken
    recompute_normals(obj)

    # Finally, clean up the dir
    files_to_delete = os.listdir(intermediate_dir)
    for each_file in files_to_delete:
        apath = os.path.join(intermediate_dir, each_file)
        if verbose:
            print("Deleting", apath)
        os.remove(apath)
    if verbose:
        print("Deleting", intermediate_dir)
    os.rmdir(intermediate_dir)
Exemplo n.º 2
0
def save_3d(mesh, mesh_name, allow_overwrite = True):
    '''
    Saves a 3D model
    '''
    dpath = os.path.join(mydir, mesh_name)
    fpath = os.path.join(dpath, "mesh.obj")
    os.makedirs(dpath, exist_ok=True)
    if os.path.isfile(fpath) and not allow_overwrite:
        raise FileExistsError(fpath)
    else:
        pyr.save_obj(mesh, fpath)
Exemplo n.º 3
0
def model(cam_pos, cam_look_at, shape_coeffs, color_coeffs, resolution, center,
          all_euler_angles, all_translations):
    # First rotate around center, then translation

    imgs = []

    #obj = pyredner.load_obj('p_ones30/final.obj', return_objects=True)[0]
    vertices, indices, uvs, normals = pyredner.generate_sphere(128, 64)
    vertices *= 80
    m = pyredner.Material(
        diffuse_reflectance=torch.ones(2, 2, 3, dtype=torch.float32))
    obj = pyredner.Object(vertices=vertices,
                          indices=indices,
                          normals=normals,
                          uvs=uvs,
                          material=m)
    v = obj.vertices.clone()

    for i in range(len(all_translations)):
        rotation_matrix = pyredner.gen_rotate_matrix(all_euler_angles[i]).to(
            pyredner.get_device())
        center = center.to(pyredner.get_device())
        # vertices = ((shape_mean + shape_basis @ shape_coeffs).view(-1, 3) - center) @ torch.t(rotation_matrix) + center + all_translations[i].to(pyredner.get_device())
        obj.vertices = (v - center) @ torch.t(rotation_matrix) + center
        obj.normals = pyredner.compute_vertex_normal(obj.vertices, indices)
        # colors = (color_mean + color_basis @ color_coeffs).view(-1, 3)
        # m = pyredner.Material(diffuse_reflectance = torch.tensor([0.5, 0.5, 0.5]))
        m = pyredner.Material(use_vertex_color=True)
        # obj = pyredner.Object(vertices=vertices, indices=indices, normals=normals, material=m, colors=colors)

        if i == 0:
            pyredner.save_obj(obj,
                              "generated/env_dataset_" + name + '/tgt_obj.obj')

        cam = pyredner.Camera(
            position=cam_pos,
            look_at=cam_look_at,  # Center of the vertices
            up=torch.tensor([0.0, 1.0, 0.0]),
            fov=torch.tensor([45.0]),
            resolution=resolution)
        scene = pyredner.Scene(camera=cam, objects=[obj], envmap=envmap)

        img = pyredner.render_pathtracing(scene=scene, num_samples=(128, 4))
        imgs.append(img)
    return imgs
Exemplo n.º 4
0
def model(cam_poses, cam_look_ats, shape_coeffs, color_coeffs, resolution):
    # First rotate around center, then translation

    imgs = []

    vertices = (shape_mean + shape_basis @ shape_coeffs).view(-1, 3)
    normals = pyredner.compute_vertex_normal(vertices, indices)
    colors = (color_mean + color_basis @ color_coeffs).view(-1, 3)
    m = pyredner.Material(use_vertex_color=False,
                          specular_reflectance=torch.tensor(
                              [1., 1., 1.], device=pyredner.get_device()),
                          roughness=torch.tensor([0.02]))
    obj = pyredner.Object(vertices=vertices,
                          indices=indices,
                          normals=normals,
                          material=m,
                          colors=colors)
    obj = pyredner.load_obj('generated/env_dataset_oness_n/tgt_obj.obj',
                            return_objects=True)[0]
    obj.material.specular_reflectance = pyredner.Texture(
        torch.tensor([0.05, 0.05, 0.05], device=pyredner.get_device()))
    obj.material.roughness = pyredner.Texture(torch.tensor([0.02]))
    pyredner.save_obj(obj, "generated/senv_dataset_" + name + '/tgt_obj.obj')

    for i in range(len(cam_poses)):
        cam = pyredner.Camera(
            position=cam_poses[i],
            look_at=cam_look_ats[i %
                                 len(cam_look_ats)],  # Center of the vertices
            up=torch.tensor([0.0, 1.0, 0.0]),
            fov=torch.tensor([45.0]),
            resolution=resolution)
        scene = pyredner.Scene(camera=cam, objects=[obj], envmap=envmap)

        img = pyredner.render_pathtracing(scene=scene, num_samples=(128, 4))
        imgs.append(img)
    return imgs
Exemplo n.º 5
0
        #pyredner.imwrite(abs(img - target[4]).data.cpu(), 'process/process2_img{:0>2d}.png'.format(t // 5))

    total_loss.backward()
    ver_optimizer.step()

    if smooth_scheme != 'None':# and t > 20:
        pyredner.smooth(vertices, indices, smooth_lmd, smooth_scheme, bound)
        pyredner.smooth(vertices, indices, smooth_lmd, smooth_scheme, bound)

    if t == 200:
        ver_optimizer=torch.optim.Adam([vertices], lr=0.05)

    print("{:.^16}total_loss = {:.6f}".format(t, total_loss))
    print((normals - pyredner.compute_vertex_normal(vertices, indices, max)).pow(2).sum())

pyredner.save_obj(obj, output_path + '/final.obj')
print(output_path + '/final.obj')

for i in range(len(cam_poses)):
    img, obj = model(cam_poses[i], cam_look_at, vertices, ambient_color, dir_light_intensity, dir_light_direction, normals)
    pyredner.imwrite(img.data.cpu(), output_path + '/view0{}.png'.format(i))

    plt.plot(losses[i], label='view0{}'.format(i))

plt.legend()
plt.ylabel("loss")
plt.xlabel("iterations")
plt.savefig(output_path + "/lossCurve.png", dpi=800)
xlim = plt.xlim()
ylim = plt.ylim()
Exemplo n.º 6
0
        # Plot the loss
        #f, (ax_loss, ax_diff_img, ax_img) = plt.subplots(1, 3)
        losses[i].append(loss.data.item())
        # Only store images every 10th iterations

    if t % 5 == 0:
        #      imgs.append(torch.pow(img.data, 1.0 / 2.2).cpu())  # Record the Gamma corrected image
        pyredner.imwrite(img.data.cpu(),
                         'process/process2_img{:0>2d}.png'.format(t // 5))
    print("{:.^20}".format(t))

    if t == 30:
        ver_optimizer = torch.optim.Adam([vertices], lr=0.01)

#for x in losses:
# print('0{:0>7.6f}'.format(x), end=' ')

pyredner.save_obj(obj, 'process/final.obj')

for i in range(len(cam_poses)):
    img, obj = model(cam_poses[i], cam_look_at, vertices, color_coeffs,
                     ambient_color, dir_light_intensity, dir_light_direction)
    pyredner.imwrite(img.data.cpu(), 'process/result/view0{}.png'.format(i))

    plt.plot(losses[i])

plt.ylabel("loss")
plt.xlabel("iterations")
plt.savefig("process/result/lossCurve.png", dpi=800)

print(losses)
Exemplo n.º 7
0
        for ind, img in enumerate(renders):
            diff = torch.abs(img - targets[ind])
            img = img.data.cpu()
            diff = diff.data.cpu()
            pyredner.imwrite(
                diff, path + "renders/diff_" + str(subdiv) + "_" + str(i) +
                "_" + str(ind) + ".png")
            pyredner.imwrite(
                img, path + "renders/geom_" + str(subdiv) + "_" + str(i) +
                "_" + str(ind) + ".png")

#    if torch.abs(loss - prev_loss) < 0.1:
#      break

        pyredner.save_obj(
            optim_objects[0],
            path + "models/output_" + str(subdiv) + "_" + str(i) + ".obj")

        loss.backward()
        optimizer.step()
        prev_loss = loss
        i += 1

    #if (len(optim_objects[0].indices) > face_target):
    #  simplify(refinemesh, refinemesh, face_target)
    #  face_target += 1000

    #refinemesh = path + "models/subdivision_" + str(subdiv) + ".obj"
    #os.system("meshlabserver -i " + path + "models/output_" + str(i - 1) + ".obj" + " -o " + refinemesh + " -s meshlab/subdivide.mlx")
pyredner.save_obj(
    optim_objects[0],
Exemplo n.º 8
0
shape_coe = 30 * torch.ones(199, device=pyredner.get_device(
))  #torch.randn(199, device=pyredner.get_device(), dtype=torch.float32)
color_coe = 3 * torch.ones(
    199, device=pyredner.get_device()
)  #torch.tensor(3 * nprd.randn(199), device=pyredner.get_device(), dtype=torch.float32)

imgs, obj = model(cam_poses, cam_look_at, shape_coe, color_coe, lights_list,
                  (1000, 1000))
for i in range(len(imgs)):
    pyredner.imwrite(
        imgs[i].cpu(),
        'generated/dataset_' + name + '/tgt_img{:0>2d}.png'.format(i))

obj.material = pyredner.Material(
    diffuse_reflectance=torch.tensor([0.5, 0.5, 0.5]))
pyredner.save_obj(obj, 'generated/dataset_' + name + '/' + name + '.obj')
np.save('generated/dataset_' + name + '/env_data.npy', env_data)
'''
import urllib

#urllib.request.urlretrieve('https://raw.githubusercontent.com/BachiLi/redner/master/tutorials/mona-lisa-cropped-256.png', 'target.png')
target = pyredner.imread('target.png').to(pyredner.get_device())
cam_pos = torch.tensor([-0.2697, -5.7891, 373.9277], requires_grad=True)
cam_look_at = torch.tensor([-0.2697, -5.7891, 54.7918], requires_grad=True)
shape_coeffs = torch.zeros(199, device=pyredner.get_device(), requires_grad=True)
color_coeffs = torch.zeros(199, device=pyredner.get_device(), requires_grad=True)
ambient_color = torch.ones(3, device=pyredner.get_device(), requires_grad=True)
dir_light_intensity = torch.zeros(3, device=pyredner.get_device(), requires_grad=True)

optimizer = torch.optim.Adam([shape_coeffs, color_coeffs, ambient_color, dir_light_intensity], lr=0.1)
cam_optimizer = torch.optim.Adam([cam_pos, cam_look_at], lr=0.5)