本文整理匯總了Python中pylearn2.gui.patch_viewer.PatchViewer.add_patch方法的典型用法代碼示例。如果您正苦於以下問題:Python PatchViewer.add_patch方法的具體用法?Python PatchViewer.add_patch怎麽用?Python PatchViewer.add_patch使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pylearn2.gui.patch_viewer.PatchViewer
的用法示例。
在下文中一共展示了PatchViewer.add_patch方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: show_samples
# 需要導入模塊: from pylearn2.gui.patch_viewer import PatchViewer [as 別名]
# 或者: from pylearn2.gui.patch_viewer.PatchViewer import add_patch [as 別名]
def show_samples(generator,Noise_Dim,data_obj,filename):
if data_obj.pitch_scale:
pitch_max = 1.0
else:
pitch_max = 108.0
rows = 4
sample_cols = 5
input_noise = np.random.uniform(-1.0,1.0,(rows*sample_cols, Noise_Dim))
samples = generator.predict(input_noise)
topo_samples = samples.reshape(samples.shape[0],4,samples.shape[-1]/4)
#get topological_view
pv = PatchViewer(grid_shape=(rows,sample_cols + 1),patch_shape=(4,samples.shape[-1]/4), \
is_color=False)
X = np.concatenate((data_obj.X_train,data_obj.X_val,data_obj.X_test),axis = 0)
topo_X = X
print('Shape of dataset is {}').format(X.shape)
X = X.reshape(X.shape[0],X.shape[1]*X.shape[2])
for i in xrange(topo_samples.shape[0]):
topo_sample = patch_quantize_01(patch_thresholding(topo_samples[i,:]/pitch_max))
pv.add_patch(topo_sample * 2. -1.,rescale=False)
if(i + 1) % sample_cols ==0:
sample = samples[i,:]
dists = np.square(X - sample).sum(axis = 1)
j = np.argmin(dists)
match = patch_quantize_01(patch_thresholding(topo_X[j,:]/pitch_max))
pv.add_patch(match*2-1,rescale=False,activation = 1)
print "Saving %s ..."%filename
pv.save(filename)
示例2: GenerateAndSave
# 需要導入模塊: from pylearn2.gui.patch_viewer import PatchViewer [as 別名]
# 或者: from pylearn2.gui.patch_viewer.PatchViewer import add_patch [as 別名]
class GenerateAndSave(TrainExtension):
"""
Keeps track of what the generator in a (vanilla) GAN returns for a
particular set of noise values.
"""
def __init__(self, generator, save_prefix, batch_size=20, grid_shape=(5, 4)):
assert isinstance(generator, Generator)
self.batch_sym = T.matrix('generate_batch')
self.generate_f = theano.function([self.batch_sym],
generator.dropout_fprop(self.batch_sym)[0])
self.batch = generator.get_noise(batch_size).eval()
self.save_prefix = save_prefix
self.patch_viewer = PatchViewer(grid_shape=grid_shape, patch_shape=(32, 32),
is_color=True)
def on_monitor(self, model, dataset, algorithm):
samples = self.generate_f(self.batch).swapaxes(0, 3)
self.patch_viewer.clear()
for sample in samples:
self.patch_viewer.add_patch(sample, rescale=True)
fname = self.save_prefix + '.%05i.png' % model.monitor.get_epochs_seen()
self.patch_viewer.save(fname)
示例3: create_connect_viewer
# 需要導入模塊: from pylearn2.gui.patch_viewer import PatchViewer [as 別名]
# 或者: from pylearn2.gui.patch_viewer.PatchViewer import add_patch [as 別名]
def create_connect_viewer(N, N1, imgs, count, W2):
"""
Create the patch to show connections between layers.
Parameters
----------
N: int
Number of rows.
N1: int
Number of elements in the first layer.
imgs: ndarray
Images of weights from the first layer.
count: int
Number of elements to show.
W2: list
Second hidden layer.
"""
pv = PatchViewer((N, count), imgs.shape[1:3], is_color=imgs.shape[3] == 3)
for i in xrange(N):
w = W2[:, i]
wneg = w[w < 0.]
wpos = w[w > 0.]
w /= np.abs(w).max()
wa = np.abs(w)
to_sort = zip(wa, range(N1), w)
s = sorted(to_sort)
for j in xrange(count):
idx = s[N1-j-1][1]
mag = s[N1-j-1][2]
if mag > 0:
act = (mag, 0)
else:
act = (0, -mag)
pv.add_patch(imgs[idx, ...], rescale=True, activation=act)
return pv
示例4: create_patch_viewer
# 需要導入模塊: from pylearn2.gui.patch_viewer import PatchViewer [as 別名]
# 或者: from pylearn2.gui.patch_viewer.PatchViewer import add_patch [as 別名]
def create_patch_viewer(grid_shape, vis_chains, m):
"""
Add the patches to show.
Parameters
----------
grid_shape: tuple
The shape of the grid to show.
vis_chains: numpy array
Visibles chains.
m: int
Number of visible chains.
"""
pv = PatchViewer(grid_shape, vis_chains.shape[1:3],
is_color=vis_chains.shape[-1] == 3)
for i in xrange(m):
pv.add_patch(vis_chains[i, :], rescale=False)
return pv
示例5: on_monitor
# 需要導入模塊: from pylearn2.gui.patch_viewer import PatchViewer [as 別名]
# 或者: from pylearn2.gui.patch_viewer.PatchViewer import add_patch [as 別名]
def on_monitor(self, *args, **kwargs):
if not hasattr(self, 'record'):
self.record = {}
self.size = {}
for dataset in self.datasets:
assert tuple(dataset.view_converter.axes) == ('c', 0, 1, 'b')
self.record[dataset] = dataset.get_topological_view().copy()
self.size[dataset] = dataset.X.shape[0]
else:
for i, dataset in enumerate(self.datasets):
size = self.size[dataset]
assert dataset.X.shape[0] == size
self.record[dataset] = np.concatenate((self.record[dataset], dataset.get_topological_view().copy()),
axis=-1)
record_view = self.record[dataset].copy()
record_view /= np.abs(record_view).max()
pv = PatchViewer(grid_shape=(record_view.shape[3]/size, size),
patch_shape = record_view.shape[1:3], is_color = record_view.shape[0] == 3)
for j in xrange(record_view.shape[3]):
pv.add_patch(np.transpose(record_view[:,:,:,j], (1, 2, 0)), rescale=False)
print 'Dataset %d: ' % i
pv.show()
x = raw_input()
示例6: plot
# 需要導入模塊: from pylearn2.gui.patch_viewer import PatchViewer [as 別名]
# 或者: from pylearn2.gui.patch_viewer.PatchViewer import add_patch [as 別名]
def plot(w):
nblocks = int(model.n_g / model.sparse_gmask.bw_g)
filters_per_block = model.sparse_gmask.bw_g * model.sparse_hmask.bw_h
block_viewer = PatchViewer((model.sparse_gmask.bw_g, model.sparse_hmask.bw_h),
(opts.height, opts.width),
is_color = opts.color,
pad=(2,2))
chan_viewer = PatchViewer(get_dims(nblocks),
(block_viewer.image.shape[0],
block_viewer.image.shape[1]),
is_color = opts.color,
pad=(5,5))
main_viewer = PatchViewer(get_dims(nplots),
(chan_viewer.image.shape[0],
chan_viewer.image.shape[1]),
is_color = opts.color,
pad=(10,10))
topo_shape = [opts.height, opts.width, opts.chans]
view_converter = DefaultViewConverter(topo_shape)
if opts.splitblocks:
os.makedirs('filters/')
for chan_i in xrange(nplots):
viewer_dims = slice(0, None) if opts.color else chan_i
for bidx in xrange(nblocks):
for fidx in xrange(filters_per_block):
fi = bidx * filters_per_block + fidx
topo_view = view_converter.design_mat_to_topo_view(w[fi:fi+1,:])
try:
block_viewer.add_patch(topo_view[0,:,:,viewer_dims])
except:
import pdb; pdb.set_trace()
if opts.splitblocks:
pl.imshow(block_viewer.image, interpolation='nearest')
pl.axis('off')
pl.title('Wv - block %i, chan %i' % (bidx, chan_i))
pl.savefig('filters/filters_chan%i_block%i.png' % (bidx, chan_i))
chan_viewer.add_patch(block_viewer.image[:,:,viewer_dims] - 0.5)
block_viewer.clear()
main_viewer.add_patch(chan_viewer.image[:,:,viewer_dims] - 0.5)
chan_viewer.clear()
return copy.copy(main_viewer.image)
示例7: topo_sample_f
# 需要導入模塊: from pylearn2.gui.patch_viewer import PatchViewer [as 別名]
# 或者: from pylearn2.gui.patch_viewer.PatchViewer import add_patch [as 別名]
# Begin modifying axes
base_conditional_data = args.conditional_sampler(generator, n, 1,
embedding_file=args.embedding_file)
print 'Mean for each axis:'
pprint.pprint(zip(args.axes, base_conditional_data[:, args.axes].mean(axis=1)))
base_conditional_data[:, args.axes] -= 0.5 * shift
mod_conditional_data = base_conditional_data.copy()
# Build up a flat array of modified conditional data
mod_conditional_steps = []
for axis in args.axes:
mod_conditional_data[:, axis] += shift
mod_conditional_steps.extend(mod_conditional_data.copy())
mod_conditional_steps = np.array(mod_conditional_steps)
samples_orig = topo_sample_f(noise_data, base_conditional_data).swapaxes(0, 3)
samples_mod = topo_sample_f(np.tile(noise_data, (m, 1)), mod_conditional_steps).swapaxes(0, 3)
pv = PatchViewer(grid_shape=(m + 1, n), patch_shape=(32,32),
is_color=True)
for sample_orig in samples_orig:
pv.add_patch(sample_orig, activation=1)
for sample_mod in samples_mod:
pv.add_patch(sample_mod)
pv.show()
示例8: function
# 需要導入模塊: from pylearn2.gui.patch_viewer import PatchViewer [as 別名]
# 或者: from pylearn2.gui.patch_viewer.PatchViewer import add_patch [as 別名]
rows = 10
cols = 5
X = dataset.get_batch_design(rows * cols )
noise = model.dnce.noise_conditional
assert noise.is_symmetric()
Y = function([],noise.random_design_matrix(sharedX(X)))()
prob = function([],T.nnet.sigmoid(model.free_energy(sharedX(Y))-model.free_energy(sharedX(X))))()
assert prob.ndim == 1
Xt = dataset.get_topological_view(X)
Yt = dataset.get_topological_view(Y)
Xt = dataset.adjust_for_viewer(Xt)
Yt = dataset.adjust_for_viewer(Yt)
pv = PatchViewer( (rows, cols * 2), Xt.shape[1:3], is_color = Xt.shape[-1] == 3)
for i in xrange(Xt.shape[0]):
assert prob[i] >= 0.0
assert prob[i] <= 1.0
assert not np.isnan(prob[i])
assert not np.isinf(prob[i])
pv.add_patch(Xt[i,:,:,:], activation = prob[i])
pv.add_patch(Yt[i,:,:,:], activation = 1. - prob[i])
pv.show()
示例9: FoveatedNORB
# 需要導入模塊: from pylearn2.gui.patch_viewer import PatchViewer [as 別名]
# 或者: from pylearn2.gui.patch_viewer.PatchViewer import add_patch [as 別名]
dataset = FoveatedNORB(which_set=which_set, center=True)
topo = dataset.get_topological_view()
b, r, c, ch = topo.shape
assert ch == 2
pv = PatchViewer((1, 2), (r, c), is_color=False)
i = 0
while True:
patch = topo[i, :, :, :]
patch = patch / np.abs(patch).max()
pv.add_patch(patch[:,:,1], rescale=False)
pv.add_patch(patch[:,:,0], rescale=False)
pv.show()
print(dataset.y[i])
choices = {'g': 'goto image', 'q': 'quit'}
if i + 1 < b:
choices['n'] = 'next image'
choice = get_choice(choices)
if choice == 'q':
quit()
示例10: make_viewer
# 需要導入模塊: from pylearn2.gui.patch_viewer import PatchViewer [as 別名]
# 或者: from pylearn2.gui.patch_viewer.PatchViewer import add_patch [as 別名]
chan_viewer.add_patch(block_viewer.image[:,:,viewer_dims] - 0.5)
block_viewer.clear()
main_viewer.add_patch(chan_viewer.image[:,:,viewer_dims] - 0.5)
chan_viewer.clear()
return copy.copy(main_viewer.image)
viewer_g = make_viewer(wvg, get_dims(model.n_g), (opts.height, opts.width), is_color=True)
viewer_h = make_viewer(wvh, get_dims(model.n_h), (opts.height, opts.width), is_color=True)
w_image = plot(wv)
viewer = PatchViewer((1, 3),
(numpy.max((viewer_g.image.shape[0], viewer_h.image.shape[0], w_image.shape[0])),
numpy.max((viewer_g.image.shape[1], viewer_h.image.shape[1], w_image.shape[1]))),
is_color = opts.color,
pad=(0,10))
viewer_dims = slice(0, None) if opts.color else 0
viewer.add_patch(viewer_g.image[:,:, viewer_dims] - 0.5)
viewer.add_patch(viewer_h.image[:,:, viewer_dims] - 0.5)
viewer.add_patch(w_image[:,:, viewer_dims] - 0.5)
pl.axis('off')
pl.imshow(viewer.image, interpolation='nearest')
pl.savefig('filters_%s.png' % opts.path)
if not opts.noshow:
pl.show()
示例11: DefaultViewConverter
# 需要導入模塊: from pylearn2.gui.patch_viewer import PatchViewer [as 別名]
# 或者: from pylearn2.gui.patch_viewer.PatchViewer import add_patch [as 別名]
topo_shape = [opts.height, opts.width, opts.chans]
viewconv = DefaultViewConverter(topo_shape)
viewdims = slice(0, None) if opts.color else 0
# load model and retrieve parameters
model = serial.load(opts.path)
wv = model.Wv.get_value().T
if opts.mu:
wv = wv * model.mu.get_value()[:, None]
wv_viewer = PatchViewer(get_dims(len(wv)), (opts.height, opts.width),
is_color = opts.color, pad=(2,2))
for i in xrange(len(wv)):
topo_wvi = viewconv.design_mat_to_topo_view(wv[i:i+1])
wv_viewer.add_patch(topo_wvi[0])
if opts.wv_only:
wv_viewer.show()
os.sys.exit()
wg = model.Wg.get_value()
wh = model.Wh.get_value()
wg_viewer2 = PatchViewer((opts.top, opts.top), (opts.height, opts.width),
is_color = opts.color, pad=(2,2))
wg_viewer1 = PatchViewer(get_dims(len(wg)/opts.top),
(wg_viewer2.image.shape[0], wg_viewer2.image.shape[1]),
is_color = opts.color, pad=(2,2))
for i in xrange(0, len(wg), opts.top):
for j in xrange(i, i + opts.top):
idx = numpy.argsort(wg[j])[-opts.top:][::-1]
for idx_j in idx:
示例12: PatchViewer
# 需要導入模塊: from pylearn2.gui.patch_viewer import PatchViewer [as 別名]
# 或者: from pylearn2.gui.patch_viewer.PatchViewer import add_patch [as 別名]
rows = m
cols = 1+len(Y_sequence)
pv = PatchViewer((rows, cols), (Xt.shape[1], Xt.shape[2]), is_color = True,
pad = (8,8) )
for i in xrange(m):
#add original patch
patch = Xt[i,:,:,:].copy()
patch = dataset.adjust_for_viewer(patch)
if patch.shape[-1] != 3:
patch = np.concatenate( (patch,patch,patch), axis=2)
pv.add_patch(patch, rescale = False, activation = (1,0,0))
orig_patch = patch
def label_to_vis(Y_elem):
prod = np.dot(Y_elem, templates)
assert Y_elem.ndim == 1
rval = np.zeros((1, prod.shape[0]))
rval[0,:] = prod
return rval
# Add the inpainted sequence
for Y_hat in Y_sequence:
cur_Y_hat = Y_hat[i,:]
Y_vis = label_to_vis(cur_Y_hat)
Y_vis = dataset.adjust_for_viewer(dataset.get_topological_view(Y_vis))
if Y_vis.ndim == 4:
示例13: CIFAR10
# 需要導入模塊: from pylearn2.gui.patch_viewer import PatchViewer [as 別名]
# 或者: from pylearn2.gui.patch_viewer.PatchViewer import add_patch [as 別名]
from pylearn2.datasets.cifar10 import CIFAR10
from pylearn2.gui.patch_viewer import PatchViewer
dataset = CIFAR10(which_set = 'test')
pv = PatchViewer((10,1),(32,32),is_color=True)
T,y = dataset.get_batch_topo(10, include_labels = True)
for i in xrange(10):
print dataset.label_names[y[i]]
pv.add_patch(dataset.adjust_for_viewer(T[i,:,:,:]),rescale=False)
pv.show()
示例14: PatchViewer
# 需要導入模塊: from pylearn2.gui.patch_viewer import PatchViewer [as 別名]
# 或者: from pylearn2.gui.patch_viewer.PatchViewer import add_patch [as 別名]
args.conditional_sampler,
embedding_file=(args.embedding_file if args.embedding_file is not None
else sampler.DEFAULT_EMBEDDING_FILE))
pv = PatchViewer(grid_shape=(m, (n + 1 if args.show_nearest_training else n)),
patch_shape=(32,32), is_color=True)
# Optionally load dataset for --show-nearest-training
dataset = None
if args.show_nearest_training:
model = serial.load(args.model_path)
# Shape: b * (0 * 1 * c)
# (topo view)
dataset = yaml_parse.load(model.dataset_yaml_src)
for i in xrange(topo_samples.shape[0]):
topo_sample = topo_samples[i, :, :, :]
pv.add_patch(topo_sample)
if (args.show_nearest_training and dataset is not None
and (i + 1) % n == 0):
sample_topo = topo_samples[i].reshape(-1)
dists = np.square(dataset.X - sample_topo).sum(axis=1)
min_j = np.argmin(dists)
match = dataset.X[min_j].reshape(32, 32, 3)
pv.add_patch(match, activation=1)
pv.show()
示例15: xrange
# 需要導入模塊: from pylearn2.gui.patch_viewer import PatchViewer [as 別名]
# 或者: from pylearn2.gui.patch_viewer.PatchViewer import add_patch [as 別名]
else:
new_w = w_di
else:
new_w = numpy.zeros((len(w_di), opts.height * opts.width)) if di else w_di
for fi in xrange(len(w_di)):
if opts.k != -1:
# build "new_w" as a linear combination of "strongest" filters in layer below
if di > 0:
temp.fill(0.)
idx = numpy.argsort(w_di[fi])[-opts.k:]
for fi_m1 in idx:
new_w[fi:fi+1] += w_di[fi, fi_m1] * prev_w[fi_m1:fi_m1+1,:]
#for fi_m1 in xrange(len(w_di[fi])):
else:
temp = w_di[fi:fi+1,:]
topo_view = view_converter.design_mat_to_topo_view(new_w[fi:fi+1])
block_viewer.add_patch(topo_view[0])
main_viewer.add_patch(block_viewer.image[:,:,0] - 0.5)
block_viewer.clear()
prev_w = new_w
pl.imshow(main_viewer.image, interpolation=None)
pl.axis('off');
pl.savefig('weights.png')
pl.show()