Code Monkey home page Code Monkey logo

mcl's Introduction

Mutual Centralized Learning

This repository contains the public release of the Python implementation of

Learning to Affiliate: Mutual Centralized Learning for Few-shot Classification (CVPR 2022)

If you use this code or find this work useful for your research, please cite:

@InProceedings{Liu_2022_CVPR,
    author    = {Liu, Yang and Zhang, Weifeng and Xiang, Chao and Zheng, Tu and Cai, Deng and He, Xiaofei},
    title     = {Learning To Affiliate: Mutual Centralized Learning for Few-Shot Classification},
    booktitle = {Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},
    month     = {June},
    year      = {2022},
    pages     = {14411-14420}
}

Overview

Code Prerequisites

The following packages are required to run the scripts:

Some comparing methods may require additional packages to run (e.g, OpenCV in DeepEMD and qpth, cvxpy in MetaOptNet).

Dataset prepare

The dataset should be placed in dir "./data/miniImagenet" (or "./data/tieredimagenet") with the following format:

MCL
├── data
│   ├── miniImagenet
│   │   ├── train
│   │   │   ├──n01532829
│   │   │   ├──────n0153282900000987.png
│   │   ├── val
│   │   │   ├──
│   │   │   ├──────
│   │   ├── test
│   │   │   ├── 
│   │   │   ├──────

The general mini-/tieredimagenet dataset can be downloaded from DeepEMD. The fine-grained datasets (i.e., CUB, meta-iNat and tiered meta-iNat) can be downloaded from FRN.

Train and test

The train/test configs, tensorboard log and saved checkpoints are saved in the following format:

Dataset_Method_NwayKshot_Backbone_Accuracy (e.g., miniImagenet_MEL_katz_N5K1_R12_67.509)
├── tensorboard_log_date
│   ├──events.out.tfevents
├── predictions.txt (evaluation acc)
├── config.yaml
├── ebest_Nway_Kshot.txt (validation best epoch .txt)
├── ebest_Nway_Kshot.pth (validation best epocg .pth)

Download the snapshot files from Google Drive and extract it into the snapshots/ folder.

Evaluate the meta-trained model

For example, MCL-Katz 5-way 1-shot ResNet12 VanillaFCN GPU 0

python experiments/run_evaluator.py \
  --cfg ./snapshots/ResNet-12/MEL_katz/VanillaFCN/miniImagenet_MEL_katz_N5K1_R12_67.509/MEL_katz_N5K1_R12.yaml \
  -c ./snapshots/ResNet-12/MEL_katz/VanillaFCN/miniImagenet_MEL_katz_N5K1_R12_67.509/ebest_5way_1shot.pth \
  --device 0

Pretraining

We provide three pretraining config files motivated by FRN, DN4 and Linear Classifier. For example, FRN pretrainer on miniimagenet ResNet12 is performed by:

python experiments/run_pre.py \
  --cfg ./configs/miniImagenet/pretrainer/FRN_pre.yaml
  --device 0

The tensorboard log and pretrained model is saved in snapshots/ResNet-12/pretrainer/.

Meta-training

For Conv-4 experiments, we directly train the model from scratch. Just select any of config files from snapshots folder to the configs directory, e.g.,

cp ./snapshots/Conv-4/MEL_katz/VanillaFCN/miniImagenet_MEL_katz_N5K1_Conv4_55.747/MEL_katz_N5K1_Conv4.yaml ./configs/miniImagenet/Conv-4/
sh ./fast_train_test.sh ./configs/miniImagenet/Conv-4/MEL_katz_N5K1_Conv4.yaml 0

For ResNet-12 experiments, we first select the config files by analogous:

cp ./snapshots/ResNet-12/**/xxx.yaml ./configs/miniImagenet/ResNet-12/

Then we manually create the target checkpoint folders and copy (or soft link) the pretrained-model (e.g., snapshots/ResNet-12/pretrainer/miniImagenet_FRN_pre/miniimagenet-e0_pre.pth) to it:

mkdir ./checkpoint/xxx/
cp ./snapshots/ResNet-12/pretrainer/miniImagenet_FRN_pre/miniimagenet-e0_pre.pth ./checkpoint/xxx/
sh ./fast_train_test.sh ./configs/miniImagenet/ResNet-12/xxx.yaml 0

where xxx is the prefix of .yaml file and 0 indicates the GPU device number.

Few-shot Classification Results

Experimental results on few-shot learning datasets with ResNet-12/Conv-4 backbone. We report average results with 10,000 randomly sampled episodes for both 1-shot and 5-shot evaluation.

MCL Centrality Plugins without Meta-training

The centrality plugins experiments follow the pre-train + evaluation setting proposed in Baseline that directly evaluates the methods without meta-training. We simply run experiments/run_evaluator.py on the pre-trained models that gives the following results:

