-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathDAPH.py
170 lines (133 loc) · 6.39 KB
/
DAPH.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
162
163
164
165
166
167
168
169
170
from utils.tools import *
from network import *
import os
import torch
import torch.optim as optim
import time
import numpy as np
torch.multiprocessing.set_sharing_strategy('file_system')
# DAPH(ACM International Conference on Multimedia (MM), 2017 )
# paper [Deep Asymmetric Pairwise Hashing](http://cfm.uestc.edu.cn/~fshen/DAPH.pdf)
def get_config():
config = {
"alpha": 10,
"gamma": 10,
"lambda": 0.01,
"beta": 0.01,
# "optimizer": {"type": optim.SGD, "optim_params": {"lr": 0.0001, "weight_decay": 0.0001}},
# "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": "[DAPH]",
"resize_size": 256,
"crop_size": 224,
"batch_size": 128,
"net": AlexNet,
# "net":ResNet,
# "dataset": "cifar10",
"dataset": "cifar10-1",
# "dataset": "cifar10-2",
# "dataset": "coco",
# "dataset": "mirflickr",
# "dataset": "voc2012",
# "dataset": "imagenet",
# "dataset": "nuswide_21",
# "dataset": "nuswide_21_m",
# "dataset": "nuswide_81_m",
"epoch": 150,
"test_map": 5,
# "device":torch.device("cpu"),
"device": torch.device("cuda:1"),
"bit_list": [48],
}
config = config_dataset(config)
return config
class DAPHLoss(torch.nn.Module):
def __init__(self, config, bit):
super(DAPHLoss, self).__init__()
self.U = torch.zeros(config["num_train"], bit).float().to(config["device"])
self.Z = torch.zeros(config["num_train"], bit).float().to(config["device"])
self.Y = torch.zeros(config["num_train"], config["n_class"]).float().to(config["device"])
self.I = torch.eye(bit).to(config["device"])
self.B = torch.randn(config["num_train"], bit).sign().to(config["device"])
self.H = torch.randn(config["num_train"], bit).sign().to(config["device"])
def forward(self, u, z, y, ind, config, isTop=1):
u = u.tanh()
z = z.tanh()
self.U[ind, :] = u.data
self.Z[ind, :] = z.data
self.Y[ind, :] = y.float()
s = (y @ y.t() > 0).float()
inner_product = u @ z.t() * 0.5
likelihood_loss = (1 + (-(inner_product.abs())).exp()).log() + inner_product.clamp(min=0) - s * inner_product
likelihood_loss = likelihood_loss.mean()
quantization_loss = isTop * (u - self.B[ind]).pow(2) + (1 - isTop) * (z - self.H[ind]).pow(2)
quantization_loss = config["alpha"] * quantization_loss.mean()
independence_loss = isTop * (u.t() @ u / u.shape[0] - self.I).pow(2) + \
(1 - isTop) * (z.t() @ z / z.shape[0] - self.I).pow(2)
independence_loss = config["lambda"] * independence_loss.mean()
balance_loss = isTop * u.sum(dim=0).pow(2) + (1 - isTop) * z.sum(dim=0).pow(2)
balance_loss = config["beta"] * balance_loss.mean()
return likelihood_loss + quantization_loss + independence_loss + balance_loss
def update_B_and_H(self, config):
self.B = (config["alpha"] * self.U + config["gamma"] * self.H).sign()
self.H = (config["alpha"] * self.Z + config["gamma"] * self.B).sign()
def calc_loss(self, config):
s = (self.Y @ self.Y.t() > 0).float()
inner_product = self.U @ self.Z.t() * 0.5
likelihood_loss = (1 + (-(inner_product.abs())).exp()).log() + inner_product.clamp(min=0) - s * inner_product
likelihood_loss = likelihood_loss.mean()
quantization_loss = (self.U - self.B).pow(2) + (self.Z - self.H).pow(2)
quantization_loss = config["alpha"] * quantization_loss.mean()
regularization_loss = config["gamma"] * (self.B - self.H).pow(2).mean()
independence_loss = (self.U.t() @ self.U / self.U.shape[0] - self.I).pow(2) + \
(self.Z.t() @ self.Z / self.Z.shape[0] - self.I).pow(2)
independence_loss = config["lambda"] * independence_loss.mean()
balance_loss = self.U.sum(dim=0).pow(2) + self.Z.sum(dim=0).pow(2)
balance_loss = config["beta"] * balance_loss.mean()
return likelihood_loss + quantization_loss + regularization_loss + independence_loss + balance_loss
def train_val(config, bit):
device = config["device"]
train_loader, test_loader, dataset_loader, num_train, num_test, num_dataset = get_data(config)
config["num_train"] = num_train
net_top = config["net"](bit, pretrained=True).to(device)
net_bottom = config["net"](bit, pretrained=False).to(device)
optimizer_top = config["optimizer"]["type"](net_top.parameters(), **(config["optimizer"]["optim_params"]))
optimizer_bottom = config["optimizer"]["type"](net_bottom.parameters(), **(config["optimizer"]["optim_params"]))
criterion = DAPHLoss(config, bit)
Best_mAP = 0
for epoch in range(config["epoch"]):
current_time = time.strftime('%H:%M:%S', time.localtime(time.time()))
print("%s[%2d/%2d][%s] bit:%d, dataset:%s, training...." % (
config["info"], epoch + 1, config["epoch"], current_time, bit, config["dataset"]), end="")
net_top.train()
net_bottom.eval()
for image, label, ind in train_loader:
image = image.to(device)
label = label.to(device)
optimizer_top.zero_grad()
u = net_top(image)
z = net_bottom(image)
loss = criterion(u, z, label.float(), ind, config)
loss.backward()
optimizer_top.step()
net_top.eval()
net_bottom.train()
for image, label, ind in train_loader:
image = image.to(device)
label = label.to(device)
optimizer_bottom.zero_grad()
u = net_top(image)
z = net_bottom(image)
loss = criterion(u, z, label.float(), ind, config, isTop=0)
loss.backward()
optimizer_bottom.step()
criterion.update_B_and_H(config)
train_loss = criterion.calc_loss(config).item()
print("\b\b\b\b\b\b\b loss:%.3f" % (train_loss))
if (epoch + 1) % config["test_map"] == 0:
Best_mAP = validate(config, Best_mAP, test_loader, dataset_loader, net_bottom, bit, epoch, num_dataset)
if __name__ == "__main__":
config = get_config()
print(config)
for bit in config["bit_list"]:
train_val(config, bit)