-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathae.py
65 lines (49 loc) · 1.9 KB
/
ae.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
'''
vae.py
contains the setup for autoencoders.
created by shadySource
THE UNLICENSE
'''
import tensorflow as tf
from tensorflow.python.keras.models import Model
from tensorflow.python.keras import backend as K
class AutoEncoder(object):
def __init__(self, encoderArchitecture,
decoderArchitecture):
self.encoder = encoderArchitecture.model
self.decoder = decoderArchitecture.model
self.ae = Model(self.encoder.inputs, self.decoder(self.encoder.outputs))
def test():
import os
import numpy as np
from PIL import Image
from tensorflow.python.keras.preprocessing.image import load_img
from models import Darknet19Encoder, Darknet19Decoder
inputShape = (256, 256, 3)
batchSize = 8
latentSize = 100
img = load_img(os.path.join(os.path.dirname(__file__), '..','images', 'img.jpg'), target_size=inputShape[:-1])
img.show()
img = np.array(img, dtype=np.float32) * (2/255) - 1
# print(np.min(img))
# print(np.max(img))
# print(np.mean(img))
img = np.array([img]*batchSize) # make fake batches to improve GPU utilization
# This is how you build the autoencoder
encoder = Darknet19Encoder(inputShape, latentSize=latentSize, latentConstraints='bvae', beta=69)
decoder = Darknet19Decoder(inputShape, latentSize=latentSize)
bvae = AutoEncoder(encoder, decoder)
bvae.ae.compile(optimizer='adam', loss='mean_absolute_error')
while True:
bvae.ae.fit(img, img,
epochs=100,
batch_size=batchSize)
# example retrieving the latent vector
latentVec = bvae.encoder.predict(img)[0]
print(latentVec)
pred = bvae.ae.predict(img) # get the reconstructed image
pred = np.uint8((pred + 1)* 255/2) # convert to regular image values
pred = Image.fromarray(pred[0])
pred.show() # display popup
if __name__ == "__main__":
test()