-
Notifications
You must be signed in to change notification settings - Fork 4
/
baseline_train.py
163 lines (137 loc) · 6.69 KB
/
baseline_train.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
import torch
import torch.optim as optim
import os
import time
import utils.evaluate as evaluate
import models.resnet as resnet
from tqdm import tqdm
from loguru import logger
from models.ADSH_Loss import ADSH_Loss
from data.data_loader import sample_dataloader
from utils import AverageMeter
import models.baseline as baseline
def train(query_dataloader, train_dataloader, retrieval_dataloader, code_length, args):
num_classes, att_size, feat_size = args.num_classes, code_length, 2048
model = baseline.baseline(code_length=code_length, num_classes=num_classes, att_size=att_size, feat_size=feat_size,
device=args.device, pretrained=True)
model.to(args.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)
scheduler = optim.lr_scheduler.MultiStepLR(optimizer, args.lr_step)
criterion = ADSH_Loss(code_length, args.gamma)
num_retrieval = len(retrieval_dataloader.dataset)
U = torch.zeros(args.num_samples, code_length).to(args.device)
B = torch.randn(num_retrieval, code_length).to(args.device)
retrieval_targets = retrieval_dataloader.dataset.get_onehot_targets().to(args.device)
cnn_losses, hash_losses, quan_losses = AverageMeter(), AverageMeter(), AverageMeter()
start = time.time()
best_mAP = 0
for it in range(args.max_iter):
iter_start = time.time()
# Sample training data for cnn learning
train_dataloader, sample_index = sample_dataloader(retrieval_dataloader, args.num_samples, args.batch_size, args.root, args.dataset)
# Create Similarity matrix
train_targets = train_dataloader.dataset.get_onehot_targets().to(args.device)
S = (train_targets @ retrieval_targets.t() > 0).float()
S = torch.where(S == 1, torch.full_like(S, 1), torch.full_like(S, -1))
# Soft similarity matrix, benefit to converge
r = S.sum() / (1 - S).sum()
S = S * (1 + r) - r
# Training CNN model
for epoch in range(args.max_epoch):
cnn_losses.reset()
hash_losses.reset()
quan_losses.reset()
pbar = tqdm(enumerate(train_dataloader),total=len(train_dataloader))
# print((len(train_dataloader)))
for batch, (data, targets, index) in pbar:
data, targets, index = data.to(args.device), targets.to(args.device), index.to(args.device)
optimizer.zero_grad()
F = model(data, targets)
U[index, :] = F.data
cnn_loss, hash_loss, quan_loss = criterion(F, B, S[index, :], sample_index[index])
cnn_losses.update(cnn_loss.item())
hash_losses.update(hash_loss.item())
quan_losses.update(quan_loss.item())
cnn_loss.backward()
optimizer.step()
# print(optimizer.param_groups[0]['lr'])
logger.info('[epoch:{}/{}][cnn_loss:{:.6f}][hash_loss:{:.6f}][quan_loss:{:.6f}]'.format(epoch+1, args.max_epoch, cnn_losses.avg, hash_losses.avg, quan_losses.avg))
scheduler.step()
# Update B
expand_U = torch.zeros(B.shape).to(args.device)
expand_U[sample_index, :] = U
B = solve_dcc(B, U, expand_U, S, code_length, args.gamma)
# Total loss
iter_loss = calc_loss(U, B, S, code_length, sample_index, args.gamma)
logger.info('[iter:{}/{}][loss:{:.6f}][iter_time:{:.2f}]'.format(it+1, args.max_iter, iter_loss, time.time()-iter_start))
# Evaluate
if (it<30 and (it+1)%5==0) or (it>=30 and (it+1)%1==0):
query_code = generate_code(model, query_dataloader, code_length, args.device)
# print(len(query_dataloader))
mAP = evaluate.mean_average_precision(
query_code.to(args.device),
B,
query_dataloader.dataset.get_onehot_targets().to(args.device),
retrieval_targets,
args.device,
args.topk,
)
logger.info(
'[iter:{}/{}][code_length:{}][mAP:{:.5f}]'.format(it + 1, args.max_iter, code_length,
mAP))
if mAP > best_mAP:
best_mAP = mAP
ret_path = os.path.join('checkpoints', args.info, 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_dataloader.dataset.get_onehot_targets, os.path.join(ret_path, 'query_targets.t'))
torch.save(retrieval_targets.cpu(), os.path.join(ret_path, 'database_targets.t'))
torch.save(model.state_dict(), os.path.join(ret_path, 'model.pkl'))
logger.info('[iter:{}/{}][code_length:{}][mAP:{:.5f}][best_mAP:{:.5f}]'.format(it+1, args.max_iter, code_length, mAP, best_mAP))
logger.info('[Training time:{:.2f}]'.format(time.time()-start))
return best_mAP
def solve_dcc(B, U, expand_U, S, code_length, gamma):
"""
Solve DCC problem.
"""
Q = (code_length * S).t() @ U + gamma * expand_U
for bit in range(code_length):
q = Q[:, bit]
u = U[:, bit]
B_prime = torch.cat((B[:, :bit], B[:, bit+1:]), dim=1)
U_prime = torch.cat((U[:, :bit], U[:, bit+1:]), dim=1)
B[:, bit] = (q.t() - B_prime @ U_prime.t() @ u.t()).sign()
return B
def calc_loss(U, B, S, code_length, omega, gamma):
"""
Calculate loss.
"""
hash_loss = ((code_length * S - U @ B.t()) ** 2).sum()
quantization_loss = ((U - B[omega, :]) ** 2).sum()
loss = (hash_loss + gamma * quantization_loss) / (U.shape[0] * B.shape[0])
return loss.item()
def generate_code(model, dataloader, code_length, device):
"""
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 batch, (data, targets, index) in enumerate(dataloader):
data, targets, index = data.to(device), targets.to(device), index.to(device)
hash_code = model(data, targets)
code[index, :] = hash_code.sign()
model.train()
return code