Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
import jax
|
4 |
+
import jax.numpy as jnp # JAX NumPy
|
5 |
+
import numpy as np
|
6 |
+
from flax import linen as nn # Linen API
|
7 |
+
from huggingface_hub import HfFileSystem
|
8 |
+
from flax.serialization import msgpack_restore, from_state_dict
|
9 |
+
import time
|
10 |
+
|
11 |
+
LATENT_DIM = 100
|
12 |
+
|
13 |
+
class Generator(nn.Module):
|
14 |
+
@nn.compact
|
15 |
+
def __call__(self, latent, training=True):
|
16 |
+
x = nn.Dense(features=32)(latent)
|
17 |
+
# x = nn.BatchNorm(not training)(x)
|
18 |
+
x = nn.relu(x)
|
19 |
+
x = nn.Dense(features=2*2*256)(x)
|
20 |
+
x = nn.BatchNorm(not training)(x)
|
21 |
+
x = nn.relu(x)
|
22 |
+
x = nn.Dropout(0.5, deterministic=not training)(x)
|
23 |
+
x = x.reshape((x.shape[0], 2, 2, -1))
|
24 |
+
x4o = nn.ConvTranspose(features=3, kernel_size=(2, 2), strides=(2, 2))(x)
|
25 |
+
x4 = nn.ConvTranspose(features=128, kernel_size=(2, 2), strides=(2, 2))(x)
|
26 |
+
x4 = LocalResponseNorm()(x4)
|
27 |
+
# x4 = nn.BatchNorm(not training)(x4)
|
28 |
+
x8 = nn.relu(x4)
|
29 |
+
# x8 = nn.Dropout(0.5, deterministic=not training)(x8)
|
30 |
+
x8o = nn.ConvTranspose(features=3, kernel_size=(2, 2), strides=(2, 2))(x8)
|
31 |
+
x8 = nn.ConvTranspose(features=64, kernel_size=(2, 2), strides=(2, 2))(x8)
|
32 |
+
x8 = LocalResponseNorm()(x8)
|
33 |
+
# x8 = nn.BatchNorm(not training)(x8)
|
34 |
+
x16 = nn.relu(x8)
|
35 |
+
# x16 = nn.Dropout(0.5, deterministic=not training)(x16)
|
36 |
+
x16o = nn.ConvTranspose(features=3, kernel_size=(2, 2), strides=(2, 2))(x16)
|
37 |
+
x16 = nn.ConvTranspose(features=32, kernel_size=(2, 2), strides=(2, 2))(x16)
|
38 |
+
x16 = LocalResponseNorm()(x16)
|
39 |
+
# x16 = nn.BatchNorm(not training)(x16)
|
40 |
+
x32 = nn.relu(x16)
|
41 |
+
# x32 = nn.Dropout(0.5, deterministic=not training)(x32)
|
42 |
+
x32o = nn.ConvTranspose(features=3, kernel_size=(2, 2), strides=(2, 2))(x32)
|
43 |
+
return (nn.tanh(x32o), nn.tanh(x16o), nn.tanh(x8o), nn.tanh(x4o))
|
44 |
+
|
45 |
+
generator = Generator()
|
46 |
+
variables = generator.init(jax.random.PRNGKey(0), jnp.zeros([1, LATENT_DIM]), training=False)
|
47 |
+
|
48 |
+
fs = HfFileSystem()
|
49 |
+
with fs.open("PrakhAI/AIPlane/g_checkpoint.msgpack", "rb") as f:
|
50 |
+
g_state = from_state_dict(variables, msgpack_restore(f.read()))
|
51 |
+
|
52 |
+
def sample_latent(key):
|
53 |
+
return jax.random.normal(key, shape=(1, LATENT_DIM))
|
54 |
+
|
55 |
+
if st.button('Generate Plane'):
|
56 |
+
latents = sample_latent(jax.random.PRNGKey(int(1_000_000 * time.time())))
|
57 |
+
g_out = generator.apply({'params': g_state['params'], 'batch_stats': g_state['batch_stats']}, latents, training=False)
|
58 |
+
img = ((np.array(g_out)+1)*255./2.).astype(np.uint8)[0]
|
59 |
+
st.image(Image.fromarray(np.repeat(img, repeats=3, axis=2)))
|
60 |
+
st.write("The model's details are at https://huggingface.co/PrakhAI/AIPlane/blob/main/README.md")
|