BRepNet

repository·master·Indexed 42 days ago

https://github.com/autodeskailab/brepnet

A topological message passing system for neural networks to operate directly on solid models using B-Rep (Boundary Representation) data structures. It includes a pipeline for processing STEP files, training models on the Fusion 360 Gallery segmentation dataset, and evaluating segmentation on custom STEP/STP data.

Tokens
7.6K
Snippets
22
Records
30
Agent score
51%

What's inside BRepNet

  1. How BRepNet convolution works

    master

    BRepNet performs convolutions on solid models by utilizing the topological information in Boundary Representation (B-Rep) data.

    Core Mechanism:

    1. Topological Walks: The network uses "topological walks" (sequences of instructions) to identify neighboring entities (faces, edges, coedges) relative to a starting coedge.
    2. Kernel Files: Users can define custom groups of neighboring entities using a _kernel_ file (e.g., kernels/winged_edge_plus.json).
    3. Convolution Step: The algorithm concatenates feature vectors from the entities defined in the kernel relative to the starting coedge. This concatenated vector is passed through an MLP to produce the hidden state for that coedge in the next layer.
    4. Pooling: After processing coedges, new hidden state vectors for faces and edges are generated by pooling the coedge hidden states onto their respective parent faces and edges.

    The implementation of this logic is located in models/brepnet.py within the BRepNetLayer.forward() method.

  2. Understand the BRepNet data pipeline

    master

    The BRepNet data pipeline consists of three main stages to prepare STEP files for model training:

    1. STEP Data Processing: Topology and geometric features are extracted from STEP files and saved into an intermediate .npz format. During this stage, segmentation files can optionally be cross-checked against Open Cascade to ensure face counts remain consistent.
    2. Dataset File Generation: A dataset file is created to define the training, validation, and test sets. This file also stores feature standardization coefficients derived from the training set. If using the official Fusion Gallery split, you can provide a train_test.json file; otherwise, a new split is generated automatically.
    3. Data Loading: The BRepNet dataloader (brepnet_dataset.py) reads the intermediate .npz files and computes the index arrays required by the network (which depend on the chosen kernel). These index arrays are cached during the first epoch for performance.
  3. Understand topological changes from STEP conversion

    master

    When converting from Autodesk Shape Manager format to STEP, certain topological changes may occur that affect the BRepNet edge graph:

    • Splitting along seam lines: Periodic surfaces like spheres, cylinders, and cones may be split along seam lines (where parameter values are $0.0$ or $2\pi$). For a solid cylinder, this connects the two loops on the periodic cylindrical face with a new edge, altering the edge graph for holes.
    • Toroidal surface conversion: Toroidal surfaces may be converted to rational NURBS surfaces during STEP conversion. This can also occur for 'suitcase corner' fillets.
    • Curve types: Open Cascade does not support helical and intersection curves. The Open Cascade dataloader handles this by looking for rational and non-rational B-Spline curves instead, assuming intersection curves will be represented as splines to maintain consistent edge feature counts.
  4. Evaluate segmentation on custom STEP data

    master

    To evaluate the model on your own STEP/STP files, use eval.evaluate_folder. This script generates "logits" files in a temp_working/logits subdirectory. Each row in the logits file corresponds to a face in the STEP data, with columns representing the probability of that face belonging to a specific segment.

    python -m eval.evaluate_folder  \
      --dataset_dir ./example_files/step_examples \
      --dataset_file ./example_files/feature_standardization/s2.0.0_step_all_features.json \
      --model ./example_files/pretrained_models/pretrained_s2.0.0_step_all_features_0519_073100.ckpt

    For a complete end-to-end workflow (evaluation through visualization), refer to the Jupyter notebook notebooks/find_and_display_segmentation.ipynb.

    python -m eval.evaluate_folder  \
      --dataset_dir ./example_files/step_examples \
      --dataset_file ./example_files/feature_standardization/s2.0.0_step_all_features.json \
      --model ./example_files/pretrained_models/pretrained_s2.0.0_step_all_features_0519_073100.ckpt
  5. Monitor training with TensorBoard

    master

    BRepNet logs training data (loss, accuracy, IoU) to a logs directory. Each run is stored in a subfolder named by date and time. To monitor training in real-time:

    cd BRepNet
    tensorboard --logdir logs

    Checkpoints (saved when validation loss reaches a minimum) are stored in the same directory as the TensorBoard logs: ./logs/<date>/<time>/checkpoints.

    tensorboard --logdir logs
  6. Download the Fusion 360 Gallery segmentation dataset

    master

    The default dataset used for BRepNet is the Fusion 360 Gallery segmentation dataset. You can download the 3.2GB zip file via curl:

    cd /path/to/where_you_keep_data/
    curl https://fusion-360-gallery-dataset.s3-us-west-2.amazonaws.com/segmentation/s2.0.0/s2.0.0.zip -o s2.0.0.zip
    unzip s2.0.0.zip
    curl https://fusion-360-gallery-dataset.s3-us-west-2.amazonaws.com/segmentation/s2.0.0/s2.0.0.zip -o s2.0.0.zip
    unzip s2.0.0.zip
  7. Train the BRepNet model

    master

    Once the data is processed, you can train the model using train.train.

    Standard Training (with UV-Net geometric features):

    python -m train.train \
      --dataset_file /path/to/where_you_keep_data/s2.0.0/processed/dataset.json \
      --dataset_dir  /path/to/where_you_keep_data/s2.0.0/processed/ \
      --max_epochs 200

    Reproducing Paper Results (without UV-Net features): To match the original BRepNet paper results, use the provided reproduction script:

    train/reproduce_paper_results.sh /path/to/where_you_keep_data/s2.0.0

    Configuration Notes:

    • Use --num_workers and --gpus to match your hardware.
    • The model uses PyTorch Lightning ddp-spawn mode, allowing for either multiple GPUs with one worker or one GPU with multiple workers.
    • For a full list of hyperparameters, run python -m train.train --help or check BRepNet.add_model_specific_args in models/brepnet.py.
    python -m train.train \
      --dataset_file /path/to/where_you_keep_data/s2.0.0/processed/dataset.json \
      --dataset_dir  /path/to/where_you_keep_data/s2.0.0/processed/ \
      --max_epochs 200
  8. Test the trained network

    master

    To test a trained model on a dataset, use the eval.test module:

    python -m eval.test \
      --dataset_file /path/to/dataset_file.json \
      --dataset_dir /path/to/data_dir \
      --model BRepNet/logs/<day>/<time>/checkpoints/epoch=x-step=x.ckpt
    python -m eval.test \
      --dataset_file /path/to/dataset_file.json \
      --dataset_dir /path/to/data_dir \
      --model BRepNet/logs/<day>/<time>/checkpoints/epoch=x-step=x.ckpt
  9. Install and set up the BRepNet environment

    master

    To use BRepNet, clone the repository and create a Conda environment using the provided environment.yml.

    GPU Support: If you are using a GPU, you must manually install the version of PyTorch that matches your CUDA version.

    For CUDA 11.1:

    conda install pytorch cudatoolkit=11.1 -c pytorch -c conda-forge

    For CUDA 11.0:

    conda install pytorch==1.7.1 torchvision==0.8.2 torchaudio==0.7.2 cudatoolkit=11.0 -c pytorch

    Handling File Handle Limits: If you encounter OSError: [Errno 24] Too many open files while training with multiple workers, increase your machine's file handle limit:

    ulimit -Sn 10000
    git clone https://github.com/AutodeskAILab/BRepNet.git
    cd BRepNet
    conda env create -f environment.yml
    conda activate brepnet
  10. Process STEP data for training

    master

    Before training, you must extract topology and geometry information from the STEP files. Use the pipeline.quickstart module to process the dataset:

    python -m pipeline.quickstart --dataset_dir /path/to/where_you_keep_data/s2.0.0 --num_workers 5
    python -m pipeline.quickstart --dataset_dir /path/to/where_you_keep_data/s2.0.0 --num_workers 5
  11. Process STEP data using extract_brepnet_data_from_step.py

    master

    Use the extract_brepnet_data_from_step.py script to read STEP files and generate intermediate numpy data. This is the first step in the BRepNet pipeline.

    python -m pipeline.extract_brepnet_data_from_step \
        --step_path /data_drive/s2.0.0_step/step \
        --output /data_drive/s2.0.0_step/processed \
        --feature_list feature_lists/all.json
  12. Build a dataset file using build_dataset_file.py

    master

    Use the build_dataset_file.py script to generate the final dataset JSON file. This file defines the data splits (train/val/test) and contains the feature standardization statistics required for training.

    python -m pipeline.build_dataset_file \
        --npz_folder /media/data/FusionGallerySegmentation/processed \
        --train_test /media/data/FusionGallerySegmentation/s1.0.0/train_test.json \
        --dataset_file  /media/data/FusionGallerySegmentation/step_dataset.json