ConceptGraphs

repository·main·Indexed 21 days ago

https://github.com/concept-graphs/concept-graphs

A framework for creating open-vocabulary 3D scene graphs for perception and planning in complex environments. It utilizes RGB-D data and foundation models such as SAM, LLaVA, and Grounded-SAM to perform 3D object mapping and scene graph generation. The system includes tools for processing ScanNet .sens files, integrating with the Replica and AI2Thor datasets, and visualizing results via Open3D.

Tokens
4.5K
Snippets
15
Records
15
Agent score
75%

What's inside ConceptGraphs

  1. Run the streamlined detection script

    main

    To execute the streamlined detection process, run the script from the conceptgraph directory using Python. The script will automatically download required models (such as YOLO and Mobile SAM) if they are not already present in your environment.

    Note that the script saves a copy of the configuration file used for the run in the experiment output directory for reproducibility.

    python scripts/streamlined_detections.py
  2. Set up LLaVA for scene graph generation

    main

    LLaVA is used for generating scene graphs. Follow the official LLaVA repository instructions for installation.

    ConceptGraphs was tested with LLaVA-7B-v0 and the LLaVA code at commit 8fc54a09a6be74b2abd913c468fb3d42ae826194. You must set the following environment variables:

    • LLAVA_PYTHON_PATH: Path to the LLaVA installation.
    • LLAVA_CKPT_PATH: Path to the LLaVA checkpoints.
    # Set the env variables as follows (change the paths accordingly)
    export LLAVA_PYTHON_PATH=/path/to/llava
    export LLAVA_CKPT_PATH=/path/to/LLaVA-7B-v0
  3. Use the Data Exporter to process .sens files

    main

    The reader.py script is a data exporter designed to extract information from ScanNet .sens files. It was developed and tested with Python 2.7. You can export depth images, color images, camera poses, and camera intrinsics by specifying various flags.

    python reader.py --filename [.sens file to export data from] --output_path [output directory to export data to]
    
    # Example usage:
    SCENE_ID=scene0011_00
    SENS_PATH=/home/qiao/data/scannet/scans/${SCENE_ID}/${SCENE_ID}.sens
    OUTPUT_PATH=/home/qiao/data/scannet/scans/${SCENE_ID}/
    python reader.py --filename $SENS_PATH --output_path $OUTPUT_PATH --export_depth_images --export_color_images --export_poses --export_intrinsics
  4. Install and configure Grounded-SAM

    main

    ConceptGraphs relies on the Grounded-Segment-Anything package. It is recommended to use the codebase at commit a4d76a2b55e348943cba4cd57d7553c354296223 for compatibility.

    Key Configuration Steps:

    1. CUDA_HOME: You must set CUDA_HOME to your CUDA toolkit path. This can be done within a conda environment using cudatoolkit-dev.
    2. Checkpoints: You must download ram_swin_large_14m.pth, groundingdino_swint_ogc.pth, and sam_vit_h_4b8939.pth.
    3. GSA_PATH: Set the GSA_PATH environment variable to the root of your Grounded-SAM installation.
    # Install cuda toolkit using conda
    conda install -c conda-forge cudatoolkit-dev
    
    # Set CUDA_HOME to your conda environment path
    export CUDA_HOME=/path/to/anaconda3/envs/conceptgraph/
    
    # Set the path to Grounded-SAM
    export GSA_PATH=/path/to/Grounded-Segment-Anything
  5. Install ConceptGraphs via Conda

    main

    To set up the ConceptGraphs environment, it is recommended to use a virtual environment (Conda or virtualenv) with Python 3.10.12. While an environment.yml is provided, following the manual installation steps below is recommended to avoid dependency conflicts.

    Key dependencies include tyro, open_clip_torch, wandb, h5py, openai, hydra-core, distinctipy, and ultralytics (for YOLO). You will also need faiss-cpu and a specific version of pytorch3d compatible with your CUDA setup.

    conda create -n conceptgraph anaconda python=3.10
    conda activate conceptgraph
    
    # Install the required libraries
    pip install tyro open_clip_torch wandb h5py openai hydra-core distinctipy
    
    # for yolo
    pip install ultralytics
    
    # Install the Faiss library (CPU version should be fine)
    conda install -c pytorch faiss-cpu=1.7.4 mkl=2021 blas=1.0=mkl
    
    ##### Install Pytorch according to your own setup #####
    # For example, if you have a GPU with CUDA 11.8 (We tested it Pytorch 2.0.1)
    conda install pytorch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 pytorch-cuda=11.8 -c pytorch -c nvidia
    
    # Install Pytorch3D
    conda install https://anaconda.org/pytorch3d/pytorch3d/0.7.4/download/linux-64/pytorch3d-0.7.4-py310_cu118_pyt201.tar.bz2
  6. Run the 3D object mapping system

    main

    Builds an object-based 3D map using the previously extracted segmentation results.

    Key Options:

    • save_objects_all_frames=True: Saves mapping results at every frame, enabling animated visualization via scripts/animate_mapping_interactive.py or scripts/animate_mapping_save.py.
    • merge_interval, merge_visual_sim_thresh, merge_text_sim_thresh: Used to perform overlap-based merging during mapping.

    Results are saved in $REPLICA_ROOT/$SCENE_NAME/pcd_saves. It is recommended to use the .pkl.gz files with the _post suffix (post-processed results).

    # Example: ConceptGraphs-Detect mapping
    SCENE_NAME=room0
    THRESHOLD=1.2
    python slam/cfslam_pipeline_batch.py \
        dataset_root=$REPLICA_ROOT \
        dataset_config=$REPLICA_CONFIG_PATH \
        stride=5 \
        scene_id=$SCENE_NAME \
        spatial_sim_type=overlap \
        mask_conf_threshold=0.25 \
        match_method=sim_sum \
        sim_threshold=${THRESHOLD} \
        dbscan_eps=0.1 \
        gsa_variant=ram_withbg_allclasses \
        skip_bg=False \
        max_bbox_area_ratio=0.5 \
        save_suffix=overlap_maskconf0.25_simsum${THRESHOLD}_dbscan.1
  7. Prepare the Replica dataset for ConceptGraphs

    main

    ConceptGraphs requires posed RGB-D images. For the Replica dataset, it is recommended to use the scanned RGB-D trajectories provided by Nice-SLAM rather than the original Replica dataset.

    Setup Steps:

    1. Download the Replica RGB-D scan dataset using the script from the Nice-SLAM repository.
    2. Set the REPLICA_ROOT environment variable to the downloaded path.
    3. Set CG_FOLDER to your concept-graphs directory.
    4. Set REPLICA_CONFIG_PATH to the specific YAML config file within the repository.
    export REPLICA_ROOT=/path/to/Replica
    
    export CG_FOLDER=/path/to/concept-graphs/
    export REPLICA_CONFIG_PATH=${CG_FOLDER}/conceptgraph/dataset/dataconfigs/replica/replica.yaml
  8. Run a 3D RGB reconstruction sanity check

    main

    Before running the full pipeline, you can perform a sanity check by running a 3D RGB reconstruction using GradSLAM. This reconstructs a replica scene and visualizes it. Note that the --visualize flag requires a GUI environment.

    Run this command from within the conceptgraph folder.

    cd conceptgraph
    
    SCENE_NAME=room0
    python scripts/run_slam_rgb.py \
        --dataset_root $REPLICA_ROOT \
        --dataset_config $REPLICA_CONFIG_PATH \
        --scene_id $SCENE_NAME \
        --image_height 480 \
        --image_width 640 \
        --stride 5 \
        --visualize
  9. Generate AI2Thor datasets

    main

    To generate datasets for AI2Thor experiments, you must first set up a specific fork of ai2thor.

    Workflow:

    1. Grid Map Generation: Create a densely captured grid map using --sample_method uniform.
    2. Human-controlled Trajectory: Use --interact to open a Unity window and record a trajectory via keyboard/GUI.
    3. Observation Generation: Generate observations from the saved trajectory using --sample_method from_file.
    4. Randomized Trajectory: Generate trajectories with randomly moved objects using --randomize_move_moveable_ratio and --randomize_move_pickupable_ratio.
    # 1. Generate Grid Map
    python scripts/generate_ai2thor_dataset.py --dataset_root $AI2THOR_DATASET_ROOT --scene_name $SCENE_NAME --sample_method uniform --n_sample -1 --grid_size 0.5
    
    # 2. Generate Human Trajectory
    python scripts/generate_ai2thor_dataset.py --dataset_root $AI2THOR_DATASET_ROOT --scene_name $SCENE_NAME --interact
    
    # 3. Generate Observations from file
    python scripts/generate_ai2thor_dataset.py --dataset_root $AI2THOR_DATASET_ROOT --scene_name $SCENE_NAME --sample_method from_file
  10. Configure the streamlined detection script using Hydra

    main

    The ./scripts/streamlined_detections.py script uses the hydra package for configuration management. Instead of passing numerous command-line arguments, you should modify the .yaml files located in ./conceptgraph/hydra_configs/.

    Configuration Hierarchy

    Configuration values are loaded in a specific order, where later files overwrite values from earlier ones:

    1. base.yaml (the foundation)
    2. replica.yaml (overwrites base)
    3. sam.yaml (overwrites previous)
    4. _self_ (the values defined in your specific config file, e.g., streamlined_detections.yaml, which take highest priority).

    Crucial Step: You must edit the path values in replica.yaml to point to your specific dataset.

    You can also create and add your own custom .yaml files to ./conceptgraph/hydra_configs/ to extend the configuration.

    # Example: ./conceptgraph/hydra_configs/streamlined_detections.yaml
    defaults:
      - base
      - replica
      - sam
      - _self_
    
    stride: 1
    exp_suffix: _streamlined_yolo_stride50_no_bg24
    save_video: true
  11. Visualize 3D object mapping results

    main

    Use scripts/visualize_cfslam_results.py to view the mapping results in an Open3D visualizer.

    Interactive Key Callbacks:

    • b: Toggle background point clouds (walls, floor, etc.) — ConceptGraphs-Detect only.
    • c: Color point clouds by object class — ConceptGraphs-Detect only.
    • r: Color point clouds by RGB.
    • f: Color point clouds by CLIP similarity with user-input text (type text in terminal).
    • i: Color point clouds by object instance ID.
    • + / -: Increase/decrease point cloud size.
    python scripts/visualize_cfslam_results.py --result_path /path/to/output.pkl.gz
  12. Extract 2D segmentation and per-region CLIP features

    main

    This step extracts (Detection) Segmentation results and per-region CLIP features. There are two modes:

    1. ConceptGraphs (Class-agnostic): Uses SAM in "segment all" mode to extract class-agnostic masks. Set --class_set none.
    2. ConceptGraphs-Detect (Class-aware): Uses a tagging and detection model to extract class-aware bounding boxes, which then prompt SAM for segmentation. Set --class_set to a value like ram.

    Results and visualizations are saved to $REPLICA_ROOT/$SCENE_NAME/.

    SCENE_NAME=room0
    
    # Option 1: ConceptGraphs (without open-vocab detector)
    python scripts/generate_gsa_results.py \
        --dataset_root $REPLICA_ROOT \
        --dataset_config $REPLICA_CONFIG_PATH \
        --scene_id $SCENE_NAME \
        --class_set none \
        --stride 5
    
    # Option 2: ConceptGraphs-Detect
    CLASS_SET=ram
    python scripts/generate_gsa_results.py \
        --dataset_root $REPLICA_ROOT \
        --dataset_config $REPLICA_CONFIG_PATH \
        --scene_id $SCENE_NAME \
        --class_set $CLASS_SET \
        --box_threshold 0.2 \
        --text_threshold 0.2 \
        --stride 5 \
        --add_bg_classes \
        --accumu_classes \
        --exp_suffix withbg_allclasses