Awesome
Introduction
This is a PyTorch implementation for the AAAI 2024 paper "Negative Pre-aware for Noisy Cross-Modal Matching". Our method NPC is built on top of the CLIP in PyTorch for end-to-end Image-text Matching. <img src = "./assets/intro1.png" width = "80%">
In Step 1, we calculate the negative impact of each sample via the siamese model A' of base model A. <img src = "./assets/intro2.png" width = "80%">
In Step 2, we train the base model A with the re-weight samples and memory bank. <img src = "./assets/intro3.png" width = "50%">
The proposed NPC achieves much better accuracy (higher R@1) and higher robustness (lower variance among R@1). <img src = "./assets/intro4.png" width = "60%">
Requirements
- python 3.7
- numpy 1.21.6 (> 1.19.5)
- torch 1.13.1
- tensorboard 2.11.2
pip install requirments.txt
Data Preparation
Split Dataset
We conducted experiments on three datasets: MSCOCO, Flickr30K, and CC120K. We followed SCAN to split image-text pairs in MSCOCO and FLickr30K into training, validation and testing sets.
- MSCOCO.
We unified the images' name format of the MSCOCO dataset for easier use. You can use
dataset/MSCOCO_rename.py
to rename the images in MSCOCO. (MSCOCO_2014) - Flickr30K. (Flickr30K).
- CC120K. We tested the proposed method on the real-world dataset Conceptual Captions. Since the full dataset is too large, we randomly selected a subset of Conceptual Captions, named CC120K, including 118,851 images for training, 1,000 for validation, and 1,000 for testing. You can download the dataset from here with the code "3ble".
Construct Noisy Datasets
We constructed noise by randomly shuffling some captions of the images.
You can obtain your noisy dataset using construct_noise.py
. And we also integrated this part of the code in data.py
. The noisy dataset will be automatically constructed and saved during training when it doesn't exist.
Since there are around 3%-20% incorrect annotations existing in the real-world dataset Conceptual Captions, we did not create noisy samples manually.
Frozen Memory Bank
We provide the frozen Memory Bank that appeared in the paper for the datasets with different noise ratios. If you want to update them during training, please use get_memorybank.py
.
Note!
If you want to use your own noisy dataset for training, the Memory Bank should also be rebuilt. You can construct the noise dataset by construct_noise.py
, and obtain the Memory Bank by get_memorybank.py
.
Download Link
The final data directory tree should be:
(Fix a mistake: the path has been defined as dataset/${DATASET_NAME}/annotations/frozen_memory_bank
before, and the correct one should be dataset/${DATASET_NAME}/annotations/memory_bank
. Thanks for correction!)
├── dataset/
├── ${DATASET_NAME}/
| ├── annotations/
| | ├── memory_bank/
| | | ├── ${noise_ratio}_mbank_img_idx.npy
| | | ├── ${noise_ratio}_mbank_txt_idx.npy
| | | └── ...
| | └──scan_split/
| | ├── ${noise_ratio}_noise_train_caps.txt #samples use for training. ${noise_ration} is in {0, 0.2, 0.4, 0.6}
| | ├── train_caps.txt # the same as `0_noise_train_caps.txt`
| | ├── train_ids.txt
| | ├── dev_caps.txt #samples use for validation
| | ├── dev_ids.txt
| | ├── test_caps.txt #samples use for testing
| | ├── test_ids.txt
| | └── ...
| └── images/ # all images in MSCOCO (or Flickr30K, CC120K)
└── ...
Models and Evaluation
You can download the models fine-tuned using NPC(ours) and CLIP(our baseline) from this link.
Save the models in folder ./pre-trained_models
, and evaluate the models via the following command. For example, evaluate the models trained on MSCOCO with 60% noise.
python main_NPC.py --eval --resume /AAAI24-NPC/pre-trained_models/npc_coco_60.pt --dataset_root /AAAI24-NPC/dataset/MSCOCO --dataset coco
python main_CLIP.py --eval --resume /AAAI24-NPC/pre-trained_models/clip_coco_60.pt --dataset_root /AAAI24-NPC/dataset/MSCOCO --dataset coco
- Experiment results on MSCOCO 1K and 5K.
- Experiment results on Flickr30K.
- Experiment results on CC120K.
Training
For training NPC
You can train a new model via the following command. Before training, you can read the params.py
carefully to check your parameter setting. The --num_anns
should be set to 5 for MSCOCO and Flickr30K, and 1 for CC120K.
python main_NPC.py --batch_size 256 --epochs 5 --lr 2e-7 --vision_model ViT-B/32 --noise_ratio ${NOISE RATIO} --num_anns ${5 or 1} --dataset_root ${YOUR PATH} --dataset coco --checkpoint_path ${YOUR PATH}
For training CLIP
Thanks to this project for providing a basic fine-tuning framework of CLIP. We have improved the code of the data loading process and the model evaluation. The --num_anns
should be set to 5 for MSCOCO and Flickr30K, and 1 for CC120K. You can fine-tune the CLIP via the following command.
python main_CLIP.py --batch_size 256 --epochs 5 --lr 5e-7 --vision_model ViT-B/32 --noise_ratio ${NOISE RATIO} --num_anns ${5 or 1} --dataset_root ${YOUR PATH} --dataset coco --checkpoint_path ${YOUR PATH}
Reference
If our proposed NPC is helpful for you, welcome to cite the following paper. :smiley:
@inproceedings{NPC,
author = {Xu Zhang and Hao Li and Mang Ye},
title = {Negative Pre-aware for Noisy Cross-Modal Matching},
booktitle = {AAAI},
year = {2024}
}