def test_sample_ae(): """ Visualize some samples from the trained unsupervised GSN. """ with open("gsn_ae_example.pkl") as f: gsn = pickle.load(f) # random point to start at mb_data = MNIST(which_set='test').X[105:106, :] history = gsn.get_samples([(0, mb_data)], walkback=1000, symbolic=False, include_first=True) history = list(itertools.chain(*history)) history = np.vstack(history) tiled = image.tile_raster_images(history, img_shape=[28,28], tile_shape=[50,50], tile_spacing=(2,2)) image.save("gsn_ae_example.png", tiled) # code to get log likelihood from kernel density estimator # this crashed on GPU (out of memory), but works on CPU pw = ParzenWindows(MNIST(which_set='test').X, .20) print pw.get_ll(history)
def test_sample_supervised(idxs=None, noisy=True): """ Visualize samples and labels produced by GSN. """ with open("gsn_sup_example.pkl") as f: gsn = pickle.load(f) gsn._corrupt_switch = noisy ds = MNIST(which_set='test', one_hot=True) if idxs is None: data = ds.X[100:150] else: data = ds.X[idxs] # change the walkback parameter to make the data fill up rows in image samples = gsn.get_samples([(0, data)], indices=[0, 2], walkback=21, symbolic=False, include_first=True) stacked = vis_samples(samples) tiled = image.tile_raster_images(stacked, img_shape=[28,28], tile_shape=[50,50], tile_spacing=(2,2)) image.save("gsn_sup_example.png", tiled)
def test_sample_ae(): """ Visualize some samples from the trained unsupervised GSN. """ with open("gsn_ae_example.pkl") as f: gsn = pickle.load(f) # random point to start at mb_data = MNIST(which_set='test').X[105:106, :] history = gsn.get_samples([(0, mb_data)], walkback=1000, symbolic=False, include_first=True) history = list(itertools.chain(*history)) history = np.vstack(history) tiled = image.tile_raster_images(history, img_shape=[28, 28], tile_shape=[50, 50], tile_spacing=(2, 2)) image.save("gsn_ae_example.png", tiled) # code to get log likelihood from kernel density estimator # this crashed on GPU (out of memory), but works on CPU pw = ParzenWindows(MNIST(which_set='test').X, .20) print(pw.get_ll(history))
def test_sample_supervised(idxs=None, noisy=True): """ Visualize samples and labels produced by GSN. """ with open("gsn_sup_example.pkl") as f: gsn = pickle.load(f) gsn._corrupt_switch = noisy ds = MNIST(which_set='test') if idxs is None: data = ds.X[100:150] else: data = ds.X[idxs] # change the walkback parameter to make the data fill up rows in image samples = gsn.get_samples([(0, data)], indices=[0, 2], walkback=21, symbolic=False, include_first=True) stacked = vis_samples(samples) tiled = image.tile_raster_images(stacked, img_shape=[28, 28], tile_shape=[50, 50], tile_spacing=(2, 2)) image.save("gsn_sup_example.png", tiled)
def demo(): num_frames = 300 height = 10 width = 10 config = BilliardVideoConfig( num_frames = num_frames, height = height, width = width, ball_diameter = 5, min_val = .1, max_val = .9, vel_multiplier = 4.0, vel_noise = .04, vel_decay = .001, frames_burnin = 1000, frames_simulate = 20000, ) bil = BilliardVideo('train', config) iterator = bil.iterator(mode = None, batch_size = 10, num_batches = 2, ignore_data_specs = True) for batch_idx, batch in enumerate(iterator): #print batch.shape #print batch.sum() assert len(batch.shape) == 5 assert batch.shape[4] == 1 # single channel if batch_idx == 0: prefix = 'billiard_frame_' for frame_idx in xrange(num_frames): frameIm = image.tile_raster_images(batch[0,frame_idx:frame_idx+1], img_shape = (height,width), tile_shape=(1,1)) image.save('%s%03d.png' % (prefix, frame_idx), frameIm) print 'Saved: %s*.png' % prefix print 'Convert to gif by running:\n convert -delay 3 %s* billiard_video.gif' % (prefix) flattened = np.reshape(batch, (batch.shape[0]*batch.shape[1], np.prod(batch.shape[2:]))) tiled = image.tile_raster_images(-flattened, img_shape=[height, width], tile_shape=[batch.shape[0], batch.shape[1]], tile_spacing=(5,1)) filename = 'billiard_batch_%03d.png' % batch_idx image.save(filename, tiled) #image.save(filename, 255-tiled) print 'Saved:', filename
from pylearn2.utils import image import numpy as np for i in xrange(5000): print i number = str(i) while len(number) != 4: number = '0' + number img = image.load('/Tmp/video/' + number + '.png') out = np.zeros((406, 406, 3)) for ofs_r in [0, 1]: for ofs_c in [0, 1]: out[ofs_r::2, ofs_c::2, :] = img image.save('/Tmp/video_resized/' + number + '.png', out)
if isinstance(space, VectorSpace): # For some reason format_as from VectorSpace is not working right samples = model.generator.mlp.fprop(Z).eval() is_color = samples.shape[-1] % 3 == 0 and samples.shape[-1] != 48 * 48 samples = dataset.get_topological_view(samples) else: 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 = space.format_as(batch=model.generator.mlp.fprop(Z), space=desired_space).eval() is_color = samples.shape[-1] == 3 from pylearn2.utils import image for i in xrange(endpoints * steps_per_point): img = samples[i, :, :, :] img /= np.abs(img).max() number = str(i) while len(number) < len(str(endpoints * steps_per_point)): number = '0' + number path = 'video/' + number + '.png' image.save(path, img)