Skip to content

Propagate from rendered image into meshes? #276

Closed
@magicknight

Description

@magicknight

Hi,

I am trying to propagate from a single rendered image into meshes, but it seems that the meshes never converge?
My poor cow was deforming into a moster like this:

gif

This is the original cow and the target cow:

target

My code looks like this:

class Model(nn.Module):
    def __init__(self, mesh, tracer, image_ref):
        super().__init__()
        self.mesh = mesh
        self.new_mesh = mesh.clone()
        self.device = tracer.device
        self.tracer = tracer
        
        # Get the reference image
        image_ref = defect_image
        self.register_buffer('image_ref', image_ref)

        # Create an optimizable parameter mesh vertices
#         self.vertices = nn.Parameter(self.mesh.verts_packed()).to(self.device)
        
        # We will learn to deform the source mesh by offsetting its vertices
        # The shape of the deform parameters is equal to the total number of vertices in src_mesh
        self.deform_verts = torch.full(self.mesh.verts_packed().shape, 0.0, device=self.device, requires_grad=True)

    def forward(self):
        self.new_mesh = self.mesh.offset_verts(self.deform_verts)
        
        # Based on the new position of the vertices we update mesh, then make new projection using the updated mesh      
        image = renderer(self.new_mesh, lights=lights, materials=materials, cameras=cameras)

        # Calculate the loss
#         loss = 0.001 * torch.sum((image - self.image_ref) ** 2) + torch.sum(self.deform_verts ** 2)
        loss = torch.sum((image - self.image_ref) ** 2)
        return loss, image

# Initialize a model using the renderer, mesh and reference image
model = Model(mesh=cow_mesh, tracer=tracer, image_ref=reference_proj).to(device)

# Create an optimizer. Here we are using Adam and we pass in the vertices of the model
optimizer = torch.optim.Adam([model.deform_verts], lr=0.005, weight_decay=0.5)

Metadata

Metadata

Assignees

Labels

how toHow to use PyTorch3D in my project

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions