By Bin Liu, Yue Cao, Yutong Lin, Qi Li, Zheng Zhang, Mingsheng Long, Han Hu.
This repo is an official implementation of "Negative Margin Matters: Understanding Margin in Few-shot Classification" on PyTorch.
This paper is initially described in arxiv, which introduces a negative margin loss to metric learning based few-shot learning methods. The negative margin loss significantly outperforms regular softmax loss, and achieves state-of-the-art accuracy on three standard few-shot classification benchmarks with few bells and whistles. These results are contrary to the common practice in the metric learning field, that the margin is zero or positive. To understand why the negative margin loss performs well for the few-shot classification, the authors analyze the discriminability of learned features w.r.t different margins for training and novel classes, both empirically and theoretically. They find that although negative margin reduces the feature discriminability for training classes, it may also avoid falsely mapping samples of the same novel class to multiple peaks or clusters, and thus benefit the discrimination of novel classes.
@article{liu2020negative,
title={Negative Margin Matters: Understanding Margin in Few-shot Classification},
author={Liu, Bin and Cao, Yue and Lin, Yutong and Li, Qi and Zhang, Zheng and Long, Mingsheng and Hu, Han},
journal={arXiv preprint arXiv:2003.12060},
year={2020}
}
The few-shot classification accuracy on the novel classes with ResNet-18 as the backbone is listed bellowing:.
Method | Mini-ImageNet 1 - shot |
Mini-ImageNet 5 - shot |
CUB 1 - shot |
CUB 5 - shot |
Mini-ImageNet -> CUB 5-shot |
---|---|---|---|---|---|
Softmax | 51.75+-0.80 | 74.27+-0.63 | 65.51+-0.87 | 82.85+-0.55 | 65.57+-0.70 |
Cosine | 51.87+-0.77 | 75.68+-0.63 | 67.02+-0.90 | 83.58+-0.54 | 62.04+-0.76 |
Neg-Sofmax | 59.02+-0.81 | 78.80+-0.61 | 71.48+-0.83 | 87.30+-0.48 | 69.30+-0.73 |
Neg-Cosine | 62.33+-0.82 | 80.94+-0.59 | 72.66+-0.85 | 89.40+-0.43 | 67.03+-0.76 |
You can download the pre-trained model checkpoints of resnet18
from OneDrive.
Anaconda
withpython >= 3.6
pytorch=1.2.0, torchvison, cuda=9.2
- others:
pip install yacs
- Change directory to
./data/CUB
- run
bash ./download_CUB.sh
- Change directory to
./data/miniImagenet
- run
bash ./download_miniImagenet.sh
(WARNING: This would download the 155G ImageNet dataset. You can comment out correponded line 5-6 in download_miniImagenet.sh
if you already have one.)
- Finish preparation for CUB and mini-ImageNet and you are done!
Run the following commands to train and evaluate:
python main.py --config [CONFIGFILENAME] \
--supp [SUPPLEMENTSTRING] \
method.backbone [BACKBONE] \
method.image_size [IMAGESIZE] \
method.metric_params.margin [MARGIN] \
[OPTIONARG]
For additional options, please refer to ./lib/config.py
.
We have also provided the train scripts to reproduce the results of resnet18
. Pleas check ./script
for details.
Our testbed builds upon several existing publicly available code. Specifically, we have modified and integrated the following code into this project:
- Framework, Backbone, Method: Closer Look at Few Shot https://github.com/wyharveychen/CloserLookFewShot
- Backbone(resnet12): MetaOpt https://github.com/kjunelee/MetaOptNet
- Backbone(WideResNet28_10): S2M2_R https://github.com/nupurkmr9/S2M2_fewshot