有个项目需要按照提示词做视频分割,感觉这个效果不错,就拿来试试。

项目地址:UniVS:https://github.com/MinghanLi/UniVS/

项目介绍:参见 CVPR 2024 | 视频分割大一统!UniVS:统一通用视频分割,在这里不做具体介绍。

1. 部署(环境:Ubuntu 20.04)

1.1 检查cuda toolkit

使用nvcc -V检查是否安装cuda toolkit,项目中需要用Cuda编译mask2former的MSDeformAttn,没有root权限安装cuda推荐这篇文章

1.2 安装

下面参照官方安装教程中的 Example conda environment setup 部分

1.2.1 conda 环境

创建conda环境、安装pytorch以及opencv:

conda create --name univs python=3.10 -y
conda activate univs
conda install pytorch==2.1.1 torchvision==0.16.1 torchaudio==2.1.1 pytorch-cuda=12.1 -c pytorch -c nvidia
pip install -U opencv-python

1.2.2 detectron2 安装

该项目依赖于detetron2,首先安装detetron2

# under your working directory
git clone git@github.com:facebookresearch/detectron2.git
cd detectron2
pip install -e .

cd ..
git clone git@github.com:MinghanLi/UniVS.git
cd UniVS
pip install -r requirements.txt
# compile MSDeformAttn
cd mask2former/modeling/pixel_decoder/ops
sh make.sh

# install davis2017_evaluation
cd univs/evaluation/davis2017-evaluation
# Install it - Python 3.6 or higher required
python setup.py install

1.2.3 UniVS 安装

# under your working directory
cd ..
git clone git@github.com:MinghanLi/UniVS.git
cd UniVS
pip install -r requirements.txt

此外,部分包没有在requirements中说明,需要安装下面几个包:

pip install kornia regex
pip install git+https://github.com/cocodataset/panopticapi.git

编译 MSDeformAttn,过程中可能会有一堆警告之类的可以不用管,最后输出成功就行:

# compile MSDeformAttn
cd mask2former/modeling/pixel_decoder/ops
sh make.sh

评估用的

# install davis2017_evaluation
cd univs/evaluation/davis2017-evaluation
# Install it - Python 3.6 or higher required
python setup.py install

1.2.4 下载推理所需预训练权重

这里参考官方说明文档以及model zoo说明。

在项目文件夹新建一个 pretrained文件夹用于存放预训练模型,不建议修改成别的路径,因为好多文件都是默认指向这个文件夹存放的。

  1. UniVS Stage3: Long Video-level Joint Training model 如果只是用于推理,直接下model_zoo.md中 2. UniVS Models 小节的 Stage3: Long Video-level Joint Training 模型即可,推荐下载表中Backbone为Swin-base的训练权重,demo用的是这个,需要修改的config少。官方只给出google网盘的连接:univs_swinb_stage3_f7_wosquare_ema.pth下载好后创建文件夹pretrained/univs_v2_cvpr/并存放其中。

  2. Prepare CLIP Text Encoder & Category Embeddings 根据model_zoo.md中 2. Prepare CLIP Text Encoder & Category Embeddings 小节的说明,下载google网盘中的combined_datasets_cls_emb_rn50x4.pth文件,下载后保存到datasets/concept_emb/中

  3. 其他需要下载的这个官方没有说明,但是需要下载,与regionCLIP有关,仍然是google网盘regionclip_pretrained-cc_rn50x4_only_lang_encoder.pth,下载后后创建文件夹pretrained/regionclip/regionclip/并存放其中。

2. 推理自己的视频

参考官方说明文档 Visualization Demo小节Visualization Demo for Custom Videos部分,该项目支持对视频和视频帧图像的推理。

对于类别导向的视觉任务,将文件存放在./datasets/custom_videos/raw/中,不要有其他的数据,检查tools/test_custom_videos/test_custom_videos.sh中的设置并执行

source  tools/test_custom_videos/test_custom_videos.sh

对于文本引导的的任务,将文件存放在./datasets/custom_videos/raw_text/中,不要有其他的数据,检查并修改tools/test_custom_videos/test_custom_videos_text.sh中的设置与CUSTOM_VIDEOS_TEXT并执行

source  tools/test_custom_videos/test_custom_videos_text.sh