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)
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()
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)
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
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
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
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
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()
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)
print 'sorting' indexed = zip(ranges, xrange(ranges.shape[0])) indexed = sorted(indexed) print 'assembling viewer' if hasattr(model, 's3c'): W = model.s3c.W.get_value() else: W = model.W.get_value() topo_view = dataset.get_weights_view(W.T) m, r, c, ch = topo_view.shape assert ch in [1, 3] n = int(np.sqrt(m)) if n**2 < m: n += 1 pv = PatchViewer((n, n), (r, c), is_color=(ch == 3)) for rng, idx in indexed: if norms != None: act = (rng, norms[idx]) else: act = rng pv.add_patch(topo_view[idx, :, :, :], rescale=True, activation=act) pv.show()
normalizer = gdpdf_normalizer elif t == SCORE: df = grad_func(- dataset.free_energy(X)) mf = grad_func( - model.free_energy(X)) normalizer = gpdf_normalizer elif t == SCORED: df = grad_dir_func(- dataset.free_energy(X)) mf = grad_dir_func( - model.free_energy(X)) normalizer = gdpdf_normalizer else: assert False g.components.append(HeatMap(f = df, normalizer = normalizer)) dimg = g.render() g.components.pop() g.components.append(HeatMap(f = mf, normalizer = normalizer)) mimg = g.render() from pylearn2.gui.patch_viewer import PatchViewer pv = PatchViewer((2,1),dimg.shape[0:2], is_color = dimg.shape[2] == 3) pv.add_patch(dimg,rescale = False) pv.add_patch(mimg, rescale = False) pv.show()
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()
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()
rows = 4 sample_cols = 5 # For some reason format_as from VectorSpace is not working right samples = model.generator.sample(rows * sample_cols).eval() topo_samples = dataset.get_topological_view(samples) pv = PatchViewer(grid_shape=(rows, sample_cols + 1), patch_shape=(32, 32), is_color=True) scale = np.abs(samples).max() X = dataset.X topo = dataset.get_topological_view() index = 0 for i in xrange(samples.shape[0]): topo_sample = topo_samples[i, :, :, :] print topo_sample.min(), topo_sample.max() pv.add_patch(topo_sample / scale, rescale=False) if (i + 1) % sample_cols == 0: sample = samples[i, :] dists = np.square(X - sample).sum(axis=1) j = np.argmin(dists) match = topo[j, :] print match.min(), match.max() pv.add_patch(match / scale, rescale=False, activation=1) pv.show()
dataset = yaml_parse.load(model.dataset_yaml_src) grid_shape = None rows = 4 sample_cols = 5 # For some reason format_as from VectorSpace is not working right samples = model.generator.sample(rows * sample_cols).eval() topo_samples = dataset.get_topological_view(samples) pv = PatchViewer(grid_shape=(rows, sample_cols + 1), patch_shape=(48, 48), is_color=False) X = dataset.X topo = dataset.get_topological_view() index = 0 for i in xrange(samples.shape[0]): topo_sample = topo_samples[i, :, :, :] 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 = topo[j, :] pv.add_patch(match * 2 - 1, rescale=False, activation=1) pv.show()
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()
############## # PLOT FILTERS ############## import pdb; pdb.set_trace() viewer = PatchViewer(get_dims(model.batch_size), (opts.height, opts.width), is_color = opts.color, pad=(2,2)) topo_shape = [opts.height, opts.width, opts.chans] view_converter = DefaultViewConverter(topo_shape) topo_view = view_converter.design_mat_to_topo_view(samples) for chan_i in xrange(nplots): topo_chan = topo_view if opts.color else topo_view[..., chan_i:chan_i+1] for bi in xrange(model.batch_size): viewer.add_patch(topo_chan[bi]) #pl.subplot(1, nplots, chan_i+1) #pl.imshow(viewer.image, interpolation=None) #pl.axis('off'); pl.title('samples (channel %i)' % chan_i) viewer.show() pl.savefig('weights.png') pl.show()
min_embs[range(n), dim] *= new_val condition_data_mod.extend(min_embs) condition_data = np.array(condition_data, dtype=theano.config.floatX) condition_data_mod = np.array(condition_data_mod, dtype=theano.config.floatX) # Now prepare generator generator = util.load_generator_from_file(args.model_path) noise_batch = generator.noise_space.make_theano_batch() conditional_batch = generator.condition_space.make_theano_batch() topo_sample_f = theano.function([noise_batch, conditional_batch], generator.dropout_fprop( (noise_batch, conditional_batch))[0]) # Sample some noise data -- this needs to be shared between orig and mod # sample pairs noise_data = generator.get_noise((m * n, generator.noise_dim)).eval() samples_orig = topo_sample_f(noise_data, condition_data).swapaxes(0, 3) samples_mod = topo_sample_f(noise_data, condition_data_mod).swapaxes(0, 3) pv = PatchViewer(grid_shape=(2 * m, n), patch_shape=(32, 32), is_color=True) for dim in range(condition_dim): for j in range(n): pv.add_patch(samples_orig[dim * n + j], activation=1) for j in range(n): pv.add_patch(samples_mod[dim * n + j]) pv.show()
dataset = yaml_parse.load(model.dataset_yaml_src) W = model.W.get_value() T = dataset.get_topological_view(W.T) from pylearn2.gui.patch_viewer import PatchViewer pv1 = PatchViewer((3,3),(32,32),is_color = True) pv2 = PatchViewer((3,4),(32,32),is_color=True) import numpy as np rng = np.random.RandomState([1,2,3]) for i in xrange(12): print i while True: print 'looping' idxs = rng.randint(0,T.shape[0],(9,)) for j in xrange(9): pv1.add_patch(T[idxs[j],:],activation=0.) pv1.show() x = raw_input('use which? (0-9): ') idx = idxs[eval(x)] break pv2.add_patch(T[idx,:],activation=0.) pv2.show()
#add filled-in patch for j in xrange(len(X_sequence)): patch = X_sequence[j][i, :, :, :] #this_drop_mask = drop_mask[i,:,:,:] #if mask_gen.sync_channels and ((1-this_drop_mask)*(patch - orig_patch)).max() > 0.: # keep_mask = 1-this_drop_mask # keep_patch = keep_mask * patch # keep_orig = keep_mask* orig_patch # diffs = keep_patch - keep_orig # patch = diffs # print 'OH NO!' # print patch.shape if patch.shape[-1] != 3: patch = np.concatenate((patch, patch, patch), axis=2) pv.add_patch(patch, rescale=False, activation=(1, 0, 0)) if mapback: patch = M[i, :, :, :] if patch.shape[-1] != 3: patch = np.concatenate((patch, patch, patch), axis=2) pv.add_patch(patch, rescale=False, activation=(0, 1, 0)) patch = zeroed[i, :, :, :] pv.add_patch(patch, rescale=True, activation=(0, 1, 0)) #add filled-in patch for j in xrange(len(M_sequence)): patch = M_sequence[j][i, :, :, :] if patch.shape[-1] != 3: patch = np.concatenate((patch, patch, patch), axis=2)
from pylearn2.utils import serial stl10 = serial.load('/data/lisa/data/stl10/stl10_32x32/train.pkl') batch = stl10.X[24:25,:] img = stl10.view_converter.design_mat_to_topo_view(batch)[0,...] / 127.5 from pylearn2.gui.patch_viewer import PatchViewer pv = PatchViewer((27,27),(6,6),pad=(1,1),is_color=True) pipeline = serial.load('/data/lisa/data/stl10/stl10_patches/preprocessor.pkl') del pipeline.items[0] from pylearn2.datasets.dense_design_matrix import DenseDesignMatrix, DefaultViewConverter for row in xrange(27): for col in xrange(27): patch = img[row:row+6,col:col+6] d = DenseDesignMatrix( topo_view = patch.reshape(1,6,6,3), view_converter = DefaultViewConverter((6,6,3)) ) d.apply_preprocessor(pipeline) pv.add_patch(d.get_topological_view()[0,...], rescale = True) pv.show()
recons_func = function([F], model.energy_function.reconstruct(F)) def reconstruct(X, use_noise): corrupt_X = X.copy() if use_noise: noise = N.random.randn(*corrupt_X.shape) scaled_noise = noise * sigma corrupt_X += scaled_noise R = recons_func(corrupt_X) return X, corrupt_X, R for i in range(0, rows): for j in range(0, examplesPerRow): x = dataset.get_batch_design(1) p.add_patch(reshape(x), rescale=True) truth, noise, reconstruction = reconstruct(x, use_noise=True) p.add_patch(reshape(truth), rescale=True) p.add_patch(reshape(noise), rescale=True) p.add_patch(reshape(reconstruction), rescale=True) print ( 'mse', N.square(reconstruction-truth).mean(), \ 'mae', N.abs(reconstruction-truth).mean() ) truth, noise, reconstruction = reconstruct(x, use_noise=False) p.add_patch(reshape(reconstruction), rescale=True) p.show()
control.push_load_data(False) dataset = yaml_parse.load(model.dataset_yaml_src) try: layer_to_chains = model.layer_to_chains except AttributeError: print "This model doesn't have negative chains." quit(-1) vis_chains = layer_to_chains[model.visible_layer] vis_chains = vis_chains.get_value() if vis_chains.ndim == 2: vis_chains = dataset.get_topological_view(vis_chains) vis_chains = dataset.adjust_for_viewer(vis_chains) m = vis_chains.shape[0] r = int(np.sqrt(m)) c = m // r while r * c < m: c += 1 pv = PatchViewer((r, c), 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) pv.show()
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) if out_prefix is None: pv.show() else: pv.save(out_prefix + ".png") if __name__ == "__main__": if len(sys.argv) == 2: _, model_path = sys.argv out_prefix = None else: _, model_path, out_prefix = sys.argv
feat.name = 'feat' w = whitener(feat) w.name = 'w' final = T.dot(w, W[:, idx]) raw_f = function([X], final) def f(X): mask = d.pdf(X) > thresh subX = X[mask, :] rval = N.zeros(X.shape[0], dtype=floatX) rval[mask] = raw_f(subX) return rval for i in xrange(grid_rows * grid_cols): print 'filter %d' % i idx.set_value(i) g.components.append( graph_2D.HeatMap(f=f, normalizer=normalize_feature, render_mode='o')) patch = g.render() pv.add_patch(patch * 2.0 - 1.0, rescale=False) del g.components[-1] pv.show()
import sys from pylearn2.utils import serial from pylearn2.datasets.preprocessing import ZCA from pylearn2.datasets.dense_design_matrix import DenseDesignMatrix, DefaultViewConverter from pylearn2.gui.patch_viewer import PatchViewer import numpy as np path = sys.argv[1] prepro = serial.load(path) zca = prepro.items[-1] assert isinstance(zca, ZCA) W = zca.P_ assert W.shape[1] % 3 == 0 n = int(np.sqrt(W.shape[1] / 3)) d = DenseDesignMatrix(X=W, view_converter=DefaultViewConverter((n, n, 3))) W = d.get_weights_view(W) pv = PatchViewer(grid_shape=(n * 3, n), patch_shape=(n, n), is_color=True) for i in xrange(n * n * 3): pv.add_patch(W[i, ...], rescale=True) pv.show()
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:
to_sort = [] for i in xrange(Wv.shape[1] - 1): for j in xrange(i + 1, Wv.shape[1]): dot = abs(np.dot(Wv[:, i], Wv[:, j])) to_sort.append((-dot, (i, j))) to_sort = sorted(to_sort)[0:100] print - to_sort[0][0] print - to_sort[99][0] from pylearn2.config import yaml_parse dataset = yaml_parse.load(model.dataset_yaml_src) weights_view = dataset.get_weights_view(Wv.T) from pylearn2.gui.patch_viewer import PatchViewer pv = PatchViewer((100, 2), (28, 28), is_color=False) for i in xrange(100): l, r = to_sort[i][1] l = weights_view[l] r = weights_view[r] pv.add_patch(l, rescale=True) pv.add_patch(r, rescale=True) pv.show()
main_viewer.add_patch(chan_viewer.image[:,:,viewer_dims] - 0.5) chan_viewer.clear() return copy.copy(main_viewer.image) w_image = plot(wv) if opts.phi: phi_image = plot(phi) nplots = 2 if opts.phi else 1 viewer = PatchViewer((1,nplots), (w_image.shape[0], w_image.shape[1]), is_color = opts.color, pad=(20,20)) viewer_dims = slice(0, None) if opts.color else 0 viewer.add_patch(w_image[:,:, viewer_dims] - 0.5) if opts.phi: viewer_dims = slice(0, None) if opts.color else 1 viewer.add_patch(phi_image[:,:,0] - 0.5) pl.imshow(viewer.image, interpolation='nearest') pl.savefig('filters_%s.png' % opts.path) pl.close() if not opts.noshow: viewer.show()
for i in xrange(Wv.shape[1]-1): for j in xrange(i+1,Wv.shape[1]): dot = abs(np.dot(Wv[:,i],Wv[:,j])) to_sort.append( (-dot, (i,j) ) ) to_sort = sorted(to_sort)[0:100] print -to_sort[0][0] print -to_sort[99][0] from pylearn2.config import yaml_parse dataset = yaml_parse.load(model.dataset_yaml_src) weights_view = dataset.get_weights_view(Wv.T) from pylearn2.gui.patch_viewer import PatchViewer pv = PatchViewer((100,2),(28,28),is_color=False) for i in xrange(100): l, r = to_sort[i][1] l = weights_view[l] r = weights_view[r] pv.add_patch(l,rescale=True) pv.add_patch(r,rescale=True) pv.show()
drop_mask, X_hat = f(X) X, drop_mask, X_hat = [ dataset.get_topological_view(mat) for mat in [X, drop_mask, X_hat] ] X = dataset.adjust_for_viewer(X) X_hat = dataset.adjust_for_viewer(X_hat) pv = PatchViewer( (rows, cols*3), (X.shape[1], X.shape[2]), is_color = True) for i in xrange(m): #add original patch patch = X[i,:,:,:] if patch.shape[-1] != 3: patch = np.concatenate( (patch,patch,patch), axis=2) pv.add_patch(patch, rescale = False) #mark the masked areas as red mask_patch = drop_mask[i,:,:,0] red_channel = patch[:,:,0] green_channel = patch[:,:,1] blue_channel = patch[:,:,2] red_channel[mask_patch == 1] = 1. green_channel[mask_patch == 1] = -1. blue_channel[mask_patch == 1] = -1. patch[:,:,0] = red_channel patch[:,:,1] = green_channel patch[:,:,2] = blue_channel pv.add_patch(patch, rescale = False) #add filled-in patch
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()
drop_mask_Y = outputs[end_X_outputs] Y_sequence = outputs[end_X_outputs+1:] pv = PatchViewer( (rows, cols), (X.shape[1], X.shape[2]), is_color = True, pad = (8,8) ) for i in xrange(m): #add original patch patch = X[i,:,:,:] if patch.shape[-1] != 3: assert patch.shape[-1] == 2, patch.shape patch = patch[:,:,0:1] patch = np.concatenate( (patch,patch,patch), axis=2) pv.add_patch(patch, rescale = False, activation = (1,0,0)) orig_patch = patch #mark the masked areas as red mask_patch = drop_mask[i,:,:,0] if drop_mask.shape[-1] > 1 and mask_gen.sync_channels and mask_gen.n_channels > 1: assert np.all(mask_patch == drop_mask[i,:,:,1]) assert np.all(mask_patch == drop_mask[i,:,:,2]) red_channel = patch[:,:,0] green_channel = patch[:,:,1] blue_channel = patch[:,:,2] mask_patch_red = drop_mask[i,:,:,0] if drop_mask.shape[-1] == 2: drop_mask = drop_mask[:, :, :, 0:1] assert drop_mask.shape[-1] == 1, drop_mask.shape if mask_gen.sync_channels or drop_mask.shape[-1] == 1:
grid_shape = None rows = 4 sample_cols = 5 # For some reason format_as from VectorSpace is not working right samples = model.generator.sample(rows * sample_cols).eval() topo_samples = dataset.get_topological_view(samples) pv = PatchViewer(grid_shape=(rows, sample_cols + 1), patch_shape=(32,32), is_color=True) scale = np.abs(samples).max() X = dataset.X topo = dataset.get_topological_view() index = 0 for i in xrange(samples.shape[0]): topo_sample = topo_samples[i, :, :, :] print topo_sample.min(), topo_sample.max() pv.add_patch(topo_sample / scale, rescale=False) if (i +1) % sample_cols == 0: sample = samples[i, :] dists = np.square(X - sample).sum(axis=1) j = np.argmin(dists) match = topo[j, :] print match.min(), match.max() pv.add_patch(match / scale, rescale=False, activation=1) pv.show()
indexed = zip( ranges, xrange(ranges.shape[0])) indexed = sorted( indexed ) print 'assembling viewer' if hasattr(model,'s3c'): W = model.s3c.W.get_value() else: W = model.W.get_value() topo_view = dataset.get_weights_view(W.T) m, r,c, ch = topo_view.shape assert ch in [1,3] n = int(np.sqrt(m)) if n ** 2 < m: n += 1 pv = PatchViewer( (n,n), (r,c), is_color = (ch == 3)) for rng, idx in indexed: if norms != None: act = (rng, norms[idx]) else: act = rng pv.add_patch( topo_view[idx,:,:,:], rescale = True, activation = act) pv.show()
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()
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] view1 = 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, :48 * 48]) view1.add_patch(topo_wvi[0]) view2 = 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, 48 * 48:]) view2.add_patch(topo_wvi[0]) pl.subplot(1, 2, 1) pl.imshow(view1.image[:, :, viewdims]) pl.gray() pl.axis('off') pl.subplot(1, 2, 2) pl.imshow(view2.image[:, :, viewdims]) pl.gray()
# 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()
cropped = cropped[:, :, ::-1] other_cropped = other_cropped[:, :, ::-1] new_X[:,:,:,i] = cropped.copy() other_new_X[:,:,:,i] = other_cropped.copy() X = new_X other_X = other_new_X b01c = f(X) max_abs = max(np.abs(b01c).max(), np.abs(other_X).max()) b01c = b01c / max_abs other_X = other_X / max_abs other_X = np.transpose(other_X, (3, 1, 2, 0)) print "Formatting" from pylearn2.gui.patch_viewer import PatchViewer pv = PatchViewer(grid_shape=(rows, cols), patch_shape=(32, 32), is_color=True) for i in xrange(m): pv.add_patch(b01c[i,:,:,:], rescale=False) pv.add_patch(other_X[i,:,:,:], rescale=False) print "Showing" pv.save('/u/goodfeli/vis.png')
assert not np.any(np.isnan(standard_error)) #print 'mean ',mean[0:5] #print 'std ',std[0:5] #print 'stderr ',standard_error[0:5] def clip_below(x, mn): mask = (x > mn) return x * mask + (1-mask)*mn def clip_above(x, mx): mask = x < mx return mask * x + (1-mask) * mx final = (mean > data_mean) * clip_below(mean - standard_error, data_mean) + \ (mean <= data_mean) * clip_above(mean + standard_error, data_mean) #print 'data mean',data_mean[0:5] #print 'final ',final[0:5] assert final.ndim == 1 img = final.reshape(1,final.shape[0]) img = dataset.get_topological_view(img) img = dataset.adjust_for_viewer(img) img = img[0,:,:,:] pv.add_patch(img, rescale=False, activation=peak) pv.show()
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()
alpha_act = model.alpha.get_value() alpha_act /= alpha_act.max() for j in xrange(model.nhid): cur_idx = idx[j] cur_p_act = p_act[cur_idx] cur_mu_act = mu_act[cur_idx] cur_alpha_act = alpha_act[cur_idx] if disable_activation: activation = None else: activation = (cur_p_act, cur_mu_act, cur_alpha_act) pv4.add_patch(weights_view[cur_idx,:], rescale = True, activation = activation) if j >= filter_start and j < filter_start + num_filters: pv5.add_patch(weights_view[cur_idx,:], rescale = True, activation = activation) if feature_type == 'exp_h': print 'features are not rescaled; showing expectation of h without adjustment' topo_feat *= 2. topo_feat -= 1. else: print "features are contrast normalized so that each column uses 0.5 grey " \ "for 0 and its maximal absolute value is at the edge of the dynamic range" for i in xrange(filter_start, filter_start + num_filters): topo_feat[:,:,:,idx[i]] /= np.abs(topo_feat[:,:,:,idx[i]]).max()
from pylearn2.utils import serial import sys _, path = sys.argv model = serial.load(path) from pylearn2.config import yaml_parse dataset = yaml_parse.load(model.dataset_yaml_src) from theano import tensor as T X = T.matrix() R = model.fprop(X) from theano import function f = function([X], R) X = dataset.get_batch_design(100) R = f(X) X = dataset.get_topological_view(X) R = dataset.get_topological_view(R) from pylearn2.gui.patch_viewer import PatchViewer pv = PatchViewer((10, 20), X.shape[1:3], is_color=False) for i in xrange(100): pv.add_patch(X[i, :, :, :] - 0.5) pv.add_patch(R[i, :, :, :] - 0.5) pv.show()
p = f() print 'p range: ',(p.min(),p.max()) mins = p.min(axis=0) print 'p min: ', (mins.min(), mins.mean(), mins.max()) print 'p mean: ',p.mean() print 'p shape: ',p.shape assert p.shape[0] == m n = p.shape[-1] pv = PatchViewer( (m,n), (p.shape[1],p.shape[2]), is_color = False ) for j in xrange(m): draw = 1. for i in xrange(n): pv.add_patch(draw * p[j,:,:,i]*2.-1.,rescale=False) if p[:,:,:,i].min(axis=0).max() > .9: draw = 0. pv.show() print 'waiting...' x = raw_input() if x == 'q': quit() print 'running...'
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()
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:
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) pv.show()
F = T.matrix() recons_func = function([F], model.energy_function.reconstruct(F)) def reconstruct(X, use_noise): corrupt_X = X.copy() if use_noise: noise = N.random.randn(*corrupt_X.shape) scaled_noise = noise * sigma corrupt_X += scaled_noise R = recons_func(corrupt_X) return X, corrupt_X, R for i in range(0,rows): for j in range(0, examplesPerRow): x = dataset.get_batch_design(1) p.add_patch( reshape(x),rescale=True) truth, noise, reconstruction = reconstruct(x, use_noise = True) p.add_patch(reshape(truth ),rescale=True) p.add_patch(reshape(noise ), rescale=True) p.add_patch(reshape(reconstruction) , rescale=True) print ( 'mse', N.square(reconstruction-truth).mean(), \ 'mae', N.abs(reconstruction-truth).mean() ) truth, noise, reconstruction = reconstruct(x, use_noise = False) p.add_patch( reshape(reconstruction) , rescale=True) p.show()
#add filled-in patch for j in xrange(len(X_sequence)): patch = X_sequence[j][i,:,:,:] #this_drop_mask = drop_mask[i,:,:,:] #if mask_gen.sync_channels and ((1-this_drop_mask)*(patch - orig_patch)).max() > 0.: # keep_mask = 1-this_drop_mask # keep_patch = keep_mask * patch # keep_orig = keep_mask* orig_patch # diffs = keep_patch - keep_orig # patch = diffs # print 'OH NO!' # print patch.shape if patch.shape[-1] != 3: patch = np.concatenate( (patch,patch,patch), axis=2) pv.add_patch(patch, rescale = False, activation = (1,0,0)) if mapback: patch = M[i,:,:,:] if patch.shape[-1] != 3: patch = np.concatenate( (patch,patch,patch),axis=2) pv.add_patch(patch, rescale = False, activation= (0,1,0)) patch = zeroed[i,:,:,:] pv.add_patch(patch, rescale = True, activation = (0,1,0)) #add filled-in patch for j in xrange(len(M_sequence)): patch = M_sequence[j][i,:,:,:] if patch.shape[-1] != 3: patch = np.concatenate( (patch,patch,patch), axis=2)
c = rng.randint(0, 8) cropped = X[:, r:r + 32, c:c + 32, i] other_cropped = other_X[:, r:r + 32, c:c + 32, i] if rng.randint(2): cropped = cropped[:, :, ::-1] other_cropped = other_cropped[:, :, ::-1] new_X[:, :, :, i] = cropped.copy() other_new_X[:, :, :, i] = other_cropped.copy() X = new_X other_X = other_new_X b01c = f(X) max_abs = max(np.abs(b01c).max(), np.abs(other_X).max()) b01c = b01c / max_abs other_X = other_X / max_abs other_X = np.transpose(other_X, (3, 1, 2, 0)) print "Formatting" from pylearn2.gui.patch_viewer import PatchViewer pv = PatchViewer(grid_shape=(rows, cols), patch_shape=(32, 32), is_color=True) for i in xrange(m): pv.add_patch(b01c[i, :, :, :], rescale=False) pv.add_patch(other_X[i, :, :, :], rescale=False) print "Showing" pv.save('/u/goodfeli/vis.png')
assert h.shape[0] == 1 n = p.shape[-1] c = int(np.sqrt(2*n)) if c % 2 != 0: c += 1 r = 2 * n / c if r * c < 2 * n: r += 1 pv = PatchViewer( (r,c), (h.shape[1],h.shape[2]), is_color = False ) for i in xrange(n): hi = h[0,:,:,i] pv.add_patch(hi*2.-1.,rescale=False) pv.add_patch(p[0,:,:,i]*2.-1.,rescale=False) pv.show() x = X.get_value()[0,:,:,:] pv = PatchViewer( (1,3), (x.shape[0],x.shape[1]), is_color = x.shape[-1] == 3) pv.add_patch(dataset.adjust_for_viewer(x),rescale=False) # mu may not exist if someboady ran ditch_mu for supervised training if model.visible_layer.mu is not None: origin = model.visible_layer.space.get_origin() mu = origin + model.visible_layer.mu.get_value() print 'mu range: ',(mu.min(),mu.max())
alpha_act /= alpha_act.max() for j in xrange(model.nhid): cur_idx = idx[j] cur_p_act = p_act[cur_idx] cur_mu_act = mu_act[cur_idx] cur_alpha_act = alpha_act[cur_idx] if disable_activation: activation = None else: activation = (cur_p_act, cur_mu_act, cur_alpha_act) pv4.add_patch(weights_view[cur_idx, :], rescale=True, activation=activation) if j >= filter_start and j < filter_start + num_filters: pv5.add_patch(weights_view[cur_idx, :], rescale=True, activation=activation) if feature_type == 'exp_h': print 'features are not rescaled; showing expectation of h without adjustment' topo_feat *= 2. topo_feat -= 1. else: print "features are contrast normalized so that each column uses 0.5 grey " \ "for 0 and its maximal absolute value is at the edge of the dynamic range" for i in xrange(filter_start, filter_start + num_filters):
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()
# 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()
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()