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

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

稿定AI绘图
稿定AI绘图

稿定推出的AI绘画工具

下载

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

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

相关专题

更多
python开发工具
python开发工具

php中文网为大家提供各种python开发工具,好的开发工具,可帮助开发者攻克编程学习中的基础障碍,理解每一行源代码在程序执行时在计算机中的过程。php中文网还为大家带来python相关课程以及相关文章等内容,供大家免费下载使用。

745

2023.06.15

python打包成可执行文件
python打包成可执行文件

本专题为大家带来python打包成可执行文件相关的文章,大家可以免费的下载体验。

634

2023.07.20

python能做什么
python能做什么

python能做的有:可用于开发基于控制台的应用程序、多媒体部分开发、用于开发基于Web的应用程序、使用python处理数据、系统编程等等。本专题为大家提供python相关的各种文章、以及下载和课程。

758

2023.07.25

format在python中的用法
format在python中的用法

Python中的format是一种字符串格式化方法,用于将变量或值插入到字符串中的占位符位置。通过format方法,我们可以动态地构建字符串,使其包含不同值。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

617

2023.07.31

python教程
python教程

Python已成为一门网红语言,即使是在非编程开发者当中,也掀起了一股学习的热潮。本专题为大家带来python教程的相关文章,大家可以免费体验学习。

1260

2023.08.03

python环境变量的配置
python环境变量的配置

Python是一种流行的编程语言,被广泛用于软件开发、数据分析和科学计算等领域。在安装Python之后,我们需要配置环境变量,以便在任何位置都能够访问Python的可执行文件。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

547

2023.08.04

python eval
python eval

eval函数是Python中一个非常强大的函数,它可以将字符串作为Python代码进行执行,实现动态编程的效果。然而,由于其潜在的安全风险和性能问题,需要谨慎使用。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

577

2023.08.04

scratch和python区别
scratch和python区别

scratch和python的区别:1、scratch是一种专为初学者设计的图形化编程语言,python是一种文本编程语言;2、scratch使用的是基于积木的编程语法,python采用更加传统的文本编程语法等等。本专题为大家提供scratch和python相关的文章、下载、课程内容,供大家免费下载体验。

705

2023.08.11

c++主流开发框架汇总
c++主流开发框架汇总

本专题整合了c++开发框架推荐,阅读专题下面的文章了解更多详细内容。

80

2026.01.09

热门下载

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

精品课程

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

共4课时 | 0.6万人学习

Django 教程
Django 教程

共28课时 | 3万人学习

SciPy 教程
SciPy 教程

共10课时 | 1.1万人学习

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

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