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
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 save_layer_output(layer, name, shape): tensor = layer.inbound_nodes[-1].output_tensors[0] model = Model(input_tensor, tensor) model.compile(optimizer='sgd', loss='mse') pred = model.predict(X_test[idx-1:idx]) # @UndefinedVariable reshaped = pred.reshape(*shape) tiled = tile_raster_images(X=reshaped.T, img_shape=shape[:-1], tile_shape=(2, shape[-1]/2), tile_spacing=(1, 1)) image = Image.fromarray(tiled) image.save('%s/%s' % (folder, name))
def save_weights(layer, name, shape): weights = layer.get_weights()[0].reshape(*shape) tiled = tile_raster_images(X=weights.T, img_shape=shape[:-1], tile_shape=(4, 4), tile_spacing=(1, 1)) image = Image.fromarray(tiled) image.save('%s/%s' % (folder, name))
def save_weights(layer, name, shape): weights = layer.get_weights()[0].reshape(*shape) tiled = tile_raster_images(X=weights.T, img_shape=shape[:-1], tile_shape=(4, 4), tile_spacing=(1, 1)) image = Image.fromarray(tiled) image.save('%s/%s' % (folder, name)) for i in range(2, 6): # get only the residual layer1 = model.get_layer('residual_%d' % (i - 1)) layer2 = model.get_layer('residual_%d' % i) l1_tensor = layer1.inbound_nodes[-1].output_tensors[0] l2_tensor = layer2.inbound_nodes[-1].output_tensors[0] diff = merge([l2_tensor, l1_tensor], mode=lambda x: x[1] - x[0], output_shape=lambda x: x[0]) diff_model = Model(input_tensor, diff) diff_model.compile(optimizer='sgd', loss='mse') pred = diff_model.predict(X_test[idx-1:idx]) reshaped = pred.reshape(28, 28, 8) tiled = tile_raster_images(reshaped.T, (28, 28), (2, 4), (1, 1)) image = Image.fromarray(tiled) image.save('%s/residual_at_layer_%d.png' % (folder, i)) # save the filters save_weights(model.get_layer('residual_%d' % i), 'filters_at_layer_%d.png' % i, (3, 3, 64)) # get the output before applying the activation function save_layer_output(model.get_layer('residual_%d' % i), 'preactivation_at_layer_%d.png' % i, (28, 28, 8)) # get the output after applying the actvation function save_layer_output(model.get_layer('activation_%d' % i), 'activation_at_layer_%d.png' % i, (28, 28, 8))
def evaluate_picture(model, **kwargs): """ Currently this doesn't work very well """ if 'MY_PICTURE' not in os.environ: print('Set MY_PICTURE as an environment variable (path to picture you want to train on)') sys.exit(1) picture_path = os.environ['MY_PICTURE'] if not os.path.isfile(picture_path): print('Nothing found at "%s"' % picture_path) sys.exit(1) if 'IMAGE_PATH' not in os.environ: print('Set IMAGE_PATH as an environment variable (parent directory to save generaged images)') sys.exit(1) image_path = os.path.join(os.environ['IMAGE_PATH'], kwargs.get('save_dir', 'picture')) if not os.path.exists(image_path): os.makedirs(image_path) os.chdir(image_path) im = Image.open(picture_path).convert('L') height, width = im.size greyscale_map = im.getdata() greyscale_map = np.array(greyscale_map, dtype='uint8') data = greyscale_map.reshape(width, height) output = Image.fromarray(data) output.save('original_image.png') data = np.asarray(data, dtype=dtype) / np.max(data) n_train_batches = 500 batch_size = 25 nb_epochs = 15 k = 15 lr = 0.1 use_momentum = True momentum = 0.5 persistent = True train_data = np.zeros((n_train_batches, width * height), dtype='uint8') for i in range(n_train_batches): translated = shift(data, (random.randint(-3, 3), random.randint(-3, 3)), mode='wrap') # if random.random() > 0.8: # translated = rotate(translated, 180, reshape=False, mode='wrap') # if random.random() > 0.8: # translated = np.fliplr(translated) rotated = rotate(translated, random.random() * 6 - 3, reshape=False, mode='wrap') rotated[np.random.rand(width, height) > 0.8] = 0 train_data[i] = rotated.reshape(1, width * height) for epoch in range(nb_epochs): model.train(train_data, nb_epochs=20, batch_size=batch_size, k=k, lr=lr, use_momentum=use_momentum, momentum=momentum, persistent=persistent) image = Image.fromarray(tile_raster_images(X=model.W.get_value(borrow=True).T, img_shape=(width, height), tile_shape=(5, 5), tile_spacing=(1, 1))) image.save('filters_at_epoch_%i.png' % epoch) lr *= 0.9 print('Completed metaepoch %d, learning rate is now %.3e' % (epoch, lr)) if epoch > nb_epochs / 2: momentum = 0.9 n_chains = 1 n_samples = 10 n_steps = 10 data = data.reshape(1, width * height) sample_function = model.get_sampler(data) image_data = np.zeros(((width + 1) * n_samples + 1, (height + 1) * n_chains - 1), dtype='uint8') for idx in range(n_samples): image_data[(width + 1) * idx: (width + 1) * idx + width, :] = tile_raster_images(X=data, img_shape=(width, height), tile_shape=(1, n_chains), tile_spacing=(1, 1)) data = sample_function(n_steps) image = Image.fromarray(image_data) image.save('samples.png')
def evaluate_mnist(model, **kwargs): if 'MNIST_PATH' not in os.environ: print('You must set MNIST_PATH as an environment variable (pointing at mnist.pkl.gz). You can download the ' + 'MNIST data from http://deeplearning.net/data/mnist/mnist.pkl.gz') sys.exit(1) mnist_path = os.environ['MNIST_PATH'] if 'IMAGE_PATH' not in os.environ: print('Set IMAGE_PATH as an environment variable (parent directory to save generaged images)') sys.exit(1) image_path = os.path.join(os.environ['IMAGE_PATH'], kwargs.get('save_dir', 'mnist')) print('Loading MNIST files from "%s"' % mnist_path) print('Saving files in "%s"' % image_path) if not os.path.exists(image_path): os.makedirs(image_path) os.chdir(image_path) f = gzip.open(mnist_path, 'rb') train_set, validation_set, test_set = pkl.load(f) def split_data(xy): x, y = xy x = np.asarray(x, dtype=theano.config.floatX) y = np.asarray(y, dtype='int32') return x, y train_X, train_y = split_data(train_set) validation_X, validation_y = split_data(validation_set) test_X, test_y = split_data(test_set) # hyperparameters batch_size = 20 nb_epochs = 50 k = 1 lr = 0.1 use_momentum = True momentum = 0.5 persistent = True for epoch in range(nb_epochs): model.train(train_X, nb_epochs=1, batch_size=batch_size, k=k, lr=lr, use_momentum=use_momentum, momentum=momentum, validation_data=validation_X[0:256], persistent=persistent) image = Image.fromarray(tile_raster_images(X=model.W.get_value(borrow=True).T, img_shape=(28, 28), tile_shape=(10, 10), tile_spacing=(1, 1))) image.save('filters_at_epoch_%i.png' % epoch) if epoch == 20: momentum = 0.9 k = epoch / 5 + 1 lr *= 0.95 # testing params number_of_test_samples = test_X.shape[0] n_chains = 10 n_samples = 10 n_steps = 1000 test_idx = rng.randint(number_of_test_samples - n_chains) v_sample = test_X[test_idx:test_idx+n_chains] sample_function = model.get_sampler(v_sample) image_data = np.zeros((29 * n_samples + 1, 29 * n_chains - 1), dtype='uint8') for idx in range(n_samples): image_data[29 * idx: 29 * idx + 28, :] = tile_raster_images(X=v_sample, img_shape=(28, 28), tile_shape=(1, n_chains), tile_spacing=(1, 1)) v_sample = sample_function(n_steps) image = Image.fromarray(image_data) image.save('samples.png') # a prediction model (needs improvement) model.build(batch_size, test_X.shape[1]) input = T.matrix('input', dtype=dtype) target = T.matrix('target', dtype=dtype) target_idx = T.vector('target_idx', dtype='int32') W2 = init_glorot('W2', shape=(model.n_hid, 10)) b2 = init_zeros('b2', shape=(10,)) z1 = model.visible_to_hidden(input) z2 = T.nnet.softmax(T.dot(z1, W2) + b2) params = [W2, b2] cost = ((target - z2) ** 2).mean() eval_cost = T.cast(T.eq(T.argmax(z2, axis=1), target_idx), dtype='int32').mean() updates = [(p, p - 0.1 * T.grad(cost, p)) for p in params] train = theano.function(inputs=[input, target], outputs=[], updates=updates) test = theano.function(inputs=[input, target_idx], outputs=eval_cost) n_batches = train_X.shape[0] // batch_size nb_epochs = 50 for epoch in range(nb_epochs): for batch in range(n_batches): data_X = train_X[batch * batch_size: (batch + 1) * batch_size] idx_y = train_y[batch * batch_size: (batch + 1) * batch_size] data_y = np.zeros(shape=(idx_y.shape[0], 10), dtype=dtype) data_y[np.arange(idx_y.shape[0]), idx_y] = 1 train(data_X, data_y) print('Epoch %d: %.3f' % (epoch, test(test_X, test_y)))