0
点赞
收藏
分享

微信扫一扫

从0开始搭建vue + flask 旅游景点数据分析系统(九):旅游景点管理之增删改查

目录

一、引言 

二、视频分类(video-classification)

2.1 概述

2.2 技术原理

2.3 应用场景

2.4 pipeline参数

2.4.1 pipeline对象实例化参数

2.4.2 pipeline对象使用参数 

2.4 pipeline实战

2.5 模型排名

三、总结


一、引言 

 pipeline(管道)是huggingface transformers库中一种极简方式使用大模型推理的抽象,将所有大模型分为音频(Audio)、计算机视觉(Computer vision)、自然语言处理(NLP)、多模态(Multimodal)等4大类,28小类任务(tasks)。共计覆盖32万个模型

今天介绍CV计算机视觉的第六篇:视频分类(video-classification),在huggingface库内有1100个视频分类模型。

二、视频分类(video-classification)

2.1 概述

视频分类是为整个视频分配标签或类别的任务。每个视频预计只有一个类别。视频分类模型将视频作为输入,并返回关于该视频属于哪个类别的预测。

2.2 技术原理

视频分类(video-classification)最典型的模型莫过于微软的xclip系列,xclip为clip模型的拓展,采用(视频-文本)进行对比学习训练。微软提供了包括microsoft/xclip-base-patch32、microsoft/xclip-base-patch16等不同块分辨率训练的模型。比如microsoft/xclip-base-patch32,块分辨率大小为32,使用每段视频 8 帧进行训练,分辨率为 224x224。详细论文可参考《Expanding Language-Image Pretrained Models for General Video Recognition》

2.3 应用场景

2.4 pipeline参数

2.4.1 pipeline对象实例化参数

2.4.2 pipeline对象使用参数 

2.4 pipeline实战

使用hf_hub_download下载或使用本地视频:

亲测pipeline不能用,于是使用Auto模型方法,与使用Autotokenizer处理文本不同,对于图片的处理使用AutoImageProcessor(处理视频的本质就是先将视频拆帧成图片,再对图片进行处理)

import os
os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"
os.environ["CUDA_VISIBLE_DEVICES"] = "2"

import av
import torch
import numpy as np

from transformers import AutoImageProcessor, VideoMAEForVideoClassification,TimesformerForVideoClassification
from huggingface_hub import hf_hub_download

np.random.seed(0)

def read_video_pyav(container, indices):
'''
通过PyAV库解码视频中的特定帧。
Decode the video with PyAV decoder.
Args:
container (`av.container.input.InputContainer`): PyAV container.
indices (`List[int]`): List of frame indices to decode.
Returns:
result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3).
'''

frames = []
container.seek(0)
start_index = indices[0]
end_index = indices[-1]
for i, frame in enumerate(container.decode(video=0)):
if i > end_index:
break
if i >= start_index and i in indices:
frames.append(frame)
return np.stack([x.to_ndarray(format="rgb24") for x in frames])


def sample_frame_indices(clip_len, frame_sample_rate, seg_len):
'''
从视频中按照特定规则采样帧的索引.
Sample a given number of frame indices from the video.
Args:
clip_len (`int`): Total number of frames to sample.
frame_sample_rate (`int`): Sample every n-th frame.
seg_len (`int`): Maximum allowed index of sample's last frame.
Returns:
indices (`List[int]`): List of sampled frame indices
'''

converted_len = int(clip_len * frame_sample_rate)
end_idx = np.random.randint(converted_len, seg_len)
start_idx = end_idx - converted_len
indices = np.linspace(start_idx, end_idx, num=clip_len)
indices = np.clip(indices, start_idx, end_idx - 1).astype(np.int64)
return indices


# video clip consists of 300 frames (10 seconds at 30 FPS)
file_path = "./transformers_basketball.avi"
"""
file_path = hf_hub_download(
repo_id="nielsr/video-demo", filename="eating_spaghetti.mp4", repo_type="dataset"
)
"""

container = av.open(file_path)

# sample 16 frames
indices = sample_frame_indices(clip_len=16, frame_sample_rate=1, seg_len=container.streams.video[0].frames)
video = read_video_pyav(container, indices)

