Code Monkey home page Code Monkey logo

negative-margin.few-shot's Introduction

Negative Margin Matters: Understanding Margin in Few-shot Classification

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.

Update on 2020/07/01

Our paper was accepted by ECCV 2020 as spotlight!

Introduction

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.

Citation

@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}
}

Main Results

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 resnet-18 from OneDrive.

Getting started

Environment

  • Anaconda with python >= 3.6
  • pytorch=1.2.0, torchvison, cuda=9.2
  • others: pip install yacs

Datasets

CUB

  • Change directory to ./data/CUB
  • run bash ./download_CUB.sh

mini-ImageNet

  • 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.)

mini-ImageNet->CUB

  • Finish preparation for CUB and mini-ImageNet and you are done!

Train and eval

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 some scripts to reproduce the results in the paper. Pleas check ./script for details.

References

Our testbed builds upon several existing publicly available code. Specifically, we have modified and integrated the following code into this project:

negative-margin.few-shot's People

Contributors

bl0 avatar caoyue10 avatar impiga avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

negative-margin.few-shot's Issues

dataset

The link to download the dataset is not available now, is there any way to download it?

number of classes during training

Hi! Thank you for your great work and sharing all codes.

For experiments on the CUB dataset, you set the number of output of linear classifier to 200 during training stage.
So, when using the neg-cosine loss, the number of weights W is 200, which is different from the Eq. (1) in p.4 of your paper.

During training stage, I think that the number of classes should be 100 as same as the number of base classes.
The weights corresponding to the classes which are base classes also used to computing the loss and gradients.

I'm not sure that this have meaningful positive or negative effects on the performances.
It would be very helpful if you let me know why you set the number of classes to 200.

p.s. Since I'm very new to few-shot learning task so I might misunderstand. Then, please understand in advance.

Best,
Seon.

Type error: step() missing 1 required positional argument: 'epoch'

Hi, I found a bug here https://github.com/bl0/negative-margin.few-shot/blob/master/lib/utils.py#L77
I found if epoch argument does not have a default value like None, it would raise a type error in my machine, and models can't be trained, since _LRScheduler super class would call self.step() in its __init__ method.

So, have you ever used GradualWarmupScheduler scheduler in the pre-training stage? And would this scheduler have a lot effects on the final performance?

Thanks a lot.

why the input size is 224*224?

@bl0 thanks for your work!I am wondering why your input is 224224? As far as I know, most of the work of few-shot classification is evaluated on 8484 size

本篇论文中的3.4第一段

“the probability of a sample in the novel category j classified by pre-trained backbone fθ(m) and classifier W(m) as a base category k is..."这句话的意思是预测新类是某个基类的概率吗?就是说基类和新类有相同的类别吗,可是元学习中它们两个不应该有交叉啊,求指教

The results of mini2CUB are opposite

Sorry to bother you again!I reproduced the experiment on my own device and found that most of the results were basically the same. However, for the mini2CUB experiment, I found that the experimental results of neg-softmax and neg-cosine were just opposite to those in the paper.The experimental result of neg-softmax is about 67%, while that of neg-cosine is about 69%.

data distributions in angular space

Sorry for bothering,

May I ask how do you draw the FIG 3(the visualizations of the data distributions on angular space)?

Looking forward to your reply,

Best regards.

How to reproduce the results when using Conv4 and neg-cosine?

Hi, I am trying to reproduce the results when using Conv4 as the backbone and neg-cosine as the metric. However, the results seem to be lower than the reported results on miniImagenet:


| train acc | 1 shot val | 5 shot val | 1 shot test | 5 shot test |
| 55.79% | 47.05% ± 0.75% | 65.36% ± 0.69% | 46.96% ± 0.76% | 64.77% ± 0.65% |

and the parameters are set as:

{'dataset': {'base_file': 'data/miniImagenet/base.json',
             'base_val_file': '',
             'novel_file': 'data/miniImagenet/novel.json',
             'num_class': 64,
             'type': 'miniImagenet',
             'val_file': 'data/miniImagenet/val.json'},
 'method': {'backbone': 'Conv4',
            'image_size': 84,
            'metric': 'cosineface',
            'metric_params': CfgNode({'scale_factor': 30.0, 'margin': -0.02}),
            'metric_params_test': CfgNode({'scale_factor': 5, 'margin': 0.0})},
 'misc': {'checkpoint_dir': './output/miniImagenet/exp_neg-cosinemargin-0.02/checkpoints',
          'evaluate': False,
          'log_dir': './output/miniImagenet/exp_neg-cosinemargin-0.02/tensorboard',
          'num_workers': 8,
          'output_dir': './output/miniImagenet/exp_neg-cosinemargin-0.02',
          'resume': '',
          'rng_seed': 0},
 'test': {'batch_size': 1024,
          'finetune_params': {'iter': 100,
                              'optim': 'SGD',
                              'sgd_params': {'dampening': 0.9,
                                             'lr': 0.01,
                                             'momentum': 0.9,
                                             'weight_decay': 0.001}},
          'n_query': 16,
          'n_support': [1, 5],
          'n_way': 5,
          'num_episode': 600,
          'split': 'novel'},
 'train': {'adam_params': CfgNode({'lr': 0.003, 'weight_decay': 0.0}),
           'batch_size': 256,
           'drop_rate': 0.0,
           'dropblock_size': 5,
           'lr_scheduler': 'warmup_cosine',
           'optim': 'Adam',
           'print_freq': 10,
           'sgd_params': {'lr': 1.0,
                          'momentum': 0.9,
                          'nesterov': True,
                          'weight_decay': 0.0005},
           'stop_epoch': 400,
           'warmup_params': CfgNode({'multiplier': 16, 'epoch': 100})},
 'val': CfgNode({'num_episode': 100, 'freq': 20, 'n_support': 5})}

I get similar results when changing the margin to -0.2.
Are there some problems with the above parameters?
Appreciate your reply, thanks!

how to train the model with res12?

when I train the model with your default config.py(resnet18, input size:224x224), it works well. However, when I train neg-softmax_1-shot with resnet12(input size :84x84) as the backbone will cause an error of ‘out of memory’. how many memory be requried for resnet12?

理论分析部分

作者您好,请问理论分析两个假设的依据是什么,就是上文的实验观察么?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.