-
Notifications
You must be signed in to change notification settings - Fork 50
/
selftraining.py
291 lines (252 loc) · 11.4 KB
/
selftraining.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: Cheng Wang, L.Song
"""
from __future__ import print_function, absolute_import
import argparse
import time
import os.path as osp
import os
import numpy as np
import torch
from torch import nn
from torch.nn import init
from torch.backends import cudnn
from torch.utils.data import DataLoader
from reid import datasets
from reid import models
from reid.dist_metric import DistanceMetric
from reid.loss import TripletLoss
from reid.trainers import Trainer
from reid.evaluators import Evaluator, extract_features
from reid.utils.data import transforms as T
from reid.utils.data.preprocessor import Preprocessor
from reid.utils.data.sampler import RandomIdentitySampler
from reid.utils.serialization import load_checkpoint, save_checkpoint
from sklearn.cluster import DBSCAN
from reid.rerank import re_ranking
def get_data(name, data_dir, height, width, batch_size,
workers):
root = osp.join(data_dir, name)
dataset = datasets.create(name, root, num_val=0.1)
normalizer = T.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
# use all training and validation images in target dataset
train_set = dataset.trainval
num_classes = dataset.num_trainval_ids
transformer = T.Compose([
T.Resize((height,width)),
T.ToTensor(),
normalizer,
])
extfeat_loader = DataLoader(
Preprocessor(train_set, root=dataset.images_dir,
transform=transformer),
batch_size=batch_size, num_workers=workers,
shuffle=False, pin_memory=True)
test_loader = DataLoader(
Preprocessor(list(set(dataset.query) | set(dataset.gallery)),
root=dataset.images_dir, transform=transformer),
batch_size=batch_size, num_workers=workers,
shuffle=False, pin_memory=True)
return dataset, num_classes, extfeat_loader, test_loader
def get_source_data(name, data_dir, height, width, batch_size,
workers):
root = osp.join(data_dir, name)
dataset = datasets.create(name, root, num_val=0.1)
normalizer = T.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
# use all training images on source dataset
train_set = dataset.train
num_classes = dataset.num_train_ids
transformer = T.Compose([
T.Resize((height,width)),
T.ToTensor(),
normalizer,
])
extfeat_loader = DataLoader(
Preprocessor(train_set, root=dataset.images_dir,
transform=transformer),
batch_size=batch_size, num_workers=workers,
shuffle=False, pin_memory=True)
return dataset, extfeat_loader
def main(args):
np.random.seed(args.seed)
torch.manual_seed(args.seed)
cudnn.benchmark = True
# Create data loaders
assert args.num_instances > 1, "num_instances should be greater than 1"
assert args.batch_size % args.num_instances == 0, \
'num_instances should divide batch_size'
if args.height is None or args.width is None:
args.height, args.width = (144, 56) if args.arch == 'inception' else \
(256, 128)
# get source data
src_dataset, src_extfeat_loader = \
get_source_data(args.src_dataset, args.data_dir, args.height,
args.width, args.batch_size, args.workers)
# get target data
tgt_dataset, num_classes, tgt_extfeat_loader, test_loader = \
get_data(args.tgt_dataset, args.data_dir, args.height,
args.width, args.batch_size, args.workers)
# Create model
# Hacking here to let the classifier be the number of source ids
if args.src_dataset == 'dukemtmc':
model = models.create(args.arch, num_classes=632, pretrained=False)
elif args.src_dataset == 'market1501':
model = models.create(args.arch, num_classes=676, pretrained=False)
else:
raise RuntimeError('Please specify the number of classes (ids) of the network.')
# Load from checkpoint
if args.resume:
print('Resuming checkpoints from finetuned model on another dataset...\n')
checkpoint = load_checkpoint(args.resume)
model.load_state_dict(checkpoint['state_dict'], strict=False)
else:
raise RuntimeWarning('Not using a pre-trained model.')
model = nn.DataParallel(model).cuda()
# Distance metric
metric = DistanceMetric(algorithm=args.dist_metric)
# Evaluator
evaluator = Evaluator(model, print_freq=args.print_freq)
print("Test with the original model trained on target domain (direct transfer):")
evaluator.evaluate(test_loader, tgt_dataset.query, tgt_dataset.gallery)
if args.evaluate:
return
# Criterion
criterion = [
TripletLoss(args.margin, args.num_instances).cuda(),
TripletLoss(args.margin, args.num_instances).cuda(),
]
# Optimizer
optimizer = torch.optim.SGD(
model.parameters(), lr=args.lr, momentum=0.9,
)
# training stage transformer on input images
normalizer = T.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
train_transformer = T.Compose([
T.Resize((args.height,args.width)),
T.RandomHorizontalFlip(),
T.ToTensor(),
normalizer,
T.RandomErasing(probability=0.5, sh=0.2, r1=0.3)
])
# Start training
for iter_n in range(args.iteration):
if args.lambda_value == 0:
source_features = 0
else:
# get source datas' feature
source_features, _ = extract_features(model, src_extfeat_loader, print_freq=args.print_freq)
# synchronization feature order with src_dataset.train
source_features = torch.cat([source_features[f].unsqueeze(0) for f, _, _ in src_dataset.train], 0)
# extract training images' features
print('Iteration {}: Extracting Target Dataset Features...'.format(iter_n+1))
target_features, _ = extract_features(model, tgt_extfeat_loader, print_freq=args.print_freq)
# synchronization feature order with dataset.train
target_features = torch.cat([target_features[f].unsqueeze(0) for f, _, _ in tgt_dataset.trainval], 0)
# calculate distance and rerank result
print('Calculating feature distances...')
target_features = target_features.numpy()
rerank_dist = re_ranking(
source_features, target_features, lambda_value=args.lambda_value)
if iter_n==0:
# DBSCAN cluster
tri_mat = np.triu(rerank_dist, 1) # tri_mat.dim=2
tri_mat = tri_mat[np.nonzero(tri_mat)] # tri_mat.dim=1
tri_mat = np.sort(tri_mat,axis=None)
top_num = np.round(args.rho*tri_mat.size).astype(int)
eps = tri_mat[:top_num].mean()
print('eps in cluster: {:.3f}'.format(eps))
cluster = DBSCAN(eps=eps,min_samples=4,metric='precomputed', n_jobs=8)
# select & cluster images as training set of this epochs
print('Clustering and labeling...')
labels = cluster.fit_predict(rerank_dist)
num_ids = len(set(labels)) - 1
print('Iteration {} have {} training ids'.format(iter_n+1, num_ids))
# generate new dataset
new_dataset = []
for (fname, _, _), label in zip(tgt_dataset.trainval, labels):
if label==-1:
continue
# dont need to change codes in trainer.py _parsing_input function and sampler function after add 0
new_dataset.append((fname,label,0))
print('Iteration {} have {} training images'.format(iter_n+1, len(new_dataset)))
train_loader = DataLoader(
Preprocessor(new_dataset, root=tgt_dataset.images_dir,
transform=train_transformer),
batch_size=args.batch_size, num_workers=4,
sampler=RandomIdentitySampler(new_dataset, args.num_instances),
pin_memory=True, drop_last=True)
# train model with new generated dataset
trainer = Trainer(model, criterion, print_freq=args.print_freq)
evaluator = Evaluator(model, print_freq=args.print_freq)
# Start training
for epoch in range(args.epochs):
trainer.train(epoch, train_loader, optimizer)
# Evaluate
rank_score = evaluator.evaluate(
test_loader, tgt_dataset.query, tgt_dataset.gallery)
return (rank_score.map, rank_score.market1501[0])
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Triplet loss classification")
# data
parser.add_argument('--src_dataset', type=str, default='dukemtmc',
choices=datasets.names())
parser.add_argument('--tgt_dataset', type=str, default='market1501',
choices=datasets.names())
parser.add_argument('--batch_size', type=int, default=128)
parser.add_argument('--workers', type=int, default=4)
parser.add_argument('--split', type=int, default=0)
parser.add_argument('--height', type=int,
help="input height, default: 256 for resnet*, "
"144 for inception")
parser.add_argument('--width', type=int,
help="input width, default: 128 for resnet*, "
"56 for inception")
parser.add_argument('--combine-trainval', action='store_true',
help="train and val sets together for training, "
"val set alone for validation")
parser.add_argument('--num_instances', type=int, default=4,
help="each minibatch consist of "
"(batch_size // num_instances) identities, and "
"each identity has num_instances instances, "
"default: 4")
# model
parser.add_argument('--arch', type=str, default='resnet50',
choices=models.names())
# loss
parser.add_argument('--margin', type=float, default=0.5,
help="margin of the triplet loss, default: 0.5")
parser.add_argument('--lambda_value', type=float, default=0.1,
help="balancing parameter, default: 0.1")
parser.add_argument('--rho', type=float, default=1.6e-3,
help="rho percentage, default: 1.6e-3")
# optimizer
parser.add_argument('--lr', type=float, default=6e-5,
help="learning rate of all parameters")
# training configs
parser.add_argument('--resume', type=str, metavar='PATH',
default = '')
parser.add_argument('--evaluate', type=int, default=0,
help="evaluation only")
parser.add_argument('--seed', type=int, default=1)
parser.add_argument('--print_freq', type=int, default=1)
parser.add_argument('--iteration', type=int, default=30)
parser.add_argument('--epochs', type=int, default=70)
# metric learning
parser.add_argument('--dist_metric', type=str, default='euclidean',
choices=['euclidean', 'kissme'])
# misc
parser.add_argument('--data_dir', type=str, metavar='PATH',
default='')
parser.add_argument('--logs_dir', type=str, metavar='PATH',
default='')
args = parser.parse_args()
mean_ap, rank1 = main(args)
results_file = np.asarray([mean_ap, rank1])
file_name = time.strftime("%H%M%S", time.localtime())
file_name = osp.join(args.logs_dir, file_name)
np.save(file_name, results_file)