-
Notifications
You must be signed in to change notification settings - Fork 55
/
data_manager.py
274 lines (233 loc) · 11.6 KB
/
data_manager.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
from __future__ import absolute_import
from __future__ import print_function
from torch.utils.data import DataLoader
from .dataset_loader import ImageDataset, VideoDataset
from .datasets import init_imgreid_dataset, init_vidreid_dataset
from .transforms import build_transforms
from .samplers import RandomIdentitySampler
class BaseDataManager(object):
@property
def num_train_pids(self):
return self._num_train_pids
@property
def num_train_cams(self):
return self._num_train_cams
def return_dataloaders(self):
"""
Return trainloader and testloader dictionary
"""
return self.trainloader, self.testloader_dict
def return_testdataset_by_name(self, name):
"""
Return query and gallery, each containing a list of (img_path, pid, camid).
"""
return self.testdataset_dict[name]['query'], self.testdataset_dict[name]['gallery'],\
self.testdataset_dict[name]['query_flip'], self.testdataset_dict[name]['gallery_flip']
class ImageDataManager(BaseDataManager):
"""
Image-ReID data manager
"""
def __init__(self,
use_gpu,
source_names,
target_names,
root,
split_id=0,
height=256,
width=128,
train_batch_size=32,
test_batch_size=100,
workers=4,
train_sampler='',
data_augment='none',
num_instances=4, # number of instances per identity (for RandomIdentitySampler)
cuhk03_labeled=False, # use cuhk03's labeled or detected images
cuhk03_classic_split=False # use cuhk03's classic split or 767/700 split
):
super(ImageDataManager, self).__init__()
self.use_gpu = use_gpu
self.source_names = source_names
self.target_names = target_names
self.root = root
self.split_id = split_id
self.height = height
self.width = width
self.train_batch_size = train_batch_size
self.test_batch_size = test_batch_size
self.workers = workers
self.train_sampler = train_sampler
self.num_instances = num_instances
self.cuhk03_labeled = cuhk03_labeled
self.cuhk03_classic_split = cuhk03_classic_split
self.pin_memory = True if self.use_gpu else False
# Build train and test transform functions
transform_train = build_transforms(self.height, self.width, is_train=True, data_augment=data_augment)
transform_test = build_transforms(self.height, self.width, is_train=False, data_augment=data_augment)
transform_test_flip = build_transforms(self.height, self.width, is_train=False, data_augment=data_augment, flip=True)
print("=> Initializing TRAIN (source) datasets")
self.train = []
self._num_train_pids = 0
self._num_train_cams = 0
for name in self.source_names:
dataset = init_imgreid_dataset(
root=self.root, name=name, split_id=self.split_id, cuhk03_labeled=self.cuhk03_labeled,
cuhk03_classic_split=self.cuhk03_classic_split
)
for img_path, pid, camid in dataset.train:
pid += self._num_train_pids
camid += self._num_train_cams
self.train.append((img_path, pid, camid))
self._num_train_pids += dataset.num_train_pids
self._num_train_cams += dataset.num_train_cams
if self.train_sampler == 'RandomIdentitySampler':
print('!!! Using RandomIdentitySampler !!!')
self.trainloader = DataLoader(
ImageDataset(self.train, transform=transform_train),
sampler=RandomIdentitySampler(self.train, self.train_batch_size, self.num_instances),
batch_size=self.train_batch_size, shuffle=False, num_workers=self.workers,
pin_memory=self.pin_memory, drop_last=True
)
else:
self.trainloader = DataLoader(
ImageDataset(self.train, transform=transform_train),
batch_size=self.train_batch_size, shuffle=True, num_workers=self.workers,
pin_memory=self.pin_memory, drop_last=True
)
print("=> Initializing TEST (target) datasets")
self.testloader_dict = {name: {'query': None, 'gallery': None} for name in self.target_names}
self.testdataset_dict = {name: {'query': None, 'gallery': None} for name in self.target_names}
for name in self.target_names:
dataset = init_imgreid_dataset(
root=self.root, name=name, split_id=self.split_id, cuhk03_labeled=self.cuhk03_labeled,
cuhk03_classic_split=self.cuhk03_classic_split
)
self.testloader_dict[name]['query'] = DataLoader(
ImageDataset(dataset.query, transform=transform_test),
batch_size=self.test_batch_size, shuffle=False, num_workers=self.workers,
pin_memory=self.pin_memory, drop_last=False
)
self.testloader_dict[name]['gallery'] = DataLoader(
ImageDataset(dataset.gallery, transform=transform_test),
batch_size=self.test_batch_size, shuffle=False, num_workers=self.workers,
pin_memory=self.pin_memory, drop_last=False
)
self.testloader_dict[name]['query_flip'] = DataLoader(
ImageDataset(dataset.query, transform=transform_test_flip),
batch_size=self.test_batch_size, shuffle=False, num_workers=self.workers,
pin_memory=self.pin_memory, drop_last=False
)
self.testloader_dict[name]['gallery_flip'] = DataLoader(
ImageDataset(dataset.gallery, transform=transform_test_flip),
batch_size=self.test_batch_size, shuffle=False, num_workers=self.workers,
pin_memory=self.pin_memory, drop_last=False
)
self.testdataset_dict[name]['query'] = dataset.query
self.testdataset_dict[name]['gallery'] = dataset.gallery
print("\n")
print(" **************** Summary ****************")
print(" train names : {}".format(self.source_names))
print(" # train datasets : {}".format(len(self.source_names)))
print(" # train ids : {}".format(self._num_train_pids))
print(" # train images : {}".format(len(self.train)))
print(" # train cameras : {}".format(self._num_train_cams))
print(" test names : {}".format(self.target_names))
print(" *****************************************")
print("\n")
class VideoDataManager(BaseDataManager):
"""
Video-ReID data manager
"""
def __init__(self,
use_gpu,
source_names,
target_names,
root,
split_id=0,
height=256,
width=128,
train_batch_size=32,
test_batch_size=100,
workers=4,
seq_len=15,
sample_method='evenly',
image_training=True # train the video-reid model with images rather than tracklets
):
super(VideoDataManager, self).__init__()
self.use_gpu = use_gpu
self.source_names = source_names
self.target_names = target_names
self.root = root
self.split_id = split_id
self.height = height
self.width = width
self.train_batch_size = train_batch_size
self.test_batch_size = test_batch_size
self.workers = workers
self.seq_len = seq_len
self.sample_method = sample_method
self.image_training = image_training
self.pin_memory = True if self.use_gpu else False
# Build train and test transform functions
transform_train = build_transforms(self.height, self.width, is_train=True)
transform_test = build_transforms(self.height, self.width, is_train=False)
print("=> Initializing TRAIN (source) datasets")
self.train = []
self._num_train_pids = 0
self._num_train_cams = 0
for name in self.source_names:
dataset = init_vidreid_dataset(root=self.root, name=name, split_id=self.split_id)
for img_paths, pid, camid in dataset.train:
pid += self._num_train_pids
camid += self._num_train_cams
if self.image_training:
# decompose tracklets into images
for img_path in img_paths:
self.train.append((img_path, pid, camid))
else:
self.train.append((img_paths, pid, camid))
self._num_train_pids += dataset.num_train_pids
self._num_train_cams += dataset.num_train_cams
if image_training:
# each batch has image data of shape (batch, channel, height, width)
self.trainloader = DataLoader(
ImageDataset(self.train, transform=transform_train),
batch_size=self.train_batch_size, shuffle=True, num_workers=self.workers,
pin_memory=self.pin_memory, drop_last=True
)
else:
# each batch has image data of shape (batch, seq_len, channel, height, width)
self.trainloader = DataLoader(
VideoDataset(self.train, seq_len=self.seq_len, sample_method=self.sample_method, transform=transform_test),
batch_size=self.train_batch_size, shuffle=True, num_workers=self.workers,
pin_memory=self.pin_memory, drop_last=True
)
print("=> Initializing TEST (target) datasets")
self.testloader_dict = {name: {'query': None, 'gallery': None} for name in self.target_names}
self.testdataset_dict = {name: {'query': None, 'gallery': None} for name in self.target_names}
for name in self.target_names:
dataset = init_vidreid_dataset(root=self.root, name=name, split_id=self.split_id)
self.testloader_dict[name]['query'] = DataLoader(
VideoDataset(dataset.query, seq_len=self.seq_len, sample_method=self.sample_method, transform=transform_test),
batch_size=self.test_batch_size, shuffle=False, num_workers=self.workers,
pin_memory=self.pin_memory, drop_last=False,
)
self.testloader_dict[name]['gallery'] = DataLoader(
VideoDataset(dataset.gallery, seq_len=self.seq_len, sample_method=self.sample_method, transform=transform_test),
batch_size=self.test_batch_size, shuffle=False, num_workers=self.workers,
pin_memory=self.pin_memory, drop_last=False,
)
self.testdataset_dict[name]['query'] = dataset.query
self.testdataset_dict[name]['gallery'] = dataset.gallery
print("\n")
print(" **************** Summary ****************")
print(" train names : {}".format(self.source_names))
print(" # train datasets : {}".format(len(self.source_names)))
print(" # train ids : {}".format(self._num_train_pids))
if self.image_training:
print(" # train images : {}".format(len(self.train)))
else:
print(" # train tracklets: {}".format(len(self.train)))
print(" # train cameras : {}".format(self._num_train_cams))
print(" test names : {}".format(self.target_names))
print(" *****************************************")
print("\n")