forked from zjy2050/HDCRL-ReID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_usl.py
361 lines (305 loc) · 16.1 KB
/
train_usl.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
from __future__ import print_function, absolute_import
import argparse
import os.path as osp
import random
import numpy as np
import sys
import collections
import copy
import time
from datetime import timedelta
from sklearn.cluster import DBSCAN
import torch
from torch import nn
from torch.backends import cudnn
from torch.utils.data import DataLoader
import torch.nn.functional as F
from hdcrl import datasets
from hdcrl import models
from hdcrl.models.hm import HybridMemory
from hdcrl.trainers import Trainer_USL
from hdcrl.evaluators import Evaluator, extract_features
from hdcrl.utils.data import IterLoader
from hdcrl.utils.data import transforms as T
from hdcrl.utils.data.sampler import RandomMultipleGallerySampler
from hdcrl.utils.data.preprocessor import Preprocessor
from hdcrl.utils.logging import Logger
from hdcrl.utils.serialization import load_checkpoint, save_checkpoint, copy_state_dict
from hdcrl.utils.faiss_rerank import compute_jaccard_distance
start_epoch = best_mAP = 0
def get_data(name, data_dir):
root = osp.join(data_dir, name)
dataset = datasets.create(name, root)
return dataset
def get_train_loader(args, dataset, height, width, batch_size, workers,
num_instances, iters, trainset=None):
normalizer = T.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
train_transformer = T.Compose([
T.Resize((height, width), interpolation=3),
T.RandomHorizontalFlip(p=0.5),
T.Pad(10),
T.RandomCrop((height, width)),
T.ToTensor(),
normalizer,
T.RandomErasing(probability=0.5, mean=[0.485, 0.456, 0.406])
])
train_set = sorted(dataset.train) if trainset is None else sorted(trainset)
rmgs_flag = num_instances > 0
if rmgs_flag:
sampler = RandomMultipleGallerySampler(train_set, num_instances)
else:
sampler = None
train_loader = IterLoader(
DataLoader(Preprocessor(train_set, root=dataset.images_dir, transform=train_transformer, mutual=True),
batch_size=batch_size, num_workers=workers, sampler=sampler,
shuffle=not rmgs_flag, pin_memory=True, drop_last=True), length=iters)
return train_loader
def get_test_loader(dataset, height, width, batch_size, workers, testset=None):
normalizer = T.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
test_transformer = T.Compose([
T.Resize((height, width), interpolation=3),
T.ToTensor(),
normalizer
])
if (testset is None):
testset = list(set(dataset.query) | set(dataset.gallery))
test_loader = DataLoader(
Preprocessor(testset, root=dataset.images_dir, transform=test_transformer),
batch_size=batch_size, num_workers=workers,
shuffle=False, pin_memory=True)
return test_loader
def create_model(args):
model = models.create(args.arch, num_features=args.features, norm=True, dropout=args.dropout, num_classes=0)
model_ema = models.create(args.arch, num_features=args.features, norm=True, dropout=args.dropout, num_classes=0)
# use CUDA
model.cuda()
model_ema.cuda()
model = nn.DataParallel(model)
model_ema = nn.DataParallel(model_ema)
return model, model_ema
def main():
args = parser.parse_args()
if args.seed is not None:
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
cudnn.deterministic = True
main_worker(args)
def main_worker(args):
global start_epoch, best_mAP
start_time = time.monotonic()
cudnn.benchmark = True
sys.stdout = Logger(osp.join(args.logs_dir, 'log.txt'))
print("==========\nArgs:{}\n==========".format(args))
# Create datasets
iters = args.iters if (args.iters>0) else None
print("==> Load unlabeled dataset")
dataset = get_data(args.dataset, args.data_dir)
test_loader = get_test_loader(dataset, args.height, args.width, args.batch_size, args.workers)
# Create model
model, model_ema = create_model(args)
# Create hybrid memory
memory = HybridMemory(model.module.num_features, len(dataset.train),
temp=args.temp, momentum=args.momentum).cuda()
# Initialize target-domain instance features
print("==> Initialize instance features in the hybrid memory")
cluster_loader = get_test_loader(dataset, args.height, args.width,
args.batch_size, args.workers, testset=sorted(dataset.train))
features, _ = extract_features(model, cluster_loader, print_freq=50)
features = torch.cat([features[f].unsqueeze(0) for f, _, _ in sorted(dataset.train)], 0)
memory.features = F.normalize(features, dim=1).cuda()
del cluster_loader, features
# Evaluator
evaluator = Evaluator(model)
evaluator_ema = Evaluator(model_ema)
# Optimizer
params = [{"params": [value]} for _, value in model.named_parameters() if value.requires_grad]
optimizer = torch.optim.Adam(params, lr=args.lr, weight_decay=args.weight_decay)
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=args.step_size, gamma=0.1)
# Trainer
trainer = Trainer_USL(model, model_ema, memory)
for epoch in range(args.epochs):
# Calculate distance
print('==> Create pseudo labels for unlabeled data with self-paced policy')
features = memory.features.clone()
rerank_dist = compute_jaccard_distance(features, k1=args.k1, k2=args.k2)
del features
# if (epoch==0):
# # DBSCAN cluster
# eps = args.eps
# eps_tight = eps-args.eps_gap
# eps_loose = eps+args.eps_gap
# print('Clustering criterion: eps: {:.3f}, eps_tight: {:.3f}, eps_loose: {:.3f}'.format(eps, eps_tight, eps_loose))
# cluster = DBSCAN(eps=eps, min_samples=4, metric='precomputed', n_jobs=-1)
# cluster_tight = DBSCAN(eps=eps_tight, min_samples=4, metric='precomputed', n_jobs=-1)
# cluster_loose = DBSCAN(eps=eps_loose, min_samples=4, metric='precomputed', n_jobs=-1)
# # select & cluster images as training set of this epochs
# pseudo_labels = cluster.fit_predict(rerank_dist)
# pseudo_labels_tight = cluster_tight.fit_predict(rerank_dist)
# pseudo_labels_loose = cluster_loose.fit_predict(rerank_dist)
# num_ids = len(set(pseudo_labels)) - (1 if -1 in pseudo_labels else 0)
# num_ids_tight = len(set(pseudo_labels_tight)) - (1 if -1 in pseudo_labels_tight else 0)
# num_ids_loose = len(set(pseudo_labels_loose)) - (1 if -1 in pseudo_labels_loose else 0)
# # generate new dataset and calculate cluster centers
# def generate_pseudo_labels(cluster_id, num):
# labels = []
# outliers = 0
# for i, ((fname, _, cid), id) in enumerate(zip(sorted(dataset.train), cluster_id)):
# if id!=-1:
# labels.append(id)
# else:
# labels.append(num+outliers)
# outliers += 1
# return torch.Tensor(labels).long()
# pseudo_labels = generate_pseudo_labels(pseudo_labels, num_ids)
# pseudo_labels_tight = generate_pseudo_labels(pseudo_labels_tight, num_ids_tight)
# pseudo_labels_loose = generate_pseudo_labels(pseudo_labels_loose, num_ids_loose)
# # compute R_indep and R_comp
# N = pseudo_labels.size(0)
# label_sim = pseudo_labels.expand(N, N).eq(pseudo_labels.expand(N, N).t()).float()
# label_sim_tight = pseudo_labels_tight.expand(N, N).eq(pseudo_labels_tight.expand(N, N).t()).float()
# label_sim_loose = pseudo_labels_loose.expand(N, N).eq(pseudo_labels_loose.expand(N, N).t()).float()
# R_comp = 1-torch.min(label_sim, label_sim_tight).sum(-1)/torch.max(label_sim, label_sim_tight).sum(-1)
# R_indep = 1-torch.min(label_sim, label_sim_loose).sum(-1)/torch.max(label_sim, label_sim_loose).sum(-1)
# assert((R_comp.min()>=0) and (R_comp.max()<=1))
# assert((R_indep.min()>=0) and (R_indep.max()<=1))
# cluster_R_comp, cluster_R_indep = collections.defaultdict(list), collections.defaultdict(list)
# cluster_img_num = collections.defaultdict(int)
# for i, (comp, indep, label) in enumerate(zip(R_comp, R_indep, pseudo_labels)):
# cluster_R_comp[label.item()].append(comp.item())
# cluster_R_indep[label.item()].append(indep.item())
# cluster_img_num[label.item()]+=1
# cluster_R_comp = [min(cluster_R_comp[i]) for i in sorted(cluster_R_comp.keys())]
# cluster_R_indep = [min(cluster_R_indep[i]) for i in sorted(cluster_R_indep.keys())]
# cluster_R_indep_noins = [iou for iou, num in zip(cluster_R_indep, sorted(cluster_img_num.keys())) if cluster_img_num[num]>1]
# if (epoch==0):
# indep_thres = np.sort(cluster_R_indep_noins)[min(len(cluster_R_indep_noins)-1,np.round(len(cluster_R_indep_noins)*0.9).astype('int'))]
# pseudo_labeled_dataset = []
# outliers = 0
# for i, ((fname, _, cid), label) in enumerate(zip(sorted(dataset.train), pseudo_labels)):
# indep_score = cluster_R_indep[label.item()]
# comp_score = R_comp[i]
# if ((indep_score<=indep_thres) and (comp_score.item()<=cluster_R_comp[label.item()])):
# pseudo_labeled_dataset.append((fname,label.item(),cid))
# else:
# pseudo_labeled_dataset.append((fname,len(cluster_R_indep)+outliers,cid))
# pseudo_labels[i] = len(cluster_R_indep)+outliers
# outliers+=1
if (epoch == 0):
eps = args.eps
cluster = DBSCAN(eps=eps, min_samples=4, metric='precomputed', n_jobs=-1)
# select & cluster images as training set of this epochs
pseudo_labels = cluster.fit_predict(rerank_dist)
num_ids = len(set(pseudo_labels)) - (1 if -1 in pseudo_labels else 0)
labels = []
pseudo_labeled_dataset = []
outliers = 0
for i, ((fname, _, cid), label) in enumerate(zip(sorted(dataset.train), pseudo_labels)):
if label != -1:
labels.append(label)
pseudo_labeled_dataset.append((fname, label, cid))
else:
L = num_ids + outliers
labels.append(L)
pseudo_labeled_dataset.append((fname, L, cid))
outliers += 1
pseudo_labels = torch.Tensor(labels).long()
# statistics of clusters and un-clustered instances
index2label = collections.defaultdict(int)
for label in pseudo_labels:
index2label[label.item()]+=1
index2label = np.fromiter(index2label.values(), dtype=float)
print('==> Statistics for epoch {}: {} clusters, {} un-clustered instances'
.format(epoch, (index2label>1).sum(), (index2label==1).sum()))
memory.labels = pseudo_labels.cuda()
train_loader = get_train_loader(args, dataset, args.height, args.width,
args.batch_size, args.workers, args.num_instances, iters,
trainset=pseudo_labeled_dataset)
train_loader.new_epoch()
trainer.train(epoch, train_loader, optimizer, print_freq=args.print_freq, train_iters=len(train_loader))
# if ((epoch+1)%args.eval_step==0 or (epoch==args.epochs-1)):
# mAP = evaluator.evaluate(test_loader, dataset.query, dataset.gallery, cmc_flag=False)
# is_best = (mAP>best_mAP)
# best_mAP = max(mAP, best_mAP)
# save_checkpoint({
# 'state_dict': model.state_dict(),
# 'epoch': epoch + 1,
# 'best_mAP': best_mAP,
# }, is_best, fpath=osp.join(args.logs_dir, 'checkpoint.pth.tar'))
# print('\n * Finished epoch {:3d} model mAP: {:5.1%} best: {:5.1%}{}\n'.
# format(epoch, mAP, best_mAP, ' *' if is_best else ''))
if ((epoch + 1) % args.eval_step == 0 or (epoch == args.epochs - 1)):
mAP_1 = evaluator.evaluate(test_loader, dataset.query, dataset.gallery, cmc_flag=False)
mAP_2 = evaluator_ema.evaluate(test_loader, dataset.query, dataset.gallery, cmc_flag=False)
is_best = (mAP_1 > best_mAP) or (mAP_2 > best_mAP)
best_mAP = max(mAP_1, mAP_2, best_mAP)
save_checkpoint({
'state_dict': model.state_dict(),
'epoch': epoch + 1,
'best_mAP': best_mAP,
}, is_best, fpath=osp.join(args.logs_dir, 'model.pth.tar'))
save_checkpoint({
'state_dict': model_ema.state_dict(),
'epoch': epoch + 1,
'best_mAP': best_mAP,
}, (is_best and (mAP_1 <= mAP_2)), fpath=osp.join(args.logs_dir, 'model_ema.pth.tar'))
print('\n * Finished epoch {:3d} model mAP: {:5.1%} model_ema mAP: {:5.1%} best: {:5.1%}{}\n'.
format(epoch, mAP_1, mAP_2, best_mAP, ' *' if is_best else ''))
lr_scheduler.step()
print ('==> Test with the best model:')
checkpoint = load_checkpoint(osp.join(args.logs_dir, 'model_best.pth.tar'))
model.load_state_dict(checkpoint['state_dict'])
evaluator.evaluate(test_loader, dataset.query, dataset.gallery, cmc_flag=True)
end_time = time.monotonic()
print('Total running time: ', timedelta(seconds=end_time - start_time))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Self-paced contrastive learning on unsupervised re-ID")
# data
parser.add_argument('-d', '--dataset', type=str, default='market1501',
choices=datasets.names())
parser.add_argument('-b', '--batch-size', type=int, default=256)
parser.add_argument('-j', '--workers', type=int, default=4)
parser.add_argument('--height', type=int, default=256, help="input height")
parser.add_argument('--width', type=int, default=128, help="input width")
parser.add_argument('--num-instances', type=int, default=16,
help="each minibatch consist of "
"(batch_size // num_instances) identities, and "
"each identity has num_instances instances, "
"default: 0 (NOT USE)")
# cluster
parser.add_argument('--eps', type=float, default=0.5,
help="max neighbor distance for DBSCAN")
parser.add_argument('--k1', type=int, default=30,
help="hyperparameter for jaccard distance")
parser.add_argument('--k2', type=int, default=6,
help="hyperparameter for jaccard distance")
# model
parser.add_argument('-a', '--arch', type=str, default='resnet50',
choices=models.names())
parser.add_argument('--features', type=int, default=0)
parser.add_argument('--dropout', type=float, default=0)
parser.add_argument('--momentum', type=float, default=0.2,
help="update momentum for the hybrid memory")
# optimizer
parser.add_argument('--lr', type=float, default=0.00035,
help="learning rate")
parser.add_argument('--weight-decay', type=float, default=5e-4)
parser.add_argument('--epochs', type=int, default=70)
parser.add_argument('--iters', type=int, default=100)
parser.add_argument('--step-size', type=int, default=30)
# training configs
parser.add_argument('--seed', type=int, default=1)
parser.add_argument('--print-freq', type=int, default=10)
parser.add_argument('--eval-step', type=int, default=1)
parser.add_argument('--temp', type=float, default=0.05,
help="temperature for scaling contrastive loss")
# path
working_dir = osp.dirname(osp.abspath(__file__))
parser.add_argument('--data-dir', type=str, metavar='PATH',
# default=osp.join(working_dir, 'data'))
default='../data')
parser.add_argument('--logs-dir', type=str, metavar='PATH',
default=osp.join(working_dir, 'logs'))
main()