comparison AMT/utils.py @ 396:116b2de2c0a4

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