-
Notifications
You must be signed in to change notification settings - Fork 39
/
dataset.py
301 lines (255 loc) · 9.02 KB
/
dataset.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import os
import re
import pickle
from PIL import Image
from torch.utils import data
import numpy as np
from torchvision import transforms as T
from utils import select_triplet, select_car_triplet, get_all_hard_triplets
from tqdm import tqdm
class Faces_YALE(data.Dataset):
def __init__(self, root, num_per_cls=11,
transforms=None, train=True, test=False):
'''
获取图像路径,并根据训练,验证,测试划分数据
'''
self.test = test # 是否是测试模式
# 获取文件路径
print('root: ', root)
imgs = [os.path.join(root, img) for img in os.listdir(root)]
# 文件名排序
imgs = sorted(imgs, key=lambda x: int(
re.match(r'.*s(\d+)\.png', x).group(1)))
print(imgs)
img_num = len(imgs)
# 随机打乱数据
# np.random.seed(100) # 设置随机种子,确定
# imgs = np.random.permutation(imgs) # Yale数据集暂时不能随机打乱
# 划分训练数据集和验证数据集7: 3
if self.test:
self.imgs = imgs
elif train:
self.imgs = imgs[:int(0.7 * img_num)] # 前70%
else:
self.imgs = imgs[int(0.7 * img_num):] # 后30%
# 数据中心化
if transforms is None:
normalize = T.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
# 测试数据集或者验证数据集无需数据增强
if self.test or train: # or not train
self.transforms = T.Compose([
T.Resize(224),
T.CenterCrop(224),
T.ToTensor(),
normalize
])
else:
self.transforms = T.Compose([
T.Resize(256),
T.RandomSizedCrop(224),
T.RandomHorizontalFlip(),
T.ToTensor(),
normalize
])
def __getitem__(self, index):
'''
一次返回一张图片的数据和label
'''
img_path = self.imgs[index]
face_id = int(re.match(r'.*s(\d+)\.png', img_path).group(1))
# print('face id: ', face_id)
label = int(face_id / 11)
data = Image.open(img_path)
data = self.transforms(data) # 对数据进行缩放、裁剪、增强,归一化的变换
return data, label
def __len__(self):
return len(self.imgs)
class FACE_LFW(data.Dataset):
'''
处理LYW数据集, 通过自定义dataset读取数据
'''
def __init__(self,
root,
transforms=None,
NUM_PER_CLS=20):
'''
初始化数据集
'''
self.num_per_cls = NUM_PER_CLS
# 获取文件路径
print('root: ', root)
imgs = [os.path.join(root, img) for img in os.listdir(root)]
self.imgs = imgs
# 数据中心化
if transforms is None:
self.transforms = T.Compose([
T.Resize(224),
T.CenterCrop(224),
T.ToTensor(),
T.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])])
else:
self.transforms = transforms
def __getitem__(self, index):
'''
通过索引获取数据和标签
'''
img_path = self.imgs[index]
label = int(index / self.num_per_cls)
data = Image.open(img_path)
data = self.transforms(data)
return data, label
def __len__(self):
return len(self.imgs)
class Hard_Triplet(data.Dataset):
'''
基于FaceNet Hard Triplet
'''
def __init__(self, root, model_path, transform=None):
'''
hard triplets初始化, 数据预处理方式
'''
self.triplets = get_all_hard_triplets(root, model_path, 62, 20)
# 数据预处理
if transform == None:
self.transform = T.Compose([
T.Resize(224),
T.CenterCrop(224),
T.ToTensor(),
T.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])])
else:
self.transform = transform
def __getitem__(self, index):
'''
一次获取一个hard triplet
'''
triplet = self.triplets[index]
data = [self.transform(Image.open(img)) for img in triplet[:3]]
label = triplet[3:]
return data, label
def __len__(self):
'''
返回所有hard triplet数量
'''
return len(self.triplets)
class Triplet(data.Dataset):
'''
每次随机产生一个triplet数据
'''
def __init__(self,
root,
num_cls=62,
num_tripets=1000,
limit=20,
transforms=None,
train=True,
test=False):
'''
组织数据: 随机产生num_triplets个triplets
'''
self.test = test
self.triplets = [select_triplet(root, num_cls, limit, False)
for i in range(num_tripets)] # order: anchor, positive, negative
# print(self.triplets)
# 数据预处理
if transforms is None:
normalize = T.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
# 测试数据集或者验证数据集无需数据增强
if self.test or train: # or not train
self.transforms = T.Compose([
T.Resize(224),
T.CenterCrop(224),
T.ToTensor(),
normalize
])
else: # 数据增强
self.transforms = T.Compose([
T.Resize(256),
T.RandomSizedCrop(224),
T.RandomHorizontalFlip(),
normalize,
T.ToTensor()
])
else:
self.transforms = transforms
def __getitem__(self, index):
'''
每次返回一个triplet
'''
triplet = self.triplets[index]
# print(triplet)
data = [self.transforms(Image.open(img_path))
for img_path in triplet[:3]]
label = triplet[3:]
# print(label)
return data, label
def __len__(self):
'''
返回triplet数量
'''
return len(self.triplets)
# --------------------Triplet for cars
class Car_triplet(data.Dataset):
'''
汽车的Triplet数据集
'''
def __init__(self,
root,
label_path,
label2img_path,
num_triplets=5000):
'''
初始化数据集
'''
print('root: ', root)
self.labels = pickle.load(open(label_path, 'rb'))
print('total %d kinds of car.' % len(self.labels))
self.label2img = pickle.load(open(label2img_path, 'rb'))
# 生成triplets数据集
self.triplets = [select_car_triplet(root, self.labels, self.label2img)
for i in range(num_triplets)]
# print(self.triplets)
normalize = T.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
# 数据预处理
self.transforms = T.Compose([
T.Compose([
T.Resize(224),
T.CenterCrop(224),
T.ToTensor(),
normalize # normalize与ToTensor能否改变顺序
])
])
def __getitem__(self, index):
'''
每次返回一个triplet
'''
triplet = self.triplets[index]
data = [self.transforms(Image.open(img)) for img in triplet[:3]]
label = triplet[3:]
return data, label
def __len__(self):
'''
返回triplet数量
'''
return len(self.triplets)
# Car test dataset...
# if __name__ == '__main__':
# triplets = [select_car_triplet('car_train_data', 196) for i in tqdm(range(1000))]
# # print(triplets)
# for x in triplets:
# print(x)
# print('--Test done.')
# if __name__ == '__main__':
# labels = pickle.load(open('quali_labels.pkl', 'rb'))
# img2label = pickle.load(open('img_names2labels.pkl', 'rb'))
# label2img = pickle.load(open('label2img_names.pkl', 'rb'))
# # class_names = pickle.load(open('class_names.pkl', 'rb'))
# triplets = [select_car_triplet('car_train_data',
# labels,
# img2label,
# label2img) for i in range(100)]
# print(triplets)