-
Notifications
You must be signed in to change notification settings - Fork 2
/
DCDH.py
285 lines (236 loc) · 11 KB
/
DCDH.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import torch
import torch.optim as optim
import os
import time
import utils.evaluate as evaluate
from scipy.linalg import hadamard
from loguru import logger
from data.data_loader import sample_dataloader
from utils import AverageMeter
import models.dcdh as dcdh
from utils.tools import compute_result, CalcTopMap
from tqdm import tqdm
import random
import numpy as np
from torch import nn
import torch.nn.functional as F
def Log(x):
"""
Log trick for numerical stability
"""
lt = torch.log(1+torch.exp(-torch.abs(x))) + torch.max(x, torch.tensor([0.]).cuda())
return lt
class DualClasswiseLoss(nn.Module):
def __init__(self, num_classes, feat_dim, inner_param=0.1, sigma=0.25, use_gpu=True):
super(DualClasswiseLoss, self).__init__()
self.num_classes = num_classes
self.feat_dim = feat_dim
self.use_gpu = use_gpu
self.sigma = sigma
self.inner_param = inner_param
self.centers = nn.Parameter(torch.randn(self.num_classes, self.feat_dim).cuda())
def forward(self, x, labels):
"""
Args:
x: shape of (batch_size, feat_dim).
labels: shape of (batch_size, ) or (batch_size, 1)
"""
# compute L_1 with single constraint.
batch_size = x.size(0)
distmat = torch.pow(x, 2).sum(dim=1, keepdim=True).expand(batch_size, self.num_classes) + \
torch.pow(self.centers, 2).sum(dim=1, keepdim=True).expand(self.num_classes, batch_size).t()
distmat.addmm_(x, self.centers.t(), beta=1, alpha=-2)
dist_div = torch.exp(-0.5*self.sigma*distmat)/(torch.exp(-0.5*self.sigma*distmat).sum(dim=1, keepdim=True) + 1e-6)
classes = torch.arange(self.num_classes).long()
if self.use_gpu:
classes = classes.cuda()
labels = labels.view(-1, 1).expand(batch_size, self.num_classes)
mask = labels.eq(classes.expand(batch_size, self.num_classes))
dist_log = torch.log(dist_div+1e-6) * mask.float()
loss = -dist_log.sum() / batch_size
# compute L_2 with inner constraint on class centers.
centers_norm = F.normalize(self.centers, dim=1)
theta_x = 0.5 * self.feat_dim * centers_norm.mm(centers_norm.t())
mask = torch.eye(self.num_classes, self.num_classes).bool().cuda()
theta_x.masked_fill_(mask, 0)
loss_iner = Log(theta_x).sum() / (self.num_classes*(self.num_classes-1))
loss_full = loss + self.inner_param * loss_iner
return loss_full
def train(
test_loader,
train_loader,
database_loader,
query_loader_zs,
database_loader_zs,
code_length,
args,
):
"""
Training model.
Args
test_loader, database_loader(torch.utils.data.dataloader.DataLoader): Data loader.
code_length(int): Hashing code length.
args.device(torch.args.device): GPU or CPU.
lr(float): Learning rate.
Returns
mAP(float): Mean Average Precision.
"""
# Initialization
# model = alexnet.load_model(code_length).to(args.device)
if args.max_iter == 50:
text_step=45
elif args.max_iter == 40:
text_step =35
device = args.device
args.num_train = len(train_loader.dataset)
# args.step_continuation = 20
print("DCDH for zero shot")
model = dcdh.dcdh(code_length,pretrained=args.pretrain).to(device)
print('backbone is resnet50')
print(f'Total number of parameters: {sum(p.numel() for p in model.parameters() if p.requires_grad)}')
from thop import profile
input = torch.randn(1, 3, 224, 224)
target_label = 0
target = torch.tensor([target_label])
one_hot = torch.nn.functional.one_hot(target, num_classes=190)
flops, params = profile(model, inputs=(input))
print(f'FLOPs: {flops}')
model.to(device)
if args.optim == 'SGD':
optimizer = optim.SGD(model.parameters(), lr=args.lr, weight_decay=args.wd, momentum=args.momen, nesterov=args.nesterov)
elif args.optim == 'Adam':
optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.wd)
elif args.optim == 'AdamW':
optimizer = optim.AdamW(model.parameters(), lr=args.lr, weight_decay=args.wd)
scheduler = optim.lr_scheduler.MultiStepLR(optimizer, args.lr_step)
# criterion = DualClasswiseLoss(args, code_length)
criterion = DualClasswiseLoss(num_classes=args.num_classes, inner_param=0.1, sigma=0.25, feat_dim=code_length, use_gpu=True)
losses = AverageMeter()
start = time.time()
best_mAP = 0
corresponding_mAP_all = 0
corresponding_zs_mAP = 0
corresponding_zs_mAP_all = 0
print("start training")#*******************************************wp
for it in range(args.max_iter):
iter_start = time.time()
# Sample training data for cnn learning
train_dataloader, sample_index = sample_dataloader(train_loader, args.num_samples, args.batch_size, args.root, args.dataset)
for epoch in range(args.max_epoch):
# epoch_start = time.time()
# criterion.scale = (epoch // args.step_continuation + 1) ** 0.5
model.train()
losses.reset()
pbar = tqdm(enumerate(train_dataloader),total=len(train_dataloader),ncols = 50)
# print((len(train_dataloader)))
for batch, (data, targets, index) in pbar:
# for batch, (data, targets, index) in enumerate(train_dataloader):
# print(targets.shape)
data, targets, index = data.to(device), targets.to(device), index.to(device)
labels = torch.argmax(targets, dim=1)
optimizer.zero_grad()
u = model(data)
loss_dual = criterion(u, labels)
hash_binary = torch.sign(u)
targets = targets.float()
W = torch.pinverse(targets.t() @ targets) @ targets.t() @ hash_binary # Update W
eta = 0.01
batchB = torch.sign(torch.mm(targets, W) + eta * u) # Update B
loss_vertex = (u - batchB).pow(2).sum() / len(data) # quantization loss
loss = loss_dual + eta * loss_vertex
losses.update(loss.item())
loss.backward()
optimizer.step()
logger.info('[epoch:{}/{}][loss:{:.6f}]'.format(epoch+1, args.max_epoch, losses.avg))
scheduler.step()
logger.info('[iter:{}/{}][iter_time:{:.2f}]'.format(it+1, args.max_iter,
time.time()-iter_start))
if (it < text_step and (it + 1) % args.val_freq == 0) or (it >= text_step and (it + 1) % 1 == 0):
# if (it + 1) % 1 == 0 :
query_code = generate_code(model, test_loader, code_length, args.device)
query_targets = test_loader.dataset.get_onehot_targets()
B = generate_code(model, database_loader, code_length, args.device)
db_label= database_loader.dataset.get_onehot_targets()
# if args.num_zs != 0:
zs_test_binary = generate_code(model, query_loader_zs, code_length, args.device)
zs_test_label = query_loader_zs.dataset.get_onehot_targets()
zs_db_binary = generate_code(model, database_loader_zs, code_length, args.device)
zs_db_label = database_loader_zs.dataset.get_onehot_targets()
db_all_binary = torch.cat((B, zs_db_binary), 0)
db_all_label = torch.cat((db_label, zs_db_label), 0)
mAP = evaluate.mean_average_precision(
query_code.to(args.device),
B,
query_targets[:,:args.num_classes].to(args.device),
db_label[:,:args.num_classes].to(args.device),
args.device,
args.topk,
)
# if args.num_zs != 0:
mAP_all = evaluate.mean_average_precision(
query_code.to(args.device),
db_all_binary.to(args.device),
query_targets.to(args.device),
db_all_label.to(args.device),
args.device,
args.topk,
)
zs_mAP_all = evaluate.mean_average_precision(
zs_test_binary.to(args.device),
db_all_binary.to(args.device),
zs_test_label.to(args.device),
db_all_label.to(args.device),
args.device,
args.topk,
)
zs_mAP = evaluate.mean_average_precision(
zs_test_binary.to(args.device),
zs_db_binary.to(args.device),
zs_test_label.to(args.device),
zs_db_label.to(args.device),
args.device,
args.topk,
)
if mAP > best_mAP:
best_mAP = mAP
corresponding_mAP_all = mAP_all
corresponding_zs_mAP = zs_mAP
corresponding_zs_mAP_all = zs_mAP_all
ret_path = os.path.join('checkpoints', args.info, 'best_mAP',str(code_length))
if not os.path.exists(ret_path):
os.makedirs(ret_path)
torch.save(query_code.cpu(), os.path.join(ret_path, 'query_code.t'))
torch.save(B.cpu(), os.path.join(ret_path, 'database_code.t'))
torch.save(query_targets.cpu(), os.path.join(ret_path, 'query_targets.t'))
torch.save(db_label.cpu(), os.path.join(ret_path, 'database_targets.t'))
torch.save(zs_test_binary.cpu(), os.path.join(ret_path, 'zs_test_binary.t'))
torch.save(zs_db_binary.cpu(), os.path.join(ret_path, 'zs_db_binary.t'))
torch.save(zs_test_label.cpu(), os.path.join(ret_path, 'zs_test_label.t'))
torch.save(zs_db_label.cpu(), os.path.join(ret_path, 'zs_db_label.t'))
torch.save(model.state_dict(), os.path.join(ret_path, 'model.pkl'))
model = model.to(args.device)
logger.info('[iter:{}/{}][code_length:{}][mAP:{:.5f}][mAP_all:{:.5f}][best_mAP:{:.5f}]'.format(it+1, args.max_iter, code_length, mAP,mAP_all ,best_mAP))
logger.info('[iter:{}/{}][code_length:{}][zs_mAP:{:.5f}][zs_mAP_all:{:.5f}]'.format(it+1, args.max_iter, code_length, zs_mAP, zs_mAP_all))
logger.info('[Training time:{:.2f}]'.format(time.time()-start))
return best_mAP, corresponding_mAP_all, corresponding_zs_mAP, corresponding_zs_mAP_all
def generate_code(model, dataloader, code_length, device):
# query_dataloader wp
"""
Generate hash code
Args
dataloader(torch.utils.data.DataLoader): Data loader.
code_length(int): Hash code length.
device(torch.device): Using gpu or cpu.
Returns
code(torch.Tensor): Hash code.
"""
model.eval()
with torch.no_grad():
N = len(dataloader.dataset)
code = torch.zeros([N, code_length]).to(device)
for data, _, index in dataloader:
data = data.to(device)
hash_code = model(data)
code[index, :] = hash_code.sign()
model.train()
return code