PartCrafter
repository·main·Indexed 25 days ago
https://github.com/wgsxm/partcrafterA structured 3D generative model that uses compositional latent diffusion transformers to generate multiple parts and objects from a single RGB image. It includes tools for 3D part-level and scene-level generation, VLM-based part suggestion, style transfer for real-world images, and a complete pipeline for preprocessing GLB meshes and training on Objaverse data.
What's inside PartCrafter
- To preprocess a single GLB file, run the following scripts in sequence to sample surface points, render images, remove backgrounds, and optionally calculate IoU. These scripts are minimal implementations designed to illustrate the pipeline for a single object.
Install PartCrafter
mainTo install PartCrafter, it is recommended to use a Conda environment with Python 3.11. The installation involves setting up PyTorch with CUDA 12.4 support, cloning the repository, and running the provided setup script. If you lack root access, you can install required graphics libraries via Conda.
Note for Windows users: Follow the instructions in this pull request or use this fork.
# Create and activate conda environment conda create -n partcrafter python=3.11.13 conda activate partcrafter # Install PyTorch with CUDA 12.4 pip install torch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1 --index-url https://download.pytorch.org/whl/cu124 # Install dependencies git clone https://github.com/wgsxm/PartCrafter.git cd PartCrafter bash settings/setup.sh # Optional: Install graphics libraries if no root access conda install -c conda-forge libegl libglu pyopenglPreprocess datasets for training
mainTo prepare a dataset for training, use the
preprocess.pyscript. This script processes GLB files and saves the preprocessed data.Command:
python datasets/preprocess/preprocess.py --input <input_dir> --output <output_dir>python datasets/preprocess/preprocess.py --input assets/objects --output preprocessed_dataGenerate 3D part-level objects from an image
mainUse
scripts/inference_partcrafter.pyto generate a structured 3D object from a single RGB image. The model automatically downloads the necessary PartCrafter and RMBG weights.Key Arguments:
--image_path: Path to the input image.--num_parts: Number of parts to generate.--tag: A label for the output directory.--render: Enables rendering of the result.--rmbg: (Recommended for custom images) Removes the background and resizes the image appropriately.
python scripts/inference_partcrafter.py \ --image_path assets/images/np3_2f6ab901c5a84ed6bbdf85a67b22a2ee.png \ --num_parts 3 --tag robot --renderPreprocess a folder of meshes
mainTo preprocess an entire directory of meshes at once, use the
preprocess.pyscript. This will process all meshes in the input folder and automatically generate a configuration file namedobject_part_configs.jsonin the output directory.python datasets/preprocess/preprocess.py --input assets/objects --output preprocessed_dataGenerate 3D scenes from an image
mainUse
scripts/inference_partcrafter_scene.pyto generate a full 3D scene from an image. This uses thePartCrafter-Scenemodel weights (automatically downloaded).Key Arguments:
--image_path: Path to the scene image.--num_parts: Number of parts/objects in the scene.--tag: A label for the output directory.--render: Enables rendering.
Note:
--part_suggestand--style_transferare also supported for scene-level generation.python scripts/inference_partcrafter_scene.py \ --image_path assets/images_scene/np6_0192a842-531c-419a-923e-28db4add8656_DiningRoom-31158.png \ --num_parts 6 --tag dining_room --renderTrain PartCrafter
mainTraining requires downloading TripoSG weights first. The training scripts are optimized for 8 H20 GPUs. You can train from scratch or finetune with different part counts or token counts.
Prerequisites:
- Download TripoSG:
huggingface-cli download VAST-AI/TripoSG --local-dir pretrained_weights/TripoSG - Configure
WANDB_API_KEYin the training script if usingwandb.
Training Scenarios:
- From scratch: Use
scripts/train_partcrafter.shwith a base config. - Finetuning (Larger part count): Use
--load_pretrained_modeland--load_pretrained_model_ckptto build upon a previous checkpoint. - Finetuning (More tokens): Use a config with higher token counts (e.g.,
mp16_nt1024.yaml).
# Train from scratch bash scripts/train_partcrafter.sh --config configs/mp8_nt512.yaml --use_ema \ --gradient_accumulation_steps 4 \ --output_dir output_partcrafter \ --tag scaleup_mp8_nt512 # Finetune with larger number of parts bash scripts/train_partcrafter.sh --config configs/mp16_nt512.yaml --use_ema \ --gradient_accumulation_steps 4 \ --output_dir output_partcrafter \ --load_pretrained_model scaleup_mp8_nt512 \ --load_pretrained_model_ckpt 10 \ --tag scaleup_mp16_nt512- Download TripoSG:
Configure the dataset for training
mainThe training code requires a JSON configuration file where each entry represents an object. You can use the generated
object_part_configs.jsonor create your own following this schema. Each object entry must include paths to the mesh, surface points, and rendered image, along with part-related metadata.[ { "mesh_path": "/path/to/object.glb", "surface_path": "/path/to/object.npy", "image_path": "/path/to/object.png", "num_parts": 4, "iou_mean": 0.5, "iou_max": 0.9, "valid": true }, { "..." } ]Apply style transfer for real-world images
mainTo bridge the domain gap between real-world photos and the Objaverse-style training data, use the
--style_transferflag. This converts the input photo into an Objaverse-style 3D rendering before processing.Configuration:
- Default model:
gemini-3.1-flash-image-preview. - Use
--style_providerand--style_modelto override the provider or model.
# Style transfer for real-world photos GEMINI_API_KEY=your_key python scripts/inference_partcrafter.py \ --image_path real_photo.jpg \ --num_parts 4 --style_transfer --rmbg --render # Combining part suggestion and style transfer GEMINI_API_KEY=your_key python scripts/inference_partcrafter.py \ --image_path real_photo.jpg \ --part_suggest --style_transfer --rmbg --render- Default model:
Use VLM-based part suggestion
mainInstead of manually specifying
--num_parts, you can use a Vision Language Model (VLM) to analyze the image and suggest an appropriate part count. This requires aGEMINI_API_KEYenvironment variable.Configuration:
- Default model:
gemini-3-flash-preview. - Use
--part_providerand--part_modelto override the provider or model.
- Default model:
Configure geometry extraction parameters in PartCrafterPipeline
mainWhen calling the pipeline, you can control the precision and performance of the 3D mesh reconstruction via several octree and decoding parameters:
Parameter Type Description dense_octree_depthintDepth of the dense octree used for geometry extraction. hierarchical_octree_depthintDepth of the hierarchical octree used for geometry extraction. max_num_expanded_coordsintMaximum number of expanded coordinates allowed during extraction. flash_octree_depthintDepth used for flash-based octree operations. use_flash_decoderboolWhether to use the flash decoder during the VAE decoding stage. boundsTuple[float]orList[float]The spatial bounding box for the geometry extraction process. Dataset configuration field reference
mainThe following keys are used in the dataset configuration JSON file:
mesh_path: The path to the GLB file of the object.surface_path: The path to the.npyfile containing the object surface points.image_path: The path to the rendered image of the object (after background removal).num_parts: The number of parts the object is composed of.iou_mean: The mean Intersection over Union (IoU) of the object parts.iou_max: The maximum IoU of the object parts.valid: A boolean flag. Iffalse, the object is filtered out during training.