Home

Awesome

Learning with Memory-based Virtual Classes for Deep Metric Learning

Official PyTorch implementation of "Learning with Memory-based Virtual Classes for Deep Metric Learning" (ICCV 2021)

Byungsoo Ko*<sup>1</sup>, Geonmo Gu*<sup>1</sup>, Han-Gyu Kim<sup>2</sup> (* Authors contributed equally.)

<sup>1</sup>@NAVER/LINE Vision, <sup>2</sup>@NAVER Clova Speech

Overview

MemVir

MemVir is a novel training strategy for any softmax variants and proxy-based losses in deep metric learning. In conventional training, the loss function is computed with actual classes. On the other hand, in MemVir, classes from previous steps (virtual classes) are used to compute the loss function along with the actual classes. Moreover, the number of classes and embeddings are gradually increased by adding virtual classes, where C and B denote number of classes and batch size, N and M are hyper-parameters for MemVir.

<p align="center"> <img src="figures/overview.png" width="400"> </p>

How it works?

Below figure is t-SNE visualization of 512-dimensional embedding space. Embedding features are extracted by model trained with MemVir(5,100) on CARS196 training data. Each color indicates step for embedding features. As described in the figure, MemVir offers better utilization of embedding space by alleviating strong focus on seen classes for generalization.

<img src="figures/tsne.png">

Experimental results

MemVir improves performance for every loss and benchmark dataset.

<p align="center"> <img src="figures/evaluation.png" width="400"> </p>

Getting Started

Installation

  1. Clone the repository locally
$ git clone https://github.com/navervision/MemVir
  1. Create conda virtual environment
$ conda create -n memvir python=3.7 anaconda
$ conda activate memvir
  1. Install pytorch
$ conda install pytorch torchvision cudatoolkit=<YOUR_CUDA_VERSION> -c pytorch
  1. Install faiss
$ conda install faiss-gpu cudatoolkit=<YOUR_CUDA_VERSION> -c pytorch
  1. Install requirements
$ pip install -r requirements.txt

Prepare Data

$ wget http://ai.stanford.edu/~jkrause/car196/car_ims.tgz
$ tar zxvf car_ims.tgz -C ./dataset
# Dataset structure
/dataset/carDB/
  train/
    class1/
      img1.jpeg
    class2/
      img2.jpeg
  test/
    class1/
      img3.jpeg
    class2/
      img4.jpeg
# Rearrange dataset structure
$ python dataset/prepare_cars.py

Train models

Norm-SoftMax loss with CARS196

# Norm-SoftMax
$ python main.py --gpu=0 \
--save_path=./logs/CARS196_norm_softmax \
--data=./dataset/carDB --data_name=cars196 \
--dim=512 --batch_size=128 --epochs=130 \
--freeze_BN --loss=Norm_SoftMax \
--decay_step=50 --decay_stop=50 --n_instance=1 \
--scale=23.0 --check_epoch=5

MemVir + Norm-SoftMax loss with CARS196

# MemVir + Norm-SoftMax
$ python main.py --gpu=0 \
--save_path=./logs/CARS196_memvir_norm_softmax \
--data=./dataset/carDB --data_name=cars196 \
 --dim=512 --batch_size=128 --epochs=2000 \
--freeze_BN --loss=NormSoftmax \
--decay_step=2000 --decay_stop=2000 --n_instance=1 \
--scale=23.0 --check_epoch=10 --memvir=1 \
--mem_num_step=45 --mem_step_gap=10 --warm_epoch=40

Proxy-NCA loss with CARS196

# Proxy-NCA
$ python main.py --gpu=0 \
--save_path=./logs/CARS196_proxy_nca \
--data=./dataset/carDB --data_name=cars196 \
--dim=512 --batch_size=128 --epochs=130 \
--freeze_BN --loss=Proxy_NCA \
--decay_step=50 --decay_stop=50 --n_instance=1 \
--scale=12.0 --check_epoch=5

MemVir + Proxy-NCA loss with CARS196

# MemVir + Proxy-NCA
$ python main.py --gpu=0 \
--save_path=./logs/CARS196_memvir_proxy_nca \
--data=./dataset/carDB --data_name=cars196 \
--dim=512 --batch_size=128 --epochs=2000 \
--freeze_BN --loss=ProxyNCA \
--decay_step=2000 --decay_stop=2000 --n_instance=1 \
--scale=12.0 --check_epoch=10 --memvir=1 \
--mem_num_step=20 --mem_step_gap=25 --warm_epoch=40

Check Test Results

$ tensorboard --logdir=logs --port=10000

Experimental results

Recall@1

Loss12345MeanDiff.Std.
Norm-SoftMax83.3883.2583.2583.1883.0583.22-0.12
MemVir + Norm-SoftMax86.3686.0586.2685.9285.9586.11+2.890.19
Proxy-NCA83.7483.6983.6283.3283.0683.49-0.29
MemVir + Proxy-NCA86.8485.9386.2785.9385.9986.19+2.710.39

RP

Loss12345MeanDiff.Std.
Norm-SoftMax35.8535.5135.2835.2835.2435.43-0.26
MemVir + Norm-SoftMax38.6138.2738.3138.0238.2538.29+2.860.21
Proxy-NCA36.0835.8535.7935.6635.6635.81-0.17
MemVir + Proxy-NCA38.1138.0237.9037.7938.0337.97+2.160.13

MAP

Loss12345MeanDiff.Std.
Norm-SoftMax25.5625.5625.0024.9324.9025.19-0.34
MemVir + Norm-SoftMax28.7328.4728.5328.1228.4428.46+3.270.22
Proxy-NCA25.6625.5225.3725.3625.3325.45-0.14
MemVir + Proxy-NCA28.4028.0428.1028.1628.1428.17+2.720.14

Reference

Citation

If you find MemVir useful in your research, please consider to cite the following paper.

@inproceedings{ko2021learning,
    title={Learning with Memory-based Virtual Classes for Deep Metric Learning},
    author={Byungsoo Ko, Geonmo Gu, and Han-Gyu Kim},
    booktitle={Proceedings of the IEEE/CVF International Conference on Computer Vision (ICCV)},
    year={2021}
}

License

Copyright 2021-present NAVER Corp.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.