50系显卡运行mast3r-slam的方法

部署运行你感兴趣的模型镜像

初始化

我使用anaconda进行安装的 先按照他说的,git下来

git clone https://github.com/rmurai0610/MASt3R-SLAM

然后安装anaconda

bash ./Anaconda3-2025.06-1-Linux-x86_64.sh

再创建虚拟环境

conda create -n mast3r-slam python=3.11
conda activate mast3r-slam

安装cuda工具

之后再安装cudatoolkit,要安装12.8以上的。

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin
sudo 
mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600wget https://developer.download.nvidia.com/compute/cuda/12.8.0/local_installers/cuda-repo-ubuntu2204-12-8-local_12.8.0-570.86.10-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu2204-12-8-local_12.8.0-570.86.10-1_amd64.deb
sudo cp /var/cuda-repo-ubuntu2204-12-8-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda-toolkit-12-8

安装之后 用nvcc -V查看验证
应该也可以

conda install nvidia/label/cuda-12.8.0::cuda-toolkit

我尝试了好多次 都不行的
下一步就要安装lietorch库了

安装pytorch

pip3 install --pre --force-reinstall torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cu128

还有一种方法是直接编译pytorch

git clone --recursive https://github.com/pytorch/pytorch
cd pytorch
export TORCH_CUDA_ARCH_LIST="8.0;8.6;8.9;9.0;12.0"
python setup.py bdist_wheel

安装lietorch

export TORCH_CUDA_ARCH_LIST="8.0;8.6;8.9;9.0;12.0"
pip install --no-build-isolation git+https://github.com/princeton-vl/lietorch.git

编译其他的东西并进行操作

编译的时候注意在setpu.py中进行修改,添加对5090的支持,这是修改后的:

from pathlib import Path
from setuptools import setup

import torch
from torch.utils.cpp_extension import BuildExtension, CppExtension
import os

ROOT = os.path.dirname(os.path.abspath(__file__))
has_cuda = torch.cuda.is_available()

include_dirs = [
    os.path.join(ROOT, "mast3r_slam/backend/include"),
    os.path.join(ROOT, "thirdparty/eigen"),
]

sources = [
    "mast3r_slam/backend/src/gn.cpp",
]
extra_compile_args = {
    "cores": ["j8"],
    "cxx": ["-O3"],
}

if has_cuda:
    from torch.utils.cpp_extension import CUDAExtension

    sources.append("mast3r_slam/backend/src/gn_kernels.cu")
    sources.append("mast3r_slam/backend/src/matching_kernels.cu")
    extra_compile_args["nvcc"] = [
        "-O3",
        # 保留主流架构,添加 RTX 5090 支持的 compute_90
        "-gencode=arch=compute_75,code=sm_75",  # RTX 20 系列
        "-gencode=arch=compute_80,code=sm_80",  # RTX 30 系列
        "-gencode=arch=compute_86,code=sm_86",  # RTX 30/40 系列
        "-gencode=arch=compute_90,code=sm_90", 
        "-gencode=arch=compute_90,code=compute_90", 
    "-gencode=arch=compute_120,code=sm_120",  # 添加 RTX 5090 的 sm_120 支持
    "-gencode=arch=compute_120,code=compute_120",
    ]
    ext_modules = [
        CUDAExtension(
            "mast3r_slam_backends",
            include_dirs=include_dirs,
            sources=sources,
            extra_compile_args=extra_compile_args,
        )
    ]
else:
    print("CUDA not found, cannot compile backend!")

setup(
    ext_modules=ext_modules,
    cmdclass={"build_ext": BuildExtension},
)

如果有其他的部分出现架构不支持的情况,也可以按照上面的对其进行修改,在setup.py中添加
“-gencode=arch=compute_120,code=sm_120”,
“-gencode=arch=compute_120,code=compute_120”,

pip install -e thirdparty/mast3r
pip install -e thirdparty/in3d
pip install --no-build-isolation -e .

下载模型 文件并运行

mkdir -p checkpoints/
wget https://download.europe.naverlabs.com/ComputerVision/MASt3R/MASt3R_ViTLarge_BaseDecoder_512_catmlpdpt_metric.pth -P checkpoints/
wget https://download.europe.naverlabs.com/ComputerVision/MASt3R/MASt3R_ViTLarge_BaseDecoder_512_catmlpdpt_metric_retrieval_trainingfree.pth -P checkpoints/
wget https://download.europe.naverlabs.com/ComputerVision/MASt3R/MASt3R_ViTLarge_BaseDecoder_512_catmlpdpt_metric_retrieval_codebook.pkl -P checkpoints/

bash ./scripts/download_tum.sh
python main.py --dataset datasets/tum/rgbd_dataset_freiburg1_room/ --config config/calib.yaml

一些bug及其修改意见

bug1

错误提示 name followed by “::” must be a class or namespace name 表明:编译器认为 torch::linalg 不是一个类或命名空间,因此无法解析后面的 ::linalg_norm。

这是因为 PyTorch 的 C++ API 中,linalg_norm 函数并不在 torch::linalg 命名空间下,正确的调用方式应该是 torch::linalg_norm(直接作为 torch 命名空间下的函数)。代码中多写了一个 linalg::,导致语法错误。

修改这三行代码,去掉多余的 linalg::,使用正确的 PyTorch C++ API 调用方式:
将以下三行错误代码:

// 错误行 1
delta_norm = torch::linalg::linalg_norm(dx, std::optional<c10::Scalar>(), {}, false, {});

// 错误行 2
delta_norm = torch::linalg::linalg_norm(dx, std::optional<c10::Scalar>(), {}, false, {});

