| 1 | # Finetune SpatialLM |
| 2 | |
| 3 | Here we provide a simple example of how to finetune SpatialLM. In this example, we finetune SpatialLM on [ARKitScenes](https://github.com/apple/ARKitScenes) using a new set of object categories and enable random, gravity-aligned scene orientations. During inference, only the z-axis needs to be kept as the up axis; the x and y axes do not need to be aligned. |
| 4 | |
| 5 | ## Installation |
| 6 | |
| 7 | First, follow the installation instructions in [README.md](./README.md#installation). Then, install the training dependencies: |
| 8 | |
| 9 | ```bash |
| 10 | poe install-training |
| 11 | ``` |
| 12 | |
| 13 | ## Dataset |
| 14 | |
| 15 | Download the preprocessed [ARKitScenes-SpatialLM](https://huggingface.co/datasets/ysmao/arkitscenes-spatiallm) dataset from Hugging Face. For details about the dataset format and structure, refer to the dataset repository. |
| 16 | |
| 17 | ```bash |
| 18 | huggingface-cli download ysmao/arkitscenes-spatiallm --repo-type dataset --local-dir arkitscenes-spatiallm |
| 19 | ``` |
| 20 | |
| 21 | After downloading the dataset, extract the files from the compressed chunks: |
| 22 | |
| 23 | ```bash |
| 24 | cd arkitscenes-spatiallm |
| 25 | chmod +x extract.sh |
| 26 | ./extract.sh |
| 27 | ``` |
| 28 | |
| 29 | The GT oriented bounding boxes are stored as TXT files in the `layout` folder, and the point clouds are stored in PLY format in the `pcd` folder. The script `spatiallm/tuner/create_dataset.py` is used to convert the raw data into training data in the ShareGPT format. The downloaded dataset contains the preprocessed training data in ShareGPT format already. |
| 30 | |
| 31 | ## Training |
| 32 | |
| 33 | The training script is adapted from [LLaMA-Factory](https://github.com/hiyouga/LLaMA-Factory), a great open-source project for LLM finetuning. For SpatialLM, we included a minimum subset of features focused on SFT finetuning. Refer to LLaMA-Factory for additional features and broader support. |
| 34 | |
| 35 | Refer to [configs/spatiallm_sft.yaml](./configs/spatiallm_sft.yaml) to ensure the configuration settings match your requirements. Key options include: |
| 36 | |
| 37 | - `model_name_or_path`: Path to the pretrained SpatialLM model weights |
| 38 | - `template`: Select either `spatiallm_qwen` or `spatiallm_llama` as appropriate |
| 39 | - `dataset_dir`: Path to the downloaded ARKitScenes-SpatialLM dataset |
| 40 | - `save_dir`: Directory to save the preprocessed training dataset |
| 41 | - `output_dir`: Directory for saving model checkpoints |
| 42 | - `learning_rate`: Learning rate for finetuning |
| 43 | - `num_train_epochs`: Number of training epochs for finetuning |
| 44 | |
| 45 | ### Running Finetuning |
| 46 | |
| 47 | To start finetuning, run the following command. Note that full finetuning requires approximately 60GB of CUDA VRAM. More efficient finetuning options, such as LoRA or quantized finetuning are not yet implemented. |
| 48 | |
| 49 | ```bash |
| 50 | python train.py configs/spatiallm_sft.yaml |
| 51 | ``` |
| 52 | |
| 53 | If you are training on distributed systems, please set the following environment variables as appropriate for your setup: |
| 54 | |
| 55 | - `MASTER_ADDR`: The IP address or hostname of the master node (e.g., `127.0.0.1` for single node or the address of the main node in a cluster). |
| 56 | - `MASTER_PORT`: A free port on the master node for communication (e.g., `29500`). |
| 57 | - `NNODES`: Total number of nodes participating in training. |
| 58 | - `NODE_RANK`: Rank of the current node (integer in `[0, NNODES-1]`). |
| 59 | - `NPROC_PER_NODE`: Number of processes (usually GPUs) to launch on each node. |
| 60 | |
| 61 | Example for a single-node (multi-GPU) setup: |
| 62 | |
| 63 | ```bash |
| 64 | export MASTER_ADDR=127.0.0.1 |
| 65 | export MASTER_PORT=29500 |
| 66 | export NNODES=1 |
| 67 | export NODE_RANK=0 |
| 68 | export NPROC_PER_NODE=4 # Adjust to the number of GPUs available |
| 69 | ``` |
| 70 | |
| 71 | For multi-node training, set `MASTER_ADDR` to the address of the master node, and adjust `NNODES` and `NODE_RANK` accordingly. |
| 72 | |
| 73 | ## Testing |
| 74 | |
| 75 | Once training is complete, you can use the inference and visualization script to view the prediction results from your finetuned model checkpoints. For more details, please refer to [README.md](./README.md#inference). |
| 76 | |
| 77 | For example here we used finetuned model [SpatialLM1.1-Qwen-0.5B-ARKitScenes-SFT](https://huggingface.co/ysmao/SpatialLM1.1-Qwen-0.5B-ARKitScenes-SFT). |
| 78 | |
| 79 | ```bash |
| 80 | # Inference |
| 81 | python inference.py -d object -p arkitscenes-spatiallm/pcd/42446137.ply -o 42446137.txt --model_path ysmao/SpatialLM1.1-Qwen-0.5B-ARKitScenes-SFT |
| 82 | # Visualization |
| 83 | python visualize.py -p arkitscenes-spatiallm/pcd/42446137.ply -l 42446137.txt --save 42446137.rrd |
| 84 | # Open Rerun Window |
| 85 | rerun 42446137.rrd |
| 86 | ``` |
| 87 | |
| 88 | ## Results |
| 89 | |
| 90 | Here are some example results comparing ground truth (GT) layouts with predictions (Pred) from the finetuned SpatialLM1.1-Qwen-0.5B model on the ARKitScenes dataset. |
| 91 | |
| 92 | <div align="center"> |
| 93 | |
| 94 | | GT | Pred | |
| 95 | | :---------------------------------------: | :-------------------------------------------: | |
| 96 | |  |  | |
| 97 | |  |  | |
| 98 | |  |  | |
| 99 | |
| 100 | </div> |