Rethinking Nearest Neighbors for Visual Classification


Environment settings

Check out scripts/

Setup data

Download the following fine-grained datasets and ImageNet.

In current version, you need to modify each data file under knn/data/finetune/*.py


The numerical experiment results with corresponding hyper-parameters can be found here:

To use the code in this repo, here are some key configs:

  • DATA.FEATURE: specify which representation to use. includes more details
  • DATA.BATCH_SIZE: ViT-based backbone requires a smaller batchsize
  • RUN_N_TIMES: ensure only run once in case duplicated submision
  • MODEL.TYPE: base or joint training
  • OUTPUT_DIR: output dir of the final model and logs
  • SOLVER.BASE_LR: learning rate for the experiment
  • SOLVER.WEIGHT_DECAY: weight decay value for the experiment
  • MODEL.KNN_LAMBDA: alpha in Eq 4

Linear evaluation

See script/ and script/

End-to-end finetuning

See script/


This repo are released under the CC-BY-NC 4.0 license. See LICENSE for additional details.


We thank the researchers who propose NEWT for providing the features for the datasets.


View Github