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

📄arXiv🤗HFPaper🎧NotebookLM Audio

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.

Demo

There is a demonstration of using DeCo. We provide a handy Jupyter Notebook!

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
--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

AMBER

  • Generate the MLLM's responses and save them in a jsonl file:
python amber_llava.py
  • Calculate metric score using the generated jsonl file:
python inference.py

POPE

python pope_eval.py 

MME

python mme_llava.py

Additional Experiment's Results

We compare the baseline, DoLa, DeCo, and the combination of DoLa and DeCo on the LLM benchmark, such as StrategyQA and GSM8K, using llama-7b.

Method StrategyQA GSM8K
baseline 59.8 10.8
DoLa 64.1 10.5
DeCo 61.2 10.2
DoLa+DeCo 60.0 9.6

We compare the baseline, DoLa, DeCo, and the combination of DoLa and DeCo on CHAIR, using llava-v1.5-7b.

Method CHAIRs CHAIRi
baseline 45.0 14.7
DoLa 47.8 13.8
DeCo 37.8 11.1
DoLa+DeCo 44.2 11.9

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:

@misc{wang2024mllmseedynamiccorrection,
      title={MLLM can see? Dynamic Correction Decoding for Hallucination Mitigation}, 
      author={Chenxi Wang and Xiang Chen and Ningyu Zhang and Bozhong Tian and Haoming Xu and Shumin Deng and Huajun Chen},
      year={2024},
      eprint={2410.11779},
      archivePrefix={arXiv},
      primaryClass={cs.CL},
      url={https://arxiv.org/abs/2410.11779}, 
}