Skip to content

Texture Vertex.num_verts_per_mesh deep copy when num_verts_per_mesh is a list #623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

JudyYe
Copy link
Contributor

@JudyYe JudyYe commented Apr 2, 2021

Hi,

Nice work! Thank you for this pytorch3d repo which boosted my research efficiency. However, I found a potential unexpected behavior regarding TextureVertex. Here is my guess, proposed fixed, and a toy example.

The TexturesVertex._num_verts_per_mesh is not correctly copied in clone(), detach(), join_batch()when this property is a list. In fact, it is created as a list as in __init__ here or here

As a result, when a list of Mehses is join_batched(), the num_verts_per_mesh in the list will be unexpectedly modified although the merged meshes is in correct shape. A toyish self-contained test is as followed:

verts, faces = create_cube('cpu', 1)
textures = TexturesVertex(torch.ones_like(verts))
mesh_list = []
for i in range(2):
    meshes = Meshes(verts, faces, textures)
    mesh_list.append(meshes)
for i, each in enumerate(mesh_list):
    print(i, each.textures._num_verts_per_mesh)
# output: 
# 0 [8]
# 1 [8]
joined_meshes = join_meshes_as_batch(mesh_list)
print('joined', joined_meshes.textures._num_verts_per_mesh)
# output: 
# joined [8, 8]
for i, each in enumerate(mesh_list):
    print(i, each.textures._num_verts_per_mesh)
# output: 
0 [8, 8] // !!!!
1 [8, 8] // !!!!

Note the output becomes [8, 8] from [8] after join_batch(). After the fixed, the last output is [8] as expected.

create_cube is:

def create_cube(device, N=1, align='center'):
    """
    :return: verts: (1, 8, 3) faces: (1, 12, 3)
    """
    cube_verts = torch.tensor(
        [
            [0, 0, 0],
            [0, 0, 1],
            [0, 1, 0],
            [0, 1, 1],
            [1, 0, 0],
            [1, 0, 1],
            [1, 1, 0],
            [1, 1, 1],
        ],
        dtype=torch.float32,
        device=device,
    )
    if align == 'center':
        cube_verts -= .5

    # faces corresponding to a unit cube: 12x3
    cube_faces = torch.tensor(
        [
            [0, 1, 2],
            [1, 3, 2],  # left face: 0, 1
            [2, 3, 6],
            [3, 7, 6],  # bottom face: 2, 3
            [0, 2, 6],
            [0, 6, 4],  # front face: 4, 5
            [0, 5, 1],
            [0, 4, 5],  # up face: 6, 7
            [6, 7, 5],
            [6, 5, 4],  # right face: 8, 9
            [1, 7, 3],
            [1, 5, 7],  # back face: 10, 11
        ],
        dtype=torch.int64,
        device=device,
    )  # 12, 3

    return cube_verts.unsqueeze(0).expand(N, 8, 3), cube_faces.unsqueeze(0).expand(N, 12, 3)

@facebook-github-bot
Copy link
Contributor

Hi @JudyYe!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 2, 2021
@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@facebook-github-bot
Copy link
Contributor

@bottler has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@bottler
Copy link
Contributor

bottler commented Apr 9, 2021

Thanks for letting us know about this. I agree there is a bug here and these fixes are correct. I plan to commit them together with another fix in the same area.

@gkioxari
Copy link
Contributor

@JudyYe
Thank you very much for the kind words! It makes us happy that this library has helped you :)
Just came here to say this :D

@JudyYe
Copy link
Contributor Author

JudyYe commented Apr 15, 2021

@gkioxari

Aww. Thank you Georgia! That is so kind of you. Thank you for putting such efforts in maintaining the repo. The whole team is so prompt. And you made my fan-girl moment of the day! :p

@JudyYe JudyYe closed this Apr 15, 2021
@bottler bottler reopened this Apr 15, 2021
facebook-github-bot pushed a commit that referenced this pull request Apr 20, 2021
Summary:
When a list of Meshes is `join_batched()`, the `num_verts_per_mesh` in the list would be unexpectedly modified.

Also some cleanup around `_num_verts_per_mesh`.

Pull Request resolved: #623

Test Plan: A modification to an existing test checks this.

Reviewed By: nikhilaravi

Differential Revision: D27682104

Pulled By: bottler

fbshipit-source-id: 9d00913dfb4869bd6c7d3f5cc9156b7b6f1aecc9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants