boazchung commited on
Commit
e230f23
1 Parent(s): a477dbf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -55
app.py CHANGED
@@ -1,56 +1,56 @@
1
- import gradio as gr
2
- import tensorflow as tf
3
- from tensorflow import keras
4
- from math import sqrt, ceil
5
-
6
- from huggingface_hub import from_pretrained_keras
7
-
8
- import numpy as np
9
-
10
-
11
- model = from_pretrained_keras("keras-io/conditional-gan")
12
-
13
- latent_dim = 128
14
-
15
- def generate_latent_points(digit, latent_dim, n_samples, n_classes=10):
16
- # generate points in the latent space
17
- random_latent_vectors = tf.random.normal(shape=(n_samples, latent_dim))
18
- labels = tf.keras.utils.to_categorical([digit for _ in range(n_samples)], n_classes)
19
- return tf.concat([random_latent_vectors, labels], 1)
20
-
21
- def create_digit_samples(digit, n_samples):
22
- if digit in range(10):
23
- latent_dim = 128
24
- random_vector_labels = generate_latent_points(int(digit), latent_dim, int(n_samples))
25
- examples = model.predict(random_vector_labels)
26
- examples = examples * 255.0
27
- size = ceil(sqrt(n_samples))
28
- digit_images = np.zeros((28*size, 28*size), dtype=float)
29
- n = 0
30
- for i in range(size):
31
- for j in range(size):
32
- if n == n_samples:
33
- break
34
- digit_images[i* 28 : (i+1)*28, j*28 : (j+1)*28] = examples[n, :, :, 0]
35
- n += 1
36
- digit_images = (digit_images/127.5) -1
37
- return digit_images
38
-
39
- description = "Keras implementation for Conditional GAN to generate samples for specific digit of MNIST"
40
- article = "Author:<a href=\"https://huggingface.co/rajrathi\"> Rajeshwar Rathi</a>; Based on the keras example by <a href=\"https://keras.io/examples/generative/conditional_gan/\">Sayak Paul</a>"
41
- title = "Conditional GAN for MNIST"
42
-
43
- examples = [[1, 10], [3, 5], [5, 15]]
44
-
45
-
46
- iface = gr.Interface(
47
- fn = create_digit_samples,
48
- inputs = ["number", "number"],
49
- outputs = ["image"],
50
- examples = examples,
51
- description = description,
52
- title = title,
53
- article = article
54
- )
55
-
56
  iface.launch()
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ from tensorflow import keras
4
+ from math import sqrt, ceil
5
+
6
+ from huggingface_hub import from_pretrained_keras
7
+
8
+ import numpy as np
9
+
10
+
11
+ model = from_pretrained_keras("keras-io/conditional-gan")
12
+
13
+ latent_dim = 128
14
+
15
+ def generate_latent_points(digit, latent_dim, n_samples, n_classes=10):
16
+ # generate points in the latent space
17
+ random_latent_vectors = tf.random.normal(shape=(n_samples, latent_dim))
18
+ labels = tf.keras.utils.to_categorical([digit for _ in range(n_samples)], n_classes)
19
+ return tf.concat([random_latent_vectors, labels], 1)
20
+
21
+ def create_digit_samples(digit, n_samples):
22
+ if digit in range(10):
23
+ latent_dim = 128
24
+ random_vector_labels = generate_latent_points(int(digit), latent_dim, int(n_samples))
25
+ examples = model.predict(random_vector_labels)
26
+ examples = examples * 255.0
27
+ size = ceil(sqrt(n_samples))
28
+ digit_images = np.zeros((28*size, 28*size), dtype=float)
29
+ n = 0
30
+ for i in range(size):
31
+ for j in range(size):
32
+ if n == n_samples:
33
+ break
34
+ digit_images[i* 28 : (i+1)*28, j*28 : (j+1)*28] = examples[n, :, :, 0]
35
+ n += 1
36
+ digit_images = (digit_images/127.5) -1
37
+ return digit_images
38
+
39
+ description = "Keras implementation for Conditional GAN to generate samples for specific digit of MNIST"
40
+ article = "Author:<a href=\"https://huggingface.co/rajrathi\"> Rajeshwar Rathi</a>; Based on the keras example by <a href=\"https://keras.io/examples/generative/conditional_gan/\">Sayak Paul</a>"
41
+ title = "cGAN MNIST"
42
+
43
+ examples = [[1, 10], [3, 5], [5, 15]]
44
+
45
+
46
+ iface = gr.Interface(
47
+ fn = create_digit_samples,
48
+ inputs = ["number", "number"],
49
+ outputs = ["image"],
50
+ examples = examples,
51
+ description = description,
52
+ title = title,
53
+ article = article
54
+ )
55
+
56
  iface.launch()