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)
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)
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')
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
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')
total_dimension = space.get_total_dimension() import numpy as np num_colors = 1 if total_dimension % 3 == 0: num_colors = 3 w = int(np.sqrt(total_dimension / num_colors)) from pylearn2.space import Conv2DSpace desired_space = Conv2DSpace(shape=[w, w], num_channels=num_colors, axes=('b',0,1,'c')) samples_func = function([], space.format_as(batch=model.generator.mlp.fprop(Z), space=desired_space)) streams = [] for i in xrange(36): print "stream",i Z.set_value(make_Z().astype(Z.dtype)) streams.append(samples_func()) from pylearn2.gui.patch_viewer import PatchViewer for i in xrange(endpoints * steps_per_point): print "file ",i viewer = PatchViewer(grid_shape=(6,6), patch_shape=(streams[0].shape[1], streams[0].shape[2]), is_color=False) for j in xrange(36): viewer.add_patch(streams[j][i, :, :, :] * 2. - 1., rescale=False) number = str(i) while len(number) < len(str(endpoints * steps_per_point)): number = '0' + number path = '/Tmp/video/' + number + '.png' viewer.save(path)
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) if out_prefix is None: pv.show() else: pv.save(out_prefix+".png")
w = int(np.sqrt(total_dimension / num_colors)) from pylearn2.space import Conv2DSpace desired_space = Conv2DSpace(shape=[w, w], num_channels=num_colors, axes=('b', 0, 1, 'c')) samples_func = function([], space.format_as(batch=model.generator.mlp.fprop(Z), space=desired_space)) streams = [] for i in xrange(36): print "stream", i Z.set_value(make_Z().astype(Z.dtype)) streams.append(samples_func()) from pylearn2.gui.patch_viewer import PatchViewer for i in xrange(endpoints * steps_per_point): print "file ", i viewer = PatchViewer(grid_shape=(6, 6), patch_shape=(streams[0].shape[1], streams[0].shape[2]), is_color=False) for j in xrange(36): viewer.add_patch(streams[j][i, :, :, :] * 2. - 1., rescale=False) number = str(i) while len(number) < len(str(endpoints * steps_per_point)): number = '0' + number path = '/Tmp/video/' + number + '.png' viewer.save(path)
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) for row in xrange(27): for col in xrange(27): pv.add_patch(img[row:row+6,col:col+6], rescale = False) pv.show() pv.save('patches.png')