使用 CLIP 与 CLAP 进行视频情感分析
现在,您将使用 CLIP/CLAP 对之前准备的广告进行情绪分析。为实现多模态情绪分类,您将把两个模型的预测结果取平均进行融合(称为「后期融合」或 late fusion)。
您之前创建的视频(video)及其对应的音频(audio_sample)仍然可用:

一组情绪标签已加载为 emotions。
本练习是课程的一部分
使用 Hugging Face 的多模态模型
练习说明
- 使用
laion/clap-htsat-unfused模型,为zero-shot-audio-classification创建一个音频分类器 pipeline。 - 使用
openai/clip-vit-base-patch32模型(这是我们在视频中使用模型的较小变体),为zero-shot-image-classification创建一个图像分类器 pipeline。 - 使用图像分类器 pipeline 为视频中的每一帧图像生成预测。
- 使用音频分类器 pipeline 为
audio_sample生成预测。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Make an audio classifier pipeline
audio_classifier = ____(model="____", task="____")
# Make an image classifier pipeline
image_classifier = ____(model="____", task="____")
# Create emotion scores for each video frame
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}
# Make audio scores
audio_scores = ____(____, candidate_labels=____)
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}")