I am trying to optimize the translations of the vertices of a scene with torch.optim.adam. It is a code piece from the redner tutorial series, which works fine with the initial setting. It tries to optimize a scene with shifting all the vertices by the same value called translation. Here is the original code:
vertices = []
for obj in base:
vertices.append(obj.vertices.clone())
def model(translation):
for obj, v in zip(base, vertices):
obj.vertices = v + translation
# Assemble the 3D scene.
scene = pyredner.Scene(camera = camera, objects = objects)
# Render the scene.
img = pyredner.render_albedo(scene)
return img
# Initial guess
# Set requires_grad=True since we want to optimize them later
translation = torch.tensor([10.0, -10.0, 10.0], device = pyredner.get_device(), requires_grad=True)
init = model(translation)
# Visualize the initial guess
t_optimizer = torch.optim.Adam([translation], lr=0.5)
I tried to modify the code such that it calculates an individual translation for each of the vertices. For this I applied the following modifications to the code above, that makes the shape of the translation from torch.Size([3]) to torch.Size([43380, 3]):
# translation = torch.tensor([10.0, -10.0, 10.0], device = pyredner.get_device(), requires_grad=True)
translation = base[0].vertices.clone().detach().requires_grad_(True)
translation[:] = 10.0
This introduces the ValueError: can't optimize a non-leaf Tensor. Could you please help me work around the problem.
PS: I am sorry for the long text, I am very new to this subject, and I wanted to state the problem as comprehensive as possible.