-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdemo_optimize_pytorch_geometry_nodes.py
More file actions
156 lines (118 loc) · 4.3 KB
/
demo_optimize_pytorch_geometry_nodes.py
File metadata and controls
156 lines (118 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import numpy as np
import torch
import torch.nn as nn
import os
import shutil
import matplotlib.pyplot as plt
import argparse
# rendering components
from pytorch3d.renderer import (
FoVPerspectiveCameras, look_at_view_transform, look_at_rotation,
RasterizationSettings, MeshRenderer, MeshRasterizer, BlendParams,
SoftSilhouetteShader, HardPhongShader, PointLights, TexturesVertex,
)
from PytorchGeoNodes.GeometryNodes import GeometryNodes
from PytorchGeoNodes.BlenderShapeProgram import BlenderShapeProgram
shape_program = BlenderShapeProgram(config_path='configs_shape_programs/sp_cabinet.json')
params_tree = shape_program.parse_params_tree_()
params_tree.set_values_from_dict({
'Width': 1.050,
'Board Thickness': 0.04,
'Depth': 0.6,
'Height': 0.98,
'Number of Dividing Boards': 3,
'Dividing Board Thickness': 0.04,
'Has Drawers': False,
'Has Back': False,
'Has Legs': True,
'Leg Width': 0.04,
'Leg Height': 0.04,
'Leg Depth': 0.04
}, is_normalized=False)
shape_program.set_params_from_tree(params_tree)
device = torch.device("cuda:0")
# input_params_dict = {}
input_params_dict = {
'Depth': nn.Parameter(torch.tensor([[1.0]], device=device)),
'Height': nn.Parameter(torch.tensor([[1.6]], device=device))}
geometry_nodes = GeometryNodes(shape_program)
geometry_nodes.to(device)
# render non-diff using pytorch3d
non_diff_py3d_mesh = shape_program.py3d_get_mesh().to(device)
R, T = look_at_view_transform(2.7, 0, 0, device=device)
cameras = FoVPerspectiveCameras(device=device, R=R, T=T)
blend_params = BlendParams(sigma=1e-4, gamma=1e-4)
raster_settings = RasterizationSettings(
image_size=256,
blur_radius=np.log(1. / 1e-4 - 1.) * blend_params.sigma,
faces_per_pixel=100,
)
silhouette_renderer = MeshRenderer(
rasterizer=MeshRasterizer(
cameras=cameras,
raster_settings=raster_settings
),
shader=SoftSilhouetteShader(blend_params=blend_params)
)
raster_settings = RasterizationSettings(
image_size=256,
blur_radius=0.0,
faces_per_pixel=1,
)
# We can add a point light in front of the object.
lights = PointLights(device=device, location=((2.0, 2.0, -2.0),))
phong_renderer = MeshRenderer(
rasterizer=MeshRasterizer(
cameras=cameras,
raster_settings=raster_settings
),
shader=HardPhongShader(device=device, cameras=cameras, lights=lights)
)
optim = torch.optim.Adam(list(input_params_dict.values()))
def loss_fn(pred_mesh, non_diff_py3d_mesh, R, T, get_image=False):
pred_mesh = pred_mesh.to_mesh()
pred_image = silhouette_renderer(meshes_world=pred_mesh, R=R, T=T)
gt_image = silhouette_renderer(meshes_world=non_diff_py3d_mesh, R=R, T=T)
loss = torch.sum((pred_image - gt_image) ** 2)
if get_image:
return loss, pred_image, gt_image
return loss
@torch.no_grad()
def log(pred_mesh, non_diff_py3d_mesh, R, T):
loss, pred_image, gt_image = loss_fn(pred_mesh, non_diff_py3d_mesh, R, T, get_image=True)
# convert to numpy
pred_image_np = pred_image.detach().cpu().numpy()[0][..., 3]
gt_image_np = gt_image.detach().cpu().numpy()[0][..., 3]
abs_diff = np.abs(pred_image_np - gt_image_np)
print('iter', iter, 'loss', loss.item())
plt.figure()
plt.subplot(1, 3, 1)
plt.imshow(pred_image_np)
plt.subplot(1, 3, 2)
plt.imshow(gt_image_np)
plt.subplot(1, 3, 3)
plt.imshow(abs_diff)
# plt.show()
plt.savefig(os.path.join(demo_dir, '%04d.png' % iter))
plt.close()
parser = argparse.ArgumentParser(description='Reconstruct objects')
parser.add_argument('--experiment_path', type=str, help='Dataset path')
args = parser.parse_args()
demo_dir = args.experiment_path
if os.path.exists(demo_dir):
shutil.rmtree(demo_dir)
os.makedirs(demo_dir)
for iter in range(1000):
# randomize dist, elev, azim
dist = np.random.uniform(2.5, 3.5)
elev = np.random.uniform(-np.pi / 8, np.pi / 8)
azim = np.random.uniform(-np.pi / 8, np.pi / 8)
R, T = look_at_view_transform(dist, elev, azim, degrees=False, device=device)
_, outputs = geometry_nodes.forward(input_params_dict, transform2blender_coords=True)
pred_mesh = outputs[0][0][0]
loss = loss_fn(pred_mesh, non_diff_py3d_mesh, R, T)
optim.zero_grad()
loss.backward()
optim.step()
if iter % 100 == 0:
log(pred_mesh, non_diff_py3d_mesh, R, T)