Home

Awesome

Enhancing Few-shot Image Classification with Cosine Transformer

This repo contains the official implementation code for the paper Enhancing Few-shot Image Classification with Cosine Transformer (IEEE Access). In this project, we developed a transformer-based algorithm FS-CT for few-shot classification and cross-attention mechansim, where we proved that cosine similarity benefits attention mechanism and and improve few-shot algorithms across settings and datasets. In particular, with the proposed Cosine attention, we achieve a more stable and consistent output as correlation map between support and query feature and thus improve ViT-bases few-shot algorithms' performance greatly.

Table of Content <!-- omit in toc -->

Few-shot Cosine Transformer

The overall architecture of the proposed Few-shot Cosine Transformer, which includes two main components: (a) learnable prototypical embedding that calculates the categorical proto representation given random support features that might be either in the far margin of the distribution or very close to each other and (b) Cosine transformer that determines the similarity matrix between proto representations and query samples for the few-shot classification tasks. The heart of the transformer architecture is Cosine attention, an attention mechanism with cosine similarity and no softmax function to deal with two different sets of features. The Cosine transformer shares a similar architecture with a standard transformer encoder block, with two skip connections to preserve information, a two-layer feed-forward network, and layer normalization between them to reduce noise. The outcome value is through a cosine linear layer, with cosine similarity replacing the dot-product, before feeding to softmax for query prediction.

Experiments

Dependencies environment

Dataset

Running scripts

Results

Our method Few-Shot TransFormer achieves the following performances on:

Dataset1-shot Accuracy5-shot Accuracy
mini-ImageNet55.87+-0.86%73.42+-0.67%
CIFAR-FS67.06+-0.89%82.89+-0.61%
CUB81.23+-0.77%92.25+-0.37%

All results are stored in record/official_results.txt

References

Acknowledgment

Citation

If you find our code useful, please consider citing our work using the bibtex:

@article{nguyen2023FSCT,
  author={Nguyen, Quang-Huy and Nguyen, Cuong Q. and Le, Dung D. and Pham, Hieu H.},
  journal={IEEE Access}, 
  title={Enhancing Few-Shot Image Classification With Cosine Transformer}, 
  year={2023},
  volume={11},
  number={},
  pages={79659-79672},
  doi={10.1109/ACCESS.2023.3298299}}

Contact

If you have any concerns or support need on this repository, please send me an email at quanghuy0497@gmail.com