PartCrafter

repository·main·Indexed 25 days ago

https://github.com/wgsxm/partcrafter

A 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.

Tokens
5K
Snippets
9
Records
31
Agent score
82%

What's inside PartCrafter

  1. Preprocess a single 3D object

    main
    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.
  2. Install PartCrafter

    main

    To 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 pyopengl
  3. Preprocess datasets for training

    main

    To prepare a dataset for training, use the preprocess.py script. 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_data
  4. Generate 3D part-level objects from an image

    main

    Use scripts/inference_partcrafter.py to 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 --render
  5. Preprocess a folder of meshes

    main

    To preprocess an entire directory of meshes at once, use the preprocess.py script. This will process all meshes in the input folder and automatically generate a configuration file named object_part_configs.json in the output directory.

    python datasets/preprocess/preprocess.py --input assets/objects --output preprocessed_data
  6. Generate 3D scenes from an image

    main

    Use scripts/inference_partcrafter_scene.py to generate a full 3D scene from an image. This uses the PartCrafter-Scene model 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_suggest and --style_transfer are 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 --render
  7. Train PartCrafter

    main

    Training 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_KEY in the training script if using wandb.

    Training Scenarios:

    1. From scratch: Use scripts/train_partcrafter.sh with a base config.
    2. Finetuning (Larger part count): Use --load_pretrained_model and --load_pretrained_model_ckpt to build upon a previous checkpoint.
    3. 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
  8. Configure the dataset for training

    main

    The training code requires a JSON configuration file where each entry represents an object. You can use the generated object_part_configs.json or 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
        }, 
        {
            "..."
        }
    ]
  9. Apply style transfer for real-world images

    main

    To bridge the domain gap between real-world photos and the Objaverse-style training data, use the --style_transfer flag. This converts the input photo into an Objaverse-style 3D rendering before processing.

    Configuration:

    • Default model: gemini-3.1-flash-image-preview.
    • Use --style_provider and --style_model to 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
  10. Use VLM-based part suggestion

    main

    Instead 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 a GEMINI_API_KEY environment variable.

    Configuration:

    • Default model: gemini-3-flash-preview.
    • Use --part_provider and --part_model to override the provider or model.
  11. Configure geometry extraction parameters in PartCrafterPipeline

    main

    When calling the pipeline, you can control the precision and performance of the 3D mesh reconstruction via several octree and decoding parameters:

    ParameterTypeDescription
    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] or List[float]The spatial bounding box for the geometry extraction process.
  12. Dataset configuration field reference

    main

    The 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 .npy file 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. If false, the object is filtered out during training.