436
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
1
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
2 import numpy
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
3
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
4
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
5 def make_weights_image(mat, xres, yres, i, j, nrow, ncol):
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
6 """
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
7 Displays the filters implemented by a weight matrix.
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
8
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
9 Each filter corresponds to a row of mat and will be represented
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
10 by a xres*yres image.
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
11
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
12 Units from i to j will be included in the picture.
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
13
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
14 The picture will have nrow rows of filters and ncol columns
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
15 of filters. Unused spots for filters will be filled with zeros.
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
16
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
17 The return value is a matrix suitable for display with
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
18 matplotlib's imshow.
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
19 """
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
20
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
21 assert j > i
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
22 n = j - i
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
23 result = numpy.zeros((ncol * xres, nrow * yres))
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
24 submat = mat[i:j]
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
25 for k, row in enumerate(submat):
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
26 x = (k % ncol)*xres
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
27 y = (k / ncol)*yres
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
28 entry = row.reshape((xres, yres))
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
29 lmin, lmax = numpy.min(entry), numpy.max(entry)
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
30 ldiff = lmax - lmin
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
31 #entry = (entry - lmin) / ldiff
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
32 result[x:x + xres, y:y + yres] = entry
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
33 return result.T
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
34
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
35
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
36
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
37
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
38
|
Olivier Breuleux <breuleuo@iro.umontreal.ca>
parents:
diff
changeset
|
39
|