Code release for "Transfer Adversarial Hashing for Hamming Space Retrieval" (AAAI 2018)
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
data add binaryproto Jun 15, 2018
examples TAH init Jan 21, 2018
include/caffe visda ready Jun 14, 2018
models fix bug Jun 15, 2018
src visda ready Jun 14, 2018
tools TAH init Jan 21, 2018
CMakeLists.txt TAH init Jan 21, 2018 TAH init Jan 21, 2018
Makefile.config.example Update Jun 15, 2018 fix bug Jun 15, 2018


Caffe implementation for "Transfer Adversarial Hashing for Hamming Space Retrieval" (AAAI 2018)


Linux or OSX

NVIDIA GPU + CUDA-7.5 or CUDA-8.0 and corresponding CuDNN


Python 2.7

Modification on Caffe

  • Add "MultiLabelData" layer to process multi-label dataset.
  • Add "PairwiseLoss" layer implementing the t-distribution pairwise loss described in our paper.
  • Add "GradientScaler" layer to inverse and scale the gradient for adversarial learning.


We use NUS-WIDE and Visda datasets in our experiments. You can download the NUS-WIDE dataset here. As for Visda dataset, we use the recognition part of Visual Domain Adaptation Challenge dataset on TASK-CV 2017 Workshop, which can be downloaded here.

You can also modify the list file(txt format) in ./data as you like. Each line in the list file follows the following format:

<image path><space><one hot label representation>


The compiling process is the same as caffe. You can refer to Caffe installation instructions here.


First, you need to download the AlexNet pre-trained model on ImageNet from here and move it to ./models/bvlc_reference_caffenet. Then, you can train the model for each dataset using the followling command.

dataset_name = imagenet, nuswide_81 or coco
./build/tools/caffe train -solver models/train/dataset_name/solver.prototxt -weights ./models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel -gpu gpu_id


You can evaluate the Mean Average Precision(MAP) result on each dataset using the followling command.

python models/predict/ --gpu_id 1,2,3 --nthreads 4 --database_path data/visda/real_database_list.txt --test_path data/visda/real_test_list.txt --code_path real --snapshot_path models/train/caffemodel/synthetic_48bit.caffemodel

We provide some trained models for each dataset for each code length in our experiment for evaluation. You can download them here if you want to use them.

If you have generated the hash code by the previous step or by other method and want to test the MAP of the hash code. You can specify the code_path parameter.

python models/predict/ --code_path real --load_code True


If you use this code for your research, please consider citing:

  author    = {Zhangjie Cao and
               Mingsheng Long and
               Chao Huang and
               Jianmin Wang},
  title     = {Transfer Adversarial Hashing for Hamming Space Retrieval},
  booktitle = {Proceedings of the Thirty-Second {AAAI} Conference on Artificial Intelligence,
               New Orleans, Louisiana, USA, February 2-7, 2018},
  year      = {2018},
  url       = {},
  timestamp = {Thu, 03 May 2018 17:03:19 +0200},
  biburl    = {},
  bibsource = {dblp computer science bibliography,}


If you have any problem about our code, feel free to contact or describe your problem in Issues.