[ICCV 2023] - 使用文本反转的零样本合成图像检索
[2024/05/07] The extended version of our ICCV 2023 paper is now public: iSEARLE: Improving Textual Inversion for Zero-Shot Composed Image Retrieval . The code will be released upon acceptance.
This is the official repository of the ICCV 2023 paper "Zero-Shot Composed Image Retrieval with Textual Inversion" and its extended version "iSEARLE: Improving Textual Inversion for Zero-Shot Composed Image Retrieval".
You are currently viewing the code and model repository. If you are looking for more information about the newly-proposed dataset CIRCO see the repository.
Composed Image Retrieval (CIR) aims to retrieve a target image based on a query composed of a reference image and a relative caption that describes the difference between the two images. The high effort and cost required for labeling datasets for CIR hamper the widespread usage of existing methods, as they rely on supervised learning. In this work, we propose a new task, Zero-Shot CIR (ZS-CIR), that aims to address CIR without requiring a labeled training dataset. Our approach, named zero-Shot composEd imAge Retrieval with textuaL invErsion (SEARLE), maps the visual features of the reference image into a pseudo-word token in CLIP token embedding space and integrates it with the relative caption. To support research on ZS-CIR, we introduce an open-domain benchmarking dataset named Composed Image Retrieval on Common Objects in context (CIRCO), which is the first dataset for CIR containing multiple ground truths for each query. The experiments show that SEARLE exhibits better performance than the baselines on the two main datasets for CIR tasks, FashionIQ and CIRR, and on the proposed CIRCO.
Workflow of our method. Top: in the pre-training phase, we generate pseudo-word tokens of unlabeled images with an optimization-based textual inversion and then distill their knowledge to a textual inversion network. Bottom: at inference time on ZS-CIR, we map the reference image to a pseudo-word $S_*$ and concatenate it with the relative caption. Then, we use CLIP text encoder to perform text-to-image retrieval.
@article{agnolucci2024isearle,
title={iSEARLE: Improving Textual Inversion for Zero-Shot Composed Image Retrieval},
author={Agnolucci, Lorenzo and Baldrati, Alberto and Bertini, Marco and Del Bimbo, Alberto},
journal={arXiv preprint arXiv:2405.02951},
year={2024},
}@inproceedings{baldrati2023zero,
title={Zero-Shot Composed Image Retrieval with Textual Inversion},
author={Baldrati, Alberto and Agnolucci, Lorenzo and Bertini, Marco and Del Bimbo, Alberto},
booktitle={Proceedings of the IEEE/CVF International Conference on Computer Vision},
pages={15338--15347},
year={2023}
}Getting Started
We recommend using the Anaconda package manager to avoid dependency/reproducibility problems. For Linux systems, you can find a conda installation guide here.
git clone https://github.com/miccunifi/SEARLEconda create -n searle -y python=3.8
conda activate searle
conda install -y -c pytorch pytorch=1.11.0 torchvision=0.12.0
pip install comet-ml==3.33.6 transformers==4.24.0 tqdm pandas==1.4.2
pip install git+https://github.com/openai/CLIP.gitDownload the FashionIQ dataset following the instructions in the official repository.
After downloading the dataset, ensure that the folder structure matches the following:
├── FashionIQ
│ ├── captions
| | ├── cap.dress.[train | val | test].json
| | ├── cap.toptee.[train | val | test].json
| | ├── cap.shirt.[train | val | test].json
│ ├── image_splits
| | ├── split.dress.[train | val | test].json
| | ├── split.toptee.[train | val | test].json
| | ├── split.shirt.[train | val | test].json
│ ├── images
| | ├── [B00006M009.jpg | B00006M00B.jpg | B00006M6IH.jpg | ...]Download the CIRR dataset following the instructions in the official repository.
After downloading the dataset, ensure that the folder structure matches the following:
├── CIRR
│ ├── train
| | ├── [0 | 1 | 2 | ...]
| | | ├── [train-10108-0-img0.png | train-10108-0-img1.png | ...]
│ ├── dev
| | ├── [dev-0-0-img0.png | dev-0-0-img1.png | ...]
│ ├── test1
| | ├── [test1-0-0-img0.png | test1-0-0-img1.png | ...]
│ ├── cirr
| | ├── captions
| | | ├── cap.rc2.[train | val | test1].json
| | ├── image_splits
| | | ├── split.rc2.[train | val | test1].jsonDownload the CIRCO dataset following the instructions in the official repository.
After downloading the dataset, ensure that the folder structure matches the following:
├── CIRCO
│ ├── annotations
| | ├── [val | test].json
│ ├── COCO2017_unlabeled
| | ├── annotations
| | | ├── image_info_unlabeled2017.json
| | ├── unlabeled2017
| | | ├── [000000243611.jpg | 000000535009.jpg | ...]Download ImageNet1K (ILSVRC2012) test set following the instructions in the official site.
After downloading the dataset, ensure that the folder structure matches the following:
├── ImageNet1K
│ ├── test
| | ├── [ILSVRC2012_test_[00000001 | ... | 00100000].JPEG]SEARLE Inference with Pre-trained Models
To compute the metrics on the validation set of FashionIQ, CIRR or CIRCO using the SEARLE pre-trained models, simply run the following command:
python src/validate.py --eval-type [searle | searle-xl] --dataset --dataset-path --eval-type if 'searle', uses the pre-trained SEARLE model to predict the pseudo tokens;
if 'searle-xl', uses the pre-trained SEARLE-XL model to predict the pseudo tokens,
options: ['searle', 'searle-xl']
--dataset Dataset to use, options: ['fashioniq', 'cirr', 'circo']
--dataset-path Path to the dataset root folder
--preprocess-type Preprocessing type, options: ['clip', 'targetpad'] (default=targetpad)Since we release the pre-trained models via torch.hub, the models will be automatically downloaded when running the inference script.
The metrics will be printed on the screen.
To generate the predictions file for uploading on the CIRR Evaluation Server or the CIRCO Evaluation Server using the SEARLE pre-trained models, please execute the following command:
python src/generate_test_submission.py --submission-name --eval-type [searle | searle-xl] --dataset --dataset-path …Since we release the pre-trained models via torch.hub, the models will be automatically downloaded when running the inference script.
The predictions file will be saved in the data/test_submissions/{dataset}/ folder.
SEARLE Minimal Working Example
…SEARLE
This section provides instructions for reproducing the results of the SEARLE method. It covers the steps to train the textual inversion network and perform inference using the trained model.
To perform both the optimization-based textual inversion and the training of the textual inversion network phi, we need to generate a set of phrases for each concept in the dictionary. The concepts are taken from the Open Images V7 dataset.
Run the following command to generate the phrases:
python src/gpt_phrases_generation.py --exp-name Name of the experiment (default="GPTNeo27B")
--gpt-model GPT model to use (default="EleutherAI/gpt-neo-2.7B")
--max-length Maximum length of the generated phrases (default=35)
--num-return-sequences Number of generated phrases for each concept (default=256)
--temperature Temperature of the sampling (default=0.5)
--no-repeat-ngram-size Size of the n-gram to avoid repetitions (default=2)
--resume-experiment Resume the experiment if it exists (default=false)Since the phrase generation process can be time-consuming, you can download the pre-generated phrases used in our
experiments here. After downloading, unzip the file in the data/GPT_phrases folder so that the
folder structure matches the following: data/GPT_phrases/GPTNeo27B/concept_to_phrases.pkl
We associate to each image a set of textual concepts taken from the Open Images V7 dataset.
Run the following command to associate concepts with the images:
python src/image_concepts_association.py --clip-model-name --dataset imagenet --dataset-path --split test --clip-model-name CLIP model to use, e.g 'ViT-B/32', 'ViT-L/14'
--dataset-path Path to the ImageNet root folder
--batch-size Batch size (default=32)
--num-workers Number of workers (default=8)
--preprocess-type Preprocessing type, options: ['clip', 'targetpad'] (default=targetpad)The associations will be saved in a CSV file located in the data/similar_concept/imagenet/test folder.
Perform the Optimization-based Textual Inversion on the ImageNet test set.
Run the following command to perform OTI:
python src/oti_inversion.py --exp-name --clip-model-name --dataset imagenet --dataset-path --split test …The OTI pre-inverted-tokens will be saved in the data/oti_pseudo_tokens/imagenet/test/{exp_name} folder.
Finally, train the Textual Inversion Network by distilling the knowledge from the OTI pre-inverted-tokens.
It is recommended to have a properly initialized Comet.ml account to have better logging of the metrics (nevertheless, all the metrics will also be logged on a csv file).
To train the Textual Inversion Network, run the following command:
python src/train_phi.py --exp-name --clip-model-name --imagenet-dataset-path --cirr-dataset-path --oti-exp-name --save-training …The Textual Inversion Network checkpoints will be saved in the data/phi_models/{exp_name} folder.
To evaluate the Textual Inversion Network on the validation sets, run the following command:
python src/validate.py --exp-name --eval-type phi --dataset --dataset-path --phi-checkpoint-name --exp-name Name of the experiment (should be the same as --exp-name in step 3)
--dataset Dataset to use, options: ['fashioniq', 'cirr', 'circo']
--dataset-path Path to the dataset root folder
--phi-checkpoint-name Name of the Textual Inversion Network checkpoint, e.g. 'phi_20.pt'
--preprocess-type Preprocessing type, options: ['clip', 'targetpad'] (default=targetpad)The metrics will be printed on the screen.
To generate the predictions file to be uploaded on the CIRR Evaluation Server or on the [CIRCO Evalu
暂无开放 Issues,或尚未同步最近议题。