This repository contains the implementation for TRACE (Textual Relevance Augmentation and Contextual Encoding), a methodology for fine-tuning CLIP models for multimodal hate speech detection. Accepted to the Special Track on AI for Social Impact (AISI) at AAAI 2026.
It includes scripts for two primary tasks: the Facebook Hateful Memes (FHM) challenge and the MultiOFF (Multimodal Offensive Language) dataset.
Visual grounding (left) and parameter-efficient fine-tuning (right).
clip_vitL_14_ft.py: Main TRACE finetuning pipeline on FHM.clip_xlm_roberta_ft.py: CLIP-XLM-RoBERTa finetuning for FHM.siglip2_ft.py: SigLIP2-based TRACE variant for FHM.vg_caption_gen.py: Caption generation workflow for FHM.benco_eval.py,error_analysis.py: Post-hoc FHM analyses.images/: Figures used in the paper.MultiOFF/: MultiOFF training, captioning, evaluation scripts.utils/: Shared helpers such asutils.caption_selection.select_best_captionsandutils.loss_functions.calculate_loss_gs.zero-shot-eval/: Zero-shot evaluation scripts on FHM.
Install the required packages using pip:
pip install -r requirements.txtFor zero-shot evaluations using proprietary models, you will need to set your API keys as environment variables:
export GOOGLE_API_KEY="your_google_api_key"
export OPENAI_API_KEY="your_openai_api_key"The vg_caption_gen.py script is used to generate descriptive captions for the FHM dataset. It uses a pipeline of models:
- RAM++ for tag recognition.
- GroundingDINO for object detection based on tags.
- Gemini or InternVL to generate a final caption based on the image and grounding information.
Configuration, such as input/output file paths, is handled inside the script.
python vg_caption_gen.pyCaption generation for the MultiOFF dataset is handled by MultiOFF/multioff_cap_gen.py.
Training configurations are set directly within the training scripts.
To run training on the FHM dataset, execute one of the scripts:
For CLIP-ViT-L/14:
python clip_vitL_14_ft.pyFor CLIP-XLM-RoBERTa-Large:
python clip_xlm_roberta_ft.pyFor SigLIP2:
python siglip2_ft.pyTo train a model on the MultiOFF dataset, use the scripts within the MultiOFF/ directory.
For CLIP-RoBERTa:
python MultiOFF/clip_roberta_multioff.pyBefore running, you may want to adjust the following inside the main() function of the desired script:
- Data Path: Update the
data_pathvariable to point to your dataset JSON file. - Batch Size & Epochs: Modify
actual_batch_size,target_batch_size, andnum_epochs. - Loss Configuration: Change the
loss_configdictionary to enable or disable different loss components for ablation studies.
The code for the FHM task supports three main ablation settings, which can be configured by modifying the loss_config dictionary in the main training scripts.
Example loss_config in the script:
loss_config = {
'classification': True, # Classification loss (always enabled)
'contrastive': False, # Contrastive loss between image and text embeddings
'relevance': True # Relevance loss from the caption scorer
}-
Classification + Relevance (Default):
contrastive: False,relevance: True- Uses classification and relevance losses. Captions are selected using the caption scorer.
-
Classification + Contrastive:
contrastive: True,relevance: False- Uses classification and contrastive losses. Captions are selected based on cosine similarity between image and caption embeddings.
-
All Losses:
contrastive: True,relevance: True- Uses classification, contrastive, and relevance losses with dynamic weighting. Caption selection is based on a combination of relevance scores and cosine similarity.
Update the model path and data paths inside the scripts before running.
- FHM Benign Confounders (
benco_eval.py): Evaluates model performance on samples with benign confounders from the FHM dataset.python benco_eval.py
- FHM Error Analysis (
error_analysis.py): Performs a detailed analysis of mis-predicted samples from the FHM test set.python error_analysis.py
- MultiOFF Evaluation (
multioff_eval.py): Evaluates models trained on the MultiOFF dataset.python MultiOFF/multioff_eval.py
These scripts evaluate the zero-shot capabilities of various large vision-language models on the FHM dataset. Ensure your API keys are configured.
- InternVL (
lvlm_eval.py):python lvlm_eval.py
- Gemini (
gemini_eval.py):python gemini_eval.py
- GPT-4o (
gpt-4o-eval.py):python gpt-4o-eval.py
To run statistical significance tests between two models' prediction files, use stats_significance.py. You will need to provide paths to the prediction files inside the script.
python utils/stats-significance.pyThe FHM dataset should be a JSON file with the following structure:
[
{
"img": "path/to/image.jpg",
"text": "text on the meme",
"label": 0,
"ivl_8b_new_caption": "caption from IVL model",
"gemini_caption": "caption from Gemini model",
"split": "train"
},
...
]The data format for the MultiOFF dataset is handled by the scripts within the MultiOFF/ directory.
-
Generate Captions:
- Modify and run
vg_caption_gen.pyto populate the FHM dataset with rich captions.
- Modify and run
-
Configure Training:
- Open
clip_xlm_roberta_ft.pyorclip_vitL_14_ft.py. - Set the
data_pathto your JSON dataset. - Adjust
loss_configfor the desired ablation experiment.
- Open
-
Run Training:
python clip_xlm_roberta_ft.py
-
Evaluate the Fine-tuned Model:
- Modify
benco_eval.pyorerror_analysis.pyto point to your saved model checkpoint. - Run the evaluation script:
python benco_eval.py.
- Modify
If you use this repository, please cite the paper:
@article{koushik2025tracetextualrelevanceaugmentation,
title={TRACE: Textual Relevance Augmentation and Contextual Encoding for Multimodal Hate Detection},
author={Girish A. Koushik and Helen Treharne and Aditya Joshi and Diptesh Kanojia},
year={2025},
eprint={2504.17902},
archivePrefix={arXiv},
primaryClass={cs.CV},
url={https://arxiv.org/abs/2504.17902},
}
This code is released under the MIT License. See LICENSE for details.