/
utils.py
263 lines (209 loc) · 8.61 KB
/
utils.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
import os,cv2
import numpy as np
from torch.utils.data.sampler import Sampler
import sys
import os.path as osp
import scipy.io as scio
def GenIdx(train_color_label, train_thermal_label):
color_pos = []
unique_label_color = np.unique(train_color_label)
for i in range(len(unique_label_color)):
tmp_pos = [k for k,v in enumerate(train_color_label) if v==unique_label_color[i]]
color_pos.append(tmp_pos)
thermal_pos = []
unique_label_thermal = np.unique(train_thermal_label)
for i in range(len(unique_label_thermal)):
tmp_pos = [k for k,v in enumerate(train_thermal_label) if v==unique_label_thermal[i]]
thermal_pos.append(tmp_pos)
return color_pos, thermal_pos
class IdentitySampler(Sampler):
"""Sample person identities evenly in each batch.
Args:
train_color_label, train_thermal_label: labels of two modalities
color_pos, thermal_pos: positions of each identity
batchSize: batch size
"""
def __init__(self, train_color_label, train_thermal_label, color_pos, thermal_pos, batchSize):
uni_label = np.unique(train_color_label)
self.n_classes = len(uni_label)
sample_color = np.arange(batchSize)
sample_thermal = np.arange(batchSize)
N = np.maximum(len(train_color_label), len(train_thermal_label))
for j in range(N//batchSize+1):
batch_idx = np.random.choice(uni_label, batchSize, replace = False)
for i in range(batchSize):
sample_color[i] = np.random.choice(color_pos[batch_idx[i]], 1)
sample_thermal[i] = np.random.choice(thermal_pos[batch_idx[i]], 1)
if j ==0:
index1= sample_color
index2= sample_thermal
else:
index1 = np.hstack((index1, sample_color))
index2 = np.hstack((index2, sample_thermal))
self.index1 = index1
self.index2 = index2
self.N = N
def __iter__(self):
return iter(np.arange(len(self.index1)))
def __len__(self):
return self.N
class IdentitySampler2(Sampler):
"""Sample person identities evenly in each batch.
Args:
train_color_label, train_thermal_label: labels of two modalities
color_pos, thermal_pos: positions of each identity
batchSize: batch size
"""
def __init__(self, train_color_label, train_thermal_label,
color_pos, thermal_pos,
batchSize, pics=4):
uni_label = np.unique(train_color_label)
self.n_classes = len(uni_label)
sample_color = np.arange(batchSize)
sample_thermal = np.arange(batchSize)
self.pics = pics
batch = batchSize // pics
N = np.maximum(len(train_color_label), len(train_thermal_label))
for j in range(N//batch+1):
batch_idx = np.random.choice(uni_label, batch, replace = False)
for i in range(batch):
temp = i * pics
sample_color[temp:temp+pics] = np.random.choice(color_pos[batch_idx[i]], pics)
sample_thermal[temp:temp+pics] = np.random.choice(thermal_pos[batch_idx[i]], pics)
if j ==0:
index1= sample_color
index2= sample_thermal
else:
index1 = np.hstack((index1, sample_color))
index2 = np.hstack((index2, sample_thermal))
self.index1 = index1
self.index2 = index2
self.N = N
def __iter__(self):
return iter(np.arange(len(self.index1)))
def __len__(self):
return self.N * self.pics
class IdentitySampler3(Sampler):
"""Sample person identities evenly in each batch.
Args:
train_color_label, train_thermal_label: labels of two modalities
color_pos, thermal_pos: positions of each identity
batchSize: batch size
"""
def __init__(self, train_color_label, train_thermal_label, color_pos, thermal_pos, batchSize, per_img):
uni_label = np.unique(train_color_label)
self.n_classes = len(uni_label)
sample_color = np.arange(batchSize)
sample_thermal = np.arange(batchSize)
N = np.maximum(len(train_color_label), len(train_thermal_label))
#per_img = 4
per_id = batchSize / per_img
for j in range(N//batchSize+1):
batch_idx = np.random.choice(uni_label, int(per_id), replace = False)
for s, i in enumerate(range(0, batchSize, per_img)):
sample_color[i:i+per_img] = np.random.choice(color_pos[batch_idx[s]], per_img, replace=False)
sample_thermal[i:i+per_img] = np.random.choice(thermal_pos[batch_idx[s]], per_img, replace=False)
if j ==0:
index1= sample_color
index2= sample_thermal
else:
index1 = np.hstack((index1, sample_color))
index2 = np.hstack((index2, sample_thermal))
self.index1 = index1
self.index2 = index2
self.N = N
def __iter__(self):
return iter(np.arange(len(self.index1)))
def __len__(self):
return self.N
class IdentitySampler5(Sampler):
"""Sample person identities evenly in each batch.
Args:
train_color_label, train_thermal_label: labels of two modalities
color_pos, thermal_pos: positions of each identity
batchSize: batch size
"""
def __init__(self, train_color_label, train_thermal_label, color_pos, thermal_pos, batchSize, per_img):
uni_label = np.unique(train_color_label)
self.n_classes = len(uni_label)
sample_color = np.arange(batchSize)
sample_thermal = np.arange(batchSize)
N = np.maximum(len(train_color_label), len(train_thermal_label))
#per_img = 4
per_id = batchSize / per_img
for j in range(10):
batch_idx = uni_label[int(j*per_id) : int((j+1)*per_id)]
print(batch_idx)
for s, i in enumerate(range(0, batchSize, per_img)):
sample_color[i:i+per_img] = color_pos[batch_idx[s]][0:per_img]
sample_thermal[i:i+per_img] = thermal_pos[batch_idx[s]][0:per_img]
if j ==0:
index1= sample_color
index2= sample_thermal
else:
index1 = np.hstack((index1, sample_color))
index2 = np.hstack((index2, sample_thermal))
self.index1 = index1
self.index2 = index2
self.N = N
def __iter__(self):
return iter(np.arange(len(self.index1)))
def __len__(self):
return self.N
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
class Logger(object):
"""
Write console output to external text file.
Code imported from https://github.com/Cysu/open-reid/blob/master/reid/utils/logging.py.
"""
def __init__(self, fpath=None):
self.console = sys.stdout
self.file = None
if fpath is not None:
self.mkdir_if_missing(osp.dirname(fpath))
self.file = open(fpath, 'w')
def __del__(self):
self.close()
def __enter__(self):
pass
def __exit__(self, *args):
self.close()
def write(self, msg):
self.console.write(msg)
if self.file is not None:
self.file.write(msg)
def flush(self):
self.console.flush()
if self.file is not None:
self.file.flush()
os.fsync(self.file.fileno())
def close(self):
self.console.close()
if self.file is not None:
self.file.close()
def mkdir_if_missing(self,directory):
if not osp.exists(directory):
try:
os.makedirs(directory)
except OSError as e:
if e.errno != errno.EEXIST:
raise
def draw_rect(img,color_mode):
img = np.array(img)
rects = [(0, 0, img.shape[1], img.shape[0])]
for x, y, w, h in rects:
cv2.rectangle(img, (x, y), (x+w, y+h), color_mode, 2)
return img