forked from dyhBUPT/YYDS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_loader.py
237 lines (176 loc) · 8.35 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
import numpy as np
from PIL import Image
import torch.utils.data as data
from transformers import RobertaTokenizerFast
from utils_new import SYSU_LABEL2PID, SYSU_Refer, RegDB_Refer, LLCM_Refer, tokenize, RoBERTa_path
class SYSUData(data.Dataset):
def __init__(self, data_dir, transform=None, colorIndex = None, thermalIndex = None):
# 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
self.tokenizer = RobertaTokenizerFast.from_pretrained(RoBERTa_path, local_files_only=True)
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]]
assert target1 == target2
img1 = self.transform(img1)
img2 = self.transform(img2)
text = SYSU_Refer[SYSU_LABEL2PID[target1]]
input_ids, attention_mask = tokenize(text, self.tokenizer)
return dict(
img1=img1,
img2=img2,
text=text,
target=target1,
input_ids=input_ids,
attention_mask=attention_mask,
)
def __len__(self):
return len(self.cIndex)
class RegDBData(data.Dataset):
def __init__(self, data_dir, trial, transform=None, colorIndex = None, thermalIndex = None):
# Load training images (path) and labels
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, self.label2pid = 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, 384), 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, 384), 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
self.tokenizer = RobertaTokenizerFast.from_pretrained(RoBERTa_path, local_files_only=True)
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]]
assert target1 == target2
img1 = self.transform(img1)
img2 = self.transform(img2)
text = RegDB_Refer[self.label2pid[target1]]
input_ids, attention_mask = tokenize(text, self.tokenizer)
return dict(
img1=img1,
img2=img2,
text=text,
target=target1,
input_ids=input_ids,
attention_mask=attention_mask,
)
def __len__(self):
return len(self.cIndex)
class LLCMData(data.Dataset):
def __init__(self, data_dir, transform=None, colorIndex = None, thermalIndex = None):
# Load training images (path) and labels
train_color_list = data_dir + 'idx/train_vis.txt'
train_thermal_list = data_dir + 'idx/train_nir.txt'
color_img_file, train_color_label, self.label2pid = 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, 384), 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, 384), Image.ANTIALIAS)
pix_array = np.array(img)
train_thermal_image.append(pix_array)
#print(pix_array.shape)
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
self.tokenizer = RobertaTokenizerFast.from_pretrained(RoBERTa_path, local_files_only=True)
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]]
assert target1 == target2
img1 = self.transform(img1)
img2 = self.transform(img2)
text = LLCM_Refer[self.label2pid[target1]]
input_ids, attention_mask = tokenize(text, self.tokenizer)
return dict(
img1=img1,
img2=img2,
text=text,
target=target1,
input_ids=input_ids,
attention_mask=attention_mask,
)
def __len__(self):
return len(self.cIndex)
class TestData(data.Dataset):
def __init__(self, dataset, test_img_file, test_label, transform=None, img_size = (144,384)):
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
self.dataset = dataset
self.tokenizer = RobertaTokenizerFast.from_pretrained(RoBERTa_path, local_files_only=True)
def __getitem__(self, index):
img1, target1 = self.test_image[index], self.test_label[index]
img1 = self.transform(img1)
if self.dataset == 'sysu':
text = SYSU_Refer[target1]
elif self.dataset == 'regdb':
text = RegDB_Refer[target1]
elif self.dataset == 'llcm':
text = LLCM_Refer[target1]
input_ids, attention_mask = tokenize(text, self.tokenizer)
return dict(
img=img1,
target=target1,
input_ids=input_ids,
attention_mask=attention_mask,
)
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]
LABEL2PID = {int(s.split(' ')[1]): int(s.split('/')[1]) for s in data_file_list}
return file_image, file_label, LABEL2PID