/
sample_density.py
208 lines (173 loc) · 7.69 KB
/
sample_density.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
"""Script to plot approximated probability distributions of each component
of noise vectors. For the initial inputs, these should simply match N(0,1),
as they were sampled from that. After LIS modules, the probability
distributions are expected to change.
You must have trained a G-LIS to use this.
Example:
python sample_density.py --image_size 80 --code_size 256 --norm weight \
--r_iterations 1 \
--load_path_g /path/to/checkpoints/exp01/net_archive/last_gen.pt \
--save_path /path/to/checkpoints/exp01/density/
"""
from __future__ import print_function, division
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import argparse
import math
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.datasets as datasets
import torchvision.transforms as transforms
from torch.autograd import Variable
import os
import os.path
import numpy as np
import imgaug as ia
from scipy import misc
import time
import random
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('whitegrid')
from common import plotting
from common.model import *
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--batch_size', type = int, default = 256,
help = 'input batch size')
parser.add_argument('--image_size', type = int, default = -1,
help = 'image size')
parser.add_argument('--width', type = int, default = -1,
help = 'image width')
parser.add_argument('--height', type = int, default = -1,
help = 'image height')
parser.add_argument('--code_size', type = int, default = 128,
help = 'size of latent code')
parser.add_argument('--nfeature', type = int, default = 64,
help = 'number of features of first conv layer')
parser.add_argument('--nlayer', type = int, default = -1,
help = 'number of down/up conv layers')
parser.add_argument('--norm', default = 'none',
help = 'type of normalization: none | batch | weight | weight-affine')
parser.add_argument('--load_path_g', required = True,
help = 'path of G to use')
parser.add_argument('--save_path', required = True,
help = 'path to save sampled images to')
parser.add_argument('--r_iterations', type = int, default = 3,
help = 'how many LIS modules to use in G')
parser.add_argument('--nb_points', type = int, default = 500000,
help = 'number of points from which to estimate densities')
parser.add_argument('--g_upscaling', default='fractional',
help = 'upscaling method to use in G: fractional|nearest|bilinear')
opt = parser.parse_args()
print(opt)
if (opt.height > 0) and (opt.width > 0):
pass
elif opt.image_size > 0:
opt.height = opt.image_size
opt.width = opt.image_size
else:
raise ValueError('must specify valid image size')
if opt.nlayer < 0:
opt.nlayer = 0
s = max(opt.width, opt.height)
while s >= 8:
s = (s + 1) // 2
opt.nlayer = opt.nlayer + 1
gen = GeneratorLearnedInputSpace(opt.width, opt.height, opt.nfeature, opt.nlayer, opt.code_size, opt.norm, n_lis_layers=opt.r_iterations, upscaling=opt.g_upscaling)
print(gen)
gen.cuda()
gen.load_state_dict(torch.load(opt.load_path_g))
gen.eval()
makedirs(opt.save_path)
print("Generating points...")
codes = torch.randn(opt.nb_points, opt.code_size).cuda()
codes_r = [generate_codes_by_r(gen, codes, r_idx, opt.batch_size) for r_idx in range(1+opt.r_iterations)]
np.set_printoptions(precision=6, suppress=True)
print("means before LIS", np.mean(codes_r[0], axis=0))
print("std before LIS", np.std(codes_r[0], axis=0))
print("Plotting...")
for v_idx in range(50):
lines_r = [points_to_line(codes_r[r_idx][:, v_idx]) for r_idx in range(opt.r_iterations+1)]
save_lines(lines_r, v_idx, opt.save_path)
fig, ax = plt.subplots(nrows=1, ncols=1)
for r_idx in range(1+opt.r_iterations):
xx, yy = lines_r[r_idx]
ax.plot(xx, yy, label="after %d LIS modules" % (r_idx,))
ax.set_xlim(-6, 6)
ax.set_ylim(0, 0.06)
ax.legend()
fig.savefig(os.path.join(opt.save_path, 'density_plots', 'density_all_v%03d.jpg' % (v_idx,)), bbox_inches="tight")
plt.close()
for r_idx in range(1+opt.r_iterations):
fig, ax = plt.subplots(nrows=1, ncols=1)
xx, yy = lines_r[0]
ax.plot(xx, yy, label="after 0 LIS modules")
if r_idx > 0:
xx, yy = lines_r[r_idx]
ax.plot(xx, yy, c="red", label="after %d LIS modules" % (r_idx,))
ax.set_xlim(-6, 6)
ax.set_ylim(0, 0.06)
fig.savefig(os.path.join(opt.save_path, 'density_plots', 'density_r%02d_v%03d.jpg' % (r_idx, v_idx,)), bbox_inches="tight")
plt.close()
sns_plot = None
for r_idx in range(1+opt.r_iterations):
sns_plot = sns.kdeplot(codes_r[r_idx][:, v_idx], bw=0.35, label="%d lis modules" % (r_idx,))
sns_plot.set(xlim=(-6, 6))
sns_plot.set(ylim=(-0, 0.5))
plt.legend()
fig = sns_plot.get_figure()
fig.savefig(os.path.join(opt.save_path, 'density_plots', 'density_kde_all_v%03d.jpg' % (v_idx,)), bbox_inches="tight")
plt.clf()
for r_idx in range(1+opt.r_iterations):
sns_plot = None
sns_plot = sns.kdeplot(codes_r[0][v_idx], bw=0.35, label="%d lis modules" % (0,))
if r_idx > 0:
sns_plot = sns.kdeplot(codes_r[r_idx][:, v_idx], bw=0.35, label="%d lis modules" % (r_idx,))
sns_plot.set(xlim=(-6, 6))
sns_plot.set(ylim=(-0, 0.5))
plt.legend()
fig = sns_plot.get_figure()
fig.savefig(os.path.join(opt.save_path, 'density_plots', 'density_kde_r%02d_v%03d.jpg' % (r_idx, v_idx,)), bbox_inches="tight")
plt.clf()
def makedirs(save_path):
if not os.path.exists(save_path):
os.makedirs(save_path)
for sub_folder in ['density_plots']:
if not os.path.exists(os.path.join(save_path, sub_folder)):
os.mkdir(os.path.join(save_path, sub_folder))
def generate_codes_by_r(gen, code, n_execute_lis_layers, batch_size):
if n_execute_lis_layers == 0:
result = code.cpu().numpy()
else:
codes_all = []
for i in range((code.size(0) - 1) // batch_size + 1):
this_batch_size = min(batch_size, code.size(0) - i * batch_size)
batch_code = Variable(code[i * batch_size : i * batch_size + this_batch_size])
generated_images, codes_result = gen(batch_code, n_execute_lis_layers=n_execute_lis_layers)
codes_result = codes_result[-1]
codes_result = codes_result.data.cpu().numpy()
codes_all.extend(list(codes_result))
result = np.array(codes_all, dtype=np.float32)
return np.clip(result, -6, 6)
def points_to_line(values, nb_bins=100):
print("[points_to_line]", values.shape)
heights, bins = np.histogram(values, bins=nb_bins)
# Normalize
heights = heights / float(sum(heights))
bin_mids = bins[:-1] + np.diff(bins) / 2.
return bin_mids, heights
def save_lines(lines_r, v_idx, save_path):
for r_idx in range(len(lines_r)):
xx, yy = lines_r[r_idx]
fp = os.path.join(save_path, 'density_plots', 'density_r%02d_v%03d.csv' % (r_idx, v_idx,))
with open(fp, "w") as f:
f.write("x,y\n")
for x, y in zip(xx, yy):
f.write("%.8f,%.8f\n" % (x, y))
if __name__ == "__main__":
main()