49
|
1 """
|
|
2 Illustrate filters (or data) in a grid of small image-shaped tiles.
|
|
3
|
|
4 Note: taken from the pylearn codebase on Feb 4, 2010 (fsavard)
|
|
5 """
|
|
6
|
|
7 import numpy
|
|
8 from PIL import Image
|
|
9
|
|
10 def scale_to_unit_interval(ndar,eps=1e-8):
|
|
11 ndar = ndar.copy()
|
|
12 ndar -= ndar.min()
|
|
13 ndar *= 1.0 / (ndar.max()+eps)
|
|
14 return ndar
|
|
15
|
|
16 def tile_raster_images(X, img_shape, tile_shape, tile_spacing=(0,0),
|
|
17 scale_rows_to_unit_interval=True,
|
|
18 output_pixel_vals=True
|
|
19 ):
|
|
20 """
|
|
21 Transform an array with one flattened image per row, into an array in which images are
|
|
22 reshaped and layed out like tiles on a floor.
|
|
23
|
|
24 This function is useful for visualizing datasets whose rows are images, and also columns of
|
|
25 matrices for transforming those rows (such as the first layer of a neural net).
|
|
26
|
|
27 :type X: a 2-D ndarray or a tuple of 4 channels, elements of which can be 2-D ndarrays or None
|
|
28 :param X: a 2-D array in which every row is a flattened image.
|
|
29 :type img_shape: tuple; (height, width)
|
|
30 :param img_shape: the original shape of each image
|
|
31 :type tile_shape: tuple; (rows, cols)
|
|
32 :param tile_shape: the number of images to tile (rows, cols)
|
|
33
|
|
34 :returns: array suitable for viewing as an image. (See:`PIL.Image.fromarray`.)
|
|
35 :rtype: a 2-d array with same dtype as X.
|
|
36
|
|
37 """
|
|
38 assert len(img_shape) == 2
|
|
39 assert len(tile_shape) == 2
|
|
40 assert len(tile_spacing) == 2
|
|
41
|
|
42 out_shape = [(ishp + tsp) * tshp - tsp for ishp, tshp, tsp
|
|
43 in zip(img_shape, tile_shape, tile_spacing)]
|
|
44
|
|
45 if isinstance(X, tuple):
|
|
46 assert len(X) == 4
|
|
47 if output_pixel_vals:
|
|
48 out_array = numpy.zeros((out_shape[0], out_shape[1], 4), dtype='uint8')
|
|
49 else:
|
|
50 out_array = numpy.zeros((out_shape[0], out_shape[1], 4), dtype=X.dtype)
|
|
51
|
|
52 #colors default to 0, alpha defaults to 1 (opaque)
|
|
53 if output_pixel_vals:
|
|
54 channel_defaults = [0,0,0,255]
|
|
55 else:
|
|
56 channel_defaults = [0.,0.,0.,1.]
|
|
57
|
|
58 for i in xrange(4):
|
|
59 if X[i] is None:
|
|
60 out_array[:,:,i] = numpy.zeros(out_shape,
|
|
61 dtype='uint8' if output_pixel_vals else out_array.dtype
|
|
62 )+channel_defaults[i]
|
|
63 else:
|
|
64 out_array[:,:,i] = tile_raster_images(X[i], img_shape, tile_shape, tile_spacing, scale_rows_to_unit_interval, output_pixel_vals)
|
|
65 return out_array
|
|
66
|
|
67 else:
|
|
68 H, W = img_shape
|
|
69 Hs, Ws = tile_spacing
|
|
70
|
|
71 out_array = numpy.zeros(out_shape, dtype='uint8' if output_pixel_vals else X.dtype)
|
|
72 for tile_row in xrange(tile_shape[0]):
|
|
73 for tile_col in xrange(tile_shape[1]):
|
|
74 if tile_row * tile_shape[1] + tile_col < X.shape[0]:
|
|
75 if scale_rows_to_unit_interval:
|
|
76 this_img = scale_to_unit_interval(X[tile_row * tile_shape[1] + tile_col].reshape(img_shape))
|
|
77 else:
|
|
78 this_img = X[tile_row * tile_shape[1] + tile_col].reshape(img_shape)
|
|
79 out_array[
|
|
80 tile_row * (H+Hs):tile_row*(H+Hs)+H,
|
|
81 tile_col * (W+Ws):tile_col*(W+Ws)+W
|
|
82 ] \
|
|
83 = this_img * (255 if output_pixel_vals else 1)
|
|
84 return out_array
|
|
85
|
|
86
|