forked from NVlabs/stylegan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretrained_example.py
executable file
·201 lines (148 loc) · 6.27 KB
/
pretrained_example.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
# Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
#
# This work is licensed under the Creative Commons Attribution-NonCommercial
# 4.0 International License. To view a copy of this license, visit
# http://creativecommons.org/licenses/by-nc/4.0/ or send a letter to
# Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
"""Minimal script for generating an image using pre-trained StyleGAN generator."""
import os
import pickle
import numpy as np
import PIL.Image
from PIL import Image
import dnnlib
import dnnlib.tflib as tflib
import config
from training import misc
fmt = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
rnd = np.random.RandomState(5)
num_classes = 10
def main():
# Initialize TensorFlow.
tflib.init_tf()
# Load pre-trained network.
url = 'https://drive.google.com/uc?id=1MEGjdvVpUsu1jB4zrXZN7Y4kBBOzizDQ' # karras2019stylegan-ffhq-1024x1024.pkl
with dnnlib.util.open_url(url, cache_dir=config.cache_dir) as f:
_G, _D, Gs = pickle.load(f)
# _G = Instantaneous snapshot of the generator. Mainly useful for resuming a previous training run.
# _D = Instantaneous snapshot of the discriminator. Mainly useful for resuming a previous training run.
# Gs = Long-term average of the generator. Yields higher-quality results than the instantaneous snapshot.
# Print network details.
Gs.print_layers()
# Pick latent vector.
latents = rnd.randn(1, Gs.input_shape[1])
# Generate image.
images = Gs.run(latents, None, truncation_psi=0.7, randomize_noise=True, output_transform=fmt)
# Save image.
os.makedirs(config.result_dir, exist_ok=True)
png_filename = os.path.join(config.result_dir, 'example.png')
PIL.Image.fromarray(images[0], 'RGB').save(png_filename)
def main_conditional():
# Initialize TensorFlow
tflib.init_tf()
# Load pre-trained network
dir = 'results/00004-sgan-cifar10-1gpu-cond/'
fn = 'network-snapshot-010372.pkl'
_G, _D, Gs = pickle.load(open(os.path.join(dir,fn), 'rb'))
# Print network details
Gs.print_layers()
# rnd = np.random.RandomState(10)
# Initialize conditioning
conditioning = np.eye(num_classes)
for i, rnd in enumerate([np.random.RandomState(i) for i in np.arange(20)]):
# Pick latent vector.
latents = rnd.randn(num_classes, Gs.input_shape[1])
# Generate image.
images = Gs.run(latents, conditioning, truncation_psi=0.7, randomize_noise=True, output_transform=fmt)
images = images.reshape(32*10, 32, 3)
# Save image.
png_filename = os.path.join(dir, 'example_{}.png'.format(i))
PIL.Image.fromarray(images, 'RGB').save(png_filename)
def main_binary():
# Initialize Tensorflow
tflib.init_tf()
# Load pre-trained network
dir = 'results/00005-sgancelebahq-binary-1gpu-cond-wgangp/'
dir = 'results/00006-sgancelebahq-binary-1gpu-cond-wgangp/'
fn = 'network-snapshot-006926.pkl'
_, _, Gs = pickle.load(open(os.path.join(dir,fn), 'rb'))
# Print network details
Gs.print_layers()
# Create binary attributes
# eyeglasses, male, black_hair, smiling, young
classes = {
'5_o_Clock_Shadow': 0,
'Arched_Eyebrows': 0,
'Attractive': 1,
'Bags_Under_Eyes': 0,
'Bald': 0,
'Bangs': 0,
'Big_Lips': 0,
'Big_Nose': 0,
'Black_Hair': 0,
'Blond_Hair': 0,
'Blurry': 0,
'Brown_Hair': 1,
'Bushy_Eyebrows': 0,
'Chubby': 0,
'Double_Chin': 0,
'Eyeglasses': 0,
'Goatee': 0,
'Gray_Hair': 0,
'Heavy_Makeup': 1,
'High_Cheekbones': 1,
'Male': 0,
'Mouth_Slightly_Open': 1,
'Mustache': 0,
'Narrow_Eyes': 0,
'No_Beard': 0,
'Oval_Face': 1,
'Pale_Skin': 0,
'Pointy_Nose': 0,
'Receding_Hairline': 0,
'Rosy_Cheeks': 0,
'Sideburns': 0,
'Smiling': 0,
'Straight_Hair': 0,
'Wavy_Hair': 1,
'Wearing_Earrings': 0,
'Wearing_Hat': 0,
'Wearing_Lipstick': 1,
'Wearing_Necklace': 0,
'Wearing_Necktie': 0,
'Young': 1
}
print([attr for (attr,key) in classes.items() if key==1])
binary = np.array(list(classes.values())).reshape(1,-1)
for i, rnd in enumerate([np.random.RandomState(i) for i in np.arange(20)]):
latent = rnd.randn(1, Gs.input_shape[1])
image = Gs.run(latent, binary, truncation_psi=0.7, randomize_noise=True, output_transform=fmt)
image = image.reshape(256,256,3)
png_filename = os.path.join(dir, 'examples/example{}.png'.format(i))
PIL.Image.fromarray(image, 'RGB').save(png_filename)
def main_textual():
# Initialize Tensorflow
tflib.init_tf()
dir = 'results/00015-sgancoco_train-1gpu-cond'
fn = 'network-snapshot-025000.pkl'
_, _, Gs = pickle.load(open(os.path.join(dir,fn), 'rb'))
# Print network details
Gs.print_layers()
embeddings = np.load('datasets/coco_test/coco_test-rxx.labels')
fns=np.load('datasets/coco_test/fns.npy')
# Use only 1 description (instead of all 5, to compare to attnGAN)
embeddings = embeddings[0::5]
fns = fns[0::5]
for i, rnd in enumerate([np.random.RandomState(i) for i in np.arange(embeddings.shape[0])]):
latent = rnd.randn(1, Gs.input_shape[1])
emb = embeddings[i].reshape(1,-1)
image = Gs.run(latent, emb, truncation_psi=0.8, randomize_noise=True, output_transform=fmt)
image = image.reshape(256,256,3)
png_filename = os.path.join(dir, 'examples/{}.png'.format(fns[i]))
image = Image.fromarray(image)
image.save(png_filename)
if __name__ == "__main__":
# main()
# main_conditional()
# main_binary()
main_textual()