示例#1
0
def main():
    ROOT = 'tests/meshes/bowling_Pin/'
    FNAME = '10492_Bowling Pin_v1_max2011_iteration-2.obj'
    mesh = loadMesh(fname=ROOT+FNAME)
    mesh_xyz = mesh.vertices.T
    m_norm = np.average(np.linalg.norm(mesh_xyz, axis=0))
    mesh_xyz /= m_norm

    norms, stdd = project(mesh_xyz)
    obs = mk_obsrv(stdd,  norms)

    coords, weights, xyz = get_lebdev_grid()

    #coords = leb2sk(coords)

    interpolator = Interpolator(obs, r=1.0)
    interpolated = interpolator(coords)

    SHFit.theta = coords[:, 0] + np.pi
    SHFit.phi = coords[:, 1]
    SHFit.xyz = xyz.copy()

    coefficients = []

    for n in range(30):
        for l in range(-n, n+1):
            sph_harm_pts = make_harmonic(m=l, n=n,
                                         theta=SHFit.theta, phi=SHFit.phi)

            integral = 4*np.pi*np.sum(weights*np.real(sph_harm_pts)*interpolated[:,2])
            fit = SHFit(l, n, integral)
            print(fit)
            coefficients.append(fit)

    fig, ax = plt.subplots(nrows=1, ncols=3, sharey=False)

    cs = ax[0].scatter(obs[:, 1], obs[:, 0], c=obs[:, 2])
    cbar = fig.colorbar(cs)
    ax[1].scatter(interpolated[:, 1], interpolated[:, 0], c=interpolated[:, 2])
    ax[2].scatter(coords[:, 0], coords[:, 1], c=sph_harm_pts[:])
    plt.show()

    from mpl_toolkits.mplot3d import Axes3D
    fig = plt.figure()
    ax1 = fig.add_subplot(121, projection='3d')
    ax2 = fig.add_subplot(122, projection='3d')

    #new_pts = xyz * (np.real(sph_harm_pts)**2)[:,None]
    #new_pts = norms * stdd

    new_pts = np.zeros(SHFit.xyz.shape, dtype=SHFit.xyz.dtype)
    for c in coefficients:
       new_pts += c.reconstructed
    #print(new_pts)

    ax1.scatter(*new_pts.T)
    ax2.scatter(*(xyz*interpolated[:,2][:,None]).T, alpha=0.2)
    plt.show()
示例#2
0
def encode(target_img,
           num_steps=1000,
           w_avg_samples=10000,
           initial_learning_rate=0.1,
           initial_noise_factor=0.05,
           lr_rampdown_length=0.25,
           lr_rampup_length=0.05,
           noise_ramp_length=0.75,
           regularize_noise_weight=1e5,
           device='cuda',
           verbose=True):

    global G
    assert G is not None, 'Error: no model loaded'

    # Load target image
    if isinstance(target_img, np.ndarray):
        target_img = PIL.Image.fromarray(target_img.astype(np.uint8))
    w, h = target_img.size
    s = min(w, h)
    target_img = target_img.crop(
        ((w - s) // 2, (h - s) // 2, (w + s) // 2, (h + s) // 2))
    target_img = target_img.resize((G.img_resolution, G.img_resolution),
                                   PIL.Image.LANCZOS)
    target_img = np.array(target_img, dtype=np.uint8)
    target_img = torch.tensor(target_img.transpose([2, 0, 1]), device=device)

    # copy model
    G = copy.deepcopy(G).eval().requires_grad_(False).to(
        device)  # type: ignore

    # run projector
    projected_w_steps = projector.project(
        G,
        target=target_img,
        num_steps=num_steps,
        w_avg_samples=w_avg_samples,
        initial_learning_rate=initial_learning_rate,
        initial_noise_factor=initial_noise_factor,
        lr_rampdown_length=lr_rampdown_length,
        lr_rampup_length=lr_rampup_length,
        noise_ramp_length=noise_ramp_length,
        regularize_noise_weight=regularize_noise_weight,
        device=device,
        verbose=verbose)

    # return final step
    projected_w = projected_w_steps[-1].unsqueeze(0)
    return projected_w
示例#3
0
def project_img(G, fname, num_steps):
    img = prepare_img_for_projection(G, fname)
    projected_w_steps = project(G, target=img, num_steps=num_steps, device=get_device(), verbose=True)
    w = projected_w_steps[-1].unsqueeze(0).detach().cpu().numpy()
    return w