Code Monkey home page Code Monkey logo

Comments (6)

eliphatfs avatar eliphatfs commented on July 22, 2024 4

I have a basic alternative solution using the trimesh library.
Also, if you have this same concern as me: vertices generated by marching cubes are very dense, and vertex color is enough for the color resolution.

@torch.no_grad()
def decode_render(
    xm: Union[Transmitter, VectorDecoder],
    latent: torch.Tensor,
    cameras: DifferentiableCameraBatch,
    rendering_mode: str = "stf",
):
    decoded = xm.renderer.render_views(
        AttrDict(cameras=cameras),
        params=(xm.encoder if isinstance(xm, Transmitter) else xm).bottleneck_to_params(
            latent[None]
        ),
        options=AttrDict(rendering_mode=rendering_mode, render_with_direction=False),
    )
    arr = decoded.channels.clamp(0, 255).to(torch.uint8)[0].cpu().numpy()
    return decoded, [Image.fromarray(x) for x in arr]


render_mode = 'stf' # you can change this to 'stf' for mesh rendering
size = 2  # this is the size of the renders; higher values take longer to render.

cameras = create_pan_cameras(size, device)
for i, latent in enumerate(latents):
    decoded, images = decode_render(xm, latent, cameras, rendering_mode=render_mode)
    assert len(decoded['meshes']) == 1
    mesh = decoded['meshes'][0]
    trimesh.exchange.export.export_mesh(trimesh.Trimesh(
        mesh.vertices.cpu().numpy(),
        mesh.faces.cpu().numpy(),
        vertex_colors=mesh.vertex_colors.cpu().numpy()
    ), "barrel-gen.glb")

from shap-e.

HeronErin avatar HeronErin commented on July 22, 2024 1

Update, If anybody else needs this, see PR #20

And if you want a ply file to display color in blender use this tutorial

from shap-e.

dennisushi avatar dennisushi commented on July 22, 2024

I used meshlab to generate a texture.png from the vertex colors. The export to .obj takes care of the rest.

Ref:
facebookresearch/pifuhd#8 (comment)

from shap-e.

vangoghvapor avatar vangoghvapor commented on July 22, 2024

I have a basic alternative solution using the trimesh library. Also, if you have this same concern as me: vertices generated by marching cubes are very dense, and vertex color is enough for the color resolution.

@torch.no_grad()
def decode_render(
    xm: Union[Transmitter, VectorDecoder],
    latent: torch.Tensor,
    cameras: DifferentiableCameraBatch,
    rendering_mode: str = "stf",
):
    decoded = xm.renderer.render_views(
        AttrDict(cameras=cameras),
        params=(xm.encoder if isinstance(xm, Transmitter) else xm).bottleneck_to_params(
            latent[None]
        ),
        options=AttrDict(rendering_mode=rendering_mode, render_with_direction=False),
    )
    arr = decoded.channels.clamp(0, 255).to(torch.uint8)[0].cpu().numpy()
    return decoded, [Image.fromarray(x) for x in arr]


render_mode = 'stf' # you can change this to 'stf' for mesh rendering
size = 2  # this is the size of the renders; higher values take longer to render.

cameras = create_pan_cameras(size, device)
for i, latent in enumerate(latents):
    decoded, images = decode_render(xm, latent, cameras, rendering_mode=render_mode)
    assert len(decoded['meshes']) == 1
    mesh = decoded['meshes'][0]
    trimesh.exchange.export.export_mesh(trimesh.Trimesh(
        mesh.vertices.cpu().numpy(),
        mesh.faces.cpu().numpy(),
        vertex_colors=mesh.vertex_colors.cpu().numpy()
    ), "barrel-gen.glb")

what is 'trimesh.exchange'? it doesn't seem to exist in my library

from shap-e.

vangoghvapor avatar vangoghvapor commented on July 22, 2024

couple follow up questions, how could this be implemented using other render modes (the others throw errors)?
how could this be implemented with pytorch3d (texture seems to be implemented differently vs a trimesh)?
can this be added as an example in the project?

from shap-e.

InTimmyDate avatar InTimmyDate commented on July 22, 2024

I have a basic alternative solution using the trimesh library. Also, if you have this same concern as me: vertices generated by marching cubes are very dense, and vertex color is enough for the color resolution.

@torch.no_grad()
def decode_render(
    xm: Union[Transmitter, VectorDecoder],
    latent: torch.Tensor,
    cameras: DifferentiableCameraBatch,
    rendering_mode: str = "stf",
):
    decoded = xm.renderer.render_views(
        AttrDict(cameras=cameras),
        params=(xm.encoder if isinstance(xm, Transmitter) else xm).bottleneck_to_params(
            latent[None]
        ),
        options=AttrDict(rendering_mode=rendering_mode, render_with_direction=False),
    )
    arr = decoded.channels.clamp(0, 255).to(torch.uint8)[0].cpu().numpy()
    return decoded, [Image.fromarray(x) for x in arr]


render_mode = 'stf' # you can change this to 'stf' for mesh rendering
size = 2  # this is the size of the renders; higher values take longer to render.

cameras = create_pan_cameras(size, device)
for i, latent in enumerate(latents):
    decoded, images = decode_render(xm, latent, cameras, rendering_mode=render_mode)
    assert len(decoded['meshes']) == 1
    mesh = decoded['meshes'][0]
    trimesh.exchange.export.export_mesh(trimesh.Trimesh(
        mesh.vertices.cpu().numpy(),
        mesh.faces.cpu().numpy(),
        vertex_colors=mesh.vertex_colors.cpu().numpy()
    ), "barrel-gen.glb")

Where do I get Transmitter, VectorDecoder, and DifferentiableCameraBatch?

from shap-e.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.