forked from swuxyj/DeepHash-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathADSH.py
161 lines (131 loc) · 5.87 KB
/
ADSH.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from utils.tools import *
from network import *
import os
import torch
import torch.optim as optim
import torch.nn as nn
from tqdm import tqdm
import time
import numpy as np
torch.multiprocessing.set_sharing_strategy('file_system')
# ADSH(AAAI2018)
# paper [Asymmetric Deep Supervised Hashing](https://cs.nju.edu.cn/lwj/paper/AAAI18_ADSH.pdf)
# code1 [ADSH matlab + pytorch](https://github.com/jiangqy/ADSH-AAAI2018)
# code2 [ADSH_pytorch](https://github.com/TreezzZ/ADSH_PyTorch)
def get_config():
config = {
"gamma": 200,
"num_samples": 2000,
"max_iter": 150,
"epoch": 3,
"test_map": 10,
# "optimizer": {"type": optim.SGD, "optim_params": {"lr": 0.001, "weight_decay": 5e-4}},
# "optimizer": {"type": optim.RMSprop, "optim_params": {"lr": 1e-5, "weight_decay": 10 ** -5}},
"optimizer": {"type": optim.Adam, "optim_params": {"lr": 1e-4, "weight_decay": 1e-5}},
"info": "[ADSH]",
"resize_size": 256,
"crop_size": 224,
"batch_size": 64,
"net": AlexNet,
"dataset": "cifar10-1",
# "dataset": "nuswide_21",
"save_path": "save/ADSH",
# "device":torch.device("cpu"),
"device": torch.device("cuda:1"),
"bit_list": [48],
}
# if config["dataset"] == "nuswide_21":
# config["gamma"] = 0
config = config_dataset(config)
return config
def calc_sim(database_label, train_label):
S = (database_label @ train_label.t() > 0).float()
# soft constraint
r = S.sum() / (1 - S).sum()
S = S * (1 + r) - r
return S
def train_val(config, bit):
device = config["device"]
num_samples = config["num_samples"]
train_loader, test_loader, dataset_loader, num_train, num_test, num_dataset = get_data(config)
# get database_labels
clses = []
for _, cls, _ in tqdm(dataset_loader):
clses.append(cls)
database_labels = torch.cat(clses).to(device).float()
net = config["net"](bit).to(device)
optimizer = config["optimizer"]["type"](net.parameters(), **(config["optimizer"]["optim_params"]))
Best_mAP = 0
V = torch.zeros((num_dataset, bit)).to(device)
for iter in range(config["max_iter"]):
current_time = time.strftime('%H:%M:%S', time.localtime(time.time()))
print("%s[%2d/%2d][%s] bit:%d, dataset:%s, training...." % (
config["info"], iter + 1, config["max_iter"], current_time, bit, config["dataset"]), end="")
net.train()
# sampling and construct similarity matrix
select_index = np.random.permutation(range(num_dataset))[0: num_samples]
if "cifar" in config["dataset"]:
train_loader.dataset.data = np.array(dataset_loader.dataset.data)[select_index]
train_loader.dataset.targets = np.array(dataset_loader.dataset.targets)[select_index]
else:
train_loader.dataset.imgs = np.array(dataset_loader.dataset.imgs)[select_index].tolist()
sample_label = database_labels[select_index]
Sim = calc_sim(sample_label, database_labels)
U = torch.zeros((num_samples, bit)).to(device)
train_loss = 0
for epoch in range(config["epoch"]):
for image, label, ind in train_loader:
image = image.to(device)
label = label.to(device).float()
net.zero_grad()
S = calc_sim(label, database_labels)
u = net(image)
u = u.tanh()
U[ind, :] = u.data
square_loss = (u @ V.t() - bit * S).pow(2)
quantization_loss = config["gamma"] * (V[select_index[ind]] - u).pow(2)
loss = (square_loss.sum() + quantization_loss.sum()) / (num_train * u.size(0))
train_loss += loss.item()
loss.backward()
optimizer.step()
train_loss = train_loss / len(train_loader) / epoch
print("\b\b\b\b\b\b\b loss:%.3f" % (train_loss))
# learning binary codes: discrete coding
barU = torch.zeros((num_dataset, bit)).to(device)
barU[select_index, :] = U
# calculate Q
Q = -2 * bit * Sim.t() @ U - 2 * config["gamma"] * barU
for k in range(bit):
sel_ind = np.setdiff1d([ii for ii in range(bit)], k)
V_ = V[:, sel_ind]
U_ = U[:, sel_ind]
Uk = U[:, k]
Qk = Q[:, k]
# formula 10
V[:, k] = -(2 * V_ @ (U_.t() @ Uk) + Qk).sign()
if (iter + 1) % config["test_map"] == 0:
# print("calculating test binary code......")
tst_binary, tst_label = compute_result(test_loader, net, device=device)
# print("calculating dataset binary code.......")
trn_binary, trn_label = compute_result(dataset_loader, net, device=device)
# print("calculating map.......")
mAP = CalcTopMap(trn_binary.numpy(), tst_binary.numpy(), trn_label.numpy(), tst_label.numpy(),
config["topK"])
if mAP > Best_mAP:
Best_mAP = mAP
if "save_path" in config:
if not os.path.exists(config["save_path"]):
os.makedirs(config["save_path"])
print("save in ", config["save_path"])
np.save(os.path.join(config["save_path"], config["dataset"] + str(mAP) + "-" + "trn_binary.npy"),
trn_binary.numpy())
torch.save(net.state_dict(),
os.path.join(config["save_path"], config["dataset"] + "-" + str(mAP) + "-model.pt"))
print("%s epoch:%d, bit:%d, dataset:%s, MAP:%.3f, Best MAP: %.3f" % (
config["info"], iter + 1, bit, config["dataset"], mAP, Best_mAP))
print(config)
if __name__ == "__main__":
config = get_config()
print(config)
for bit in config["bit_list"]:
train_val(config, bit)