-
Notifications
You must be signed in to change notification settings - Fork 14
/
places_gen.py
337 lines (250 loc) · 10.9 KB
/
places_gen.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
import tensorflow as tf
import math
from tqdm import tqdm
from tensorflow.python.platform import flags
from torch.utils.data import DataLoader, Dataset
from models import ResNetModel, CelebAModel
from utils import ReplayBuffer
import os.path as osp
import numpy as np
from logger import TensorBoardOutputFormat
from scipy.misc import imsave
from torchvision import transforms
import os
from itertools import product
from PIL import Image
import torch
from data import CelebAHQOverfit, LSUNBed
from fid import get_fid_score
flags.DEFINE_integer('batch_size', 256, 'Size of inputs')
flags.DEFINE_integer('data_workers', 4, 'Number of workers to do things')
flags.DEFINE_string('logdir', 'cachedir', 'directory for logging')
flags.DEFINE_string('savedir', 'cachedir', 'location where log of experiments will be stored')
flags.DEFINE_integer('num_filters', 64, 'number of filters for conv nets -- 32 for miniimagenet, 64 for omniglot.')
flags.DEFINE_float('step_lr', 1000.0, 'size of gradient descent size')
flags.DEFINE_bool('cclass', True, 'not cclass')
flags.DEFINE_bool('proj_cclass', False, 'use for backwards compatibility reasons')
flags.DEFINE_bool('spec_norm', True, 'Whether to use spectral normalization on weights')
flags.DEFINE_bool('use_bias', True, 'Whether to use bias in convolution')
flags.DEFINE_bool('use_attention', False, 'Whether to use self attention in network')
flags.DEFINE_integer('num_steps', 80, 'number of steps to optimize the label')
flags.DEFINE_string('task', 'negation_figure', 'conceptcombine, combination_figure, negation_figure, or_figure, negation_eval')
flags.DEFINE_bool('eval', False, 'Whether to quantitively evaluate models')
flags.DEFINE_bool('sigmoid', False, 'simoid is not true')
flags.DEFINE_bool('latent_energy', False, 'latent energy in model')
flags.DEFINE_bool('proj_latent', False, 'Projection of latents')
# Whether to train for gentest
flags.DEFINE_bool('train', False, 'whether to train on generalization into multiple different predictions')
FLAGS = flags.FLAGS
def conceptcombineeval(model_list, select_idx):
dataset = LSUNBed()
dataloader = torch.utils.data.DataLoader(
dataset, batch_size=64, shuffle=True, num_workers=4)
n = 64
labels = []
for six in select_idx:
label_ix = np.eye(2)[six]
label_batch = np.tile(label_ix[None, :], (n, 1))
label = torch.Tensor(label_batch).cuda()
labels.append(label)
def get_color_distortion(s=1.0):
# s is the strength of color distortion.
color_jitter = transforms.ColorJitter(0.8*s, 0.8*s, 0.8*s, 0.4*s)
rnd_color_jitter = transforms.RandomApply([color_jitter], p=0.8)
rnd_gray = transforms.RandomGrayscale(p=0.2)
color_distort = transforms.Compose([
rnd_color_jitter,
rnd_gray])
return color_distort
color_transform = get_color_distortion(0.5)
im_size = 128
transform = transforms.Compose([transforms.RandomResizedCrop(im_size, scale=(0.9, 1.0)), transforms.RandomHorizontalFlip(), color_transform, transforms.ToTensor()])
gt_ims = []
fake_ims = []
im = None
for _, data, label in tqdm(dataloader):
gt_ims.extend(list((data.numpy() * 255).astype(np.uint8)))
# if im is None:
im = torch.rand(n, 3, 128, 128).cuda()
im_noise = torch.randn_like(im).detach()
# First get good initializations for sampling
for i in range(10):
for i in range(20):
im_noise.normal_()
im = im + 0.001 * im_noise
# im.requires_grad = True
im.requires_grad_(requires_grad=True)
energy = 0
for model, label in zip(model_list, labels):
energy = model.forward(im, None) + energy
# print("step: ", i, energy.mean())
im_grad = torch.autograd.grad([energy.sum()], [im])[0]
im = im - FLAGS.step_lr * im_grad
im = im.detach()
im = torch.clamp(im, 0, 1)
im = im.detach().cpu().numpy().transpose((0, 2, 3, 1))
im = (im * 255).astype(np.uint8)
ims = []
for i in range(im.shape[0]):
im_i = np.array(transform(Image.fromarray(np.array(im[i]))))
ims.append(im_i)
im = torch.Tensor(np.array(ims)).cuda()
# Then refine the images
for i in range(FLAGS.num_steps):
im_noise.normal_()
im = im + 0.001 * im_noise
# im.requires_grad = True
im.requires_grad_(requires_grad=True)
energy = 0
for model, label in zip(model_list, labels):
energy = model.forward(im, None) + energy
print("step: ", i, energy.mean())
im_grad = torch.autograd.grad([energy.sum()], [im])[0]
im = im - FLAGS.step_lr * im_grad
im = im.detach()
im = torch.clamp(im, 0, 1)
im_cpu = im.detach().cpu()
fake_ims.extend(list((im_cpu.numpy().transpose((0, 2, 3, 1)) * 255).astype(np.uint8)))
if len(gt_ims) > 10000:
break
select_fake_ims = fake_ims[-100:]
select_fake_ims = np.array(select_fake_ims)
select_fake_ims = select_fake_ims.reshape((10, 10, 128, 128, 3)).transpose((0, 2, 1, 3, 4)).reshape((1280, 1280, 3))
imsave("select_sample.png", select_fake_ims)
get_fid_score(gt_ims, fake_ims)
fake_ims = np.array(fake_ims)
fake_ims_flat = fake_ims.reshape(fake_ims.shape[0], -1)
std_im = np.std(fake_ims, axis=0).mean()
print("standard deviation of image", std_im)
import pdb
pdb.set_trace()
print("here")
def conceptcombine(model_list, select_idx):
n = 64
labels = []
for six in select_idx:
label_ix = np.eye(2)[six]
label_batch = np.tile(label_ix[None, :], (n, 1))
label = torch.Tensor(label_batch).cuda()
labels.append(None)
im = torch.rand(n, 3, 128, 128).cuda()
im_noise = torch.randn_like(im).detach()
def get_color_distortion(s=1.0):
# s is the strength of color distortion.
color_jitter = transforms.ColorJitter(0.8*s, 0.8*s, 0.8*s, 0.4*s)
rnd_color_jitter = transforms.RandomApply([color_jitter], p=0.8)
rnd_gray = transforms.RandomGrayscale(p=0.2)
color_distort = transforms.Compose([
rnd_color_jitter,
rnd_gray])
return color_distort
color_transform = get_color_distortion(0.5)
im_size = 128
transform = transforms.Compose([transforms.RandomResizedCrop(im_size, scale=(0.9, 1.0)), transforms.RandomHorizontalFlip(), color_transform, transforms.ToTensor()])
# First get good initializations for sampling
for i in range(10):
for i in range(20):
im_noise.normal_()
im = im + 0.001 * im_noise
# im.requires_grad = True
im.requires_grad_(requires_grad=True)
energy = 0
for model, label in zip(model_list, labels):
energy = model.forward(im, label) + energy
# print("step: ", i, energy.mean())
im_grad = torch.autograd.grad([energy.sum()], [im])[0]
im = im - FLAGS.step_lr * im_grad
im = im.detach()
im = torch.clamp(im, 0, 1)
im = im.detach().cpu().numpy().transpose((0, 2, 3, 1))
im = (im * 255).astype(np.uint8)
ims = []
for i in range(im.shape[0]):
im_i = np.array(transform(Image.fromarray(np.array(im[i]))))
ims.append(im_i)
im = torch.Tensor(np.array(ims)).cuda()
# Then refine the images
for i in range(FLAGS.num_steps):
im_noise.normal_()
im = im + 0.001 * im_noise
# im.requires_grad = True
im.requires_grad_(requires_grad=True)
energy = 0
for model, label in zip(model_list, labels):
energy = model.forward(im, label) + energy
print("step: ", i, energy.mean())
im_grad = torch.autograd.grad([energy.sum()], [im])[0]
im = im - FLAGS.step_lr * im_grad
im = im.detach()
im = torch.clamp(im, 0, 1)
output = im.detach().cpu().numpy()
output = output.transpose((0, 2, 3, 1))
output = output.reshape((-1, 8, 128, 128, 3)).transpose((0, 2, 1, 3, 4)).reshape((-1, 128 * 8, 3))
imsave("debug.png", output)
def combination_figure(sess, kvs, select_idx):
n = 64
print("here")
labels = kvs['labels']
x_mod = kvs['x_mod']
X_NOISE = kvs['X_NOISE']
model_base = kvs['model_base']
weights = kvs['weights']
feed_dict = {}
for i, label in enumerate(labels):
j = select_idx[i]
feed_dict[label] = np.tile(np.eye(2)[j:j+1], (16, 1))
x_noise = np.random.uniform(0, 1, (n, 128, 128, 3))
# x_noise = np.random.uniform(0, 1, (n, 128, 128, 3)) / 2 + np.random.uniform(0, 1, (n, 1, 1, 3)) * 1. / 2
feed_dict[X_NOISE] = x_noise
output = sess.run([x_mod], feed_dict)[0]
output = output.reshape((n * 128, 128, 3))
imsave("debug.png", output)
def negation_figure(sess, kvs, select_idx):
n = 64
labels = kvs['labels']
x_mod = kvs['x_mod']
X_NOISE = kvs['X_NOISE']
model_base = kvs['model_base']
weights = kvs['weights']
feed_dict = {}
for i, label in enumerate(labels):
j = select_idx[i]
feed_dict[label] = np.tile(np.eye(2)[j:j+1], (n, 1))
x_noise = np.random.uniform(0, 1, (n, 128, 128, 3))
feed_dict[X_NOISE] = x_noise
output = sess.run([x_mod], feed_dict)[0]
output = output.reshape((n * 128, 128, 3))
imsave("debug.png", output)
def combine_main(models, resume_iters, select_idx):
model_list = []
for model, resume_iter in zip(models, resume_iters):
model_path = osp.join("cachedir", model, "model_{}.pth".format(resume_iter))
checkpoint = torch.load(model_path)
FLAGS_model = checkpoint['FLAGS']
model_base = CelebAModel(FLAGS_model)
model_base.load_state_dict(checkpoint['ema_model_state_dict_0'])
model_base = model_base.cuda()
model_list.append(model_base)
conceptcombineeval(model_list, select_idx)
if __name__ == "__main__":
models_orig = ['lsun_dataset_uncond']
resume_iters_orig = ["30000"]
##################################
# Settings for the composition_figure
models = [models_orig[0]]
resume_iters = [resume_iters_orig[0]]
select_idx = [1]
# select_idx = [1]
# models = models + [models_orig[0]]
# resume_iters = resume_iters + [resume_iters_orig[0]]
# select_idx = select_idx + [0]
# models = models + [models_orig[2]]
# resume_iters = resume_iters + [resume_iters_orig[2]]
# select_idx = select_idx + [1]
# models = models + [models_orig[3]]
# resume_iters = resume_iters + [resume_iters_orig[3]]
# select_idx = select_idx + [1]
FLAGS.step_lr = FLAGS.step_lr / len(models)
# List of 4 attributes that might be good
# Young -> Female -> Smiling -> Wavy
combine_main(models, resume_iters, select_idx)