facebookresearch / pytorch3d

PyTorch3D is FAIR's library of reusable components for deep learning with 3D data
https://pytorch3d.org/
Other
8.81k stars 1.32k forks source link

Unexpected behavior in reduced cosine distance between point-clouds in `chamfer` #1534

Closed ArjunNarayanan closed 1 year ago

ArjunNarayanan commented 1 year ago

Loss is near zero for normals with opposite orientation

The use of torch.abs in the following lines ends up assigning a small loss to normals that are of opposite orientation,

        cham_norm_x = 1 - torch.abs(
            F.cosine_similarity(x_normals, x_normals_near, dim=2, eps=1e-6)
        )
        cham_norm_y = 1 - torch.abs(
            F.cosine_similarity(y_normals, y_normals_near, dim=2, eps=1e-6)
        )

Example

import torch
from pytorch3d.loss import chamfer_distance

verts = torch.rand([1,1000,3])
normals = torch.rand([1,1000,3])

chd, chn = chamfer_distance(verts, verts, x_normals=normals, y_normals=normals)
# chd = 0, chn = 1e-8

chd, chn = chamfer_distance(verts, verts, x_normals=normals, y_normals=-normals)
# chd = 0, chn = 1e-8

I would expect the cosine similarity loss to have a large value in the second case because the normals are flipped in direction.

In my application I have a mesh that is small in the thickness dimension. When I compute the chamfer distance to the target shape, I want the inner and outer surfaces of the mesh to match appropriately with the target shape. Since the normals have roughly opposite orientation, I would expect that using the cosine similarity functionality of chamfer_distance would help. But because of the issue I point out here, it does not work as I would hope.

bottler commented 1 year ago

This is a similar question to #1458 . The behaviour is deliberate, but if you need something else you could adapt the code to what you need.