Skip to content

The code of the paper "Negative Pre-aware for Noisy Cross-modal Matching" in AAAI 2024.

Notifications You must be signed in to change notification settings

ZhangXu0963/NPC

Repository files navigation

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.

In Step 1, we calculate the negative impact of each sample via the siamese model A' of base model A.

In Step 2, we train the base model A with the re-weight samples and memory bank.

The proposed NPC achieves much better accuracy (higher R@1) and higher robustness (lower variance among R@1).

Requirements

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.
Noise Ratio Method MSCOCO 1K MSCOCO 5K
Image-to-Text Text-to-Image Image-to-Text Text-to-Image
R@1R@5R@10 R@1R@5R@10R@1R@5R@10 R@1R@5R@10
0% CLIP 79.995.198.1 65.090.398.1 62.284.690.9 45.172.381.8
NPC 82.296.598.7 68.392.098.7 65.487.393.1 48.575.484.4
20% CLIP 75.093.197.2 58.786.197.2 55.580.387.8 38.865.575.7
NPC 79.995.998.4 66.390.898.4 61.685.491.6 46.073.482.9
40% CLIP 70.791.796.2 54.783.496.2 51.676.184.7 35.361.372.1
NPC 79.495.198.3 65.090.198.3 61.384.790.6 44.772.281.7
50% CLIP 69.090.695.8 52.882.095.8 49.074.583.0 33.759.170.1
NPC 78.595.098.1 64.189.698.1 60.183.590.4 43.671.180.9
60% CLIP 67.088.895.0 49.779.695.0 45.171.580.9 30.955.666.9
NPC 78.294.497.7 63.189.097.7 59.982.989.7 43.070.280.0
  • Experiment results on Flickr30K.
Noise Ratio Method Image-to-Text Text-to-Image
R@1R@5R@10 R@1R@5R@10
0% CLIP 86.297.699.2 72.992.396.0
NPC 87.998.199.4 75.093.797.2
20% CLIP 82.395.598.3 66.088.593.5
NPC 87.397.598.8 72.992.195.8
40% CLIP 76.293.396.5 59.485.090.9
NPC 85.697.598.4 71.391.395.3
60% CLIP 66.387.393.0 52.178.887.4
NPC 83.095.998.6 68.189.694.2
  • Experiment results on CC120K.
Method Image-to-Text Text-to-Image
R@1R@5R@10 R@1R@5R@10
CLIP 68.887.092.9 67.886.490.9
NPC 71.192.096.2 73.090.594.8

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

@inproceedings{NPC,
author = {Xu Zhang and Hao Li and Mang Ye},
title = {Negative Pre-aware for Noisy Cross-Modal Matching},
booktitle = {AAAI},
year = {2024}
}

About

The code of the paper "Negative Pre-aware for Noisy Cross-modal Matching" in AAAI 2024.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages