-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdata_loader.py
251 lines (201 loc) · 9.26 KB
/
data_loader.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
import numpy as np
from PIL import Image
import torch.utils.data as data
from ChannelAug import ChannelAdap, ChannelAdapGray, ChannelRandomErasing
import torchvision.transforms as transforms
import random
class ChannelExchange(object):
""" Adaptive selects a channel or two channels.
Args:
probability: The probability that the Random Erasing operation will be performed.
sl: Minimum proportion of erased area against input image.
sh: Maximum proportion of erased area against input image.
r1: Minimum aspect ratio of erased area.
mean: Erasing value.
"""
def __init__(self, gray=2):
self.gray = gray
def __call__(self, img):
idx = random.randint(0, self.gray)
if idx == 0:
# random select R Channel
img[1, :, :] = img[0, :, :]
img[2, :, :] = img[0, :, :]
elif idx == 1:
# random select B Channel
img[0, :, :] = img[1, :, :]
img[2, :, :] = img[1, :, :]
elif idx == 2:
# random select G Channel
img[0, :, :] = img[2, :, :]
img[1, :, :] = img[2, :, :]
else:
tmp_img = 0.2989 * img[0, :, :] + 0.5870 * img[1, :, :] + 0.1140 * img[2, :, :]
img[0, :, :] = tmp_img
img[1, :, :] = tmp_img
img[2, :, :] = tmp_img
return img
class SYSUData(data.Dataset):
def __init__(self, data_dir, transform=None, colorIndex = None, thermalIndex = None):
data_dir = '/media/hijune/datadisk/reid-data/SYSU RGB-IR Re-ID/SYSU-MM01'
# Load training images (path) and labels
train_color_image = np.load(data_dir + 'train_rgb_resized_img.npy')
self.train_color_label = np.load(data_dir + 'train_rgb_resized_label.npy')
train_thermal_image = np.load(data_dir + 'train_ir_resized_img.npy')
self.train_thermal_label = np.load(data_dir + 'train_ir_resized_label.npy')
# BGR to RGB
self.train_color_image = train_color_image
self.train_thermal_image = train_thermal_image
self.transform = transform
self.cIndex = colorIndex
self.tIndex = thermalIndex
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
self.transform_thermal = transforms.Compose([
transforms.ToPILImage(),
transforms.Pad(10),
transforms.RandomCrop((288, 144)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
ChannelRandomErasing(probability = 0.5),
ChannelAdapGray(probability =0.5)])
self.transform_color = transforms.Compose([
transforms.ToPILImage(),
transforms.Pad(10),
transforms.RandomCrop((288, 144)),
transforms.RandomHorizontalFlip(),
# transforms.RandomGrayscale(p = 0.1),
transforms.ToTensor(),
normalize,
ChannelRandomErasing(probability = 0.5)])
self.transform_color1 = transforms.Compose([
transforms.ToPILImage(),
transforms.Pad(10),
transforms.RandomCrop((288, 144)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
ChannelRandomErasing(probability = 0.5),
ChannelExchange(gray = 2)])
def __getitem__(self, index):
img1, target1 = self.train_color_image[self.cIndex[index]], self.train_color_label[self.cIndex[index]]
img2, target2 = self.train_thermal_image[self.tIndex[index]], self.train_thermal_label[self.tIndex[index]]
img1_0 = self.transform_color(img1)
img1_1 = self.transform_color1(img1)
img2_0 = self.transform_thermal(img2)
# img1 = self.transform(img1)
# img2 = self.transform(img2)
return img1_0, img1_1, img2_0, target1, target2
def __len__(self):
return len(self.train_color_label)
class RegDBData(data.Dataset):
def __init__(self, data_dir, trial, transform=None, colorIndex = None, thermalIndex = None):
# Load training images (path) and labels
data_dir = '/media/hijune/datadisk/reid-data/RegDB/'
train_color_list = data_dir + 'idx/train_visible_{}'.format(trial) + '.txt'
train_thermal_list = data_dir + 'idx/train_thermal_{}'.format(trial) + '.txt'
color_img_file, train_color_label = load_data(train_color_list)
thermal_img_file, train_thermal_label = load_data(train_thermal_list)
train_color_image = []
for i in range(len(color_img_file)):
img = Image.open(data_dir+ color_img_file[i])
img = img.resize((144, 288), Image.ANTIALIAS)
pix_array = np.array(img)
train_color_image.append(pix_array)
train_color_image = np.array(train_color_image)
train_thermal_image = []
for i in range(len(thermal_img_file)):
img = Image.open(data_dir+ thermal_img_file[i])
img = img.resize((144, 288), Image.ANTIALIAS)
pix_array = np.array(img)
train_thermal_image.append(pix_array)
train_thermal_image = np.array(train_thermal_image)
# BGR to RGB
self.train_color_image = train_color_image
self.train_color_label = train_color_label
# BGR to RGB
self.train_thermal_image = train_thermal_image
self.train_thermal_label = train_thermal_label
self.transform = transform
self.cIndex = colorIndex
self.tIndex = thermalIndex
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
self.transform_thermal = transforms.Compose([
transforms.ToPILImage(),
transforms.Pad(10),
transforms.RandomCrop((288, 144)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
ChannelRandomErasing(probability = 0.5),
ChannelAdapGray(probability =0.5)])
self.transform_color = transforms.Compose( [
transforms.ToPILImage(),
transforms.Pad(10),
transforms.RandomCrop((288, 144)),
transforms.RandomHorizontalFlip(),
# transforms.RandomGrayscale(p = 0.1),
transforms.ToTensor(),
normalize,
ChannelRandomErasing(probability = 0.5)])
self.transform_color1 = transforms.Compose( [
transforms.ToPILImage(),
transforms.Pad(10),
transforms.RandomCrop((288, 144)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
ChannelRandomErasing(probability = 0.5),
ChannelExchange(gray = 2)])
def __getitem__(self, index):
img1, target1 = self.train_color_image[self.cIndex[index]], self.train_color_label[self.cIndex[index]]
img2, target2 = self.train_thermal_image[self.tIndex[index]], self.train_thermal_label[self.tIndex[index]]
img1_0 = self.transform_color(img1)
img1_1 = self.transform_color1(img1)
img2 = self.transform_thermal(img2)
return img1_0, img1_1, img2, target1, target2
def __len__(self):
return len(self.train_color_label)
class TestData(data.Dataset):
def __init__(self, test_img_file, test_label, transform=None, img_size = (144,288)):
test_image = []
for i in range(len(test_img_file)):
img = Image.open(test_img_file[i])
img = img.resize((img_size[0], img_size[1]), Image.ANTIALIAS)
pix_array = np.array(img)
test_image.append(pix_array)
test_image = np.array(test_image)
self.test_image = test_image
self.test_label = test_label
self.transform = transform
def __getitem__(self, index):
img1, target1 = self.test_image[index], self.test_label[index]
img1 = self.transform(img1)
return img1, target1
def __len__(self):
return len(self.test_image)
class TestDataOld(data.Dataset):
def __init__(self, data_dir, test_img_file, test_label, transform=None, img_size = (144,288)):
test_image = []
for i in range(len(test_img_file)):
img = Image.open(data_dir + test_img_file[i])
img = img.resize((img_size[0], img_size[1]), Image.ANTIALIAS)
pix_array = np.array(img)
test_image.append(pix_array)
test_image = np.array(test_image)
self.test_image = test_image
self.test_label = test_label
self.transform = transform
def __getitem__(self, index):
img1, target1 = self.test_image[index], self.test_label[index]
img1 = self.transform(img1)
return img1, target1
def __len__(self):
return len(self.test_image)
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 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