-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataprep.py
157 lines (142 loc) · 6.45 KB
/
dataprep.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
import os
import pickle
import tqdm
from PIL import Image
import numpy as np
class PreProcessData(object):
def __init__(self, config, img_width, img_height, img_cells, input_path, output_path):
self.img_width = img_width
self.img_height = img_height
self.img_cells = img_cells
self.input_path = input_path
self.output_path = output_path
self.config = config
def _open_image(self, path):
img = Image.open(path)
img = img.resize((self.img_width, self.img_height))
return np.array(np.asarray(img), dtype='float64')
def find_image(self, name, img_num, data_path, predict=False):
img_num = '0'*4 + img_num # add the necessary 0's 0003
img_num = img_num[-4:]
img_path = os.path.join(data_path, 'lfw2', name, f'{name}_{img_num}.jpg')
img_data = self._open_image(img_path)
if not predict:
img_data = img_data.reshape(self.img_width, self.img_height, self.img_cells)
return img_data
def find_ytf_image(self, name, img_num, data_path, predict=False):
new_path = os.path.join(data_path, name)
images = os.listdir(new_path)
# print("LLLLLLLLL", int(img_num), "length", len(images))
img = images[int(img_num)-1]
img_path = os.path.join(new_path, img)
# print(img_path)
_img = Image.open(img_path).convert('L')
# print("old img size", _img.size)
#old_res = 301
#new_size = 105
_img = _img.resize((105, 105))
# print("new img size", _img.size)
img_data = np.array(np.asarray(_img), dtype='float64')
if not predict:
img_data = img_data.reshape(self.img_width, self.img_height, self.img_cells)
return img_data
def open_ytf(self, set_name):
ytf_path = os.path.join("./dataset/ytf/ytf_split/")
print("opening Youtube Faces Data")
print("YTS: Loading training data")
file_path = os.path.join(ytf_path, f'{set_name}.txt')
print("Splitting data according to data in:")
print(file_path) # train.txt or test.txt
print("Loading data...")
x_first = []
x_second = []
y = []
names = []
images_path = os.path.join(ytf_path, set_name)
with open(file_path, 'r') as file:
lines = file.readlines()
for line in tqdm.tqdm(lines):
line = line.split()
if len(line) == 4: # Class 0 - non-identical
names.append(line)
first_person_name, first_image_num, second_person_name, second_image_num = line[0], line[1], line[2], \
line[3]
first_image = self.find_ytf_image(name=first_person_name,
img_num=first_image_num,
data_path=images_path)
second_image = self.find_ytf_image(name=second_person_name,
img_num=second_image_num,
data_path=images_path)
x_first.append(first_image)
x_second.append(second_image)
y.append(0)
elif len(line) == 3: # Class 1 - identical
names.append(line)
person_name, first_image_num, second_image_num = line[0], line[1], line[2]
first_image = self.find_ytf_image(name=person_name,
img_num=first_image_num,
data_path=images_path)
second_image = self.find_ytf_image(name=person_name,
img_num=second_image_num,
data_path=images_path)
x_first.append(first_image)
x_second.append(second_image)
y.append(1)
elif len(line) == 1:
print(f'line with a single value: {line}')
return x_first, x_second, y, names
def load(self, set_name):
"""
Two classes
Class 0: [Person A, img#, Person B, img#]
Class 1: [Person A, img#, img#]
"""
file_path = os.path.join(self.input_path, 'splits', f'{set_name}.txt')
print("Splitting data according to data in:")
print(file_path) # train.txt or test.txt
print("Loading data...")
x_first = []
x_second = []
y = []
names = []
with open(file_path, 'r') as file:
lines = file.readlines()
for line in tqdm.tqdm(lines):
line = line.split()
if len(line) == 4: # Class 0 - non-identical
names.append(line)
first_person_name, first_image_num, second_person_name, second_image_num = line[0], line[1], line[2], \
line[3]
first_image = self.find_image(name=first_person_name,
img_num=first_image_num,
data_path=self.input_path)
second_image = self.find_image(name=second_person_name,
img_num=second_image_num,
data_path=self.input_path)
x_first.append(first_image)
x_second.append(second_image)
y.append(0)
elif len(line) == 3: # Class 1 - identical
names.append(line)
person_name, first_image_num, second_image_num = line[0], line[1], line[2]
first_image = self.find_image(name=person_name,
img_num=first_image_num,
data_path=self.input_path)
second_image = self.find_image(name=person_name,
img_num=second_image_num,
data_path=self.input_path)
x_first.append(first_image)
x_second.append(second_image)
y.append(1)
elif len(line) == 1:
print(f'line with a single value: {line}')
print('Done loading dataset')
lfw_data = [[x_first, x_second], y, names]
if self.config['ytf']:
x_yft_first, x_yft_second, y_yft, names_yft = self.open_ytf(set_name=set_name)
combined_data = [[x_first+x_yft_first, x_second+x_yft_second], y + y_yft, names + names_yft]
else:
combined_data = lfw_data
with open(self.output_path, 'wb') as f:
pickle.dump(combined_data, f)
print("Loaded all data")