(CVPR2025)Best paper VGGT论文的复现

(CVPR2025)Best paper VGGT论文的复现

1.配置环境

vggt github链接地址(https://github.com/facebookresearch/vggt).

①首先使用anaconda激活一个虚拟环境名字叫做vggt,python = 3.10。

conda create -n vggt python=3.10

②安装依赖,这里我们避免torch的cuda或者cpu版本的问题(我们不知道某个pypi源下到底是cuda还是cpu版本,清华为CPU),将torch单独安装,这里是cuda11.8为例,下载较慢挂梯子。

pip install torch==2.3.1 torchvision==0.18.1 torchaudio==2.3.1 --index-url https://download.pytorch.org/whl/cu118
pip install Pillow huggingface_hub einops safetensors -i https://pypi.tuna.tsinghua.edu.cn/simple

③克隆仓库

git clone git@github.com:facebookresearch/vggt.git 

④下载权重

由于权重是在Hugging Face,国内仍然下载非常慢,因此我们也是挂上梯子后,从(https://huggingface.co/facebook/VGGT-1B/resolve/main/model.pt)下载,然后保存到自己的本地路径。

2.运行demo

①非可视化界面运行,修改13行为自己的权重路径,19行为数据集路径

import torch
from vggt.models.vggt import VGGT
from vggt.utils.load_fn import load_and_preprocess_images
import os


device = "cuda" if torch.cuda.is_available() else "cpu"
# bfloat16 is supported on Ampere GPUs (Compute Capability 8.0+) 
dtype = torch.bfloat16 if torch.cuda.get_device_capability()[0] >= 8 else torch.float16

# Initialize the model and load the pretrained weights.
# This will automatically download the model weights the first time it's run, which may take a while.
checkpoint_path = "./checkpoint/model.pt"
state_dict = torch.load(checkpoint_path)
model = VGGT().to(device)
model.load_state_dict(state_dict)

# 指定图片文件夹路径
folder_path = './images'
# 图片格式
image_extensions = ('.png', '.jpg', '.jpeg', '.bmp', '.gif', '.tiff')

# 获取文件夹下所有图片的路径
image_names = [
    os.path.join(folder_path, file) 
    for file in os.listdir(folder_path) 
    if file.lower().endswith(image_extensions)
]

# 打印结果(可选)
print(image_names) 
images = load_and_preprocess_images(image_names).to(device)

with torch.no_grad():
    with torch.cuda.amp.autocast(dtype=dtype):
        # Predict attributes including cameras, depth maps, and point maps.
        predictions = model(images)

运行会报如下错误

You’re encountering this error because a Python module you’re trying to use was compiled against an older version of NumPy (1.x), but you currently have NumPy 2.1.2 installed. NumPy 2 introduced some backward-incompatible changes, which can cause older compiled extensions to malfunction or crash.

numpy 版本问题,需要卸载numpy,执行下面的代码。

pip uninstall numpy
pip install "numpy<2" -i https://pypi.tuna.tsinghua.edu.cn/simple

②可视化交互界面运行,下载可视化界面的依赖

pip install -r requirements_demo.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

执行完上面的代码后,会重新下载numpy 2.*版本,会冲突,再卸载numpy,使用pip install “numpy<2” -i https://pypi.tuna.tsinghua.edu.cn/simple,再下载numpy 1.*的版本,报以下错忽视即可。

在这里插入图片描述

修改原仓库的demo_gradio.py,将加载模型的代码改为本地加载。对应32-34行,改为如下代码,checkpoint_path对应自己的路径即可。

checkpoint_path = "./checkpoint/model.pt"
state_dict = torch.load(checkpoint_path)
model = VGGT()
model.load_state_dict(state_dict)

命令行运行 python demo_gradio.py即可。

在这里插入图片描述

在选项中选择filter Sky如下图的时候会报错,本人认为网络问题,可以选择其他选项。


参考内容未提及CVPR 2025会议论文复现方法。不过,通常论文复现可按以下通用步骤进行: 1. **理解论文**:仔细研读论文,明确研究目标、方法、实验设置等关键内容。 2. **获取代码与数据**:若论文有公开代码,可直接获取;同时准备好复现所需的数据集。 3. **环境搭建**:根据代码要求,安装相应的编程语言、深度学习框架及依赖库。 4. **代码调试**:运行代码,调试并解决可能出现的错误。 5. **实验复现**:按照论文的实验设置,运行代码进行实验,并记录结果。 以复现基于PyTorch的图像分类模型为例,简单代码示例如下: ```python import torch import torch.nn as nn import torch.optim as optim from torchvision import datasets, transforms # 数据预处理 transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,)) ]) # 加载数据集 train_dataset = datasets.MNIST(root='./data', train=True, download=True, transform=transform) train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=64, shuffle=True) # 定义简单的神经网络模型 class SimpleNet(nn.Module): def __init__(self): super(SimpleNet, self).__init__() self.fc1 = nn.Linear(784, 128) self.fc2 = nn.Linear(128, 10) def forward(self, x): x = x.view(-1, 784) x = torch.relu(self.fc1(x)) x = self.fc2(x) return x model = SimpleNet() # 定义损失函数和优化器 criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(model.parameters(), lr=0.01) # 训练模型 for epoch in range(5): running_loss = 0.0 for i, data in enumerate(train_loader, 0): inputs, labels = data optimizer.zero_grad() outputs = model(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() running_loss += loss.item() print(f'Epoch {epoch + 1}, Loss: {running_loss / len(train_loader)}') ```
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值