|
<!--Copyright 2023 The HuggingFace Team. All rights reserved. |
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
|
the License. You may obtain a copy of the License at |
|
|
|
http://www.apache.org/licenses/LICENSE-2.0 |
|
|
|
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
|
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
|
specific language governing permissions and limitations under the License. |
|
--> |
|
|
|
# Shap-E |
|
|
|
[[open-in-colab]] |
|
|
|
Shap-E๋ ๋น๋์ค ๊ฒ์ ๊ฐ๋ฐ, ์ธํ
๋ฆฌ์ด ๋์์ธ, ๊ฑด์ถ์ ์ฌ์ฉํ ์ ์๋ 3D ์์
์ ์์ฑํ๊ธฐ ์ํ conditional ๋ชจ๋ธ์
๋๋ค. ๋๊ท๋ชจ 3D ์์
๋ฐ์ดํฐ์
์ ํ์ต๋์๊ณ , ๊ฐ ์ค๋ธ์ ํธ์ ๋ ๋ง์ ๋ทฐ๋ฅผ ๋ ๋๋งํ๊ณ 4K point cloud ๋์ 16K๋ฅผ ์์ฑํ๋๋ก ํ์ฒ๋ฆฌํฉ๋๋ค. Shap-E ๋ชจ๋ธ์ ๋ ๋จ๊ณ๋ก ํ์ต๋ฉ๋๋ค: |
|
|
|
1. ์ธ์ฝ๋๊ฐ 3D ์์
์ ํฌ์ธํธ ํด๋ผ์ฐ๋์ ๋ ๋๋ง๋ ๋ทฐ๋ฅผ ๋ฐ์๋ค์ด๊ณ ์์
์ ๋ํ๋ด๋ implicit functions์ ํ๋ผ๋ฏธํฐ๋ฅผ ์ถ๋ ฅํฉ๋๋ค. |
|
2. ์ธ์ฝ๋๊ฐ ์์ฑํ latents๋ฅผ ๋ฐํ์ผ๋ก diffusion ๋ชจ๋ธ์ ํ๋ จํ์ฌ neural radiance fields(NeRF) ๋๋ textured 3D ๋ฉ์๋ฅผ ์์ฑํ์ฌ ๋ค์ด์คํธ๋ฆผ ์ ํ๋ฆฌ์ผ์ด์
์์ 3D ์์
์ ๋ ์ฝ๊ฒ ๋ ๋๋งํ๊ณ ์ฌ์ฉํ ์ ์๋๋ก ํฉ๋๋ค. |
|
|
|
์ด ๊ฐ์ด๋์์๋ Shap-E๋ฅผ ์ฌ์ฉํ์ฌ ๋๋ง์ 3D ์์
์ ์์ฑํ๋ ๋ฐฉ๋ฒ์ ๋ณด์
๋๋ค! |
|
|
|
์์ํ๊ธฐ ์ ์ ๋ค์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ค์น๋์ด ์๋์ง ํ์ธํ์ธ์: |
|
|
|
```py |
|
# Colab์์ ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ค์นํ๊ธฐ ์ํด ์ฃผ์์ ์ ์ธํ์ธ์ |
|
#!pip install -q diffusers transformers accelerate trimesh |
|
``` |
|
|
|
## Text-to-3D |
|
|
|
3D ๊ฐ์ฒด์ gif๋ฅผ ์์ฑํ๋ ค๋ฉด ํ
์คํธ ํ๋กฌํํธ๋ฅผ [`ShapEPipeline`]์ ์ ๋ฌํฉ๋๋ค. ํ์ดํ๋ผ์ธ์ 3D ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๋ฐ ์ฌ์ฉ๋๋ ์ด๋ฏธ์ง ํ๋ ์ ๋ฆฌ์คํธ๋ฅผ ์์ฑํฉ๋๋ค. |
|
|
|
```py |
|
import torch |
|
from diffusers import ShapEPipeline |
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
|
|
pipe = ShapEPipeline.from_pretrained("openai/shap-e", torch_dtype=torch.float16, variant="fp16") |
|
pipe = pipe.to(device) |
|
|
|
guidance_scale = 15.0 |
|
prompt = ["A firecracker", "A birthday cupcake"] |
|
|
|
images = pipe( |
|
prompt, |
|
guidance_scale=guidance_scale, |
|
num_inference_steps=64, |
|
frame_size=256, |
|
).images |
|
``` |
|
|
|
์ด์ [`~utils.export_to_gif`] ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฏธ์ง ํ๋ ์ ๋ฆฌ์คํธ๋ฅผ 3D ๊ฐ์ฒด์ gif๋ก ๋ณํํฉ๋๋ค. |
|
|
|
```py |
|
from diffusers.utils import export_to_gif |
|
|
|
export_to_gif(images[0], "firecracker_3d.gif") |
|
export_to_gif(images[1], "cake_3d.gif") |
|
``` |
|
|
|
<div class="flex gap-4"> |
|
<div> |
|
<img class="rounded-xl" src="https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/shap_e/firecracker_out.gif"/> |
|
<figcaption class="mt-2 text-center text-sm text-gray-500">prompt = "A firecracker"</figcaption> |
|
</div> |
|
<div> |
|
<img class="rounded-xl" src="https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/shap_e/cake_out.gif"/> |
|
<figcaption class="mt-2 text-center text-sm text-gray-500">prompt = "A birthday cupcake"</figcaption> |
|
</div> |
|
</div> |
|
|
|
## Image-to-3D |
|
|
|
๋ค๋ฅธ ์ด๋ฏธ์ง๋ก๋ถํฐ 3D ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ค๋ฉด [`ShapEImg2ImgPipeline`]์ ์ฌ์ฉํฉ๋๋ค. ๊ธฐ์กด ์ด๋ฏธ์ง๋ฅผ ์ฌ์ฉํ๊ฑฐ๋ ์์ ํ ์๋ก์ด ์ด๋ฏธ์ง๋ฅผ ์์ฑํ ์ ์์ต๋๋ค. [Kandinsky 2.1](../api/pipelines/kandinsky) ๋ชจ๋ธ์ ์ฌ์ฉํ์ฌ ์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํด ๋ณด๊ฒ ์ต๋๋ค. |
|
|
|
```py |
|
from diffusers import DiffusionPipeline |
|
import torch |
|
|
|
prior_pipeline = DiffusionPipeline.from_pretrained("kandinsky-community/kandinsky-2-1-prior", torch_dtype=torch.float16, use_safetensors=True).to("cuda") |
|
pipeline = DiffusionPipeline.from_pretrained("kandinsky-community/kandinsky-2-1", torch_dtype=torch.float16, use_safetensors=True).to("cuda") |
|
|
|
prompt = "A cheeseburger, white background" |
|
|
|
image_embeds, negative_image_embeds = prior_pipeline(prompt, guidance_scale=1.0).to_tuple() |
|
image = pipeline( |
|
prompt, |
|
image_embeds=image_embeds, |
|
negative_image_embeds=negative_image_embeds, |
|
).images[0] |
|
|
|
image.save("burger.png") |
|
``` |
|
|
|
์น์ฆ๋ฒ๊ฑฐ๋ฅผ [`ShapEImg2ImgPipeline`]์ ์ ๋ฌํ์ฌ 3D representation์ ์์ฑํฉ๋๋ค. |
|
|
|
```py |
|
from PIL import Image |
|
from diffusers import ShapEImg2ImgPipeline |
|
from diffusers.utils import export_to_gif |
|
|
|
pipe = ShapEImg2ImgPipeline.from_pretrained("openai/shap-e-img2img", torch_dtype=torch.float16, variant="fp16").to("cuda") |
|
|
|
guidance_scale = 3.0 |
|
image = Image.open("burger.png").resize((256, 256)) |
|
|
|
images = pipe( |
|
image, |
|
guidance_scale=guidance_scale, |
|
num_inference_steps=64, |
|
frame_size=256, |
|
).images |
|
|
|
gif_path = export_to_gif(images[0], "burger_3d.gif") |
|
``` |
|
|
|
<div class="flex gap-4"> |
|
<div> |
|
<img class="rounded-xl" src="https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/shap_e/burger_in.png"/> |
|
<figcaption class="mt-2 text-center text-sm text-gray-500">cheeseburger</figcaption> |
|
</div> |
|
<div> |
|
<img class="rounded-xl" src="https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/shap_e/burger_out.gif"/> |
|
<figcaption class="mt-2 text-center text-sm text-gray-500">3D cheeseburger</figcaption> |
|
</div> |
|
</div> |
|
|
|
## ๋ฉ์ ์์ฑํ๊ธฐ |
|
|
|
Shap-E๋ ๋ค์ด์คํธ๋ฆผ ์ ํ๋ฆฌ์ผ์ด์
์ ๋ ๋๋งํ textured ๋ฉ์ ์ถ๋ ฅ์ ์์ฑํ ์๋ ์๋ ์ ์ฐํ ๋ชจ๋ธ์
๋๋ค. ์ด ์์ ์์๋ ๐ค Datasets ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ [Dataset viewer](https://huggingface.co/docs/hub/datasets-viewer#dataset-preview)๋ฅผ ์ฌ์ฉํด ๋ฉ์ ์๊ฐํ๋ฅผ ์ง์ํ๋ `glb` ํ์ผ๋ก ๋ณํํฉ๋๋ค. |
|
|
|
`output_type` ๋งค๊ฐ๋ณ์๋ฅผ `"mesh"`๋ก ์ง์ ํจ์ผ๋ก์จ [`ShapEPipeline`]๊ณผ [`ShapEImg2ImgPipeline`] ๋ชจ๋์ ๋ํ ๋ฉ์ ์ถ๋ ฅ์ ์์ฑํ ์ ์์ต๋๋ค: |
|
|
|
```py |
|
import torch |
|
from diffusers import ShapEPipeline |
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
|
|
pipe = ShapEPipeline.from_pretrained("openai/shap-e", torch_dtype=torch.float16, variant="fp16") |
|
pipe = pipe.to(device) |
|
|
|
guidance_scale = 15.0 |
|
prompt = "A birthday cupcake" |
|
|
|
images = pipe(prompt, guidance_scale=guidance_scale, num_inference_steps=64, frame_size=256, output_type="mesh").images |
|
``` |
|
|
|
๋ฉ์ ์ถ๋ ฅ์ `ply` ํ์ผ๋ก ์ ์ฅํ๋ ค๋ฉด [`~utils.export_to_ply`] ํจ์๋ฅผ ์ฌ์ฉํฉ๋๋ค: |
|
|
|
<Tip> |
|
|
|
์ ํ์ ์ผ๋ก [`~utils.export_to_obj`] ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ฉ์ ์ถ๋ ฅ์ `obj` ํ์ผ๋ก ์ ์ฅํ ์ ์์ต๋๋ค. ๋ค์ํ ํ์์ผ๋ก ๋ฉ์ ์ถ๋ ฅ์ ์ ์ฅํ ์ ์์ด ๋ค์ด์คํธ๋ฆผ์์ ๋์ฑ ์ ์ฐํ๊ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค! |
|
|
|
</Tip> |
|
|
|
```py |
|
from diffusers.utils import export_to_ply |
|
|
|
ply_path = export_to_ply(images[0], "3d_cake.ply") |
|
print(f"Saved to folder: {ply_path}") |
|
``` |
|
|
|
๊ทธ ๋ค์ trimesh ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ `ply` ํ์ผ์ `glb` ํ์ผ๋ก ๋ณํํ ์ ์์ต๋๋ค: |
|
|
|
```py |
|
import trimesh |
|
|
|
mesh = trimesh.load("3d_cake.ply") |
|
mesh_export = mesh.export("3d_cake.glb", file_type="glb") |
|
``` |
|
|
|
๊ธฐ๋ณธ์ ์ผ๋ก ๋ฉ์ ์ถ๋ ฅ์ ์๋์ชฝ ์์ ์ ์ด์ ์ด ๋ง์ถฐ์ ธ ์์ง๋ง ํ์ ๋ณํ์ ์ ์ฉํ์ฌ ๊ธฐ๋ณธ ์์ ์ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค: |
|
|
|
```py |
|
import trimesh |
|
import numpy as np |
|
|
|
mesh = trimesh.load("3d_cake.ply") |
|
rot = trimesh.transformations.rotation_matrix(-np.pi / 2, [1, 0, 0]) |
|
mesh = mesh.apply_transform(rot) |
|
mesh_export = mesh.export("3d_cake.glb", file_type="glb") |
|
``` |
|
|
|
๋ฉ์ ํ์ผ์ ๋ฐ์ดํฐ์
๋ ํฌ์งํ ๋ฆฌ์ ์
๋ก๋ํด Dataset viewer๋ก ์๊ฐํํ์ธ์! |
|
|
|
<div class="flex justify-center"> |
|
<img class="rounded-xl" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/3D-cake.gif"/> |
|
</div> |
|
|