Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,19 @@
|
|
| 1 |
import nltk; nltk.download('wordnet')
|
| 2 |
|
| 3 |
#@title Load Model
|
| 4 |
-
selected_model = '
|
| 5 |
|
| 6 |
# Load model
|
| 7 |
-
from IPython.utils import io
|
| 8 |
import torch
|
| 9 |
import PIL
|
| 10 |
import numpy as np
|
| 11 |
import ipywidgets as widgets
|
| 12 |
from PIL import Image
|
| 13 |
-
import imageio
|
| 14 |
from models import get_instrumented_model
|
| 15 |
from decomposition import get_or_compute
|
| 16 |
from config import Config
|
| 17 |
-
from skimage import img_as_ubyte
|
| 18 |
import gradio as gr
|
| 19 |
import numpy as np
|
| 20 |
-
from ipywidgets import fixed
|
| 21 |
|
| 22 |
# Speed up computation
|
| 23 |
torch.autograd.set_grad_enabled(False)
|
|
@@ -65,95 +61,6 @@ for item in lst:
|
|
| 65 |
latent_stdevs.append(comps[item][i])
|
| 66 |
|
| 67 |
|
| 68 |
-
#@title Define functions
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
# Taken from https://github.com/alexanderkuk/log-progress
|
| 72 |
-
def log_progress(sequence, every=1, size=None, name='Items'):
|
| 73 |
-
from ipywidgets import IntProgress, HTML, VBox
|
| 74 |
-
from IPython.display import display
|
| 75 |
-
|
| 76 |
-
is_iterator = False
|
| 77 |
-
if size is None:
|
| 78 |
-
try:
|
| 79 |
-
size = len(sequence)
|
| 80 |
-
except TypeError:
|
| 81 |
-
is_iterator = True
|
| 82 |
-
if size is not None:
|
| 83 |
-
if every is None:
|
| 84 |
-
if size <= 200:
|
| 85 |
-
every = 1
|
| 86 |
-
else:
|
| 87 |
-
every = int(size / 200) # every 0.5%
|
| 88 |
-
else:
|
| 89 |
-
assert every is not None, 'sequence is iterator, set every'
|
| 90 |
-
|
| 91 |
-
if is_iterator:
|
| 92 |
-
progress = IntProgress(min=0, max=1, value=1)
|
| 93 |
-
progress.bar_style = 'info'
|
| 94 |
-
else:
|
| 95 |
-
progress = IntProgress(min=0, max=size, value=0)
|
| 96 |
-
label = HTML()
|
| 97 |
-
box = VBox(children=[label, progress])
|
| 98 |
-
display(box)
|
| 99 |
-
|
| 100 |
-
index = 0
|
| 101 |
-
try:
|
| 102 |
-
for index, record in enumerate(sequence, 1):
|
| 103 |
-
if index == 1 or index % every == 0:
|
| 104 |
-
if is_iterator:
|
| 105 |
-
label.value = '{name}: {index} / ?'.format(
|
| 106 |
-
name=name,
|
| 107 |
-
index=index
|
| 108 |
-
)
|
| 109 |
-
else:
|
| 110 |
-
progress.value = index
|
| 111 |
-
label.value = u'{name}: {index} / {size}'.format(
|
| 112 |
-
name=name,
|
| 113 |
-
index=index,
|
| 114 |
-
size=size
|
| 115 |
-
)
|
| 116 |
-
yield record
|
| 117 |
-
except:
|
| 118 |
-
progress.bar_style = 'danger'
|
| 119 |
-
raise
|
| 120 |
-
else:
|
| 121 |
-
progress.bar_style = 'success'
|
| 122 |
-
progress.value = index
|
| 123 |
-
label.value = "{name}: {index}".format(
|
| 124 |
-
name=name,
|
| 125 |
-
index=str(index or '?')
|
| 126 |
-
)
|
| 127 |
-
|
| 128 |
-
def name_direction(sender):
|
| 129 |
-
if not text.value:
|
| 130 |
-
print('Please name the direction before saving')
|
| 131 |
-
return
|
| 132 |
-
|
| 133 |
-
if num in named_directions.values():
|
| 134 |
-
target_key = list(named_directions.keys())[list(named_directions.values()).index(num)]
|
| 135 |
-
print(f'Direction already named: {target_key}')
|
| 136 |
-
print(f'Overwriting... ')
|
| 137 |
-
del(named_directions[target_key])
|
| 138 |
-
named_directions[text.value] = [num, start_layer.value, end_layer.value]
|
| 139 |
-
save_direction(random_dir, text.value)
|
| 140 |
-
for item in named_directions:
|
| 141 |
-
print(item, named_directions[item])
|
| 142 |
-
|
| 143 |
-
def save_direction(direction, filename):
|
| 144 |
-
filename += ".npy"
|
| 145 |
-
np.save(filename, direction, allow_pickle=True, fix_imports=True)
|
| 146 |
-
print(f'Latent direction saved as {filename}')
|
| 147 |
-
|
| 148 |
-
def mix_w(w1, w2, content, style):
|
| 149 |
-
for i in range(0,5):
|
| 150 |
-
w2[i] = w1[i] * (1 - content) + w2[i] * content
|
| 151 |
-
|
| 152 |
-
for i in range(5, 16):
|
| 153 |
-
w2[i] = w1[i] * (1 - style) + w2[i] * style
|
| 154 |
-
|
| 155 |
-
return w2
|
| 156 |
-
|
| 157 |
def display_sample_pytorch(seed, truncation, directions, distances, scale, start, end, w=None, disp=True, save=None, noise_spec=None):
|
| 158 |
# blockPrint()
|
| 159 |
model.truncation = truncation
|
|
@@ -177,40 +84,9 @@ def display_sample_pytorch(seed, truncation, directions, distances, scale, start
|
|
| 177 |
if disp == False:
|
| 178 |
print(save)
|
| 179 |
final_im.save(f'out/{seed}_{save:05}.png')
|
| 180 |
-
if disp:
|
| 181 |
-
display(final_im)
|
| 182 |
|
| 183 |
return final_im
|
| 184 |
|
| 185 |
-
def generate_mov(seed, truncation, direction_vec, scale, layers, n_frames, out_name = 'out', noise_spec = None, loop=True):
|
| 186 |
-
"""Generates a mov moving back and forth along the chosen direction vector"""
|
| 187 |
-
# Example of reading a generated set of images, and storing as MP4.
|
| 188 |
-
movieName = f'{out_name}.mp4'
|
| 189 |
-
offset = -10
|
| 190 |
-
step = 20 / n_frames
|
| 191 |
-
imgs = []
|
| 192 |
-
for i in log_progress(range(n_frames), name = "Generating frames"):
|
| 193 |
-
print(f'\r{i} / {n_frames}', end='')
|
| 194 |
-
w = model.sample_latent(1, seed=seed).cpu().numpy()
|
| 195 |
-
|
| 196 |
-
model.truncation = truncation
|
| 197 |
-
w = [w]*model.get_max_latents() # one per layer
|
| 198 |
-
for l in layers:
|
| 199 |
-
if l <= model.get_max_latents():
|
| 200 |
-
w[l] = w[l] + direction_vec * offset * scale
|
| 201 |
-
|
| 202 |
-
#save image and display
|
| 203 |
-
out = model.sample_np(w)
|
| 204 |
-
final_im = Image.fromarray((out * 255).astype(np.uint8))
|
| 205 |
-
imgs.append(out)
|
| 206 |
-
#increase offset
|
| 207 |
-
offset += step
|
| 208 |
-
if loop:
|
| 209 |
-
imgs += imgs[::-1]
|
| 210 |
-
with imageio.get_writer(movieName, mode='I') as writer:
|
| 211 |
-
for image in log_progress(list(imgs), name = "Creating animation"):
|
| 212 |
-
writer.append_data(img_as_ubyte(image))
|
| 213 |
-
|
| 214 |
|
| 215 |
#@title Demo UI
|
| 216 |
|
|
@@ -266,6 +142,6 @@ human_head = gr.inputs.Slider(label="Head", minimum=slider_min_val, maximum=slid
|
|
| 266 |
scale = 1
|
| 267 |
|
| 268 |
inputs = [seed, truncation, monster, female, skimpy, light, bodysuit, bulky, human_head, start_layer, end_layer]
|
| 269 |
-
description = "Change the seed number to generate different
|
| 270 |
|
| 271 |
gr.Interface(generate_image, inputs, ["image", "image"], description=description, live=True, title="CharacterGAN").launch()
|
|
|
|
| 1 |
import nltk; nltk.download('wordnet')
|
| 2 |
|
| 3 |
#@title Load Model
|
| 4 |
+
selected_model = 'character'
|
| 5 |
|
| 6 |
# Load model
|
|
|
|
| 7 |
import torch
|
| 8 |
import PIL
|
| 9 |
import numpy as np
|
| 10 |
import ipywidgets as widgets
|
| 11 |
from PIL import Image
|
|
|
|
| 12 |
from models import get_instrumented_model
|
| 13 |
from decomposition import get_or_compute
|
| 14 |
from config import Config
|
|
|
|
| 15 |
import gradio as gr
|
| 16 |
import numpy as np
|
|
|
|
| 17 |
|
| 18 |
# Speed up computation
|
| 19 |
torch.autograd.set_grad_enabled(False)
|
|
|
|
| 61 |
latent_stdevs.append(comps[item][i])
|
| 62 |
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
def display_sample_pytorch(seed, truncation, directions, distances, scale, start, end, w=None, disp=True, save=None, noise_spec=None):
|
| 65 |
# blockPrint()
|
| 66 |
model.truncation = truncation
|
|
|
|
| 84 |
if disp == False:
|
| 85 |
print(save)
|
| 86 |
final_im.save(f'out/{seed}_{save:05}.png')
|
|
|
|
|
|
|
| 87 |
|
| 88 |
return final_im
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
#@title Demo UI
|
| 92 |
|
|
|
|
| 142 |
scale = 1
|
| 143 |
|
| 144 |
inputs = [seed, truncation, monster, female, skimpy, light, bodysuit, bulky, human_head, start_layer, end_layer]
|
| 145 |
+
description = "Change the seed number to generate different character design. Made by <a href='https://www.mfrashad.com/' target='_blank'>@mfrashad</a>. For more details on how to build this, visit the <a href='https://github.com/mfrashad/gancreate-saai' target='_blank'>repo</a>. Please give a star if you find it useful :)"
|
| 146 |
|
| 147 |
gr.Interface(generate_image, inputs, ["image", "image"], description=description, live=True, title="CharacterGAN").launch()
|