如何在 Google Colab 上运行 Samurai

如何在 google colab 上运行 samurai

我的原始帖子
https://baxin.netlify.app/how-to-run-samurai-on-google-colab/

什么是武士?

samurai:采用运动感知内存的零镜头视觉跟踪的分段任意模型

要求

google colab 的 google 帐户抱脸账号下载数据

如何在 google colab 上运行 samurai

步骤 0. 获取 hugging face 令牌并将其添加到您的环境变量中

我们需要访问 hugging face 才能下载数据。

如果您不知道如何获取拥抱脸令牌,请参阅此页面。
另外,如果您不知道如何将 hugging face 令牌添加到环境变量中,请查看这篇文章。

步骤 1. 更改默认运行时

要在 google colab 上运行 samurai,我们需要将默认运行时更改为 gpu。
我们需要使用 t4(免费 gpu)。

步骤 2. 安装软件包

!pip install matplotlib==3.7 tikzplotlib jpeg4py opencv-python lmdb pandas scipy loguru

步骤 3. 克隆 samurai 存储库

!git clone https://github.com/yangchris11/samurai.git

步骤 4. 安装 sam2

%cd samurai/sam2!pip install -e .!pip install -e ".[notebooks]"

步骤 5. 下载检查点

%cd /content/samurai/sam2/checkpoints!./download_ckpts.sh && %cd ..

步骤 6. 从 hugging face 下载数据

在这一部分中,我们将使用 python 脚本来设置 samurai repo 在数据准备部分中提到的数据。
https://github.com/yangchris11/samurai?tab=readme-ov-file#data-preparation

我们将使用的数据是l-lt/lasot

在本例中,我们将下载 cat 数据集,因此如果您想尝试其他数据集,可以相应地更改代码。

import os# define the data directorydata_directory = '/content/samurai/data/lasot'# create the data directory if it does not existtry:    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 filecontent = '''cat-1cat-20'''# define the file pathfile_path = os.path.join(data_directory, 'testing_set.txt')# write the content to the filetry:    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 pathprint(f'file path: {file_path}')
import osfrom huggingface_hub import hf_hub_downloadimport zipfileimport shutildef 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 noneif __name__ == "__main__":    extract_path = download_and_extract()    if extract_path:        print("ndownload and extraction completed successfully!")    else:        print("ndownload and extraction failed.")

步骤 7. 推理

最后一步是运行 samurai 推理。
推理需要一段时间。

%cd /content/samurai!python scripts/main_inference.py

如果一切顺利,您应该看到以下输出:

所有代码都可以在此 github 存储库中获取。

如果您喜欢这篇文章,请在 github 上给它一个星。

以上就是如何在 Google Colab 上运行 Samurai的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1354298.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
使用堆栈实现队列
上一篇 2025年12月13日 18:25:48
使用 Python 为 Google Kubernetes Engine (GKE) 构建 Kubernetes 客户端
下一篇 2025年12月13日 18:26:17

相关推荐

发表回复

登录后才能评论
关注微信