Spaces:
Runtime error
Runtime error
Johannes Kolbe
commited on
Commit
·
49256a6
1
Parent(s):
df27e8a
code for running space
Browse files- README.md +3 -3
- app.py +63 -0
- gradio_queue.db +0 -0
- requirements.txt +4 -0
README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
---
|
2 |
-
title: Pixel
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
colorTo: pink
|
6 |
sdk: gradio
|
7 |
app_file: app.py
|
|
|
1 |
---
|
2 |
+
title: Pixel CNN MNIST
|
3 |
+
emoji: 👨🎨
|
4 |
+
colorFrom: indigo
|
5 |
colorTo: pink
|
6 |
sdk: gradio
|
7 |
app_file: app.py
|
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from cProfile import label
|
2 |
+
import glob
|
3 |
+
import gradio as gr
|
4 |
+
import tensorflow as tf
|
5 |
+
from huggingface_hub import from_pretrained_keras
|
6 |
+
import numpy as np
|
7 |
+
|
8 |
+
pixel_cnn = from_pretrained_keras("keras-io/pixel-cnn-mnist")
|
9 |
+
|
10 |
+
|
11 |
+
def infer(batch):
|
12 |
+
pixels = np.zeros(shape=(batch,) + (pixel_cnn.input_shape)[1:])
|
13 |
+
batch, rows, cols, channels = pixels.shape
|
14 |
+
|
15 |
+
# Iterate over the pixels because generation has to be done sequentially pixel by pixel.
|
16 |
+
for row in range(rows):
|
17 |
+
for col in range(cols):
|
18 |
+
for channel in range(channels):
|
19 |
+
# Feed the whole array and retrieving the pixel value probabilities for the next
|
20 |
+
# pixel.
|
21 |
+
probs = pixel_cnn.predict(pixels)[:, row, col, channel]
|
22 |
+
# Use the probabilities to pick pixel values and append the values to the image
|
23 |
+
# frame.
|
24 |
+
pixels[:, row, col, channel] = tf.math.ceil(
|
25 |
+
probs - tf.random.uniform(probs.shape)
|
26 |
+
)
|
27 |
+
|
28 |
+
for i, pic in enumerate(pixels):
|
29 |
+
tf.keras.preprocessing.image.save_img(
|
30 |
+
"/tmp/generated_image_{}.png".format(i), deprocess_image(np.squeeze(pic, -1))
|
31 |
+
)
|
32 |
+
return glob.glob("/tmp/generated*")
|
33 |
+
|
34 |
+
|
35 |
+
def deprocess_image(x):
|
36 |
+
# Stack the single channeled black and white image to RGB values.
|
37 |
+
x = np.stack((x, x, x), 2)
|
38 |
+
# Undo preprocessing
|
39 |
+
x *= 255.0
|
40 |
+
# Convert to uint8 and clip to the valid range [0, 255]
|
41 |
+
x = np.clip(x, 0, 255).astype("uint8")
|
42 |
+
return x
|
43 |
+
|
44 |
+
|
45 |
+
article = """<center>
|
46 |
+
Authors: Space by <a href='https://twitter.com/johko990' target='_blank'><b>Johannes Kolbe</b></a>, model by İhsan Soydemir after an example by ADMoreau at
|
47 |
+
<a href='https://keras.io/examples/generative/pixelcnn/' target='_blank'><b>keras.io</b></a> <br>
|
48 |
+
<a href='https://arxiv.org/abs/1606.05328' target='_blank'><b>Original paper</b></a> by van den Oord et al."""
|
49 |
+
|
50 |
+
|
51 |
+
description = """Image generation using a CNN. The model is trained on MNIST data, so the generation capabilities are limited to MNIST like images. <br>
|
52 |
+
Just use the slider to set how many images should be created.<br>
|
53 |
+
The execution might take some time."""
|
54 |
+
|
55 |
+
|
56 |
+
Iface = gr.Interface(
|
57 |
+
fn=infer,
|
58 |
+
inputs=gr.inputs.Slider(minimum=1, maximum=20, default=4, step=1, label="Number of images to generate"),
|
59 |
+
outputs=gr.outputs.Carousel(["image"]),
|
60 |
+
title="PixelCNN - MNIST Image Generation",
|
61 |
+
article=article,
|
62 |
+
description=description,
|
63 |
+
).launch(enable_queue=True)
|
gradio_queue.db
ADDED
Binary file (16.4 kB). View file
|
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
tensorflow >=2.6.0
|
2 |
+
gradio
|
3 |
+
huggingface_hub
|
4 |
+
jinja2
|