-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlthNet.py
265 lines (224 loc) · 8.95 KB
/
lthNet.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
import time
import torch
import torch.nn as nn
import torch.optim as optim
from loguru import logger
from torch.optim.lr_scheduler import CosineAnnealingLR
from models.model_loader import load_model
from utils.evaluate import mean_average_precision
def train(
train_dataloader,
query_dataloader,
retrieval_dataloader,
arch,
feature_dim,
code_length,
num_classes,
dynamic_meta_embedding,
num_prototypes,
device,
lr,
max_iter,
beta,
gamma,
mapping,
topk,
evaluate_interval,
):
"""
Training model.
Args
train_dataloader, query_dataloader, retrieval_dataloader(torch.utils.data.dataloader.DataLoader): Data loader.
arch(str): CNN model name.
code_length(int): Hash code length.
device(torch.device): GPU or CPU.
lr(float): Learning rate.
max_iter(int): Number of iterations.
alpha(float): Hyper-parameters.
topk(int): Compute top k map.
evaluate_interval(int): Interval of evaluation.
Returns
checkpoint(dict): Checkpoint.
"""
# Load model
model = load_model(arch, feature_dim, code_length, num_classes, num_prototypes).to(device)
# Create criterion, optimizer, scheduler
criterion = LTHNetLoss()
optimizer = optim.RMSprop(
model.parameters(),
lr=lr,
weight_decay=5e-4,
)
scheduler = CosineAnnealingLR(
optimizer,
max_iter,
lr / 100,
)
# Initialization
running_loss = 0.
best_map = 0.
training_time = 0.
prototypes = torch.zeros([num_prototypes, feature_dim])
prototypes = prototypes.to(device)
# Training
for it in range(max_iter):
# update prototypes
prototypes = generate_prototypes(model, train_dataloader, num_prototypes, feature_dim, device,
dynamic_meta_embedding, prototypes)
prototypes = prototypes.to(device)
model.train()
tic = time.time()
for data, targets, index in train_dataloader:
data, targets, index = data.to(device), targets.to(device), index.to(device)
optimizer.zero_grad()
#
hashcodes, assignments, _ = model(data, dynamic_meta_embedding, prototypes)
loss = criterion(hashcodes, assignments, targets, device, beta, gamma, mapping, it, max_iter)
running_loss = running_loss + loss.item()
loss.backward()
optimizer.step()
# update step
scheduler.step()
training_time = time.time() - tic
# Evaluate
if it % evaluate_interval == evaluate_interval - 1:
# Generate hash code
query_code, query_assignment = generate_code(model, query_dataloader, code_length, num_classes, device,
dynamic_meta_embedding, prototypes)
retrieval_code, retrieval_assignment = generate_code(model, retrieval_dataloader, code_length, num_classes,
device,
dynamic_meta_embedding,
prototypes)
query_targets = query_dataloader.dataset.get_onehot_targets()
retrieval_targets = retrieval_dataloader.dataset.get_onehot_targets()
# Compute map
mAP = mean_average_precision(
query_code.to(device),
retrieval_code.to(device),
query_targets.to(device),
retrieval_targets.to(device),
device,
topk,
)
# # Compute pr curve
# P, R = pr_curve(
# query_code.to(device),
# retrieval_code.to(device),
# query_targets.to(device),
# retrieval_targets.to(device),
# device,
# )
# Log
logger.info('[iter:{}/{}][loss:{:.2f}][map:{:.4f}][time:{:.2f}]'.format(
it + 1,
max_iter,
running_loss / evaluate_interval,
mAP,
training_time,
))
running_loss = 0.
# Checkpoint
if best_map < mAP:
best_map = mAP
checkpoint = {
'model': model.state_dict(),
'qB': query_code.cpu(),
'rB': retrieval_code.cpu(),
'qL': query_targets.cpu(),
'rL': retrieval_targets.cpu(),
'qAssignment': query_assignment.cpu(),
'rAssignment': retrieval_assignment.cpu(),
# 'P': P,
# 'R': R,
'map': best_map,
'prototypes': prototypes.cpu(),
'beta': beta,
'gamma': gamma,
'mapping': mapping,
}
return checkpoint
def generate_code(model, dataloader, code_length, num_classes, device, dynamic_meta_embedding, prototypes):
"""
Generate hash code
Args
dataloader(torch.utils.data.dataloader.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])
assignment = torch.zeros([N, num_classes])
for data, _, index in dataloader:
data = data.to(device)
hash_code, class_assignment, _ = model(data, dynamic_meta_embedding, prototypes)
code[index, :] = hash_code.sign().cpu()
assignment[index, :] = class_assignment.cpu()
torch.cuda.empty_cache()
return code, assignment
def generate_prototypes(model, dataloader, num_prototypes, feature_dim, device, dynamic_meta_embedding,
prototypes_placeholder):
"""
Generate prototypes (visual memory)
Args
dataloader(torch.utils.data.dataloader.DataLoader): Data loader.
code_length(int): Hash code length.
device(torch.device): Using gpu or cpu.
Returns
code(torch.Tensor): prototypes.
"""
model.eval()
with torch.no_grad():
prototypes = torch.zeros([num_prototypes, feature_dim])
counter = torch.zeros([num_prototypes])
for data, targets, _ in dataloader:
data, targets = data.to(device), targets.to(device)
_, _, direct_feature = model(data, dynamic_meta_embedding, prototypes_placeholder)
direct_feature = direct_feature.to('cpu')
index = torch.nonzero(targets, as_tuple=False)[:, 1]
index = index.to('cpu')
for j in range(len(data)):
prototypes[index[j], :] = prototypes[index[j], :] + direct_feature[j, :]
counter[index[j]] = counter[index[j]] + 1
for k in range(num_prototypes):
prototypes[k, :] = prototypes[k, :] / counter[k]
torch.cuda.empty_cache()
return prototypes
class LTHNetLoss(nn.Module):
"""
LTHNet loss function.
Args
epoch (float): the current epoch for calculating alpha (balanced or not).
beta (float): class-balanced hyper-parameter
num_per_class mapping: number of samples for each class.
gamma: cross-entropy-loss vs. class-balanced-loss
"""
def __init__(self):
super(LTHNetLoss, self).__init__()
print('Long-Tailed Hashing Loss works!')
def forward(self, hashcodes, assignments, targets, device, beta, gamma, mapping, epoch, maxIter):
# eg. mapping['0']=500, mapping['1']=100, etc.
# -------------------------------------------------------------
batch_size = assignments.size(0)
num_classes = assignments.size(1)
code_length = hashcodes.size(1)
# -------------------------------------------------------------
# mini-batch cross-entropy loss between assignments and targets: softmax-log-NLL-average
# pointwise loss
loss_cross_entropy = torch.sum(- torch.log(assignments) * targets) / batch_size
# balanced factor (class)
balance_factor = torch.zeros([num_classes])
for j in range(len(mapping)):
balance_factor[j] = (1 - beta) / (1 - beta ** mapping[str(j)])
balance_factor = balance_factor / torch.max(balance_factor)
# class-balanced loss
weights = torch.Tensor.repeat(balance_factor, [batch_size, 1]).to(device)
loss_class_balanced = torch.sum(- torch.log(assignments) * targets * weights) / batch_size
# gradual learning
# alpha = 1 - (epoch * 1.0 / maxIter) ** 2
# overall loss
# loss = alpha * loss_cross_entropy + (1 - alpha) * (gamma * loss_class_balanced)
return loss_class_balanced