文件目录
sacre_coeur/
├── dense/
│ ├── images/
│ ├── sparse/不需要/0/
│ ├── stereo/非必需
│
├──sacre.tsv
需要自己写一个生成tsv文件的代码
import os
def read_cameras_text(cameras_txt_path):
"""
从 COLMAP 的 cameras.txt 中读取相机信息
"""
cameras = {}
if not os.path.exists(cameras_txt_path):
raise FileNotFoundError(f"{cameras_txt_path} 不存在,请检查路径。")
with open(cameras_txt_path, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
# 跳过注释和空行
if line.startswith("#") or len(line) == 0:
continue
elems = line.split()
camera_id = int(elems[0])
model = elems[1]
width = int(elems[2])
height = int(elems[3])
params = list(map(float, elems[4:]))
cameras[camera_id] = {
"model": model,
"width": width,
"height": height,
"params": params
}
return cameras
def read_images_text(images_txt_path):
"""
从 COLMAP 的 images.txt 中读取图像信息(位姿等)
"""
images = []
if not os.path.exists(images_txt_path):
raise FileNotFoundError(f"{images_txt_path} 不存在,请检查路径。")
with open(images_txt_path, "r", encoding="utf-8") as f:
lines = f.readlines()
idx = 0
while idx < len(lines):
line = lines[idx].strip()
# 跳过注释和空行
if line.startswith("#") or len(line) == 0:
idx += 1
continue
# 解析图像的第一行
elems = line.split()
image_id = int(elems[0])
# qw, qx, qy, qz
q = list(map(float, elems[1:5]))
# tx, ty, tz
t = list(map(float, elems[5:8]))
camera_id = int(elems[8])
# 剩余部分是图像文件名
image_name = " ".join(elems[9:])
images.append({
"image_id": image_id,
"q": q,
"t": t,
"camera_id": camera_id,
"image_name": image_name
})
idx += 1
# 跳过下一行 (2D特征点信息)
if idx < len(lines):
idx += 1
return images
def generate_tsv_file(cameras, images, output_path):
"""
生成符合 GSW 需求的 TSV 文件,包含最关键的三列:
- filename: 对应原本的 image_name
- id: 对应原本的 image_id
- split: 表示训练/测试集 (如果没有区分需求,可一律设为train)
其余信息可按需保留:
camera_id, width, height, model, params, q, t
"""
with open(output_path, "w", encoding="utf-8") as fid:
# 注意列顺序:GSW会用到 filename / id / split
fid.write("filename\tid\tsplit\tcamera_id\twidth\theight\tmodel\tparams\tq\tt\n")
for img in images:
cam = cameras[img["camera_id"]]
cam_params_str = " ".join(map(str, cam["params"]))
q_str = " ".join(map(str, img["q"]))
t_str = " ".join(map(str, img["t"]))
# 如果不区分 train/test,可全部写成 "train"
# 或者你也可以根据某种逻辑判断是 "train" 还是 "test"
split_str = "train"
row = (
f"{img['image_name']}\t" # filename
f"{img['image_id']}\t" # id
f"{split_str}\t" # split
f"{img['camera_id']}\t"
f"{cam['width']}\t"
f"{cam['height']}\t"
f"{cam['model']}\t"
f"{cam_params_str}\t"
f"{q_str}\t"
f"{t_str}\n"
)
fid.write(row)
if __name__ == "__main__":
# 假设你的txt文件在 <current_directory>/sparse/ 下
current_directory = os.path.dirname(os.path.abspath(__file__))
colmap_data_path = os.path.join(current_directory, "sparse")
cameras_txt_path = os.path.join(colmap_data_path, "cameras.txt")
images_txt_path = os.path.join(colmap_data_path, "images.txt")
# 生成的 TSV,直接放在当前脚本同级目录下
output_tsv_path = os.path.join(current_directory, "images.tsv")
# 读取相机与图像信息
cameras_dict = read_cameras_text(cameras_txt_path)
images_list = read_images_text(images_txt_path)
# 生成 TSV (包含 filename, id, split 等列)
generate_tsv_file(cameras_dict, images_list, output_tsv_path)
print(f"TSV 文件已生成: {output_tsv_path}")
之后要将tsv文件放到root_dir所在目录的上级目录
之后我们要把自己的datasets伪装成官方的数据集来运行
写了个自动化的bash指令来运行全部程序
#!/bin/bash
# 设置场景名称和路径
SCENE_NAME="sacre_coeur"
DATA_NAME="tum" #在这里修改为自己的数据集
SOURCE_PATH="/root/autodl-fs/Gaussian-Wild/data/${DATA_NAME}/dense"
MODEL_PATH="outputs/${DATA_NAME}/model"
# 创建输出目录
mkdir -p ${MODEL_PATH}
# 训练
echo "Starting training for ${SCENE_NAME}..."
CUDA_VISIBLE_DEVICES=0 python ./train.py \
--source_path ${SOURCE_PATH} \
--scene_name ${SCENE_NAME} \
--model_path ${MODEL_PATH} \
--eval \
--resolution 2 \
--iterations 10000
# 渲染结果
echo "Rendering training and testing results..."
CUDA_VISIBLE_DEVICES=0 python ./render.py \
--model_path ${MODEL_PATH}
# # 渲染多视角视频
# echo "Rendering multi-view video..."
# CUDA_VISIBLE_DEVICES=0 python ./render.py \
# --model_path ${MODEL_PATH} \
# --skip_train \
# --skip_test \
# --render_multiview_vedio
# # 渲染外观调整效果
# echo "Rendering appearance tuning..."
# CUDA_VISIBLE_DEVICES=0 python ./render.py \
# --model_path ${MODEL_PATH} \
# --skip_train \
# --skip_test \
# --render_interpolate
# # 评估右半部分
# echo "Evaluating right half metrics..."
# CUDA_VISIBLE_DEVICES=0 python ./metrics_half.py \
# --model_path ${MODEL_PATH}
# # 评估全图像
# echo "Evaluating full image metrics..."
# CUDA_VISIBLE_DEVICES=0 python ./metrics.py \
# --model_path ${MODEL_PATH}
echo "All tasks completed!"