Rethinking Nearest Neighbors for Visual Classification

Menglin Jia, Bor-Chun Chen, Zuxuan Wu, Claire Cardie, Serge J. Belongie, Ser-Nam LimarXiv 2021

Introduction

The prevailing paradigm in computer vision involves pre-training large-scale models followed by fine-tuning a simple parametric classifier (typically a linear layer) for specific tasks. While effective, these parametric classifiers often struggle with data scarcity, domain shifts, and capturing fine-grained local similarities. This paper re-evaluates the role of $k$-Nearest Neighbor ($k$-NN) algorithms—often considered a legacy baseline—and demonstrates their potential to significantly enhance modern deep learning architectures.

The JOINT Framework

The authors propose JOINT, a two-step integration strategy that leverages the complementary strengths of "eager" neural networks (global rule building) and "lazy" $k$-NN learners (local example reliance).

Overview of the JOINT integration strategy, showing training guidance and test-time interpolation.

Step 1: Training with $k$-NN Guidance

During the training phase, the model incorporates a $k$-NN classifier to provide a "second opinion" on training samples. If the $k$-NN search identifies an example as difficult (assigning it low probability based on its neighborhood), the system increases the loss for that specific instance. This forces the parametric model to prioritize learning from "hard" examples identified by the underlying feature structure.

Step 2: Inference via Interpolation

At test time, the final prediction is derived by linearly interpolating the probability distribution from the neural network with the distribution produced by a $k$-NN search over the training set. This allows the final output to benefit from both global parametric patterns and local non-parametric evidence.

Experimental Evaluation

The JOINT method was evaluated across a diverse set of benchmarks:

  • Natural World Tasks (NeWT): 164 binary classification tasks focusing on traits like behavior and age.
  • Fine-grained Benchmarks: CUB-200-2011, Oxford Flowers, Stanford Dogs, and Stanford Cars.
  • Large-scale Classification: ImageNet (ILSVRC 2012).
  • Architectures: Evaluations included both ResNet and Vision Transformer (ViT) backbones pre-trained with supervised and self-supervised objectives (e.g., DINO, MoCo v2).

Results and Analysis

The JOINT approach consistently outperformed standard linear classifiers across nearly all tasks. The benefits were particularly pronounced in low-data regimes and for specific challenging task categories.

Relative accuracy gain of the JOINT method across different task categories.

Key findings include:

  • Low-Data Efficiency: On the CUB dataset, JOINT achieved performance comparable to a 100% data baseline using only 60% of the training data.
  • Task-Specific Gains: The method showed a +9.7% relative gain on "counting" tasks, which are traditionally difficult for standard parametric classifiers.
  • Universality: The performance boost was observed regardless of the backbone architecture or the pre-training method used, confirming $k$-NN as a robust general-purpose augmentor.

Limitations and Conclusion

Despite its success, the method introduces computational overhead due to the need for a memory bank and $k$-NN search during inference. While libraries like FAISS can mitigate this, scaling to billions of images remains a challenge. Additionally, the method's efficacy is inherently tied to the quality of the underlying feature extractor.

In conclusion, this work demonstrates that classical non-parametric methods like $k$-NN are not obsolete. Instead, when integrated thoughtfully into modern training and inference pipelines, they provide a powerful mechanism for improving the robustness and accuracy of visual classifiers.

Summary updated

BibTeX
@misc{jia2021rethinking,
  author = {Menglin Jia and Bor-Chun Chen and Zuxuan Wu and Claire Cardie and Serge J. Belongie and Ser-Nam Lim},
  title = {{Rethinking Nearest Neighbors for Visual Classification}},
  year = {2021},
  eprint = {2112.08459},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2112.08459},
}
Figure