forked from mshahbazi72/transitional-cGAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemo.py
419 lines (296 loc) · 15 KB
/
Demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/usr/bin/env python
# coding: utf-8
# # Generating 4 classes
# In[1]:
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import cv2
import PIL.Image
import torch
import legacy
import dnnlib
import numpy as np
import tqdm.notebook as tqdm
import imageio
import shutil
import os
from PIL import Image
import gradio as gr
import subprocess
import warnings
warnings.filterwarnings("ignore")
# In[2]:
# Path to the trained model
model_path = os.path.join(os.getcwd(), "models", "oct.pkl") # ["NORMAL", "CNV", "DRUSEN", "DME"]
model_path = os.path.join(os.getcwd(), "models", "oct_256_resized_1600.pkl") # ["DRUSEN", "NORMAL", "DME", "CNV"]
# Directory where temporary files should be stored
directory = os.path.join(os.getcwd(), "demo")
# Check if the directory exists
if os.path.exists(directory):
# If it exists, delete it and its contents
shutil.rmtree(directory)
# Create the directory
os.makedirs(directory)
# Available classes to choose from
classes = ["DRUSEN", "NORMAL", "DME", "CNV"]
classes_dict = {"DRUSEN": 0, "NORMAL": 1, "DME": 2, "CNV": 3}
# Torch device
device = torch.device('cuda')
# # Generate an image
# In[3]:
# Method to generate an image by choosing out of four classes
def generate_noise_image(choice):
random_seed = np.random.randint(1, 9999)
execution = f"python generate.py --outdir={directory} --seeds={random_seed} --network={model_path} --class={classes_dict[choice]} --vector-mode=True"
os.system(execution)
img_path = os.path.join(os.getcwd(), directory, f"{classes_dict[choice]}_{str(random_seed).zfill(4)}.png")
img = Image.open(img_path)
return img
iface1 = gr.Interface(
fn=generate_noise_image,
inputs=gr.inputs.Dropdown(choices=classes, default="NORMAL", label="Class"),
outputs=gr.outputs.Image(type="pil").style(height=256, width=256)
)
# # Interpolating between x images (z-vectors)
# In[4]:
def combine_images(image1_path, image2_path, output_path):
# Open the images
image1 = Image.open(image1_path)
image2 = Image.open(image2_path)
# Determine the maximum width and total height
max_width = max(image1.width, image2.width)
total_height = max(image1.height, image2.height)
# Create a new blank image with the combined size
combined_image = Image.new("RGB", (max_width * 2, total_height))
# Paste the first image on the left side
combined_image.paste(image1, (0, 0))
# Paste the second image on the right side
combined_image.paste(image2, (max_width, 0))
# Save the combined image
combined_image.save(output_path)
# In[5]:
# Method to create a video of an interpolation between x images
def generate_noise_image(sequence, frames):
try:
shutil.rmtree(directory)
except OSError as error:
print(f"Failed to delete directory '{directory}': {error}")
counts = [sequence.count(str(i)) for i in range(4)]
random_seeds = np.sort(np.random.randint(1, 9999, sum(counts)))
lvecs = []
generated_images = [] # Store the generated images
for i in range(sum(counts)):
execution = f"python generate.py --outdir={directory} --seeds={random_seeds[i]} --network={model_path} --class={sequence[i]} --vector-mode=True"
os.system(execution)
generated_image_path = os.path.join(directory, f"{sequence[i]}_{str(random_seeds[i]).zfill(4)}.png")
# Read the image using PIL
# image = Image.open(generated_image_path)
# Convert the image to a NumPy array
# image_array = np.array(image)
generated_images.append(generated_image_path)
################################################################################################################
class_idx = int(sequence[i])
with dnnlib.util.open_url(model_path) as fp:
G = legacy.load_network_pkl(fp)['G_ema'].requires_grad_(False).to(device)
@torch.no_grad()
def map_z_to_w(z, G):
label = torch.zeros((1, G.c_dim), device=device)
label[:, class_idx] = 1
w = G.mapping(z.to(device), label)
return w
# Load z from file.
z_path = os.path.join(os.getcwd(), "demo", f"{sequence[i]}_{str(random_seeds[i]).zfill(4)}.npy")
z_np = np.load(z_path)
# Convert `z_np` to a PyTorch tensor.
z = torch.as_tensor(z_np, device=device)
# Convert z to w.
w = map_z_to_w(z, G)
################################################################################################################
lvecs.append(w)
FPS = 60
FREEZE_STEPS = 30
STEPS = int(frames)
with dnnlib.util.open_url(model_path) as fp:
G = legacy.load_network_pkl(fp)['G_ema'].requires_grad_(False).to(device) # type: ignore
video = imageio.get_writer(f'{directory}/video.mp4', mode='I', fps=FPS, codec='libx264', bitrate='16M')
for i in range(sum(counts) - 1):# load z_arr from npz file
diff = lvecs[i+1] - lvecs[i]
step = diff / STEPS
current = lvecs[i].clone()
target_uint8 = np.array([256, 256, 3], dtype=np.uint8)
for j in range(STEPS):
z = current.to(device)
synth_image = G.synthesis(z, noise_mode='const')
synth_image = (synth_image + 1) * (255 / 2)
synth_image = synth_image.permute(0, 2, 3, 1).clamp(0, 255).to(torch.uint8)[0].cpu().numpy()
repeat = FREEZE_STEPS if j == 0 or j == (STEPS - 1) else 1
for i in range(repeat):
video.append_data(synth_image)
current = current + step
video.close()
out_img = os.path.join(directory, 'combined.png')
combine_images(generated_images[0], generated_images[1], out_img)
vid = os.path.join(directory, 'video.mp4')
img = os.path.join(directory, 'combined.png')
return vid, img #generated_images[0], generated_images[1]
sequence = gr.inputs.Textbox(default="13", label="Sequence input")
frames = gr.inputs.Number(default=240, label="Frames")
output_video = gr.outputs.Video(type="mp4").style(height=256, width=256)
output_image = gr.outputs.Image(type="filepath", label="Generated Images").style(height=256, width=512)
# output_image1 = gr.outputs.Image(type="numpy", label="Generated Image 1")
# output_image1 = gr.outputs.Image(type="filepath", label="Generated Image 1").style(height=256, width=256)
# output_image2 = gr.outputs.Image(type="filepath", label="Generated Image 2").style(height=256, width=256)
# output = gr.outputs.Video(type="numpy").style(height=256, width=256)
iface2 = gr.Interface(
fn=generate_noise_image,
inputs=[sequence, frames],
outputs=[output_video, output_image],
description="This Gradio interface generates a video by morphing in the latent space between given class images. To use it, enter a string consisting only of letters from 0-3 to serve as class labels."
)
# # Interpolating between x images (approximation)
# In[6]:
# Method to create a video of an interpolation between x images
def generate_noise_image(sequence, frames, n_steps):
try:
shutil.rmtree(directory)
except OSError as error:
print(f"Failed to delete directory '{directory}': {error}")
counts = [sequence.count(str(i)) for i in range(4)]
random_seeds = np.sort(np.random.randint(1, 9999, sum(counts)))
lvecs = []
for i in range(sum(counts)):
execution = f"python generate.py --outdir={directory} --seeds={random_seeds[i]} --network={model_path} --class={sequence[i]}"
os.system(execution)
outdir_path = os.path.join(directory, "projections", str(i).zfill(2))
target_dir = os.path.join(directory, f"{sequence[i]}_{str(random_seeds[i]).zfill(4)}.png")
execution = f"python projector.py --outdir={outdir_path} --target={target_dir} --network={model_path} --num-steps={int(n_steps)} --save-video=True"
os.system(execution)
lvec_path = os.path.join(os.getcwd(), directory, "projections", f"{str(i).zfill(2)}", "projected_w.npz")
lvecs.append(np.load(lvec_path)['w'])
FPS = 60
FREEZE_STEPS = 30
STEPS = int(frames)
with dnnlib.util.open_url(model_path) as fp:
G = legacy.load_network_pkl(fp)['G_ema'].requires_grad_(False).to(device) # type: ignore
video_path = os.path.join(os.getcwd(), directory, "video.mp4")
video = imageio.get_writer(video_path, mode='I', fps=FPS, codec='libx264', bitrate='16M')
for i in range(sum(counts) - 1):
diff = lvecs[i+1] - lvecs[i]
step = diff / STEPS
current = lvecs[i].copy()
target_uint8 = np.array([256, 256, 3], dtype=np.uint8)
for j in range(STEPS):
z = torch.from_numpy(current).to(device)
synth_image = G.synthesis(z, noise_mode='const')
synth_image = (synth_image + 1) * (255 / 2)
synth_image = synth_image.permute(0, 2, 3, 1).clamp(0, 255).to(torch.uint8)[0].cpu().numpy()
repeat = FREEZE_STEPS if j == 0 or j == (STEPS - 1) else 1
for i in range(repeat):
video.append_data(synth_image)
current = current + step
video.close()
return os.path.join(os.getcwd(), directory, "video.mp4")
sequence = gr.inputs.Textbox(default="13", label="Sequence input")
n_steps = gr.inputs.Number(default=100, label="Latent Steps")
frames = gr.inputs.Number(default=240, label="Frames")
output = gr.outputs.Video(type="mp4").style(height=256, width=256)
iface3 = gr.Interface(
fn=generate_noise_image,
inputs=[sequence, frames, n_steps],
outputs=output,
description="This Gradio interface generates a video by morphing in the latent space between given class images. To use it, enter a string consisting only of letters from 0-3 to serve as class labels."
)
# # Generate variations of an image
# In[7]:
# Method to create a video of an interpolation between x images
def generate_noise_image(sequence, frames):
try:
shutil.rmtree(directory)
except OSError as error:
print(f"Failed to delete directory '{directory}': {error}")
counts = [sequence.count(str(i)) for i in range(4)]
random_seeds = [np.random.randint(1, 9999)] * sum(counts)
lvecs = []
for i in range(sum(counts)):
execution = f"python generate.py --outdir={directory} --seeds={random_seeds[i]} --network={model_path} --class={sequence[i]} --vector-mode=True"
# os.system(execution)
subprocess.run(execution, shell=True)
################################################################################################################
class_idx = int(sequence[i])
with dnnlib.util.open_url(model_path) as fp:
G = legacy.load_network_pkl(fp)['G_ema'].requires_grad_(False).to(device)
@torch.no_grad()
def map_z_to_w(z, G):
label = torch.zeros((1, G.c_dim), device=device)
label[:, class_idx] = 1
w = G.mapping(z.to(device), label)
return w
# Load z from file.
# z_path = os.path.join(os.getcwd(), "demo", f"seed{str(random_seeds[i]).zfill(4)}.npy")
z_path = os.path.join(os.getcwd(), "demo", f"{sequence[i]}_{str(random_seeds[i]).zfill(4)}.npy")
z_np = np.load(z_path)
# Convert `z_np` to a PyTorch tensor.
z = torch.as_tensor(z_np, device=device)
# Convert z to w.
w = map_z_to_w(z, G)
################################################################################################################
lvecs.append(w)
FPS = 60
FREEZE_STEPS = 30
STEPS = int(frames)
with dnnlib.util.open_url(model_path) as fp:
G = legacy.load_network_pkl(fp)['G_ema'].requires_grad_(False).to(device) # type: ignore
video = imageio.get_writer(f'{directory}/video.mp4', mode='I', fps=FPS, codec='libx264', bitrate='16M')
for i in range(sum(counts) - 1):# load z_arr from npz file
diff = lvecs[i+1] - lvecs[i]
step = diff / STEPS
current = lvecs[i].clone()
target_uint8 = np.array([256, 256, 3], dtype=np.uint8)
for j in range(STEPS):
z = current.to(device)
synth_image = G.synthesis(z, noise_mode='const')
synth_image = (synth_image + 1) * (255 / 2)
synth_image = synth_image.permute(0, 2, 3, 1).clamp(0, 255).to(torch.uint8)[0].cpu().numpy()
repeat = FREEZE_STEPS if j == 0 or j == (STEPS - 1) else 1
for i in range(repeat):
video.append_data(synth_image)
current = current + step
video.close()
return f"{directory}/video.mp4"
sequence = gr.inputs.Textbox(default="13", label="Sequence input")
frames = gr.inputs.Number(default=240, label="Frames")
output = gr.outputs.Video(type="mp4").style(height=256, width=256)
iface4 = gr.Interface(
fn=generate_noise_image,
inputs=[sequence, frames],
outputs=output,
description="This Gradio interface generates a video by morphing in the latent space between given class images. To use it, enter a string consisting only of letters from 0-3 to serve as class labels."
)
# # Approximate a given image in the latent space
# In[8]:
def edit_image(input_image, num_steps=1000):
pil_image = Image.fromarray(input_image)
pil_image.save(os.path.join(directory, 'input_image.png'))
execution = f"python projector.py --outdir={directory} --network={model_path} --target={os.path.join(directory, 'input_image.png')} --num-steps={int(num_steps)}"
os.system(execution)
# return execution #os.path.join(directory, 'proj.png')
return os.path.join(directory, 'proj.png'), os.path.join(directory, 'proj.mp4')
steps = gr.inputs.Number(default=1000, label="Steps")
input_image = gr.outputs.Image(type="numpy").style(height=256, width=256)
output_image = gr.outputs.Image(type="pil").style(height=256, width=256)
output_video = gr.outputs.Video(type="mp4").style(height=256, width=512)
iface5 = gr.Interface(
fn=edit_image,
inputs=[input_image, steps],
input_names=["Input Image", "Number of Steps"],
input_labels=["Choose an image", "Number of Steps"],
outputs=[output_image, output_video],
output_names=["Edited Image", "Video File"],
output_labels=["Edited Image", "Video File"],
input_is_default=[True, False],
description="This Gradio interface generates a latent approximation using an input image.")
# # Multiple Tabs
# In[9]:
gr.TabbedInterface(
[iface1, iface2, iface3, iface4, iface5], ["Generate an image", "Generate a video (original)", "Generate a video (approximated)", "Generate variations of an image", "Project an image"]
).launch(share=True)