// 错误行 3
delta_norm = torch::linalg::linalg_norm(dx, std::optional<c10::Scalar>(), {}, false, {});

修改为正确的调用方式(去掉 linalg::):

// 正确行 1
delta_norm = torch::linalg_norm(dx, std::optional<c10::Scalar>(), {}, false, {});

// 正确行 2
delta_norm = torch::linalg_norm(dx, std::optional<c10::Scalar>(), {}, false, {});

// 正确行 3
delta_norm = torch::linalg_norm(dx, std::optional<c10::Scalar>(), {}, false, {});

bug2

错误提示 no suitable conversion function from “const at::DeprecatedTypeProperties” to “c10::ScalarType” exists 表明:
代码中尝试将一个旧的类型对象(at::DeprecatedTypeProperties)直接转换为新的 c10::ScalarType 类型,但但这两种类型在新版本中不兼容,没有直接的转换函数。

常见场景与解决方法:
这种错误通常出现在获取张量(tensor)数据类型的代码中,例如:
错误代码示例(旧 API):

// 旧版本写法,在新 PyTorch 中会报错
auto dtype = input.type();  // input 是 at::Tensor 类型
c10::ScalarType scalar_type = dtype;  // 这里会触发转换错误

正确代码示例(新 API):
将获取数据类型的方式改为新的 API,直接获取 c10::ScalarType:

// 新版本写法,兼容 c10::ScalarType
c10::ScalarType scalar_type = input.scalar_type();  // 直接获取标量类型

bug3

要解决 matching_kernels.cu(103) 中 no suitable conversion function from “const at::DeprecatedTypeProperties” to “c10::ScalarType” 错误,需要定位并修改 获取张量数据类型的代码。
错误根源:
旧版 PyTorch 中用 tensor.type() 获取的是 at::DeprecatedTypeProperties 类型,而新版 PyTorch 要求使用 c10::ScalarType 类型,两者之间无法直接转换。因此需要将 tensor.type() 替换为新版 API。
具体修改:

找到第 103 行代码
打开 mast3r_slam/backend/src/matching_kernels.cu,定位到第 103 行,你会看到类似以下的代码(核心是用了 type() 方法):


// 错误示例(旧 API)
auto dtype = input_tensor.type();  // 返回 at::DeprecatedTypeProperties
some_function_that_needs_scalar_type(dtype);  // 尝试传入旧类型,导致错误

替换为新的 API
将 tensor.type() 改为 tensor.scalar_type(),直接获取 c10::ScalarType 类型:

auto dtype = input_tensor_tensor.scalar_type();  // 返回 c10::ScalarType,直接兼容
some_function_that_needs_scalar_type(dtype);  // 不再报错

bug4

从日志中可见关键错误代码:

const auto& the_type = tokens.type();  // 旧 API,返回 at::DeprecatedTypeProperties
at::ScalarType _st = ::detail::scalar_type(the_type);  // 转换失败

在新版 PyTorch 中,tensor.type() 已被废弃,返回的 at::DeprecatedTypeProperties 无法直接转换为 c10::ScalarType(即 at::ScalarType),导致编译报错。
解决方法:
修改 kernels.cu 第 101 行,用新版 API 获取张量类型,替换旧的 type() 方法。
步骤 1:定位错误代码
打开 curope 源码目录下的 kernels.cu,找到第 101 行,代码类似:

const auto& the_type = tokens.type();  // 错误:使用了旧 API

步骤 2:替换为新版 API
将 tokens.type() 改为 tokens.scalar_type(),直接获取 c10::ScalarType 类型:

// 修改前
const auto& the_type = tokens.type();
// 修改后
at::ScalarType the_type = tokens.scalar_type();  // 直接获取兼容的类型

bug5

出现 TypeError: Can’t convert object to ‘str’ for ‘filename’ 错误,是因为 self.rgb_files[idx] 的值不是字符串类型,而 cv2.imread() 要求传入的文件名必须是字符串(str)。
常见原因及解决方法:
self.rgb_files 中存储的不是字符串(如路径对象)
如果 self.rgb_files 中的元素是 Path 对象(来自 pathlib 库)或其他非字符串类型,直接传入 cv2.imread() 会报错。

解决:将路径转换为字符串:

# 原代码
img = cv2.imread(self.rgb_files[idx])

# 修改后(转为字符串)
img = cv2.imread(str(self.rgb_files[idx]))  # 用 str() 转换 Path 对象为字符串

bug6

PyTorch 2.6 及以上版本中,torch.load() 函数的 weights_only 参数默认值从 False 改为 True。该参数用于限制只加载模型权重(避免执行潜在的恶意代码),但你的模型文件(MASt3R_ViTLarge_BaseDecoder_512_catmlpdpt_metric.pth)可能包含除权重外的其他数据(如代码逻辑),导致严格模式下加载失败。
解决办法:
手动将 torch.load() 的 weights_only 参数设置为 False(允许加载完整内容)。
需要修改代码中调用 torch.load 的位置:
找到文件 thirdparty/mast3r/mast3r/model.py,定位到报错行(约第 24 行):

ckpt = torch.load(model_path, map_location='cpu')

修改为:

ckpt = torch.load(model_path, map_location='cpu', weights_only=False)

大体上错误就这些了,相似的也可以照着这个改,最后我跑出来只有13fps,用的5090+i9 12代处理器跑得。

您可能感兴趣的与本文相关的镜像

PyTorch 2.5

PyTorch 2.5

PyTorch
Cuda

PyTorch 是一个开源的 Python 机器学习库,基于 Torch 库,底层由 C++ 实现,应用于人工智能领域,如计算机视觉和自然语言处理

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值