-
Notifications
You must be signed in to change notification settings - Fork 40
/
plot_utils.py
185 lines (125 loc) · 5.72 KB
/
plot_utils.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
import numpy as np
import matplotlib.pyplot as plt
from scipy.misc import imsave
from scipy.misc import imresize
class Plot_Reproduce_Performance():
def __init__(self, DIR, n_img_x=8, n_img_y=8, img_w=28, img_h=28, resize_factor=1.0):
self.DIR = DIR
assert n_img_x > 0 and n_img_y > 0
self.n_img_x = n_img_x
self.n_img_y = n_img_y
self.n_tot_imgs = n_img_x * n_img_y
assert img_w > 0 and img_h > 0
self.img_w = img_w
self.img_h = img_h
assert resize_factor > 0
self.resize_factor = resize_factor
def save_images(self, images, name='result.jpg'):
images = images.reshape(self.n_img_x*self.n_img_y, self.img_h, self.img_w)
imsave(self.DIR + "/"+name, self._merge(images, [self.n_img_y, self.n_img_x]))
def _merge(self, images, size):
h, w = images.shape[1], images.shape[2]
h_ = int(h * self.resize_factor)
w_ = int(w * self.resize_factor)
img = np.zeros((h_ * size[0], w_ * size[1]))
for idx, image in enumerate(images):
i = int(idx % size[1])
j = int(idx / size[1])
image_ = imresize(image, size=(w_,h_), interp='bicubic')
img[j*h_:j*h_+h_, i*w_:i*w_+w_] = image_
return img
class Plot_Manifold_Learning_Result():
def __init__(self, DIR, n_img_x=20, n_img_y=20, img_w=28, img_h=28, resize_factor=1.0, z_range=4):
self.DIR = DIR
assert n_img_x > 0 and n_img_y > 0
self.n_img_x = n_img_x
self.n_img_y = n_img_y
self.n_tot_imgs = n_img_x * n_img_y
assert img_w > 0 and img_h > 0
self.img_w = img_w
self.img_h = img_h
assert resize_factor > 0
self.resize_factor = resize_factor
assert z_range > 0
self.z_range = z_range
self._set_latent_vectors()
def _set_latent_vectors(self):
# borrowed from https://github.com/fastforwardlabs/vae-tf/blob/master/plot.py
z = np.rollaxis(np.mgrid[self.z_range:-self.z_range:self.n_img_y * 1j, self.z_range:-self.z_range:self.n_img_x * 1j], 0, 3)
self.z = z.reshape([-1, 2])
def save_images(self, images, name='result.jpg'):
images = images.reshape(self.n_img_x*self.n_img_y, self.img_h, self.img_w)
imsave(self.DIR + "/"+name, self._merge(images, [self.n_img_y, self.n_img_x]))
def _merge(self, images, size):
h, w = images.shape[1], images.shape[2]
h_ = int(h * self.resize_factor)
w_ = int(w * self.resize_factor)
img = np.zeros((h_ * size[0], w_ * size[1]))
for idx, image in enumerate(images):
i = int(idx % size[1])
j = int(idx / size[1])
image_ = imresize(image, size=(w_, h_), interp='bicubic')
img[j * h_:j * h_ + h_, i * w_:i * w_ + w_] = image_
return img
# borrowed from https://github.com/ykwon0407/variational_autoencoder/blob/master/variational_bayes.ipynb
def save_scattered_image(self, z, id, name='scattered_image.jpg'):
N = 10
plt.figure(figsize=(8, 6))
plt.scatter(z[:, 0], z[:, 1], c=np.argmax(id, 1), marker='o', edgecolor='none', cmap=discrete_cmap(N, 'jet'))
plt.colorbar(ticks=range(N))
axes = plt.gca()
axes.set_xlim([-self.z_range-2, self.z_range+2])
axes.set_ylim([-self.z_range-2, self.z_range+2])
plt.grid(True)
plt.savefig(self.DIR + "/" + name)
# borrowed from https://gist.github.com/jakevdp/91077b0cae40f8f8244a
def discrete_cmap(N, base_cmap=None):
"""Create an N-bin discrete colormap from the specified input map"""
# Note that if base_cmap is a string or None, you can simply do
# return plt.cm.get_cmap(base_cmap, N)
# The following works for string, None, or a colormap instance:
base = plt.cm.get_cmap(base_cmap)
color_list = base(np.linspace(0, 1, N))
cmap_name = base.name + str(N)
return base.from_list(cmap_name, color_list, N)
class Plot_Analogical_Reasoning_Result():
def __init__(self, DIR, dim_z, img_w=28, img_h=28, resize_factor=1.0, z_range=2):
n_img_x = 11 # first image for template
n_img_y = 4
self.DIR = DIR
assert n_img_x > 0 and n_img_y > 0 and dim_z > 0
self.dim_z = dim_z
self.n_img_x = n_img_x
self.n_img_y = n_img_y
self.n_tot_imgs = n_img_x * n_img_y
assert img_w > 0 and img_h > 0
self.img_w = img_w
self.img_h = img_h
assert resize_factor > 0
self.resize_factor = resize_factor
assert z_range > 0
self.z_range = z_range
self._set_latent_vectors()
def _set_latent_vectors(self):
# In order to maximize style-difference, let's peak 4 points
assert self.n_img_y == 4 and self.dim_z == 2
v = self.z_range * 0.7
z = [[v, v],[-v, v], [v, -v], [-v, -v]]
repeat_shape = list(np.int32(np.ones(self.n_img_y) * self.n_img_x))
z = np.repeat(z,repeat_shape,axis=0)
z = np.clip(z, -self.z_range, self.z_range)
self.z = z
def save_images(self, images, name='analogy_result.jpg'):
images = images.reshape(self.n_img_x*self.n_img_y, self.img_h, self.img_w)
imsave(self.DIR + "/"+name, self._merge(images, [self.n_img_y, self.n_img_x]))
def _merge(self, images, size):
h, w = images.shape[1], images.shape[2]
h_ = int(h * self.resize_factor)
w_ = int(w * self.resize_factor)
img = np.zeros((h_ * size[0], w_ * size[1]))
for idx, image in enumerate(images):
i = int(idx % size[1])
j = int(idx / size[1])
image_ = imresize(image, size=(w_, h_), interp='bicubic')
img[j * h_:j * h_ + h_, i * w_:i * w_ + w_] = image_
return img