Pytorch3d: Does pytorch3d support the optimization of the light parameters?

Created on 4 Nov 2020  ·  2Comments  ·  Source: facebookresearch/pytorch3d

My pytorch3d version is 0.2.0. I 'm trying to optimize the ambient color of a directional light, but the parameter doesn't change in the optimization process(I 've added the parameter list to the optimizer). I 'm wondering whether pytorch3d support the optimization of light paramters? Or can you please provide a demo to show how to optimize them? Below are the code where I encounter this problem.

The parameters I 'm trying to optimize

light_coeff = torch.randn([3],requires_grad=True, device='cuda:0')
lights = DirectionalLights(device='cuda:0',
direction=((0, 0, 1),),
ambient_color=((light_coeff[0], light_coeff[1], light_coeff[2]),),
diffuse_color=((0, 0, 0),)
)

I 'm using the differentiable renderer to draw the image

tex_renderer = MeshRenderer(
rasterizer=rasterizer,
shader=SoftPhongShader(device='cuda:0', cameras=camera, lights=lights, blend_params=blend_params)
)
render_result = tex_renderer(mesh)
img_render = render_result[:, :, :, 0:3].permute(0, 3, 1, 2) # Change from (b,h,w,3) to (b,3,h,w)

And I'm using the mse error to calculate the reconstruction loss

criterion_recon = nn.mseloss()
loss_Recon = criterion_recon(img_renderer, img_gt)

question

Most helpful comment

You're creating a tensor of light coefficients but then initializing the ambient_color as a tuple - this will be converted to a new tensor in the Lights class so the gradients wont be tracked.

Can you try doing this:

ambient_color = torch.randn([3],requires_grad=True, device='cuda:0')
lights = DirectionalLights(
  device='cuda:0',
  direction=((0, 0, 1),),
  ambient_color=ambient_color[None, ...],
  diffuse_color=((0, 0, 0),)
)

All 2 comments

You're creating a tensor of light coefficients but then initializing the ambient_color as a tuple - this will be converted to a new tensor in the Lights class so the gradients wont be tracked.

Can you try doing this:

ambient_color = torch.randn([3],requires_grad=True, device='cuda:0')
lights = DirectionalLights(
  device='cuda:0',
  direction=((0, 0, 1),),
  ambient_color=ambient_color[None, ...],
  diffuse_color=((0, 0, 0),)
)

It works! Thanks a lot!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aluo-x picture aluo-x  ·  3Comments

elcronos picture elcronos  ·  3Comments

cihanongun picture cihanongun  ·  3Comments

udemegane picture udemegane  ·  3Comments

shersoni610 picture shersoni610  ·  3Comments