Skip to content

Shap-E: add support for mesh output - #4062

Merged
patrickvonplaten merged 15 commits into
mainfrom
shap-ee-mesh
Jul 20, 2023
Merged

Shap-E: add support for mesh output #4062
patrickvonplaten merged 15 commits into
mainfrom
shap-ee-mesh

Conversation

@yiyixuxu

Copy link
Copy Markdown
Collaborator

This PR adds support to mesh output for Shap-E
#3986

importtorchfromdiffusersimportDiffusionPipelinefromdiffusers.utilsimportexport_to_plydevice=torch.device('cuda'iftorch.cuda.is_available() else'cpu')
#repo_prefix = "openai"repo_prefix="YiYiXu"pipe=DiffusionPipeline.from_pretrained(f"{repo_prefix}/shap-e", torch_dtype=torch.float16)
pipe=pipe.to(device)
guidance_scale=15.0prompt="a shark"generator=torch.Generator(device=device).manual_seed(0)
images=pipe(
prompt, generator=generator, guidance_scale=guidance_scale,
num_inference_steps=64, output_type="mesh"
).imagesply_path=export_to_ply(images[0], "3d_shark.ply")
print(f"saved to folder: {ply_path}")

@yiyixuxuyiyixuxu changed the title Shap ee meshShap-E: add support for mesh output Jul 12, 2023
@HuggingFaceDocBuilderDev

HuggingFaceDocBuilderDev commented Jul 12, 2023

Copy link
Copy Markdown

The documentation is not available anymore as the PR was closed or merged.

for k, (c1, c2) in enumerate(zip(tri[::2], tri[1::2])):
cases[i, j, k] = edge_to_index[(c1, c2) if c1 < c2 else (c2, c1)]
masks[i, j] = True
return cases, masks

@yiyixuxuyiyixuxuJul 12, 2023

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saved this lookup table as part of weights for renderer but this is how we can generate it if we want to include it in the code instead

cases is tensor of shape 256 x 5 x 3, masks is 256 x 5

@patrickvonplatenpatrickvonplaten left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! very clean! Can we maybe just add a new section to the docs, showing how one can generate a mesh (just change output type) and then maybe linking to a generated mesh?

@yiyixuxu
yiyixuxu requested a review from hystsJuly 13, 2023 05:56
Comment threadsrc/diffusers/pipelines/shap_e/renderer.py
@yiyixuxu

yiyixuxu commented Jul 13, 2023

Copy link
Copy Markdown
CollaboratorAuthor

@patrickvonplaten

linking to a generated mesh

What does this mean?

@sayakpaul

Copy link
Copy Markdown
Member

What does this mean?

I think Patrick meant hosting the generated mesh file on a repository on Hub so that folks can use that. If there are online visualization tools it would be cool to host one such generated mesh file and leave a link to the instance.

@hysts what do you suggest?

@sayakpaul

Copy link
Copy Markdown
Member

I think @yiyixuxu has updated the Shap-E repository from her personal profile. Let's not forget to transfer them to OpenAI and update the model cards accordingly with this support.

@hysts

Copy link
Copy Markdown
Contributor

I think Patrick meant hosting the generated mesh file on a repository on Hub so that folks can use that. If there are online visualization tools it would be cool to host one such generated mesh file and leave a link to the instance.

@hysts what do you suggest?

As HF Hub supports mesh visualization, maybe we can upload generated 3D mesh samples as dataset.

out.mp4

@hystshysts left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! LGTM!

@sayakpaul

Copy link
Copy Markdown
Member

#4062 (comment)

@yiyixuxu let's make sure this in the documentation :)

@hysts

Copy link
Copy Markdown
Contributor

FYI, as a minor point, the output mesh of the Shap-E model is from the bottom viewpoint, so it might be useful if export_to_ply and export_to_obj had an option to rotate the model.

The default viewpoint is like this:

and I rotate this mesh like this:

importtrimeshmesh=trimesh.load('3d_shark.obj')
rot=trimesh.transformations.rotation_matrix(-np.pi/2, [1, 0, 0])
mesh=mesh.apply_transform(rot)
mesh.export('3d_shark2.obj', file_type='obj')

