Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,9 @@ 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 |
-
from local_response_norm import LocalResponseNorm
|
| 11 |
from generator import Generator, LATENT_DIM
|
| 12 |
|
| 13 |
generator = Generator()
|
|
@@ -17,12 +15,26 @@ fs = HfFileSystem()
|
|
| 17 |
with fs.open("PrakhAI/AIPlane2/g_checkpoint.msgpack", "rb") as f:
|
| 18 |
g_state = from_state_dict(variables, msgpack_restore(f.read()))
|
| 19 |
|
| 20 |
-
def sample_latent(key):
|
| 21 |
-
return jax.random.normal(key, shape=(
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
(g_out128, _, _, _, _, _) = generator.apply({'params': g_state['params'], 'batch_stats': g_state['batch_stats']}, latents, training=False)
|
| 26 |
-
img = ((np.array(
|
| 27 |
-
st.image(Image.fromarray(img))
|
| 28 |
-
st.write("The model and its details are at https://huggingface.co/PrakhAI/AIPlane2")
|
|
|
|
| 3 |
import jax
|
| 4 |
import jax.numpy as jnp # JAX NumPy
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
from huggingface_hub import HfFileSystem
|
| 7 |
from flax.serialization import msgpack_restore, from_state_dict
|
| 8 |
import time
|
|
|
|
| 9 |
from generator import Generator, LATENT_DIM
|
| 10 |
|
| 11 |
generator = Generator()
|
|
|
|
| 15 |
with fs.open("PrakhAI/AIPlane2/g_checkpoint.msgpack", "rb") as f:
|
| 16 |
g_state = from_state_dict(variables, msgpack_restore(f.read()))
|
| 17 |
|
| 18 |
+
def sample_latent(batch, key):
|
| 19 |
+
return jax.random.normal(key, shape=(batch, LATENT_DIM))
|
| 20 |
|
| 21 |
+
def gridify(images): # num x image_width x image_height x channels
|
| 22 |
+
# Every num can be padded to make a grid of size floor(sqrt(num)) x ceil(sqrt(num)) or ceil(sqrt(num)) x ceil(sqrt(num))
|
| 23 |
+
num = images.shape[0]
|
| 24 |
+
image_width = images.shape[1]
|
| 25 |
+
image_height = images.shape[2]
|
| 26 |
+
channels = images.shape[3]
|
| 27 |
+
width = math.floor(math.sqrt(num))
|
| 28 |
+
height = math.ceil(math.sqrt(num))
|
| 29 |
+
if width * height < num:
|
| 30 |
+
width += 1
|
| 31 |
+
padded = np.concatenate([images, np.zeros((width*height-num, image_width, image_height, channels))], axis=0)
|
| 32 |
+
return padded.reshape((width, height, image_width, image_height, -1)).transpose((0, 2, 1, 3, 4)).reshape((width * image_width, height * image_height, -1))
|
| 33 |
+
|
| 34 |
+
st.write("The model and its details are at https://huggingface.co/PrakhAI/AIPlane2")
|
| 35 |
+
num_images = st.number_input(label="Number of images to generate", min_value=1, max_value=256, value=16)
|
| 36 |
+
if st.button('Generate Planes'):
|
| 37 |
+
latents = sample_latent(num_images, jax.random.PRNGKey(int(1_000_000 * time.time())))
|
| 38 |
(g_out128, _, _, _, _, _) = generator.apply({'params': g_state['params'], 'batch_stats': g_state['batch_stats']}, latents, training=False)
|
| 39 |
+
img = ((np.array(gridify(g_out128)+1)*255./2.).astype(np.uint8)
|
| 40 |
+
st.image(Image.fromarray(img))
|
|
|