image_processor = AutoImageProcessor.from_pretrained("MCG-NJU/videomae-base-finetuned-kinetics")
#model = VideoMAEForVideoClassification.from_pretrained("MCG-NJU/videomae-base-finetuned-kinetics")
model = TimesformerForVideoClassification.from_pretrained("facebook/timesformer-base-finetuned-k400")

inputs = image_processor(list(video), return_tensors="pt")

with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits

# model predicts one of the 400 Kinetics-400 classes
predicted_label = logits.argmax(-1).item()
print(model.config.id2label[predicted_label])

执行后,自动下载模型文件,构建索引,拆帧,视频分类预测:​ 

2.5 模型排名

在huggingface上,我们将视频分类(video-classification)模型按下载量从高到低排序,排在前10的模型主要由微软的xclip、南京大学的videomae、facebook的timesformer、google的vivit等四类模型构成。

三、总结

本文对transformers之pipeline的视频分类(video-classification)从概述、技术原理、pipeline参数、pipeline实战、模型排名等方面进行介绍,读者可以基于pipeline使用代码极简的代码部署计算机视觉中的视频分类(video-classification)模型,应用于视频判别场景。

期待您的3连+关注,如何还有时间,欢迎阅读我的其他文章:

《Transformers-Pipeline概述》

【人工智能】Transformers之Pipeline(概述):30w+大模型极简应用

《Transformers-Pipeline 第一章:音频(Audio)篇》

【人工智能】Transformers之Pipeline(一):音频分类(audio-classification)

【人工智能】Transformers之Pipeline(二):自动语音识别(automatic-speech-recognition)

【人工智能】Transformers之Pipeline(三):文本转音频(text-to-audio/text-to-speech)

【人工智能】Transformers之Pipeline(四):零样本音频分类(zero-shot-audio-classification)

《Transformers-Pipeline 第二章:计算机视觉(CV)篇》

【人工智能】Transformers之Pipeline(五):深度估计(depth-estimation)

【人工智能】Transformers之Pipeline(六):图像分类(image-classification)

【人工智能】Transformers之Pipeline(七):图像分割(image-segmentation)

【人工智能】Transformers之Pipeline(八):图生图(image-to-image)

【人工智能】Transformers之Pipeline(九):物体检测(object-detection)

【人工智能】Transformers之Pipeline(十):视频分类(video-classification)

【人工智能】Transformers之Pipeline(十一):零样本图片分类(zero-shot-image-classification)

【人工智能】Transformers之Pipeline(十二):零样本物体检测(zero-shot-object-detection)

《Transformers-Pipeline 第三章:自然语言处理(NLP)篇》

【人工智能】Transformers之Pipeline(十三):填充蒙版(fill-mask)

【人工智能】Transformers之Pipeline(十四):问答(question-answering)

【人工智能】Transformers之Pipeline(十五):总结(summarization)

【人工智能】Transformers之Pipeline(十六):表格问答(table-question-answering)

【人工智能】Transformers之Pipeline(十七):文本分类(text-classification)

【人工智能】Transformers之Pipeline(十八):文本生成(text-generation)

【人工智能】Transformers之Pipeline(十九):文生文(text2text-generation)

【人工智能】Transformers之Pipeline(二十):令牌分类(token-classification)

【人工智能】Transformers之Pipeline(二十一):翻译(translation)

【人工智能】Transformers之Pipeline(二十二):零样本文本分类(zero-shot-classification)

《Transformers-Pipeline 第四章:多模态(Multimodal)篇》

【人工智能】Transformers之Pipeline(二十三):文档问答(document-question-answering)

【人工智能】Transformers之Pipeline(二十四):特征抽取(feature-extraction)

【人工智能】Transformers之Pipeline(二十五):图片特征抽取(image-feature-extraction)

【人工智能】Transformers之Pipeline(二十六):图片转文本(image-to-text)

【人工智能】Transformers之Pipeline(二十七):掩码生成(mask-generation)

【人工智能】Transformers之Pipeline(二十八):视觉问答(visual-question-answering)

举报

相关推荐

0 条评论