Home

Awesome

OPERA: Alleviating Hallucination in Multi-Modal Large Language Models via Over-Trust Penalty and Retrospection-Allocation (CVPR 2024 Highlight)

License: MIT Arxiv Hugging Face Transformers GitHub Stars

This repository provides the official PyTorch implementation of the following paper:

OPERA: Alleviating Hallucination in Multi-Modal Large Language Models via Over-Trust Penalty and Retrospection-Allocation <br> Qidong Huang<sup>1,2</sup>, Xiaoyi Dong<sup>2</sup>, Pan Zhang<sup>2</sup>, Bin Wang <sup>2</sup>, Conghui He <sup>2</sup>, Jiaqi Wang<sup>2</sup>, Dahua Lin<sup>2</sup>, Weiming Zhang<sup>1</sup>, Nenghai Yu<sup>1</sup> <br> <sup>1</sup>University of Science and Technology of China, <sup>2</sup>Shanghai AI Laboratory <br>

Overview

<p align="center"><img src="./teaser.png" alt="teaser" width="500px" /></p>

Hallucination, posed as a pervasive challenge of multimodal large language models (MLLMs), has significantly impeded their real-world usage that demands precise judgment. Existing methods mitigate this issue with either training with specific designed data or inferencing with external knowledge from other sources, incurring inevitable additional costs. In this paper, we present OPERA, a novel MLLM decoding method grounded in an Over-trust Penalty and a Retrospection-Allocation strategy, serving as a nearly free lunch to alleviate the hallucination issue without additional data, knowledge, or training. Our approach begins with an interesting observation that, most hallucinations are closely tied to the knowledge aggregation patterns manifested in the self-attention matrix, i.e., MLLMs tend to generate new tokens by focusing on a few summary tokens, but not all the previous tokens. Such partial overtrust inclination results in the neglecting of image tokens and describes the image content with hallucination. Based on the observation, OPERA introduces a penalty term on the model logits during the beam-search decoding to mitigate the over-trust issue, along with a rollback strategy that retrospects the presence of summary tokens in the previously generated tokens, and re-allocate the token selection if necessary. With extensive experiments, OPERA shows significant hallucination-mitigating performance on different MLLMs and metrics, proving its effectiveness and generality.

Setup

The main implementation of OPERA is in transformers-4.29.2/src/transformers/generation/utils.py.

So it is convenient to use OPERA decoding by just installing our modified transformers package.

conda env create -f environment.yml
conda activate opera
python -m pip install -e transformers-4.29.2

Note: to implement OPERA on other version of transformers, you can follow the steps as the follows:

TL;DR

After setup the environment, you can directly use OPERA on your own MLLM model by:

# specify the location indexes of some input tokens
START_INDEX_of_IMAGE_TOKENS = <the location index of the first image token>
END_INDEX_of_IMAGE_TOKENS = <the location index of the last image token>
NUM_of_TOKENS_IN_THE_PROMPT = <the total number of tokens in the user prompt (including image tokens)>

key_position = {
  "image_start": START_INDEX_of_IMAGE_TOKENS, 
  "image_end": END_INDEX_of_IMAGE_TOKENS, 
  "response_start": NUM_of_TOKENS_IN_THE_PROMPT,
}

# add some arguments in the generate function
outputs = MLLM_model.generate(
    input_ids=input_ids,
    inputs_embeds=inputs_embeds,
    attention_mask=attention_mask,
    do_sample=False,
    num_beams=5,
    max_new_tokens=512,
    # opera
    opera_decoding=True,
    key_position=key_position,
    scale_factor=50,
    threshold=15,
    num_attn_candidates=5,
    penalty_weights=1,
)
# for a more efficient version, please use the setting below:
outputs = MLLM_model.generate(
    input_ids=input_ids,
    inputs_embeds=inputs_embeds,
    attention_mask=attention_mask,
    do_sample=False,
    num_beams=5,
    max_new_tokens=512,
    # opera
    opera_decoding=True,
    key_position=key_position,
    scale_factor=50,
    threshold=25,
    num_attn_candidates=1,
    penalty_weights=1,
)

Please refer to demo.ipynb here for more details.

Evaluation

The following evaluation requires for MSCOCO 2014 dataset. Please download here and extract it in your data path.

Besides, it needs you to prepare the following checkpoints of 7B base models:

Arguments

ArgumentExampleDescription
--modelllava-1.5Specify the MLLM model, this codebase supports instructblip, minigpt4, llava-1.5, shikra.
--data-path/path/to/datasetPath to the dataset file or folder, e.g., COCO_2014/val2014/.
--pope-typerandomType for POPE evaluation, supports random, popular, adversarial.
--scale_factor50The scale factor to scale up the self-attention weights. Default: 50.
--threshold15The threshold for attending retrospection. Default: 15.
--num_attn_candidates5The number of candidates per beam. Default: 5.
--penalty_weights1The weight of penalty term in decoding. Default: 1.

POPE

python pope_eval.py --model MODEL_NAME --data_path /path/to/COCO --pope-type random --gpu-id GPU_IDs --beam 5 --scale_factor 50 --threshold 15 --num_attn_candidates 5 --penalty_weights 1

Result on Random split:

ModelAccuracyPrecisionRecallF1 scoreYes ratio
InstructBLIP 7B90.393.887.090.347.8
MiniGPT-4 7B79.889.768.777.839.5
LLaVA-1.5 7B89.490.488.889.650.6

Result on Popular split:

ModelAccuracyPrecisionRecallF1 scoreYes ratio
InstructBLIP 7B83.481.287.084.053.6
MiniGPT-4 7B73.675.969.072.345.4
LLaVA-1.5 7B86.084.188.886.452.8

Result on Adversarial split:

ModelAccuracyPrecisionRecallF1 scoreYes ratio
InstructBLIP 7B80.777.387.081.956.3
MiniGPT-4 7B71.672.968.970.847.3
LLaVA-1.5 7B79.174.488.881.059.7

CHAIR

python chair_eval.py --model MODEL_NAME --data_path /path/to/COCO --gpu-id GPU_IDs --beam 5 --scale_factor 50 --threshold 15 --num_attn_candidates 5 --penalty_weights 1

Note: Please check out our released results in log/chair_eval_results for reproduction.

python chair.py --cap_file /path/to/jsonl --image_id_key image_id --caption_key caption --coco_path /path/to/COCO/annotations_trainval2014/annotations/ --save_path /path/to/save/jsonl

GPT-4V

The GPT-4V evaluation requires you to specify your API key in Line 88 of gpt4v_eval.py.

python gpt4v_eval.py --model MODEL_NAME --data_path /path/to/COCO --gpu-id GPU_IDs --scale_factor 50 --threshold 15 --num_attn_candidates 5 --penalty_weights 1

Acknowledgement

This repo is based on the MLLM codebase of LAVIS and MiniGPT-4 and the CHAIR code of Maxlinn. Thanks for their impressive works!

Citation

If you find this work useful for your research, please cite our paper:

@inproceedings{huang2024opera,
  title={Opera: Alleviating hallucination in multi-modal large language models via over-trust penalty and retrospection-allocation},
  author={Huang, Qidong and Dong, Xiaoyi and Zhang, Pan and Wang, Bin and He, Conghui and Wang, Jiaqi and Lin, Dahua and Zhang, Weiming and Yu, Nenghai},
  booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  pages={13418--13427},
  year={2024}
}