-
Notifications
You must be signed in to change notification settings - Fork 0
/
Encoder.py
45 lines (39 loc) · 1.48 KB
/
Encoder.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
import matplotlib.pyplot as plt
from keras.layers import Input, Dense
from keras.models import model_from_json
from keras.models import Model
from keras.utils.layer_utils import layer_from_config
from Autoencoder import Autoencoder
class Encoder(Autoencoder):
def __init__(self, path_to_model, path_to_weights):
self.autoencoder = model_from_json(open(path_to_model).read())
self.autoencoder.load_weights(path_to_weights)
self.autoencoder.summary()
self.input_img = Input(shape=(1240,))
self.encoded = Dense(
self.autoencoder.layers[1].output_dim,
activation=self.autoencoder.layers[1].activation,
weights=self.autoencoder.layers[1].get_weights()
)(self.input_img)
self.encoder = Model(input=self.input_img, output=self.encoded)
self.encoder.summary()
self.out_dim = self.encoder.layers[1].output_dim
self.in_dim = 1240
def encode(self, img):
return self.encoder.predict(img)
def draw(self, img):
return self.autoencoder.predict(img)
def decode(self, img):
return self.decoder.predict(img)
def fit(self, data, n_epochs=1000, val_split=0.1, batch_s=512):
if use_pre_trained_model:
print 'Fit is not necessary!'
return
autoencoder.fit(
data,
data,
nb_epoch=n_epochs,
batch_size=batch_s,
shuffle=True,
validation_split=val_split
)