-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSample.py
More file actions
234 lines (188 loc) · 7.3 KB
/
Sample.py
File metadata and controls
234 lines (188 loc) · 7.3 KB
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
# Copyright 2019 Stanislav Pidhorskyi
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
from __future__ import print_function
import torch.utils.data
from scipy import misc
from torch import optim
from torchvision.utils import save_image
from net import *
import numpy as np
import pickle
import time
import random
import os
from model import Model
from net import *
from tracker import LossTracker
from checkpointer import Checkpointer
from scheduler import ComboMultiStepLR
from dlutils import batch_provider
from dlutils.pytorch.cuda_helper import *
from dlutils.pytorch import count_parameters
from defaults import get_cfg_defaults
import argparse
import logging
import sys
import bimpy
import lreq
lreq.use_implicit_lreq.set(False)
im_size = 128
def process_batch(batch):
data = [misc.imresize(x, [im_size, im_size]).transpose((2, 0, 1)) for x in batch]
x = torch.from_numpy(np.asarray(data, dtype=np.float32)).cuda() / 127.5 - 1.
x = x.view(-1, 3, im_size, im_size)
return x
def place(canvas, image, x, y):
image = image.cpu().detach().numpy()
im_size = image.shape[1]
canvas[:, y * im_size : (y + 1) * im_size, x * im_size : (x + 1) * im_size] = image * 0.5 + 0.5
def save_sample(model, sample, i):
os.makedirs('results', exist_ok=True)
with torch.no_grad():
model.eval()
x_rec = model.generate(model.generator.layer_count - 1, 1, z=sample)
def save_pic(x_rec):
resultsample = x_rec * 0.5 + 0.5
resultsample = resultsample.cpu()
save_image(resultsample,
'sample_%i_lr.png' % i, nrow=16)
save_pic(x_rec)
def sample(cfg, logger):
model = Model(
startf=cfg.MODEL.START_CHANNEL_COUNT,
layer_count= cfg.MODEL.LAYER_COUNT,
maxf=cfg.MODEL.MAX_CHANNEL_COUNT,
latent_size=cfg.MODEL.LATENT_SPACE_SIZE,
truncation_psi=cfg.MODEL.TRUNCATIOM_PSI,
truncation_cutoff=cfg.MODEL.TRUNCATIOM_CUTOFF,
mapping_layers=cfg.MODEL.MAPPING_LAYERS,
channels=3)
del model.discriminator
model.eval()
#torch.cuda.manual_seed_all(110)
logger.info("Trainable parameters generator:")
count_parameters(model.generator)
if False:
model_dict = {
'generator': model.generator,
'mapping': model.mapping,
'dlatent_avg': model.dlatent_avg,
}
else:
model_dict = {
'generator_s': model.generator,
'mapping_s': model.mapping,
'dlatent_avg': model.dlatent_avg,
}
checkpointer = Checkpointer(cfg,
model_dict,
logger=logger,
save=True)
file_name = 'karras2019stylegan-ffhq'
# file_name = 'results/model_final'
checkpointer.load(file_name=file_name + '.pth')
# checkpointer.save('final_stripped')
#sample_b = torch.randn(1, cfg.MODEL.LATENT_SPACE_SIZE).view(-1, cfg.MODEL.LATENT_SPACE_SIZE)
# for i in range(100):
# if i % 20 == 0:
# sample_a = sample_b
# sample_b = torch.randn(1, cfg.MODEL.LATENT_SPACE_SIZE).view(-1, cfg.MODEL.LATENT_SPACE_SIZE)
# x = (i % 20) / 20.0
# sample = sample_a * (1.0 - x) + sample_b * x
# save_sample(model, sample, i)
print(model.generator.get_statistics(8))
# print(model.discriminator.get_statistics(8))
ctx = bimpy.Context()
ctx.init(1800, 1600, "Styles")
rnd = np.random.RandomState(5)
latents = rnd.randn(1, cfg.MODEL.LATENT_SPACE_SIZE)
sample = torch.tensor(latents).float().cuda()
def update_image(sample):
with torch.no_grad():
model.eval()
x_rec = model.generate(model.generator.layer_count - 1, 1, z=sample)
resultsample = ((x_rec * 0.5 + 0.5) * 255).type(torch.long).clamp(0, 255)
resultsample = resultsample.cpu()[0, :, :, :]
return resultsample.type(torch.uint8).transpose(0, 2).transpose(0, 1)
im = update_image(sample)
print(im.shape)
im = bimpy.Image(im)
while(not ctx.should_close()):
with ctx:
im = bimpy.Image(update_image(sample))
bimpy.image(im)
# if bimpy.button('Ok'):
if bimpy.button('NEXT'):
latents = rnd.randn(1, cfg.MODEL.LATENT_SPACE_SIZE)
sample = torch.tensor(latents).float().cuda()
# im = bimpy.Image(update_image(sample))
#bimpy.set_window_font_scale(2.0)
exit()
rnd = np.random.RandomState(111011)
latents = rnd.randn(1, cfg.MODEL.LATENT_SPACE_SIZE)
sample = torch.tensor(latents).float().cuda() # torch.randn(16, cfg.MODEL.LATENT_SPACE_SIZE).view(-1, cfg.MODEL.LATENT_SPACE_SIZE)
save_sample(model, sample, 0)
im_count = 16
canvas = np.zeros([3, im_size * (im_count + 2), im_size * (im_count + 2)])
cut_layer_b = 0
cut_layer_e = 2
styles = model.mapping(sample)
styles = list(styles.split(1, 1))
for i in range(im_count):
torch.cuda.manual_seed_all(110)
style = [x[i] for x in styles]
style = torch.cat(style, dim=0)[None, ...]
rec = model.generator.decode(style, cfg.MODEL.LAYER_COUNT - 1, 0.7)
place(canvas, rec[0], 1, 2 + i)
place(canvas, rec[0], 2 + i, 1)
for i in range(im_count):
for j in range(im_count):
style_a = [x[i] for x in styles[:cut_layer_b]]
style_b = [x[j] for x in styles[cut_layer_b:cut_layer_e]]
style_c = [x[i] for x in styles[cut_layer_e:]]
style = style_a + style_b + style_c
torch.cuda.manual_seed_all(110)
style = torch.cat(style, dim=0)[None, ...]
rec = model.generator.decode(style, cfg.MODEL.LAYER_COUNT - 1, 0.7)
place(canvas, rec[0], 2 + i, 2 + j)
save_image(torch.Tensor(canvas), 'reconstruction.png')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Adversarial, hierarchical style VAE")
parser.add_argument(
"--config-file",
default="configs/experiment_stylegan.yaml",
metavar="FILE",
help="path to config file",
type=str,
)
parser.add_argument(
"opts",
help="Modify config options using the command-line",
default=None,
nargs=argparse.REMAINDER,
)
args = parser.parse_args()
cfg = get_cfg_defaults()
cfg.merge_from_file(args.config_file)
cfg.merge_from_list(args.opts)
cfg.freeze()
logger = logging.getLogger("logger")
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler(stream=sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s %(name)s %(levelname)s: %(message)s")
ch.setFormatter(formatter)
logger.addHandler(ch)
sample(cfg, logger)