This is the PyTorch implementation of "AMPL: An Adaptive Meta-Prompt Learner for Few-Shot Image Classification".
- The code will be updated soon.
Python 3.8, Pytorch 1.11, CUDA 11.3. The code is tested on Ubuntu 20.04.
We have prepared a conda YAML file which contains all the python dependencies.
conda env create -f environment.yml
To activate this conda environment,
conda activate ampl
We use wandb to log the training stats (optional).
-
ππππImageNet
The ππππImageNet dataset was proposed by Vinyals et al. for few-shot learning evaluation. Its complexity is high due to the use of ImageNet images but requires fewer resources and infrastructure than running on the full ImageNet dataset. In total, there are 100 classes with 600 samples of color images per class. These 100 classes are divided into 64, 16, and 20 classes respectively for sampling tasks for meta-training, meta-validation, and meta-test. To generate this dataset from ImageNet, you may use the repository ππππImageNet tools.
-
ππππππ ImageNet
The π‘πππππImageNet dataset is a larger subset of ILSVRC-12 with 608 classes (779,165 images) grouped into 34 higher-level nodes in the ImageNet human-curated hierarchy. To generate this dataset from ImageNet, you may use the repository π‘πππππImageNet dataset: π‘πππππImageNet tools.
-
CIFAR-FS and FC100
CIFAR-FS and FC100 can be download using the scripts from DeepEMD.
Our model are trained on 8 RTX3090 GPUs by default (24GB memory). You can specify the argument --nproc_per_node
in the following command
file as the number of GPUs available in your server, and increase/decrease the argument --batch_size_per_gpu
if your GPU has more/less memory.
-
Pre-training (self-supervised)
In this phase, we pretrain our model using the self-supervised learning method iBOT and SMKD (use the ). All models are trained for a maximum of 1600 epochs. We evaluate our model on the validation set after training for every 50 epochs, and report the best. 1-shot and 5-shot evaluation results with Prototype method is given in the following table. We also provide full checkpoints and test-set features for pretrained models, and command to replicate the results.
--data_path
: need to be set as the location of the training set of dataset XXX (e.g. miniImageNet).--output_dir
: location where the phase1 checkpoints and evaluation files to be stored.Dataset 1-shot 5-shot Download ππππImageNet 60.93% 80.38% checkpoint features command ππππππ ImageNet 71.36% 83.28% checkpoint features command CIFAR-FS 65.70% 83.45% checkpoint features command FC100 44.20% 61.64% checkpoint features command -
Aaptive Meta-Prompt Learner (supervised)
In this second phase, we start from the checkpoint in phase 1 and further train the model using the supervised knowledge distillation method proposed in our paper. All models are trained for a maximum of 150 epochs. We evaluate our model on the validation set after training for every 5 epochs, and report the best. Similarly, 1-shot and 5-shot evaluation results with Prototype method is given in the following table. We also provide checkpoints and features for pretrained models.
--pretrained_dino_path
: should be set as the same location as--output_dir
in phase1.--pretrained_dino_file
: which checkpoint file to resume from (e.g.checkpoint1250.pth
).--output_dir
: location where the phase2 checkpoints and evaluation files to be stored.Dataset 1-shot 5-shot Download ππππImageNet 74.82% 88.47% checkpoint features command ππππππ ImageNet 78.98% 91.61% checkpoint features command CIFAR-FS 78.69% 90.68% checkpoint features command FC100 58.34% 72.25% checkpoint features command
We use eval_ampl.py
to evaluate a trained model. Before running the evaluation code, we need to specify the image data path in server_dict
of this python file.
For example, we can use the following code to do 5-way 5-shot evaluation on the model trained in Pre-training on mini-ImageNet:
- prototype:
python eval_ampl.py --server mini --num_shots 5 --ckp_path /root/autodl-nas/FSVIT_results/MINI480_phase2 --ckpt_filename checkpoint0040.pth --output_dir /root/autodl-nas/FSVIT_results/MINI480_prototype --evaluation_method cosine --iter_num 10000
- classifier:
python eval_ampl.py --server mini --num_shots 5 --ckp_path /root/autodl-nas/FSVIT_results/MINI480_phase2 --ckpt_filename checkpoint0040.pth --output_dir /root/autodl-nas/FSVIT_results/MINI480_classifier --evaluation_method classifier --iter_num 1000
@inproceedings{,
title={AMPL: An Adaptive Meta-Prompt Learner for Few-Shot Image Classification},
author={},
booktitle={},
year={},
pages={},
}