caption任务的几种评估指标:

1,018 阅读5分钟

本文调研整理了image caption 任务中几种评估指标,包括Bleu、Meteor、Rouge、Cider、Spice等。

这些指标通常不能单独使用,应结合使用以获得更全面的评估结果。每种指标都有其局限性,例如Bleu和ROUGE_L主要关注词级别的匹配,而CIDEr和SPICE则试图从更高层次(如短语或句子的全局信息)进行评估。

这里给出了一些基本的Python实现的评价指标代码,注意通常我们会使用现成的包如nltk或pycocoevalcap来计算这些评价指标,下面的代码只为展示这些评价指标的基本思想:

其中coco_eval.evaluate()会调用Bleu、Meteor、Rouge、Cider、Spice模块分别对Bleu、METEOR、ROUGE_L、CIDEr、SPICE指标进行计算。

  • Bleu、Rouge、Cider 类由 python 实现对应的计算代码

  • Meteor 类需要通过一个新进程调用 meteor-1.5.jar 来计算

  • Spice 类需要通过一个新进程调用 spice-1.0.jar 和 lib中的jar包来完成计算

指标介绍

  1. Bleu:

BLEU (BiLingual Evaluation Understudy), ref: link

BLEU分数是一种常用的机器翻译和生成文本评估指标,简单来说,它比较了生成的句子和参考句子之间 n-gram 的重叠度

Bleu_1,Bleu_2,Bleu_3,Bleu_4分别对应1-gram,2-gram,3-gram,4-gram的重叠度。高的Bleu分数表示生成的句子质量更好。

BLEU算法只关心生成序列的字词是否在参考序列中出现,而不关心参考序列中的字词是否在生成序列中出现

from nltk.translate.bleu_score import sentence_bleu
reference = [['this', 'is', 'a', 'test']]
candidate = ['this', 'is', 'a', 'test']
score = sentence_bleu(reference, candidate)

2. ### METEOR:

METEOR也是一种常用的机器翻译评价标准,比BLEU更复杂。METEOR考虑同义词,词干,以及词序等多个因素,进行更加全面的评估。 简单说,该指标考虑了基于整个语料库上的准确率和召回率,而最终得出测度,范围0~1,越高越好

注意,nltk目前并未包含METEOR评分,通常我们使用专门的METEOR jar包进行评分。

  1. ROUGE:

ROUGE (Recall-Oriented Understudy for Gisting Evaluation), ref: linklink

ROUGE_L是基于最长 公共 子序列(LCS)的评估标准,它对生成文本的召回率和精确率进行评估,专为自动文本摘要任务设计,也常用于文本生成任务。

Rouge-N是将模型生成的结果和标准结果按N-gram拆分后,计算召回率。

该指标能够衡量参考序列中的字词是在生成序列中出现过,即它能够衡量生成序列的召回率。

# pip install rouge
from rouge import Rouge 

hypothesis = "the #### transcript is a written version of each day 's cnn student news program use this transcript to he    lp students with reading comprehension and vocabulary use the weekly newsquiz to test your knowledge of storie s you     saw on cnn student news"

reference = "this page includes the show transcript use the transcript to help students with reading comprehension and     vocabulary at the bottom of the page , comment for a chance to be mentioned on cnn student news . you must be a teac    her or a student age # # or older to request a mention on the cnn student news roll call . the weekly newsquiz tests     students ' knowledge of even ts in the news"

rouger = Rouge()
scores = rouger.get_scores(hypothesis, reference)

4. ### CIDEr

CIDEr(Consensus-based Image Description Evaluation), ref: link

CIDEr针对的是图像描述任务,它不仅考虑n-gram精确匹配,而且采用TF-IDF对不同n-gram进行权重调整,更加注重描述图片的唯一性。

相较于常用于文本翻译的评价指标BLEU、ROUGE来说,CIDEr更接近人类去判断两个句子是否相近的原理,因为它利用了TF-IDF来对不同 n-gram 去赋予不同的权重,直观的来说,即经常出现的词组的权重具有更低的权重,而不常出现的词组则更特殊(具有更大的权重),人们会更注意这些特殊的单词。

CIDEr的计算相对复杂,涉及多个步骤包括TF-IDF。我们通常会直接使用pycocoevalcap包中的CiderD计算,代码如下:

# 运行错误
from pycocoevalcap.ciderD.ciderD import CiderD
scorer = CiderD(df='corpus')
score, _ = scorer.compute_score(ref, hypo)

5. ### SPICE

SPICE(Semantic Propositional Image Caption Evaluation), ref: link

SPICE的计算需要将句子解析为图形结构,并没有通用的Python库支持这样的操作。在评估中,我们通常会使用通过Stanford Scene Graph Parser生成json文件,并使用提供的SPICE评分jar包进行评估。

运行方式

需要安装java环境:

sudo apt-get install default-jre
sudo apt-get install default-jdk

评估时会自动下载相关语料到/usr/local/lib/python3.10/dist-packages/pycocoevalcap

评估脚本:

/usr/bin/python tools/test.py configs/lvlm/drivegpt/minigpt4-v2-llama2-7b_stage1.py \
    ${LAD_DATA_PATH}/weidafeng/lad_exps/minigpt4_stage1/epoch_4.pth/model.pth \
    --out ${LAD_DATA_PATH}/weidafeng/lad_exps/minigpt4_stage1/epoch_4.pth/inference_results-caption.pkl \
    --work-dir ${LAD_DATA_PATH}/weidafeng/lad_exps/minigpt4_stage1/  \
    --use-val-cfg  --close-deepspeed

推理结果保存在 --out ${LAD_DATA_PATH}/user/exps/minigpt4_stage1/epoch_4.pth/inference_results-caption.pkl,包含以下内容:

[{'image_id': '000000184613',
  'answer': ['A child holding a flowered umbrella and petting a yak.',
   'A young man holding an umbrella next to a herd of cattle.',
   'a young boy barefoot holding an umbrella touching the horn of a cow',
   'A young boy with an umbrella who is touching the horn of a cow.',
   'A boy holding an umbrella while standing next to livestock.'],
  'question': '[caption] Summarize this image in a few words.',
'pred_answer' : 'A man standing in a field holding onto an animal. \n< 18 >< 20 >< 49 >< 37 > } ' },
 {'image_id': '000000403013',
  'answer': ['A narrow kitchen filled with appliances and cooking utensils.',
   'A galley kitchen with cabinets and appliances on both sides',
   'A hallway leading into a white kitchen with appliances.',
   'Doorway view of a kitchen with a sink, stove, refrigerator and pantry.',
   'The pantry door of the small kitchen is closed.'],
  'question': '[caption] Using language, provide a short account of the image.',
'pred_answer' : 'a very small kitchen with white cabinets and a sink \n< 73 >< 28 >< 90 >< 61 > } ' },
 ...
 ]

指标: