-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathplotting.py
132 lines (98 loc) · 3.65 KB
/
plotting.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
"""
Helper functions for plotting when training autoencoders and RBMs.
"""
import os
import numpy as np
import numpy.random as npr
import matplotlib.pyplot as plt
def display_available():
return ('DISPLAY' in os.environ)
def show(image, ax=None, vlims=None, invert=False):
kwargs = dict(interpolation='none')
if image.ndim == 2:
kwargs['cmap'] = 'gray' if not invert else 'gist_yarg'
if vlims is not None:
kwargs['vmin'], kwargs['vmax'] = vlims
elif image.ndim == 3:
assert image.shape[2] == 3
if vlims is not None:
image = (image.clip(*vlims) - vlims[0]) / (vlims[1] - vlims[0])
else:
raise ValueError("Wrong number of image dimensions")
if ax is None: ax = plt.gca()
ax.imshow(image, **kwargs)
return ax
def tile(images, ax=None, rows=16, cols=24, random=False,
grid=False, gridwidth=1, gridcolor='r', **show_params):
"""
Plot tiled images to the current axis
:images Each row is one flattened image
"""
n_images = images.shape[0]
imshape = images.shape[1:]
m, n = imshape[:2]
n_channels = imshape[2] if len(imshape) > 2 else 1
inds = np.arange(n_images)
if random:
npr.shuffle(inds)
img_shape = (m*rows, n*cols)
if n_channels > 1:
img_shape = img_shape + (n_channels,)
img = np.zeros(img_shape, dtype=images.dtype)
for ind in xrange(min(rows*cols, n_images)):
i,j = (ind / cols, ind % cols)
img[i*m:(i+1)*m, j*n:(j+1)*n] = images[inds[ind]]
ax = show(img, ax=ax, **show_params)
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
if grid:
for i in xrange(1,rows):
ax.plot([-0.5, img.shape[1]-0.5], [i*m-0.5, i*m-0.5], '-',
color=gridcolor, linewidth=gridwidth)
for j in xrange(1,cols):
ax.plot([j*n-0.5, j*n-0.5], [-0.5, img.shape[0]-0.5], '-',
color=gridcolor, linewidth=gridwidth)
ax.set_xlim([-0.5, img.shape[1]-0.5])
ax.set_ylim([-0.5, img.shape[0]-0.5])
ax.invert_yaxis()
def compare(imagesetlist,
ax=None, rows=5, cols=20, vlims=None, grid=True, random=False):
d = len(imagesetlist)
n_images = imagesetlist[0].shape[0]
imshape = imagesetlist[0].shape[1:]
m, n = imshape[:2]
n_channels = imshape[2] if len(imshape) > 2 else 1
inds = np.arange(n_images)
if random:
npr.shuffle(inds)
img_shape = (d*m*rows, n*cols)
if n_channels > 1:
img_shape = img_shape + (n_channels,)
img = np.zeros(img_shape, dtype=imagesetlist[0].dtype)
for ind in range(min(rows*cols, n_images)):
i,j = (ind / cols, ind % cols)
for k in xrange(d):
img[(d*i+k)*m:(d*i+k+1)*m, j*n:(j+1)*n] = \
imagesetlist[k][inds[ind],:].reshape(imshape)
ax = show(img, ax=ax, vlims=vlims)
if grid:
for i in xrange(1,rows):
ax.plot( [-0.5, img.shape[1]-0.5], (d*i*m-0.5)*np.ones(2), 'r-' )
for j in xrange(1,cols):
ax.plot( [j*n-0.5, j*n-0.5], [-0.5, img.shape[0]-0.5], 'r-')
ax.set_xlim([-0.5, img.shape[1]-0.5])
ax.set_ylim([-0.5, img.shape[0]-0.5])
ax.invert_yaxis()
def activations(acts, func, ax=None):
if ax is None:
ax = plt.gca()
N = acts.size
nbins = max(np.sqrt(N), 10)
minact, maxact = (-2, 2)
ax.hist(acts.ravel(), bins=nbins, range=(minact,maxact), normed=True)
x = np.linspace(minact, maxact, 101)
ax.plot(x, func(x))
ax.set_xlim([minact, maxact])
def filters(filters, ax=None, **kwargs):
std = filters.std()
tile(filters, ax=ax, vlims=(-2*std, 2*std), grid=True, **kwargs)