Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions README_for_apple_silicon.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
## 🤗 Get Started with Hunyuan3D 2.1 On MacOS

Hunyuan3D 2.1 supports Macos, Windows, Linux. You may follow the next steps to use Hunyuan3D 2.1 via for your Apple Silicon chip devices:

### Install Requirements
We test our model on an A100 GPU with Python 3.10 and PyTorch for Apple silicon, reference: https://developer.apple.com/metal/pytorch/.
```bash
pip install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu
pip install py-cpuinfo
pip install -r requirements_for_apple_silicon.txt

cd hy3dpaint/custom_rasterizer
export FORCE_CPU=1
pip install .
cd ../..
cd hy3dpaint/DifferentiableRenderer
bash compile_mesh_painter_for_apple_silicon.sh
cd ../..

wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth -P hy3dpaint/ckpt
```

### Code Usage

We designed a diffusers-like API to use our shape generation model - Hunyuan3D-Shape and texture synthesis model -
Hunyuan3D-Paint.

You run this to test the demo.
```python
python demo_for_apple_silicon.py
```

```python
import sys
sys.path.insert(0, './hy3dshape')
sys.path.insert(0, './hy3dpaint')
from textureGenPipeline import Hunyuan3DPaintPipeline
from textureGenPipeline import Hunyuan3DPaintPipeline, Hunyuan3DPaintConfig
from hy3dshape.pipelines import Hunyuan3DDiTFlowMatchingPipeline

# let's generate a mesh first
shape_pipeline = Hunyuan3DDiTFlowMatchingPipeline.from_pretrained('tencent/Hunyuan3D-2.1')
mesh_untextured = shape_pipeline(image='assets/demo.png')[0]

paint_pipeline = Hunyuan3DPaintPipeline(Hunyuan3DPaintConfig(max_num_view=6, resolution=512))
mesh_textured = paint_pipeline(mesh_path, image_path='assets/demo.png')
```


### Gradio App

You could also host a [Gradio](https://www.gradio.app/) App in your own computer via:


```bash
python gradio_app.py \
--model_path tencent/Hunyuan3D-2.1 \
--subfolder hunyuan3d-dit-v2-1 \
--texgen_model_path tencent/Hunyuan3D-2.1 \
--device mps \
--low_vram_mode
```
65 changes: 65 additions & 0 deletions demo_for_apple_silicon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import sys
import gc

sys.path.insert(0, './hy3dshape')
sys.path.insert(0, './hy3dpaint')

from PIL import Image
import torch

from hy3dshape.rembg import BackgroundRemover
from hy3dshape.pipelines import Hunyuan3DDiTFlowMatchingPipeline
from textureGenPipeline import Hunyuan3DPaintPipeline, Hunyuan3DPaintConfig

try:
from torchvision_fix import apply_fix

apply_fix()
except ImportError:
print("Warning: torchvision_fix module not found, proceeding without compatibility fix")
except Exception as e:
print(f"Warning: Failed to apply torchvision fix: {e}")

# Set device
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
if torch.backends.mps.is_available():
device = torch.device("mps")

# shape
model_path = 'tencent/Hunyuan3D-2.1'


pipeline_shapegen = Hunyuan3DDiTFlowMatchingPipeline.from_pretrained(model_path, device=device)

image_path = 'assets/demo.png'
image = Image.open(image_path).convert("RGBA")
if image.mode == 'RGB':
rembg = BackgroundRemover()
image = rembg(image)

mesh = pipeline_shapegen(image=image, num_inference_steps=50)[0]
mesh.export('demo.glb')

# clean up
del pipeline_shapegen
del mesh
gc.collect()
torch.cuda.empty_cache()

# paint
max_num_view = 6 # can be 6 to 9
resolution = 512 # can be 768 or 512
conf = Hunyuan3DPaintConfig(max_num_view, resolution)
conf.realesrgan_ckpt_path = "hy3dpaint/ckpt/RealESRGAN_x4plus.pth"
conf.multiview_cfg_path = "hy3dpaint/cfgs/hunyuan-paint-pbr.yaml"
conf.custom_pipeline = "hy3dpaint/hunyuanpaintpbr"
conf.device = device

paint_pipeline = Hunyuan3DPaintPipeline(conf)

output_mesh_path = 'demo_textured.glb'
output_mesh_path = paint_pipeline(
mesh_path="demo.glb",
image_path='assets/demo.png',
output_mesh_path=output_mesh_path
)
1 change: 1 addition & 0 deletions gradio_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,7 @@ def on_export_click(file_out, file_out2, file_type,
conf.realesrgan_ckpt_path = "hy3dpaint/ckpt/RealESRGAN_x4plus.pth"
conf.multiview_cfg_path = "hy3dpaint/cfgs/hunyuan-paint-pbr.yaml"
conf.custom_pipeline = "hy3dpaint/hunyuanpaintpbr"
conf.device = args.device
tex_pipeline = Hunyuan3DPaintPipeline(conf)

# Not help much, ignore for now.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c++ -O3 -Wall -shared -std=c++11 -fPIC `python -m pybind11 --includes` mesh_inpaint_processor.cpp -o mesh_inpaint_processor`python3-config --extension-suffix` -undefined dynamic_lookup
Loading