Skip to content
/ Deco Public

MLLM can see? Dynamic Correction Decoding for Hallucination Mitigation

License

Notifications You must be signed in to change notification settings

zjunlp/Deco

Repository files navigation

MLLM Can See? Dynamic Correction Decoding For Hallucination Mitigation

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

MLLM Can See? Dynamic Correction Decoding For Hallucination Mitigation
Chenxi Wang1, Xiang Chen1, Ningyu Zhang1, Bozhong Tian1, Haoming Xu1, Shumin Deng2, Huajun Chen1
1Zhejiang University, 2National University of Singapore

Overview

teaser

Multimodal Large Language Models (MLLMs) frequently exhibit hallucination phenomena, but the underlying reasons remain poorly understood. In this paper, we present an empirical analysis and find that, although MLLMs incorrectly generate the targets in the final output, they are actually able to recognize visual objects in the preceding layers. We speculate that this may be due to the strong knowledge priors of the language model suppressing the visual information, leading to hallucinations. Motivated by this, we propose a novel dynamic correction decoding method for MLLMs (DeCo), which adaptively selects the appropriate preceding layers and proportionally integrates knowledge into the final layer to adjust the output logits. Note that DeCo is model agnostic and can be seamlessly incorporated with various classic decoding strategies and applied to different MLLMs. We evaluate DeCo on widely-used benchmarks, demonstrating that it can reduce hallucination rates by a large margin compared to baselines, highlighting its potential to mitigate hallucinations.

Setup

The main implementation of Deco is in transformers/generation/utils.py.

conda create -n deco python==3.9
conda activate deco
pip install -r requirements.txt

TL;DR

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

with torch.no_grad():
    output_dict = model.generate(
                    input_ids,
                    images=image_tensor,
                    do_sample=args.do_sample
                    temperature=args.temperature,
                    top_p=args.top_p,
                    num_beams=args.num_beams,
                    max_new_tokens=args.max_new_tokens,
                    return_dict_in_generate=True,
                    output_hidden_states=True,
                    use_deco = True,
                    alpha = 0.6,
                    threshold_top_p = 0.9, 
                    threshold_top_k = 20,
                    early_exit_layers=[i for i in range(20, 29)]
                    )
                
output_ids = output_dict.sequences
outputs = tokenizer.batch_decode(output_ids)

Evaluation

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

Arguments

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/.
--alpha 0.5 The scale factor to scale up the calibration strength.
--threshold_top_p 0.9 The threshold for controlling the number of candidate tokens.
--early-exit-layers range(20,29) The candidate layer interval can be adjusted appropriately according to the model.

CHAIR

  • Generate the MLLM's responses and save them in a jsonl file:
python chair_llava.py
  • 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/jsonl

POPE

python pope_eval.py 

MME

python mme_llava.py

Reference Repositories

Acknowledgement

The repository references the code from DoLA and OPERA and utilizes MLLM codebase of LLaVA and MiniGPT4. We extend our gratitude to the authors for their outstanding work.

Citation

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

@inproceedings{huang2024opera,
  title={MLLM Can See? Dynamic Correction Decoding For Hallucination Mitigation},
  author={Chenxi Wang, Xiang Chen, Ningyu Zhang, Bozhong Tian, Haoming Xu, Shumin Deng, Huajun Chen},
  booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  pages={13418--13427},
  year={2024}
}