Acknowledgement

  • Our pretraining codes and configs follow FRN

  • The dataset we used is from DeepEMD

Start your own methods

We reimplement many of state-of-the-art methods in our unified framework according to their original implementations, i.e., DN4, DeepEMD, FRN, DSN, MetaOptNet, R2D2, CTX, Baseline, Baseline++, NegativeMargin, ProtoNet, RelationNet, MatchingNet.

It is easy to implement your own method in our unified framework by adding the similar your-own-method.py in the directory ./modules/query/ like the others.

Contact

We have tried our best to upload the correct snapshots on the google drive. However, since the config structures had been changed a lot in development (e.g., we introduce train/val/test n_way k_shot options instead of a single n_way k_shot in case some methods using the larger shot training), some of the .yaml configs in uploaded snapshots may have conflicts during loading.

If you encounter any issues or have questions about using the code, feel free to contact me [email protected]

mcl's People

Contributors

louieyang 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

Watchers

 avatar  avatar  avatar

mcl's Issues

pretrain dn4.py

Dear author,Thank you for your innovative work
In the path MCL/modules/pretrainer Is a dn4.py file missing from ?

about 'xxx.yaml' files

Hi,
I can not find any 'xxx.yaml' files in the snapshots folder.
Could you please provide the './snapshots/ResNet-12/**/xxx.yaml' files?
Thanks and Regards.

Question about the pre-training weight of the tieredimagenet dataset

Dear author, thanks for sharing the codes.

In your google drive shared folder, I find that you only provide the weight of tieredimagenet-e0_DeepEMD_downloaded.pth in ResNet-12/pretrainer. I would like to ask you whether the weight is the pre-training weight you trained on the tiered-imagenet_DeepEMD dataset according to the settings in MCL/configs/tieredimagenet/pretrainer/FRN_pre.yaml and whether this weight correspond to the experimental results of resnet-12 on tieredimagenet in your paper.

Question about PyramidGrid

Dear author, thanks for sharing the codes.

In the paper, you mentioned that the PyramidGrid first crops the image evenly into an H × W grid before sending to the encoder. That is, for example, if the input size of an image is 84x84 and evenly cropped into 2x2 patches. The patches are resized to the same size as the input (i.e., 84x84) and stacked together before sending to the encoder. Is that right?
However, I do not see any crop operation in function forward_Grid. Can you explain how did you implement PyramidGrid?

about train in meta-iNat and tiered meta-iNat dataset

This is a excellent work, and I am building my work on it. I have downloaded the data set. However, during the training, the program prompts "no such file or directory:'/data/mcl/meta_inant/val". I noticed meta-iNat and tiered meta-iNat dataset have no val set, so I would like to ask the author how to train on the dataset. Thank you!

Is DeepEMD really a unidirectional method?

Thanks for bringing in a new perspective with a bidirectional walk method! But I have a question and hope the authors could clarify. In DeepEMD, every query feature is very likely to be assigned to multiple support features (weights of features are different; also, the optimization process does not restrict a strict matching), so the formula in figure 1(b) may not be correct. In fact, the calculation of EMD distance treats query features and support features equally, and it tries its best to find an optimal assignment depending on all support and query features. Thus assignment cannot be made based on a single query feature, in contradiction with the formula in figure 1(b). In addition, I can imagine a bidirectional optimization process (somewhat like Coordinate Ascent)of DeepEMD that in each iteration updates the connection by looking at only query or support features. DeepEMD should therefore be considered as a bidirectional method. If I am correct, DeepEMD has nearly all the benefits that MCL can offer. For example, higher emphasis is given to support(query) features that have a large average similarity to query(support) features (the weight in DeepEMD; the expected visit in random walk of MCL); higher emphasis is given to similar pairs (low cost in DeepEMD; the high probability of revisiting similar pairs in MCL). If I am again correct (maybe I miss something), I will be curious about the real motivation and benefit of MCL.

Evaluate the meta-trained model get a bad result

I try to run this command:
'python experiments/run_evaluator.py --cfg ./snapshots/ResNet-12/MEL_katz/VanillaFCN/miniImagenet_MEL_N5K5_R12_kat
z_83.986/MEL_N5K5_R12_katz.yaml -c ./snapshots/ResNet-12/MEL_katz/VanillaFCN/miniImagenet_MEL_N5K5_R12_katz_83.986/ebest_5way_5shot.pth --device 0'

and get a bad result :
image

I downloaded the snapshots you provided and I haven't made any changes to the code. The dataset link you provided does not include miniImagenet, so I downloaded the dataset miniImagenet from a different path, but I can ensure its integrity and correctness. Is there a problem with my operation?

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.