Spaces:
Runtime error
Runtime error
Commit
·
a4a2f83
1
Parent(s):
226fa65
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
|
3 |
+
from PIL import Image
|
4 |
+
from keras.models import Model
|
5 |
+
from keras.layers import Input, UpSampling2D, Conv2D, concatenate
|
6 |
+
|
7 |
+
# This is the vq-vae model from "Neural Discrete Representation Learning"
|
8 |
+
# https://arxiv.org/abs/1711.00937
|
9 |
+
# by Aäron van den Oord, Oriol Vinyals, Koray Kavukcuoglu (Google DeepMind)
|
10 |
+
# ported to keras by @Ophirblum
|
11 |
+
|
12 |
+
|
13 |
+
class Encoder:
|
14 |
+
def __init__(self, input_shape, latent_dim, num_embeddings, commitment_cost):
|
15 |
+
|
16 |
+
self.input_shape = input_shape
|
17 |
+
self.latent_dim = latent_dim
|
18 |
+
self.num_embeddings = num_embeddings
|
19 |
+
self.commitment_cost = commitment_cost
|
20 |
+
|
21 |
+
self.encoder = None
|
22 |
+
|
23 |
+
def build(self):
|
24 |
+
|
25 |
+
x = Input(shape=self.input_shape, name='encoder_input')
|
26 |
+
|
27 |
+
# Downsampling path
|
28 |
+
|
29 |
+
h = Conv2D(64, 4, strides=2, activation='relu', padding='same')(x)
|
30 |
+
h = Conv2D(128, 4, strides=2, activation='relu', padding='same')(h)
|
31 |
+
h = Conv2D(256, 4, strides=2, activation='relu', padding='same')(h)
|
32 |
+
|
33 |
+
# Latent space
|
34 |
+
|
35 |
+
z = Conv2D(self.latent_dim, 4, strides=1, activation='linear', padding='same')(h)
|
36 |
+
|
37 |
+
# Instantiate Encoder Model
|
38 |
+
|
39 |
+
self.encoder = Model(x, z)
|
40 |
+
|
41 |
+
def encode(self, x):
|
42 |
+
|
43 |
+
assert self.encoder != None, "build the encoder first"
|
44 |
+
|
45 |
+
return self.encoder.predict(x)
|