
我的原始帖子
https://baxin.netlify.app/how-to-run-samurai-on-google-colab/
samurai:采用运动感知内存的零镜头视觉跟踪的分段任意模型
我们需要访问 hugging face 才能下载数据。
如果您不知道如何获取拥抱脸令牌,请参阅此页面。
另外,如果您不知道如何将 hugging face 令牌添加到环境变量中,请查看这篇文章。
要在 google colab 上运行 samurai,我们需要将默认运行时更改为 gpu。
我们需要使用 t4(免费 gpu)。
!pip install matplotlib==3.7 tikzplotlib jpeg4py opencv-python lmdb pandas scipy loguru
!git clone https://github.com/yangchris11/samurai.git
%cd samurai/sam2 !pip install -e . !pip install -e ".[notebooks]"
%cd /content/samurai/sam2/checkpoints !./download_ckpts.sh && \ %cd ..
在这一部分中,我们将使用 python 脚本来设置 samurai repo 在数据准备部分中提到的数据。
https://github.com/yangchris11/samurai?tab=readme-ov-file#data-preparation
我们将使用的数据是l-lt/lasot
在本例中,我们将下载 cat 数据集,因此如果您想尝试其他数据集,可以相应地更改代码。
import os
# define the data directory
data_directory = '/content/samurai/data/lasot'
# create the data directory if it does not exist
try:
os.makedirs(data_directory, exist_ok=true)
print(f"directory '{data_directory}' created successfully or already exists.")
except oserror as error:
print(f"error creating directory '{data_directory}': {error}")
# define the content to be written to the file
content = '''cat-1
cat-20'''
# define the file path
file_path = os.path.join(data_directory, 'testing_set.txt')
# write the content to the file
try:
with open(file_path, 'w') as f:
f.write(content)
print(f"content written to file '{file_path}' successfully.")
except ioerror as error:
print(f"error writing to file '{file_path}': {error}")
# print the file path
print(f'file path: {file_path}')
import os
from huggingface_hub import hf_hub_download
import zipfile
import shutil
def download_and_extract(base_dir="/content/samurai/data"):
try:
# create lasot and cat directories
lasot_dir = os.path.join(base_dir, "lasot")
cat_dir = os.path.join(lasot_dir, "cat")
os.makedirs(cat_dir, exist_ok=true)
# create directory to save the zip file
zip_dir = os.path.join(base_dir, "zips")
os.makedirs(zip_dir, exist_ok=true)
print("downloading dataset...")
zip_path = hf_hub_download(
repo_id="l-lt/lasot",
filename="cat.zip",
repo_type="dataset",
local_dir=zip_dir
)
print(f"downloaded to: {zip_path}")
# extract zip file to cat directory
print("extracting zip file to cat directory...")
with zipfile.zipfile(zip_path, 'r') as zip_ref:
zip_ref.extractall(cat_dir)
print("\ncreated directory structure:")
print("lasot/")
print("└── cat/")
# display the first few cat folders
for item in sorted(os.listdir(cat_dir))[:6]:
print(f" ├── {item}/")
print(" └── ...")
return lasot_dir
except exception as e:
print(f"an error occurred: {str(e)}")
return none
if __name__ == "__main__":
extract_path = download_and_extract()
if extract_path:
print("\ndownload and extraction completed successfully!")
else:
print("\ndownload and extraction failed.")
最后一步是运行 samurai 推理。
推理需要一段时间。
%cd /content/samurai !python scripts/main_inference.py
如果一切顺利,您应该看到以下输出:
所有代码都可以在此 github 存储库中获取。
如果您喜欢这篇文章,请在 github 上给它一个星。
以上就是如何在 Google Colab 上运行 Samurai的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号