annotate AMT/utils.py @ 534:4d6493d171f6

added all sizes
author Yoshua Bengio <bengioy@iro.umontreal.ca>
date Tue, 01 Jun 2010 22:12:13 -0400
parents 116b2de2c0a4
children
rev   line source
396
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
1 """ This file contains different utility functions that are not connected
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
2 in anyway to the networks presented in the tutorials, but rather help in
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
3 processing the outputs into a more understandable way.
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
4
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
5 For example ``tile_raster_images`` helps in generating a easy to grasp
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
6 image from a set of samples or weights.
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
7 """
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
8
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
9
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
10 import numpy
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
11
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
12
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
13 def scale_to_unit_interval(ndar,eps=1e-8):
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
14 """ Scales all values in the ndarray ndar to be between 0 and 1 """
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
15 ndar = ndar.copy()
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
16 ndar -= ndar.min()
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
17 ndar *= 1.0 / (ndar.max()+eps)
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
18 return ndar
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
19
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
20
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
21 def tile_raster_images(X, img_shape, tile_shape,tile_spacing = (0,0),
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
22 scale_rows_to_unit_interval = True, output_pixel_vals = True):
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
23 """
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
24 Transform an array with one flattened image per row, into an array in
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
25 which images are reshaped and layed out like tiles on a floor.
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
26
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
27 This function is useful for visualizing datasets whose rows are images,
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
28 and also columns of matrices for transforming those rows
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
29 (such as the first layer of a neural net).
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
30
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
31 :type X: a 2-D ndarray or a tuple of 4 channels, elements of which can
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
32 be 2-D ndarrays or None;
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
33 :param X: a 2-D array in which every row is a flattened image.
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
34
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
35 :type img_shape: tuple; (height, width)
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
36 :param img_shape: the original shape of each image
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
37
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
38 :type tile_shape: tuple; (rows, cols)
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
39 :param tile_shape: the number of images to tile (rows, cols)
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
40
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
41 :param output_pixel_vals: if output should be pixel values (i.e. int8
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
42 values) or floats
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
43
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
44 :param scale_rows_to_unit_interval: if the values need to be scaled before
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
45 being plotted to [0,1] or not
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
46
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
47
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
48 :returns: array suitable for viewing as an image.
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
49 (See:`PIL.Image.fromarray`.)
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
50 :rtype: a 2-d array with same dtype as X.
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
51
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
52 """
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
53
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
54 assert len(img_shape) == 2
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
55 assert len(tile_shape) == 2
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
56 assert len(tile_spacing) == 2
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
57
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
58 # The expression below can be re-written in a more C style as
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
59 # follows :
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
60 #
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
61 # out_shape = [0,0]
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
62 # out_shape[0] = (img_shape[0]+tile_spacing[0])*tile_shape[0] -
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
63 # tile_spacing[0]
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
64 # out_shape[1] = (img_shape[1]+tile_spacing[1])*tile_shape[1] -
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
65 # tile_spacing[1]
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
66 out_shape = [(ishp + tsp) * tshp - tsp for ishp, tshp, tsp
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
67 in zip(img_shape, tile_shape, tile_spacing)]
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
68
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
69 if isinstance(X, tuple):
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
70 assert len(X) == 4
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
71 # Create an output numpy ndarray to store the image
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
72 if output_pixel_vals:
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
73 out_array = numpy.zeros((out_shape[0], out_shape[1], 4), dtype='uint8')
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
74 else:
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
75 out_array = numpy.zeros((out_shape[0], out_shape[1], 4), dtype=X.dtype)
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
76
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
77 #colors default to 0, alpha defaults to 1 (opaque)
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
78 if output_pixel_vals:
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
79 channel_defaults = [0,0,0,255]
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
80 else:
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
81 channel_defaults = [0.,0.,0.,1.]
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
82
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
83 for i in xrange(4):
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
84 if X[i] is None:
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
85 # if channel is None, fill it with zeros of the correct
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
86 # dtype
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
87 out_array[:,:,i] = numpy.zeros(out_shape,
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
88 dtype='uint8' if output_pixel_vals else out_array.dtype
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
89 )+channel_defaults[i]
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
90 else:
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
91 # use a recurrent call to compute the channel and store it
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
92 # in the output
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
93 out_array[:,:,i] = tile_raster_images(X[i], img_shape, tile_shape, tile_spacing, scale_rows_to_unit_interval, output_pixel_vals)
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
94 return out_array
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
95
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
96 else:
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
97 # if we are dealing with only one channel
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
98 H, W = img_shape
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
99 Hs, Ws = tile_spacing
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
100
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
101 # generate a matrix to store the output
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
102 out_array = numpy.zeros(out_shape, dtype='uint8' if output_pixel_vals else X.dtype)
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
103
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
104
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
105 for tile_row in xrange(tile_shape[0]):
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
106 for tile_col in xrange(tile_shape[1]):
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
107 if tile_row * tile_shape[1] + tile_col < X.shape[0]:
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
108 if scale_rows_to_unit_interval:
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
109 # if we should scale values to be between 0 and 1
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
110 # do this by calling the `scale_to_unit_interval`
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
111 # function
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
112 this_img = scale_to_unit_interval(X[tile_row * tile_shape[1] + tile_col].reshape(img_shape))
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
113 else:
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
114 this_img = X[tile_row * tile_shape[1] + tile_col].reshape(img_shape)
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
115 # add the slice to the corresponding position in the
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
116 # output array
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
117 out_array[
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
118 tile_row * (H+Hs):tile_row*(H+Hs)+H,
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
119 tile_col * (W+Ws):tile_col*(W+Ws)+W
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
120 ] \
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
121 = this_img * (255 if output_pixel_vals else 1)
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
122 return out_array
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
123
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
124
116b2de2c0a4 utils for the amazon MT code
goldfinger
parents:
diff changeset
125