Shape-E 教程:如何利用 Shap-E 模型文本到3D和圖像到3D生成
Shap-E是OpenAI開發(fā)的一種創(chuàng)新模型,它可以使用文本或圖像作為輸入來生成一系列3D對象,從而改變了3D應用領域。這項非凡的技術可以在GitHub上免費訪問,用戶可以在自己的計算機上無縫運行它,不需要OpenAI API密鑰或互聯(lián)網連接。Shap-E的多功能性也是其與眾不同之處,用戶可以將生成的3D對象在Microsoft Paint 3D等軟件中打開,甚至可以轉換成STL文件進行3D打印。這項技術正在重新定義我們處理文本到3D和圖像到3D生成的方式,以及人工智能應用程序可以從中產生哪些創(chuàng)造性的可能性。
在本教程中,我們將學習如何在Google Colab中創(chuàng)建筆記本,設置和使用OpenAI的Shap-E模型生成3D模型,并使用Blender Studio對其進行自定義。
首先,您需要前往blender.org并下載與您的操作系統(tǒng)兼容的Blender Studio。
接下來,轉到Google Colab并創(chuàng)建一個新的筆記本。
在 Google Colab 中創(chuàng)建新筆記本
現在,我們需要將 Shap-E 存儲庫克隆到我們的 Google Colab Notebook。
!git?clone?https://github.com/openai/shap-e
進入目錄并安裝要求。
%cd?shap-e!pip?install?-e?.
添加新的code cell.
在這里,我們將導入所有必要的庫。
import?torchfrom?shap_e.diffusion.sample?import?sample_latentsfrom?shap_e.diffusion.gaussian_diffusion?import?diffusion_from_configfrom?shap_e.models.download?import?load_model,?load_configfrom?shap_e.util.notebooks?import?create_pan_cameras,?decode_latent_images,?gif_widget
請點擊“Run”按鈕或按下“CMD/CTRL + Enter”鍵來運行單個代碼塊。
現在,我們將設置設備為CUDA(如果可用),否則設置為CPU。
device?=?torch.device('cuda'?if?torch.cuda.is_available()?else?'cpu')
單擊Run或CMD/CTRL + Enter。
添加新的code cell.
在這里,我們將加載模型和權重。
xm?=?load_model('transmitter',?device=device)?model?=?load_model('text300M',?device=device)?diffusion?=?diffusion_from_config(load_config('diffusion'))
請點擊“Run”或按下“CMD/CTRL + Enter”鍵。
請耐心等待,加載模型和權重需要一些時間。對我來說,這大約花費了5分鐘的時間。但是,這取決于您的互聯(lián)網連接速度。
接下來,我們將生成一個3D模型。
batch_size?=?1?#?this?is?the?size?of?the?models,?higher?values?take?longer?to?generate.guidance_scale?=?15.0?#?this?is?the?scale?of?the?guidance,?higher?values?make?the?model?look?more?like?the?prompt.prompt?=?"a?donut"?#?this?is?the?prompt,?you?can?change?this?to?anything?you?want.latents?=?sample_latents( ????batch_size=batch_size, ????model=model, ????diffusion=diffusion, ????guidance_scale=guidance_scale, ????model_kwargs=dict(texts=[prompt]?*?batch_size), ????progress=True, ????clip_denoised=True, ????use_fp16=True, ????use_karras=True, ????karras_steps=64, ????sigma_min=1E-3, ????sigma_max=160, ????s_churn=0,)
單擊Run或CMD/CTRL + Enter。
生成 3D 模型需要一些時間,根據您的batch_size更高batch_size將需要更長的時間來生成 3D 模型。對我來說,生成 3D 模型大約需要 22 秒batch_size=1。
添加新的code cell.
這里我們將渲染 3D 模型,使用render_mode = 'nerf'?Neural Radiance Fields (NeRF)來渲染 3D 模型。您可以將其更改為使用樣式傳遞函數 (STF)render_mode = 'stf'渲染模式渲染 3D 模型。
render_mode?=?'nerf'?#?you?can?change?this?to?'stf'size?=?64?#?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): ????images?=?decode_latent_images(xm,?latent,?cameras,?rendering_mode=render_mode) ????display(gif_widget(images))
請點擊“Run”或按下“CMD/CTRL + Enter”鍵。
你看到模型旋轉了嗎?很酷,是嗎?
接下來,我們將把3D模型保存為.ply和.obj文件。
請注意:.obj文件將在稍后在Blender Studio中用于自定義。
#?Example?of?saving?the?latents?as?meshes.from?shap_e.util.notebooks?import?decode_latent_meshfor?i,?latent?in?enumerate(latents): ????t?=?decode_latent_mesh(xm,?latent).tri_mesh() ????with?open(f'example_mesh_{i}.ply',?'wb')?as?f:?#?this?is?three-dimensional?geometric?data?of?model. ????????t.write_ply(f) ????with?open(f'example_mesh_{i}.obj',?'w')?as?f:?#?we?will?use?this?file?to?customize?in?Blender?Studio?later. ????????t.write_obj(f)
單擊Run或CMD/CTRL + Enter。
將選項卡切換到Files并點擊刷新。您將看到example_mesh_0.ply和example_mesh_0.obj文件。
谷歌 Colab 文件
將文件下載.obj到本地計算機。
打開 Blender Studio 并創(chuàng)建新項目。
攪拌機工作室
刪除默認多維數據集。
刪除默認立方體
點擊File>?Import>?Wavefront (.obj)。選擇.obj您從 Google Colab 下載的文件。
導入 .obj 文件
您應該會在中心看到 3D 模型。
3D模型
它本身看起來很牛,順便說一句,Shap-E 做得很好。
您可以根據需要自定義 3D 模型。
出于演示目的,我簡單地使用粉紅色的彩色澆頭和棕色的甜甜圈。
定制3D模型
Midjourney 的 AI 生成藝術
在這個教程中,我們學習了如何使用Google Colab來設置和使用Shape-E模型。此外,我們還研究了Blender Studio并嘗試了自定義生成的3D模型。