0

0

如何在 Google Colab 上运行 Samurai

DDD

DDD

发布时间:2024-11-26 13:01:46

|

825人浏览过

|

来源于dev.to

转载

如何在 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 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.")

步骤 7. 推理

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

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

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

Tago AI
Tago AI

AI生成带货视频,专为电商卖货而生

下载

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

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

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
github中文官网入口 github中文版官网网页进入
github中文官网入口 github中文版官网网页进入

github中文官网入口https://docs.github.com/zh/get-started,GitHub 是一种基于云的平台,可在其中存储、共享并与他人一起编写代码。 通过将代码存储在GitHub 上的“存储库”中,你可以: “展示或共享”你的工作。 持续“跟踪和管理”对代码的更改。

920

2026.01.21

http与https有哪些区别
http与https有哪些区别

http与https的区别:1、协议安全性;2、连接方式;3、证书管理;4、连接状态;5、端口号;6、资源消耗;7、兼容性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

2083

2024.08.16

Golang 网络安全与加密实战
Golang 网络安全与加密实战

本专题系统讲解 Golang 在网络安全与加密技术中的应用,包括对称加密与非对称加密(AES、RSA)、哈希与数字签名、JWT身份认证、SSL/TLS 安全通信、常见网络攻击防范(如SQL注入、XSS、CSRF)及其防护措施。通过实战案例,帮助学习者掌握 如何使用 Go 语言保障网络通信的安全性,保护用户数据与隐私。

2

2026.01.29

俄罗斯Yandex引擎入口
俄罗斯Yandex引擎入口

2026年俄罗斯Yandex搜索引擎最新入口汇总,涵盖免登录、多语言支持、无广告视频播放及本地化服务等核心功能。阅读专题下面的文章了解更多详细内容。

480

2026.01.28

包子漫画在线官方入口大全
包子漫画在线官方入口大全

本合集汇总了包子漫画2026最新官方在线观看入口,涵盖备用域名、正版无广告链接及多端适配地址,助你畅享12700+高清漫画资源。阅读专题下面的文章了解更多详细内容。

156

2026.01.28

ao3中文版官网地址大全
ao3中文版官网地址大全

AO3最新中文版官网入口合集,汇总2026年主站及国内优化镜像链接,支持简体中文界面、无广告阅读与多设备同步。阅读专题下面的文章了解更多详细内容。

295

2026.01.28

php怎么写接口教程
php怎么写接口教程

本合集涵盖PHP接口开发基础、RESTful API设计、数据交互与安全处理等实用教程,助你快速掌握PHP接口编写技巧。阅读专题下面的文章了解更多详细内容。

10

2026.01.28

php中文乱码如何解决
php中文乱码如何解决

本文整理了php中文乱码如何解决及解决方法,阅读节专题下面的文章了解更多详细内容。

13

2026.01.28

Java 消息队列与异步架构实战
Java 消息队列与异步架构实战

本专题系统讲解 Java 在消息队列与异步系统架构中的核心应用,涵盖消息队列基本原理、Kafka 与 RabbitMQ 的使用场景对比、生产者与消费者模型、消息可靠性与顺序性保障、重复消费与幂等处理,以及在高并发系统中的异步解耦设计。通过实战案例,帮助学习者掌握 使用 Java 构建高吞吐、高可靠异步消息系统的完整思路。

10

2026.01.28

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
最新Python教程 从入门到精通
最新Python教程 从入门到精通

共4课时 | 22.4万人学习

Django 教程
Django 教程

共28课时 | 3.6万人学习

SciPy 教程
SciPy 教程

共10课时 | 1.3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号