Spaces:
Sleeping
Sleeping
ErtugrulDemir
commited on
Commit
·
5b8ac50
1
Parent(s):
84ca743
push root
Browse filesPushing 'Anime Face Generation' generative adversarial network portfolio project from local (cloud ) to remote
- app.py +51 -0
- model_downloading_instructions.txt +4 -0
- requirements.txt +2 -0
- utils/architectures.py +179 -0
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
from utils.architectures import (get_generator, get_discriminator,
|
4 |
+
DCGAN, DCGANMonitor, LATENT_DIM)
|
5 |
+
from tensorflow.keras.losses import BinaryCrossentropy
|
6 |
+
from tensorflow.keras.optimizers import Adam
|
7 |
+
from tensorflow.keras.preprocessing.image import array_to_img # load_img,
|
8 |
+
|
9 |
+
# creating freash model architectures
|
10 |
+
generator = get_generator()
|
11 |
+
discriminator = get_discriminator()
|
12 |
+
|
13 |
+
# Load the trained TensorFlow Object Detection model
|
14 |
+
model_weights = "GANModel_Weights/DCGAN_weights"
|
15 |
+
|
16 |
+
dcgan = DCGAN(generator=generator, discriminator=discriminator, latent_dim=LATENT_DIM)
|
17 |
+
|
18 |
+
D_LR = 0.0001
|
19 |
+
G_LR = 0.0003
|
20 |
+
comp_params = {
|
21 |
+
"g_optimizer":Adam(learning_rate=G_LR, beta_1=0.5),
|
22 |
+
"d_optimizer":Adam(learning_rate=D_LR, beta_1=0.5),
|
23 |
+
"loss_fn":BinaryCrossentropy()
|
24 |
+
}
|
25 |
+
dcgan.compile(**comp_params)
|
26 |
+
|
27 |
+
dcgan.load_weights(model_weights)
|
28 |
+
|
29 |
+
def generate():
|
30 |
+
|
31 |
+
noise = tf.random.normal([1, 100])
|
32 |
+
|
33 |
+
# generate the image from noise
|
34 |
+
g_img = dcgan.generator(noise)
|
35 |
+
|
36 |
+
# denormalize the image
|
37 |
+
g_img = (g_img * 127.5) + 127.5
|
38 |
+
|
39 |
+
# adjusting the image
|
40 |
+
g_img.numpy()
|
41 |
+
img = array_to_img(g_img[0])
|
42 |
+
|
43 |
+
return img
|
44 |
+
|
45 |
+
|
46 |
+
# declerating the params
|
47 |
+
demo = gr.Interface(fn=generate, inputs=None,outputs=gr.Image())
|
48 |
+
|
49 |
+
# Launching the demo
|
50 |
+
if __name__ == "__main__":
|
51 |
+
demo.launch()
|
model_downloading_instructions.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
We can't push the study.ipynb because size of the model exceeded the github file limit.
|
2 |
+
You can download the model from this link (study.ipynb):
|
3 |
+
-> [Colab Source Code For code reading]
|
4 |
+
-> [File Link for download]
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio==3.27.0
|
2 |
+
tensorflow==2.12.0
|
utils/architectures.py
ADDED
@@ -0,0 +1,179 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
from tensorflow import keras
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
from tensorflow.keras import Sequential, layers
|
5 |
+
from tensorflow.keras.preprocessing.image import array_to_img
|
6 |
+
|
7 |
+
|
8 |
+
# Declerating the configuration params
|
9 |
+
LATENT_DIM = 100
|
10 |
+
WEIGHT_INIT = keras.initializers.RandomNormal(mean=0.0, stddev=0.02)
|
11 |
+
CHANNELS = 3
|
12 |
+
|
13 |
+
input_shape = (64, 64, 3)
|
14 |
+
alpha = 0.2
|
15 |
+
|
16 |
+
|
17 |
+
def get_generator():
|
18 |
+
|
19 |
+
|
20 |
+
# Model architecture
|
21 |
+
generator = Sequential(name='generator')
|
22 |
+
|
23 |
+
# 1d random noise
|
24 |
+
generator.add(layers.Dense(8 * 8 * 512, input_dim=LATENT_DIM))
|
25 |
+
# model.add(layers.BatchNormalization())
|
26 |
+
generator.add(layers.ReLU())
|
27 |
+
|
28 |
+
# convert 1d to 3d
|
29 |
+
generator.add(layers.Reshape((8, 8, 512)))
|
30 |
+
|
31 |
+
# upsample to 16x16
|
32 |
+
generator.add(layers.Conv2DTranspose(256, (4, 4), strides=(2, 2), padding='same', kernel_initializer=WEIGHT_INIT))
|
33 |
+
# model.add(layers.BatchNormalization())
|
34 |
+
generator.add(layers.ReLU())
|
35 |
+
|
36 |
+
# upsample to 32x32
|
37 |
+
generator.add(layers.Conv2DTranspose(128, (4, 4), strides=(2, 2), padding='same', kernel_initializer=WEIGHT_INIT))
|
38 |
+
# model.add(layers.BatchNormalization())
|
39 |
+
generator.add(layers.ReLU())
|
40 |
+
|
41 |
+
# upsample to 64x64
|
42 |
+
generator.add(layers.Conv2DTranspose(64, (4, 4), strides=(2, 2), padding='same', kernel_initializer=WEIGHT_INIT))
|
43 |
+
# model.add(layers.BatchNormalization())
|
44 |
+
generator.add(layers.ReLU())
|
45 |
+
|
46 |
+
# Output layer
|
47 |
+
generator.add(layers.Conv2D(CHANNELS, (4, 4), padding='same', activation='tanh'))
|
48 |
+
|
49 |
+
return generator
|
50 |
+
|
51 |
+
def get_discriminator():
|
52 |
+
|
53 |
+
# Creating the architecture
|
54 |
+
discriminator = Sequential(name='discriminator')
|
55 |
+
|
56 |
+
# conv layer-1
|
57 |
+
discriminator.add(layers.Conv2D(64, (4, 4), strides=(2, 2), padding='same', input_shape=input_shape))
|
58 |
+
discriminator.add(layers.BatchNormalization())
|
59 |
+
discriminator.add(layers.LeakyReLU(alpha=alpha))
|
60 |
+
|
61 |
+
# conv layer-2
|
62 |
+
discriminator.add(layers.Conv2D(128, (4, 4), strides=(2, 2), padding='same', input_shape=input_shape))
|
63 |
+
discriminator.add(layers.BatchNormalization())
|
64 |
+
discriminator.add(layers.LeakyReLU(alpha=alpha))
|
65 |
+
|
66 |
+
# conv layer-3
|
67 |
+
discriminator.add(layers.Conv2D(128, (4, 4), strides=(2, 2), padding='same', input_shape=input_shape))
|
68 |
+
discriminator.add(layers.BatchNormalization())
|
69 |
+
discriminator.add(layers.LeakyReLU(alpha=alpha))
|
70 |
+
|
71 |
+
# Fully Connected Classifier layer
|
72 |
+
discriminator.add(layers.Flatten())
|
73 |
+
discriminator.add(layers.Dropout(0.3))
|
74 |
+
|
75 |
+
# output class
|
76 |
+
discriminator.add(layers.Dense(1, activation='sigmoid'))
|
77 |
+
|
78 |
+
return discriminator
|
79 |
+
|
80 |
+
|
81 |
+
class DCGAN(keras.Model):
|
82 |
+
def __init__(self, generator, discriminator, latent_dim):
|
83 |
+
super().__init__()
|
84 |
+
self.generator = generator
|
85 |
+
self.discriminator = discriminator
|
86 |
+
self.latent_dim = latent_dim
|
87 |
+
self.g_loss_metric = keras.metrics.Mean(name='g_loss')
|
88 |
+
self.d_loss_metric = keras.metrics.Mean(name='d_loss')
|
89 |
+
|
90 |
+
|
91 |
+
@property
|
92 |
+
def metrics(self):
|
93 |
+
return [self.g_loss_metric, self.d_loss_metric]
|
94 |
+
|
95 |
+
def compile(self, g_optimizer, d_optimizer, loss_fn):
|
96 |
+
super(DCGAN, self).compile()
|
97 |
+
self.g_optimizer = g_optimizer
|
98 |
+
self.d_optimizer = d_optimizer
|
99 |
+
self.loss_fn = loss_fn
|
100 |
+
|
101 |
+
def train_step(self, real_images):
|
102 |
+
|
103 |
+
# generate random noise
|
104 |
+
batch_size = tf.shape(real_images)[0]
|
105 |
+
random_noise = tf.random.normal(shape=(batch_size, self.latent_dim))
|
106 |
+
|
107 |
+
# train the discriminator with real (1) and fake (0) images
|
108 |
+
with tf.GradientTape() as tape:
|
109 |
+
|
110 |
+
# discriminator (pred then loss)
|
111 |
+
pred_real = self.discriminator(real_images, training=True)
|
112 |
+
real_labels = tf.ones((batch_size, 1))
|
113 |
+
real_labels += 0.05 * tf.random.uniform(tf.shape(real_labels))
|
114 |
+
d_loss_real = self.loss_fn(real_labels, pred_real)
|
115 |
+
|
116 |
+
# generator (generate then implemen discriminator steps again)
|
117 |
+
fake_images = self.generator(random_noise)
|
118 |
+
|
119 |
+
pred_fake = self.discriminator(fake_images, training=True)
|
120 |
+
fake_labels = tf.zeros((batch_size, 1))
|
121 |
+
d_loss_fake = self.loss_fn(fake_labels, pred_fake)
|
122 |
+
|
123 |
+
# total discriminator loss
|
124 |
+
d_loss = (d_loss_real + d_loss_fake) / 2
|
125 |
+
|
126 |
+
# compute discriminator gradients then update the gradients
|
127 |
+
gradients = tape.gradient(d_loss, self.discriminator.trainable_variables)
|
128 |
+
self.d_optimizer.apply_gradients(zip(gradients, self.discriminator.trainable_variables))
|
129 |
+
|
130 |
+
|
131 |
+
# train the generator model
|
132 |
+
labels = tf.ones((batch_size, 1))
|
133 |
+
with tf.GradientTape() as tape:
|
134 |
+
|
135 |
+
# generate then implement discriminator
|
136 |
+
fake_images = self.generator(random_noise, training=True)
|
137 |
+
|
138 |
+
pred_fake = self.discriminator(fake_images, training=True)
|
139 |
+
g_loss = self.loss_fn(labels, pred_fake)
|
140 |
+
|
141 |
+
# compute gradients then update the gradients
|
142 |
+
gradients = tape.gradient(g_loss, self.generator.trainable_variables)
|
143 |
+
self.g_optimizer.apply_gradients(zip(gradients, self.generator.trainable_variables))
|
144 |
+
|
145 |
+
|
146 |
+
# update states for both models
|
147 |
+
self.d_loss_metric.update_state(d_loss)
|
148 |
+
self.g_loss_metric.update_state(g_loss)
|
149 |
+
|
150 |
+
return {'d_loss': self.d_loss_metric.result(), 'g_loss': self.g_loss_metric.result()}
|
151 |
+
|
152 |
+
# Custom Callback to display image per every train step
|
153 |
+
class DCGANMonitor(keras.callbacks.Callback):
|
154 |
+
def __init__(self, num_imgs=25, latent_dim=100):
|
155 |
+
self.num_imgs = num_imgs
|
156 |
+
self.latent_dim = latent_dim
|
157 |
+
self.noise = tf.random.normal([25, latent_dim])
|
158 |
+
|
159 |
+
def on_epoch_end(self, epoch, logs=None):
|
160 |
+
|
161 |
+
# generate the image from noise
|
162 |
+
g_img = self.model.generator(self.noise)
|
163 |
+
|
164 |
+
# denormalize the image
|
165 |
+
g_img = (g_img * 127.5) + 127.5
|
166 |
+
g_img.numpy()
|
167 |
+
|
168 |
+
# plot the image per step
|
169 |
+
fig = plt.figure(figsize=(8, 8))
|
170 |
+
for i in range(self.num_imgs):
|
171 |
+
plt.subplot(5, 5, i+1)
|
172 |
+
img = array_to_img(g_img[i])
|
173 |
+
plt.imshow(img)
|
174 |
+
plt.axis('off')
|
175 |
+
# plt.savefig('epoch_{:03d}.png'.format(epoch))
|
176 |
+
plt.show()
|
177 |
+
|
178 |
+
def on_train_end(self, logs=None):
|
179 |
+
self.model.generator.save('generator.h5')
|