ゼロショット動画分類

Hugging Face で学ぶマルチモーダルモデル

James Chapman

Curriculum Manager, DataCamp

ゼロショット動画分類

 

ビジネス課題:

  • 競合の広告で売上が伸びている
  • 競合の広告は当社よりポジティブか?

 

1950年代の歯磨き粉広告

1 https://repository.duke.edu/dc/adviews/dmbb37204
Hugging Face で学ぶマルチモーダルモデル

CLAP

  • Contrastive Language Audio Pretraining
  • CLIP の学習法に類似
    • テキストエンコーダと音声エンコーダを分離
  • 63.3万 件の音声+説明の対応ペア
  • コントラスト学習で音声とテキストの埋め込みを整合

埋め込みのマッチングを示す CLAP 図

1 https://arxiv.org/html/2211.06687v4
Hugging Face で学ぶマルチモーダルモデル

アプローチ: マルチモーダルZSL

CLAP でのゼロショット学習

レイトフュージョン手法の拡大図 パート1: CLAP

1 https://arxiv.org/html/2310.02298v3
Hugging Face で学ぶマルチモーダルモデル

アプローチ: マルチモーダルZSL

CLAP でのゼロショット学習

+ CLIP でのゼロショット学習

  • 共通のテキストクラス
  • CLIP と CLAP の確率を統合

レイトフュージョン手法の拡大図 パート2: CLIP+CLAP

Hugging Face で学ぶマルチモーダルモデル

動画と音声

moviepy.mp4 の音声・動画ストリームを分離:

from moviepy.editor import VideoFileClip
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip

ffmpeg_extract_subclip("advert.mp4", 0, 5, "advert_5s.mp4")


video = VideoFileClip("advert_5s.mp4")
audio = video.audio
audio.write_audiofile("advert_5s.mp3")
Hugging Face で学ぶマルチモーダルモデル

音声と動画の準備

from decord import VideoReader
from PIL import Image
video_reader = VideoReader(video_path)
video = video_reader.get_batch(range(20)).asnumpy()
video = video[:, :, :, ::-1]
video = [Image.fromarray(frame) for frame in video]


from datasets import Dataset, Audio audio_dataset = Dataset.from_dict({"audio": [audio_path]}).cast_column("audio", Audio()) audio_sample = audio_dataset[0]["audio"]["array"]
Hugging Face で学ぶマルチモーダルモデル

動画の予測

emotions = ["joy", "fear", "anger", "sadness", "disgust", "surprise", "neutral"]


image_class = pipeline(model="openai/clip-vit-large-patch14", task="zero-shot-image-classification")
predictions = image_classifier(video, candidate_labels=emotions) scores = [ {l['label']: l['score'] for l in prediction} for prediction in predictions ]
avg_image_scores = {emotion: sum([s[emotion] for s in scores])/len(scores) for emotion in emotions} print(f"Average scores: {avg_image_scores}")
Average scores: {'joy': 0.10063326267991216, 'fear': 0.0868348691612482, ...}
Hugging Face で学ぶマルチモーダルモデル

音声の予測と結合

audio_class = pipeline(model="laion/clap-htsat-unfused", 
                       task="zero-shot-audio-classification")


audio_scores = audio_class(audio_sample, candidate_labels=emotions) audio_scores = {l['label']: l['score'] for l in audio_scores}
multimodal_scores = {emotion: (avg_image_scores[emotion] + audio_scores[emotion])/2 for emotion in emotions} print(f"Multimodal scores: {multimodal_scores}")
Multimodal scores: {'joy': 0.3109628591220826, 'fear': 0.09013736313208938, 
'anger': 0.011454355076421053, 'sadness': 0.06018101833760738, 'disgust': 0.07207315033301712, 
'surprise': 0.252118631079793, 'neutral': 0.2030726027674973}
Hugging Face で学ぶマルチモーダルモデル

Passons à la pratique !

Hugging Face で学ぶマルチモーダルモデル

Preparing Video For Download...