Shallow Focus, Deep Fixes: Enhancing Shallow Layers Vision Attention Sinks to Alleviate Hallucination in LVLMs (EMNLP'25 Oral)
This repository provides the official PyTorch implementation of the following paper:
Hallucination, posed as a pervasive challenge of multi-modal 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 over-trust inclination results in the neglecting of image tokens and describes the image content with hallucination. Statistically, we observe an 80%∼95% co-currency rate between hallucination contents and such knowledge aggregation patterns. 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 effec-tiveness and generality.
The main implementation of EAH is in transformers-4.29.2/src/transformers/models/llama/modeling_llama.
So it is convenient to use EAH decoding by just changing original modeling_llama to our modeling_llama_eah.
conda env create -f environment.yml
conda activate eah
python -m pip install -e transformers-4.29.2
- Find the filefolder
transformers-4.29.2/src/transformers/models/llama/. - Overwrite original
modeling_llamato ourmodeling_llama_eah(with file namemodeling_llama).
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:
- Download LLaVA-1.5 merged 7B model and specify it at Line 14 of
eval_configs/llava-1.5_eval.yaml. - Download Vicuna 7B v1.1 model and specify it at Line 25 of
minigpt4/configs/models/blip2_instruct_vicuna7b.yaml. - Download Vicuna 7B v0 model and specify it at Line 18 of
minigpt4/configs/models/minigpt4_vicuna0.yaml. - Download MiniGPT-4 7B pretrained weights and specify it at Line 8 of
eval_configs/minigpt4_eval.yaml. - Download Shikra merged 7B model and specify it at Line 14 of
eval_configs/shikra_eval.yaml.
| Argument | Example | Description |
|---|---|---|
--model |
llava-1.5 |
Specify the MLLM model, this codebase supports instructblip, minigpt4, llava-1.5, shikra. |
--data-path |
/path/to/dataset |
Path to the dataset file or folder, e.g., COCO_2014/val2014/. |
--pope-type |
random |
Type for POPE evaluation, supports random, popular, adversarial. |
--LAYER_NUM |
1 |
Specifies which Transformer layer’s attention will be processed.. Default: 1. |
--HEAD_NUM |
k |
Specifies which attention head / head index is targeted for inspection or broadcasting. Default: k. |
--THRES |
0.002 |
Sets the attention strength threshold used to identify significant image-token attention. |
python pope_eval.py --model MODEL_NAME --data_path /path/to/COCO --pope-type random --gpu-id GPU_IDs - Generate the MLLM's responses and save them in a jsonl file:
python chair_eval.py --model MODEL_NAME --data_path /path/to/COCO --gpu-id GPU_IDs --beam 5 Note: Please check out our released results in log/chair_eval_results for reproduction.
- Calculate CHAIR using the generated jsonl file:
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/jsonlThe 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 This repo is based on the MLLM codebase of OPERA, LAVIS and MiniGPT-4 and the CHAIR code of Maxlinn. Thanks for their impressive works!
@article{seeing,
title={Seeing clearly by layer two: Enhancing attention heads to alleviate hallucination in lvlms},
author={Zhang, Xiaofeng and Quan, Yihao and Gu, Chaochen and Shen, Chen and Yuan, Xiaosong and Yan, Shaotian and Cheng, Hao and Wu, Kaijie and Ye, Jieping},
journal={The 2025 Conference on Empirical Methods in Natural Language Processing},
year={2024}
}
@inproceedings{shallow,
title={Shallow Focus, Deep Fixes: Enhancing Shallow Layers Vision Attention Sinks to Alleviate Hallucination in LVLMs},
author={Zhang, Xiaofeng and Quan, Yihao and Shen, Chen and Gu, Chaochen and Yuan, Xiaosong and Yan, Shaotian and Cao, Jiawei and Cheng, Hao and Wu, Kaijie and Ye, Jieping},
booktitle={The 2025 Conference on Empirical Methods in Natural Language Processing},
year={2025}
}