-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_fn.py
56 lines (43 loc) · 2.21 KB
/
input_fn.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
import sys
import h5py
import numpy as np
# Positive training data: CelebA Dataset
# Negative training data: ImageNet
# Get the training data: both images from CelebA and ImageNet
path_to_training_data = "train_face.h5"
class TrainingDatasetLoader:
def __init__(self, data_path):
print("Opening {}".format(data_path))
sys.stdout.flush()
# Reading data
self.cache = h5py.File(data_path, 'r')
print("Loading data into memory...")
sys.stdout.flush()
self.images = self.cache['images'][:]
self.labels = self.cache['labels'][:].astype(np.float32)
self.image_dims = self.images.shape
n_train_samples = self.image_dims[0]
self.train_inds = np.random.permutation(np.arange(n_train_samples))
self.pos_train_inds = self.train_inds[self.labels[self.train_inds, 0] == 1.0]
self.neg_train_inds = self.train_inds[self.labels[self.train_inds, 0] != 1.0]
def get_train_size(self):
return self.train_inds.shape[0]
def get_train_steps_per_epoch(self, batch_size, factor=10):
return self.get_train_size()//factor//batch_size
def get_batch(self, n, only_faces=False, p_pos=None, p_neg=None, return_inds=False):
if only_faces:
selected_inds = np.random.choice(self.pos_train_inds, size=n, replace=False, p=p_pos)
else:
selected_pos_inds = np.random.choice(self.pos_train_inds, size=n//2, replace=False, p=p_pos)
selected_neg_inds = np.random.choice(self.neg_train_inds, size=n//2, replace=False, p=p_neg)
selected_inds = np.concatenate((selected_pos_inds, selected_neg_inds))
sorted_inds = np.sort(selected_inds)
train_img = (self.images[sorted_inds, :, :, ::-1]/255.).astype(np.float32)
train_label = self.labels[sorted_inds, ...]
return (train_img, train_label, sorted_inds) if return_inds else (train_img, train_label)
def get_n_most_prob_faces(self, prob, n):
idx = np.argsort(prob)[::-1]
most_prob_inds = self.pos_train_inds[idx[:10*n:10]]
return (self.images[most_prob_inds,...]/255.).astype(np.float32)
def get_all_train_faces(self):
return self.images[self.pos_train_inds]