ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

如何从使用 nltk 计算 BLEU 转到 使用CocoEval 计算 BLUE、Cider、Meter、Rough、Spice、

2022-04-13 17:34:53  阅读:191  来源: 互联网

标签:BLEU BLUE word reference temp map Cider hypotheses preds


  仅对于Cider计算而言,nltk对输入的要求同Coco是不同的。

  前者仅要求输入的 reference 长度等于 hypotheses,并且要求 reference 为一维 List,要求 hypotheses 是二维 List。

  Coco则不同,他要求输入的 reference 长度等于 hypotheses,并且二者都是字典形式,对应的 reference = { “序号” , [ “文本” ] },hypotheses = { “序号” , [ “文本1”, “文本2” ···] }

  所以需要将模型得出的结果转为真正的字符串,这时候需要用到之前总结出的 wordmap。具体代码如下:

  对于 nltk 而言

            allcaps = allcaps[sort_ind]  # 每次 model 得出结果对应的参考值 hypotheses 
            #对应的一些处理 并存到总的 reference 中
            for j in range(allcaps.shape[0]):
                img_caps = allcaps[j].tolist()
                img_captions = list(
                    map(lambda c: [w for w in c if w not in {word_map['<start>'], word_map['<pad>']}],
                        img_caps))  # remove <start> and pads
                references.append(img_captions)

            # Hypotheses
            # 每次 model 得出的结果 reference 并进行处理
            _, preds = torch.max(scores_copy, dim=2)
            preds = preds.tolist()
            temp_preds = list()
            for j, p in enumerate(preds):
                temp_preds.append(preds[j][:decode_lengths[j]])  # remove pads
            preds = temp_preds
            hypotheses.extend(preds)
            assert len(references) == len(hypotheses)

  之后即可进行 nltk 的 BLEU 计算

from nltk.translate.bleu_score import corpus_bleu        
# Calculate BLEU-4 scores
bleu4 = corpus_bleu(references, hypotheses)

紧跟着,我们只需对此时的 reference 和 hypotheses 做少量处理 即可直接使用 Coco 计算

     # Load word map (word2ix) 读取 word map
        with open(args.word_map, 'r') as j:
             word_map = json.load(j)
        rev_word_map = {v: k for k, v in word_map.items()}  # ix2word
        
        hyp_list = hypotheses
        ref_list = references
        
        hyps = {}
        refs = {}        
        
        for iidx, hypid in enumerate(hyp_list):
            words = [rev_word_map[ind] for ind in hypid]
            temp = ''
            wordlist = []
            for word in words:
                if word != '<end>' and word != '<unk>':
                    temp += word + ' '
            wordlist.append(temp)
            hyps[str(iidx)] = wordlist
        
        for iidx, refid in enumerate(ref_list):
            wordlist = []     
            for ref in refid:
                temp = ''
                words = [rev_word_map[ind] for ind in ref]
                for word in words:
                    if word != '<end>' and word != '<unk>':
                        temp += word + ' '
                wordlist.append(temp)
            refs[str(iidx)] = wordlist
        tem_cider = cider(refs, hyps)
        tem_bleu  = bleu(refs, hyps)
        tem_rouge = rouge(refs, hyps)
        tem_spice = spice(refs, hyps)
        meteor(refs, hyps)

参考资料:

https://github.com/wangleihitcs/CaptionMetrics

https://blog.csdn.net/xiyou__/article/details/121494013

无用的资料:

https://github.com/Maluuba/nlg-eval

标签:BLEU,BLUE,word,reference,temp,map,Cider,hypotheses,preds
来源: https://www.cnblogs.com/bigcoding/p/16141197.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有