Also, it might be useful if .glb was supported because only .glb seems to be supported by the mesh visualization in the Hub.

But users can do these with a few lines of code, so maybe it doesn't have to be supported by the diffusers util function.

@pcuencapcuenca left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!

Comment threaddocs/source/en/api/pipelines/shap_e.mdx Outdated
Comment threaddocs/source/en/api/pipelines/shap_e.mdx Outdated
Comment threadsrc/diffusers/pipelines/shap_e/pipeline_shap_e.py
Comment threadsrc/diffusers/pipelines/shap_e/pipeline_shap_e_img2img.py
@yiyixuxu

Copy link
Copy Markdown
CollaboratorAuthor

@hysts

FYI, as a minor point, the output mesh of the Shap-E model is from the bottom viewpoint, so it might be useful if export_to_ply and export_to_obj had an option to rotate the model.

great point! but this requires installing trimesh so I prefer to just add a section in documentation about how to rotate the mesh

Also, it might be useful if .glb was supported because only .glb seems to be supported by the mesh visualization in the Hub.

same, I think it requires additional dependency so prefer to document it instead

CC @patrickvonplaten let me know what you think about this

yiyixuxuand others added 2 commits July 13, 2023 10:50
Co-authored-by: Pedro Cuenca <pedro@huggingface.co>
@yiyixuxu

Copy link
Copy Markdown
CollaboratorAuthor

As HF Hub supports mesh visualization, maybe we can upload generated 3D mesh samples as dataset.

this is so cool! thanks! I didn't know that
https://huggingface.co/datasets/hf-internal-testing/diffusers-images/blob/main/shap_e/3d_cake.glb

@sayakpaul

Copy link
Copy Markdown
Member

great point! but this requires installing trimesh so I prefer to just add a section in documentation about how to rotate the mesh

same, I think it requires additional dependency so prefer to document it instead

Big +1 here. Let's make sure to document them. And also let the readers know about the capability of https://huggingface.co/datasets/hf-internal-testing/diffusers-images/blob/main/shap_e/3d_cake.glb. Together, this collection is starting to look really really amazing.

Thanks for your hard work, @yiyixuxu ❤️

Comment threaddocs/source/en/api/pipelines/shap_e.mdx Outdated
Comment threadsrc/diffusers/pipelines/shap_e/pipeline_shap_e_img2img.py Outdated
Comment threadsrc/diffusers/pipelines/shap_e/pipeline_shap_e.py Outdated

@sayakpaulsayakpaul left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing! 🔥

yiyixuxuand others added 2 commits July 15, 2023 20:24
Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
@patrickvonplaten

Copy link
Copy Markdown
Contributor

Feel free to merge @yiyixuxu

@yiyixuxu

Copy link
Copy Markdown
CollaboratorAuthor

@patrickvonplaten can I have a final look? think it is good to go now

sent 2 PR on the openai repo too:
https://huggingface.co/openai/shap-e/discussions/10
https://huggingface.co/openai/shap-e-img2img/discussions/7

@yiyixuxu

Copy link
Copy Markdown
CollaboratorAuthor

ohh forgot about tests... fixing it now

@pcuencapcuenca left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests look fine to me. I suppose we just need to merge the PRs in the HF repos, right?

@patrickvonplaten

patrickvonplaten commented Jul 20, 2023

Copy link
Copy Markdown
Contributor

I think it would have been better to leave renderer as is (not delete it from the init) and instead just add a new renderer_table to the init. This would have made this PR 100% backwards compatible and would not force a warning going forward, but ok for now.

@patrickvonplaten
patrickvonplaten merged commit 47b3346 into mainJul 20, 2023
@patrickvonplaten
patrickvonplaten deleted the shap-ee-mesh branch July 20, 2023 16:06
@yiyixuxu

Copy link
Copy Markdown
CollaboratorAuthor

think it would have been better to leave renderer as is (not delete it from the init) and instead just add a new renderer_table to the init.

ohhh yes should have done that

