# HG changeset patch # User Pascal Lamblin # Date 1235160526 18000 # Node ID 4837aee2235256b23895e82af218b908e40ea6df # Parent 40cae12a9bb855636de754b887ab31ebd94f6c7c# Parent 14d22ca1c8b570949a1825136592af73f53fa2a6 merge diff -r 40cae12a9bb8 -r 4837aee22352 pylearn/datasets/MNIST.py --- a/pylearn/datasets/MNIST.py Fri Feb 20 15:08:29 2009 -0500 +++ b/pylearn/datasets/MNIST.py Fri Feb 20 15:08:46 2009 -0500 @@ -7,7 +7,7 @@ import numpy from ..io.amat import AMat -from .config import data_root +from .config import data_root # config from .dataset import Dataset def head(n=10, path=None): diff -r 40cae12a9bb8 -r 4837aee22352 pylearn/datasets/config.py --- a/pylearn/datasets/config.py Fri Feb 20 15:08:29 2009 -0500 +++ b/pylearn/datasets/config.py Fri Feb 20 15:08:46 2009 -0500 @@ -4,10 +4,15 @@ Especially, the locations of data files. """ -import os -def env_get(key, default): +import os, sys +def env_get(key, default, key2 = None): + if key2 and os.getenv(key) is None: + key=key2 + if os.getenv(key) is None: + print >> sys.stderr, "WARNING: Environment variable", key, + print >> sys.stderr, "is not set. Using default of", default return default if os.getenv(key) is None else os.getenv(key) def data_root(): - return env_get('PYLEARN_DATA_ROOT', '/u/bergstrj/pub/data/') + return env_get('PYLEARN_DATA_ROOT', os.getenv('HOME')+'/data', 'DBPATH') diff -r 40cae12a9bb8 -r 4837aee22352 pylearn/io/image_tiling.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pylearn/io/image_tiling.py Fri Feb 20 15:08:46 2009 -0500 @@ -0,0 +1,84 @@ +""" +Illustrate filters (or data) in a grid of small image-shaped tiles. +""" + +import numpy +from PIL import Image + +def scale_to_unit_interval(ndar): + ndar = ndar.copy() + ndar -= ndar.min() + ndar *= 1.0 / ndar.max() + return ndar + +def tile_raster_images(X, img_shape, tile_shape, tile_spacing=(0,0), + scale_rows_to_unit_interval=True, + output_pixel_vals=True + ): + """ + Transform an array with one flattened image per row, into an array in which images are + reshaped and layed out like tiles on a floor. + + This function is useful for visualizing datasets whose rows are images, and also columns of + matrices for transforming those rows (such as the first layer of a neural net). + + :type X: a 2-D ndarray or a tuple of 4 channels, elements of which can be 2-D ndarrays or None + :param X: a 2-D array in which every row is a flattened image. + :type img_shape: tuple; (height, width) + :param img_shape: the original shape of each image + :type tile_shape: tuple; (rows, cols) + :param tile_shape: the number of images to tile (rows, cols) + + :returns: array suitable for viewing as an image. (See:`PIL.Image.fromarray`.) + :rtype: a 2-d array with same dtype as X. + + """ + assert len(img_shape) == 2 + assert len(tile_shape) == 2 + assert len(tile_spacing) == 2 + + out_shape = [(ishp + tsp) * tshp - tsp for ishp, tshp, tsp + in zip(img_shape, tile_shape, tile_spacing)] + + if isinstance(X, tuple): + assert len(X) == 4 + if output_pixel_vals: + out_array = numpy.zeros((out_shape[0], out_shape[1], 4), dtype='uint8') + else: + out_array = numpy.zeros((out_shape[0], out_shape[1], 4), dtype=X.dtype) + + #colors default to 0, alpha defaults to 1 (opaque) + if output_pixel_vals: + channel_defaults = [0,0,0,255] + else: + channel_defaults = [0.,0.,0.,1.] + + for i in xrange(4): + if X[i] is None: + out_array[:,:,i] = numpy.zeros(out_shape, + dtype='uint8' if output_pixel_vals else out_array.dtype + )+channel_defaults[i] + else: + out_array[:,:,i] = tile_raster_images(X[i], img_shape, tile_shape, tile_spacing, scale_rows_to_unit_interval, output_pixel_vals) + return out_array + + else: + H, W = img_shape + Hs, Ws = tile_spacing + + out_array = numpy.zeros(out_shape, dtype='uint8' if output_pixel_vals else X.dtype) + for tile_row in xrange(tile_shape[0]): + for tile_col in xrange(tile_shape[1]): + if tile_row * tile_shape[1] + tile_col < X.shape[0]: + if scale_rows_to_unit_interval: + this_img = scale_to_unit_interval(X[tile_row * tile_shape[1] + tile_col].reshape(img_shape)) + else: + this_img = X[tile_row * tile_shape[1] + tile_col].reshape(img_shape) + out_array[ + tile_row * (H+Hs):tile_row*(H+Hs)+H, + tile_col * (W+Ws):tile_col*(W+Ws)+W + ] \ + = this_img * (255 if output_pixel_vals else 1) + return out_array + +