forked from soprof/face-identification-tpe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbottleneck.py
More file actions
26 lines (18 loc) · 772 Bytes
/
bottleneck.py
File metadata and controls
26 lines (18 loc) · 772 Bytes
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
import keras.backend as K
import numpy as np
class Bottleneck:
def __init__(self, model, layer):
self.fn = K.function([model.layers[0].input, K.learning_phase()], [model.layers[layer].output])
def predict(self, data_x, batch_size=32, learning_phase=False):
n_data = len(data_x)
n_batches = n_data // batch_size + (0 if n_data % batch_size == 0 else 1)
result = None
learning_phase = 1 if learning_phase else 0
for i in range(n_batches):
batch_x = data_x[i * batch_size:(i + 1) * batch_size]
batch_y = self.fn([batch_x, 0])[0]
if result is None:
result = batch_y
else:
result = np.vstack([result, batch_y])
return result