forked from liorshk/facenet_pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LFWDataset.py
80 lines (61 loc) · 2.55 KB
/
LFWDataset.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
import torchvision.datasets as datasets
import os
import numpy as np
from tqdm import tqdm
class LFWDataset(datasets.ImageFolder):
'''
'''
def __init__(self, dir,pairs_path, transform=None):
super(LFWDataset, self).__init__(dir,transform)
self.pairs_path = pairs_path
# LFW dir contains 2 folders: faces and lists
self.validation_images = self.get_lfw_paths(dir)
def read_lfw_pairs(self,pairs_filename):
pairs = []
with open(pairs_filename, 'r') as f:
for line in f.readlines()[1:]:
pair = line.strip().split()
pairs.append(pair)
return np.array(pairs)
def get_lfw_paths(self,lfw_dir,file_ext="jpg"):
pairs = self.read_lfw_pairs(self.pairs_path)
nrof_skipped_pairs = 0
path_list = []
issame_list = []
for i in tqdm(range(len(pairs))):
#for pair in pairs:
pair = pairs[i]
if len(pair) == 3:
path0 = os.path.join(lfw_dir, pair[0], pair[0] + '_' + '%04d' % int(pair[1])+'.'+file_ext)
path1 = os.path.join(lfw_dir, pair[0], pair[0] + '_' + '%04d' % int(pair[2])+'.'+file_ext)
issame = True
elif len(pair) == 4:
path0 = os.path.join(lfw_dir, pair[0], pair[0] + '_' + '%04d' % int(pair[1])+'.'+file_ext)
path1 = os.path.join(lfw_dir, pair[2], pair[2] + '_' + '%04d' % int(pair[3])+'.'+file_ext)
issame = False
if os.path.exists(path0) and os.path.exists(path1): # Only add the pair if both paths exist
path_list.append((path0,path1,issame))
issame_list.append(issame)
else:
nrof_skipped_pairs += 1
if nrof_skipped_pairs>0:
print('Skipped %d image pairs' % nrof_skipped_pairs)
return path_list
def __getitem__(self, index):
'''
Args:
index: Index of the triplet or the matches - not of a single image
Returns:
'''
def transform(img_path):
"""Convert image into numpy array and apply transformation
Doing this so that it is consistent with all other datasets
to return a PIL Image.
"""
img = self.loader(img_path)
return self.transform(img)
(path_1,path_2,issame) = self.validation_images[index]
img1, img2 = transform(path_1), transform(path_2)
return img1, img2, issame
def __len__(self):
return len(self.validation_images)