Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pytorch_pretrained_biggan import (BigGAN, one_hot_from_names, truncated_noise_sample,
|
2 |
+
save_as_images, display_in_terminal)
|
3 |
+
initial_archi = 'biggan-deep-128' #@param ['biggan-deep-128', 'biggan-deep-256', 'biggan-deep-512'] {allow-input: true}
|
4 |
+
|
5 |
+
gan_model = BigGAN.from_pretrained(initial_archi).cuda().eval()
|
6 |
+
|
7 |
+
# Prepare a input
|
8 |
+
truncation = 0.4
|
9 |
+
class_vector = one_hot_from_names(initial_class, batch_size=1)
|
10 |
+
noise_vector = truncated_noise_sample(truncation=truncation, batch_size=1)
|
11 |
+
|
12 |
+
# All in tensors
|
13 |
+
noise_vector = torch.from_numpy(noise_vector)
|
14 |
+
class_vector = torch.from_numpy(class_vector)
|
15 |
+
|
16 |
+
# If you have a GPU, put everything on cuda
|
17 |
+
noise_vector = noise_vector.to('cuda')
|
18 |
+
class_vector = class_vector.to('cuda')
|
19 |
+
gan_model.to('cuda')
|
20 |
+
|
21 |
+
# Generate an image
|
22 |
+
with torch.no_grad():
|
23 |
+
output = gan_model(noise_vector, class_vector, truncation)
|
24 |
+
|
25 |
+
# If you have a GPU put back on CPU
|
26 |
+
output = output.to('cpu')
|
27 |
+
|
28 |
+
# If you have a sixtel compatible terminal you can display the images in the terminal
|
29 |
+
# (see https://github.com/saitoha/libsixel for details)
|
30 |
+
#display_in_terminal(output)
|
31 |
+
|
32 |
+
# Save results as png images
|
33 |
+
save_as_images(output)
|