README
May 4, 2024 ยท View on GitHub
Code for the paper Pre-trained Vision-Language Models Learn Discoverable Concepts
Set up environments
conda create --name labo python=3.9.16
conda activate cdl
conda install --file requirements.txt
Directories
concept_discoverysaves the code and data for concept discovery from the Conceptual Captions Datasetutilssaves the utility functions for the concept discovery, selection and learningdownstream_preparationsaves the python files for pre-selecting concepts for downstream tasksconcept_learningsaves the python files for concept learning- The python files in the root directory work for the classification tasks before and after concept learning ##Discovery Concepts from the Conceptual Captions Dataset
In the concept_discovery directory
- download the Conceptual Captions dataset from https://ai.google.com/research/ConceptualCaptions/
- download the Stanford Core NLP toolkit from https://stanfordnlp.github.io/CoreNLP/
python parse.pyto perform semantic parsing and extract objects from the captionspython generate_concept.pyto generate concepts from the objectspython filter_concept.pyto filter similar conceptspython pre_compute_embedding.pyto compute the image and concept embeddings with CLIPpython select_visual.pyto select visually discriminative concepts
Select Concepts for Downstream Tasks
In the downstream_preparation directory
python select_few_shot.pyto pre-process downstream datasets- for ImageNet, you need to download the ImageNet dataset from https://www.image-net.org/download.php
- for CUB, you need to download the CUB dataset from https://www.vision.caltech.edu/datasets/cub_200_2011/
python concept_category_map.pyto build category-concept association with the LLMpython filter_concepts.pyto filter too similar concepts
Concept-based Classification
In the root directory
python pre_img_embedding.py {dataset} {learn} {epoch}to pre-compute the image embeddings with CLIPpython select_concepts.py {dataset} {shot} {time_concept} {learn}to select a compact and performant set of conceptspython nway.py {dataset} {shot} {time_concept} {learn} {epoch}to perform few-shot classificationpython classification.py {dataset} {epoch}to perform the concept-based classification with the fine-tuned CLIP- for the arguments
datasetmeans the dataset to useshotmeans the few-shot setting, can be 1,2,4,8,16 or alltime_conceptmeans the size of the concept bottleneck (e.g. 2 means the concept bottleneck size is 2 times of the category number)learnis a boolean value, means whether current state is before or after concept learning;epochmeans the epoch number of the checkpoint to use after concept learning
Concept Learning
In the concept_learning directory
python predict_labels.py {dataset}to generate pseudo labels for the training imagespython train_clip.py {dataset}to fine-tune the CLIP model for concept learning