제로샷 비디오 분류

Hugging Face로 배우는 멀티모달 모델

James Chapman

Curriculum Manager, DataCamp

제로샷 비디오 분류

 

비즈니스 과제:

  • 경쟁사 광고로 매출 증가
  • 경쟁사 광고가 더 긍정적인가?

 

1950년대 치약 광고

1 https://repository.duke.edu/dc/adviews/dmbb37204
Hugging Face로 배우는 멀티모달 모델

CLAP

  • Contrastive Language Audio Pretraining
  • CLIP 학습 방식과 유사
    • 텍스트 인코더와 오디오 인코더 분리
  • 633k 오디오–설명 매칭 쌍
  • 대조 학습으로 오디오·텍스트 임베딩 정렬

임베딩 매칭을 보여주는 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로 배우는 멀티모달 모델

연습해 봅시다!

Hugging Face로 배우는 멀티모달 모델

Preparing Video For Download...