orpatashnik pushed a commit to orpatashnik/diffusers that referenced this pull request Aug 1, 2023
* add output_type=mesh
* update img2img
* make style
* add doc
* make style
* Apply suggestions from code review
Co-authored-by: Pedro Cuenca <pedro@huggingface.co>
* add docstring for output_type
* add a section in doc about hub mesh visualization/ rotation
* update conversion script so default background is white
* Apply suggestions from code review
Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
* Update src/diffusers/pipelines/shap_e/pipeline_shap_e_img2img.py
Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
* renderer -> shap_e_renderer
* img2img renderer -> shap_e_renderer
* fix tests
---------
Co-authored-by: yiyixuxu <yixu310@gmail,com>
Co-authored-by: Pedro Cuenca <pedro@huggingface.co>
Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
orpatashnik pushed a commit to orpatashnik/diffusers that referenced this pull request Aug 1, 2023
* add output_type=mesh
* update img2img
* make style
* add doc
* make style
* Apply suggestions from code review
Co-authored-by: Pedro Cuenca <pedro@huggingface.co>
* add docstring for output_type
* add a section in doc about hub mesh visualization/ rotation
* update conversion script so default background is white
* Apply suggestions from code review
Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
* Update src/diffusers/pipelines/shap_e/pipeline_shap_e_img2img.py
Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
* renderer -> shap_e_renderer
* img2img renderer -> shap_e_renderer
* fix tests
---------
Co-authored-by: yiyixuxu <yixu310@gmail,com>
Co-authored-by: Pedro Cuenca <pedro@huggingface.co>
Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
orpatashnik pushed a commit to orpatashnik/diffusers that referenced this pull request Aug 1, 2023
* add output_type=mesh
* update img2img
* make style
* add doc
* make style
* Apply suggestions from code review
Co-authored-by: Pedro Cuenca <pedro@huggingface.co>
* add docstring for output_type
* add a section in doc about hub mesh visualization/ rotation
* update conversion script so default background is white
* Apply suggestions from code review
Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
* Update src/diffusers/pipelines/shap_e/pipeline_shap_e_img2img.py
Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
* renderer -> shap_e_renderer
* img2img renderer -> shap_e_renderer
* fix tests
---------
Co-authored-by: yiyixuxu <yixu310@gmail,com>
Co-authored-by: Pedro Cuenca <pedro@huggingface.co>
Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
yoonseokjin pushed a commit to yoonseokjin/diffusers that referenced this pull request Dec 25, 2023
* add output_type=mesh
* update img2img
* make style
* add doc
* make style
* Apply suggestions from code review
Co-authored-by: Pedro Cuenca <pedro@huggingface.co>
* add docstring for output_type
* add a section in doc about hub mesh visualization/ rotation
* update conversion script so default background is white
* Apply suggestions from code review
Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
* Update src/diffusers/pipelines/shap_e/pipeline_shap_e_img2img.py
Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
* renderer -> shap_e_renderer
* img2img renderer -> shap_e_renderer
* fix tests
---------
Co-authored-by: yiyixuxu <yixu310@gmail,com>
Co-authored-by: Pedro Cuenca <pedro@huggingface.co>
Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
AmericanPresidentJimmyCarter pushed a commit to AmericanPresidentJimmyCarter/diffusers that referenced this pull request Apr 26, 2024
* add output_type=mesh
* update img2img
* make style
* add doc
* make style
* Apply suggestions from code review
Co-authored-by: Pedro Cuenca <pedro@huggingface.co>
* add docstring for output_type
* add a section in doc about hub mesh visualization/ rotation
* update conversion script so default background is white
* Apply suggestions from code review
Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
* Update src/diffusers/pipelines/shap_e/pipeline_shap_e_img2img.py
Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
* renderer -> shap_e_renderer
* img2img renderer -> shap_e_renderer
* fix tests
---------
Co-authored-by: yiyixuxu <yixu310@gmail,com>
Co-authored-by: Pedro Cuenca <pedro@huggingface.co>
Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
@hlkyhlky mentioned this pull request Apr 29, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

@yiyixuxu@HuggingFaceDocBuilderDev@sayakpaul@hysts@patrickvonplaten@pcuenca