In this repo, a number of pytorch-lightning implementations of FSL algorithms are provided, including two official ones
Boosting Few-Shot Classification with View-Learnable Contrastive Learning (ICME 2021)
Rectifying the Shortcut Learning of Background for Few-Shot Learning (NeurIPS 2021)
This repository is built on top of LightningCLI, which is very convenient to use after being familiar with this tool.
dataset_and_process/samplers.py
for details.config/
.modules/PN.py
, architectures/classifier/proto_head.py
) and cosine classifier (modules/cosine_classifier.py
, architectures/classifier/CC_head.py
.is_meta
in the configuration file.Implemented results on few-shot classification datasets. The average results of 2,000 randomly sampled episodes repeated 5 times for 1/5-shot evaluation with 95% confidence interval are reported.
Models | Backbone | 5-way 1-shot | 5-way 5-shot | pretrained models |
---|---|---|---|---|
Protypical Networks | ResNet12 | 61.19+-0.40 | 76.50+-0.45 | link |
Cosine Classifier | ResNet12 | 63.89+-0.44 | 80.94+-0.05 | link |
Meta-Baseline | ResNet12 | 62.65+-0.65 | 79.10+-0.29 | link |
S2M2 | WRN-28-10 | 58.85+-0.20 | 81.83+-0.15 | link |
S2M2+Logistic_Regression | WRN-28-10 | 62.36+-0.42 | 82.01+-0.24 | |
MoCo-v2(unsupervised) | ResNet12 | 52.03+-0.33 | 72.94+-0.29 | link |
Exemplar-v2 | ResNet12 | 59.02+-0.24 | 77.23+-0.16 | link |
PN+CL | ResNet12 | 63.44+-0.44 | 79.42+-0.06 | link |
COSOC | ResNet12 | 69.28+0.49 | 85.16+-0.42 | link |
To understand the code correctly, it is highly recommended to first quickly go through the pytorch-lightning documentation, especially LightningCLI. It won't be a long journey since pytorch-lightning is built on the top of pytorch.
Just run the command:
pip install -r requirements.txt
set_config_PN.py
for PN model), set inside the parameter 'is_test' to False, set GPU ids (multi-GPU or not), dataset directory, logging dir as well as other parameters you would like to change.python config/set_config_PN.py
).bash run.sh
config/set_config_meta_baseline_pretrain.py
. The second uses config/set_config_meta_baseline_finetune.py
, with pre-training model path from the first stage, specified by the parameterpre_trained_path
in the configuration file.config/set_config_MoCo.py
and set parameter is_exampler
to True.python COS.py --save_dir [save_dir] --pretrained_Exemplar_path [model_path] --dataset_path [data_path]
. [save_dir]
specifies the saving directory of all foreground objects, [model_path]
and [data_path]
specify the pathes of pre-trained model and datasets, respectively.config/set_config_COSOC.py
and set parameter data["train_dataset_params"]
to the directory of saved data of COS algorithm, pre_trained_path
to the directory of pre-trained Exemplar.is_test
to True, pre_trained_path
to the directory of checkpoint model (with suffix '.ckpt'), and other parameters (e.g. shot, batchsize) as you disire.python config/set_config_PN.py
).bash run.sh
It is quite simple to implement your own algorithm. most of algorithms only need creation of a new LightningModule and a classifier head. We give a breif description of the code structure here.
It is usually not needed to modify this file. The file run.py
wraps the whole training and testing procedure of a FSL algorithm, for which all configurations are specified by an individual yaml file contained in the /config
folder; see config/set_config_PN.py
for example. The file run.py
contains a python class Few_Shot_CLI
, inherited from LightningCLI. It adds new hyperpameters (Also specified in configuration file) as well as testing process for FSL.
Need modification. The folder modules
contains LightningModules for FSL models, specifying model components, optimizers, logging metrics and train/val/test processes. Notably, modules/base_module.py
contains the template module for all FSL models. All other modules inherit the base module; see modules/PN.py
and modules/cosine_classifier.py
for how episodic/non-episodic models inherit from the base module.
Need modification. We divide general FSL architectures into feature extractor and classification head, specified respectively in architectures/feature_extractor
and architectures/classifier
. These are just common nn
modules in pytorch, which shall be embedded in LightningModule mentioned above. The recommended feature extractor is ResNet12, which is popular and shows promising performance. The classification head, however, varies with algorithms and need specific designs.
It is usually not needed for modification. Pytorch-lightning unifies data processing across training, val and testing into a single LightningDataModule. We disign such a datamodule in dataset_and_process/datamodules/few_shot_datamodule.py
for FSL, enabling episodic/non-episodic sampling and DDP for multi-GPU fast training. The definition of Dataset itself is in dataset_and_process/datasets
, specified as common pytorch datasets class. There is no need to modify the dataset module unless new datasets are involved.
It is usually not needed for modification. See documentation of pytorch-lightning for detailed introductions of callbacks and Plugins. They are additional functionalities added to the system in a modular fashion.
Need modification. See LightningCLI for how a yaml configuration file works. For each algorithm, there needs one specific configuration file, though most of the configurations are the same across algorithms. Thus it is convenient to copy one configuration and change it for a new algorithm.