-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
176 lines (148 loc) · 5.6 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
import os
import numpy as np
from torch.utils.data.sampler import Sampler
import sys
import os.path as osp
import torch
import errno
def load_data(input_data_path ):
with open(input_data_path) as f:
data_file_list = open(input_data_path, 'rt').read().splitlines()
# Get full list of color image and labels
file_image = [s.split(' ')[0] for s in data_file_list]
file_label = [int(s.split(' ')[1]) for s in data_file_list]
return file_image, file_label
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
def GenCamIdx(gall_img, gall_label, mode):
if mode =='indoor':
camIdx = [1,2]
else:
camIdx = [1,2,4,5]
gall_cam = []
for i in range(len(gall_img)):
gall_cam.append(int(gall_img[i][-10]))
sample_pos = []
unique_label = np.unique(gall_label)
for i in range(len(unique_label)):
for j in range(len(camIdx)):
id_pos = [k for k,v in enumerate(gall_label) if v==unique_label[i] and gall_cam[k]==camIdx[j]]
if id_pos:
sample_pos.append(id_pos)
return sample_pos
def ExtractCam(gall_img):
gall_cam = []
for i in range(len(gall_img)):
cam_id = int(gall_img[i][-10])
# if cam_id ==3:
# cam_id = 2
gall_cam.append(cam_id)
return np.array(gall_cam)
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, num_pos, batchSize, epoch):
uni_label = np.unique(train_color_label)
self.n_classes = len(uni_label)
N = np.maximum(len(train_color_label), len(train_thermal_label))
for j in range(int(N/(batchSize*num_pos))+1):
batch_idx = np.random.choice(uni_label, batchSize, replace = False)
for i in range(batchSize):
# 随机采样id为i的person的num_pos张图片 在trainset.train_color_img中的index
sample_color = np.random.choice(color_pos[batch_idx[i]], num_pos)
sample_thermal = np.random.choice(thermal_pos[batch_idx[i]], num_pos)
if j ==0 and i==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
def mkdir_if_missing(directory):
if not osp.exists(directory):
try:
os.makedirs(directory)
except OSError as e:
if e.errno != errno.EEXIST:
raise
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:
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 set_seed(seed, cuda=True):
np.random.seed(seed)
torch.manual_seed(seed)
if cuda:
torch.cuda.manual_seed(seed)
def set_requires_grad(nets, requires_grad=False):
"""Set requies_grad=Fasle for all the networks to avoid unnecessary computations
Parameters:
nets (network list) -- a list of networks
requires_grad (bool) -- whether the networks require gradients or not
"""
if not isinstance(nets, list):
nets = [nets]
for net in nets:
if net is not None:
for param in net.parameters():
param.requires_grad = requires_grad