Mercurial > ift6266
annotate transformations/testmod.py @ 59:8b9fca879543
La proportion de bruit a ete divisee par 5, ca donne une transformation beaucoup moins aggressive
author | SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca> |
---|---|
date | Mon, 08 Feb 2010 14:09:11 -0500 |
parents | 368f1907ad5a |
children | 22823acc1712 089f236759d9 |
rev | line source |
---|---|
16
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
1 # This script is to test your modules to see if they conform to the module API |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
2 # defined on the wiki. |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
3 import random, numpy, gc, time, math |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
4 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
5 # this is an example module that does stupid image value shifting |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
6 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
7 class DummyModule(object): |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
8 def get_settings_names(self): |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
9 return ['value'] |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
10 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
11 def regenerate_parameters(self, complexity): |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
12 self._value = random.gauss(0, 0.5*complexity) |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
13 return [self._value] |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
14 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
15 def transform_image(self, image): |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
16 return numpy.clip(image+self._value, 0, 1) |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
17 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
18 #import <your module> |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
19 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
20 # instanciate your class here (rather than DummyModule) |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
21 mod = DummyModule() |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
22 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
23 def error(msg): |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
24 print "ERROR:", msg |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
25 sys.exit(1) |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
26 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
27 def warn(msg): |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
28 print "WARNING:", msg |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
29 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
30 def timeit(f, lbl): |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
31 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
32 gc.disable() |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
33 t = time.time() |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
34 f() |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
35 est = time.time() - t |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
36 gc.enable() |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
37 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
38 loops = max(1, int(10**math.floor(math.log(10/est, 10)))) |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
39 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
40 gc.disable() |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
41 t = time.time() |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
42 for _ in xrange(loops): |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
43 f() |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
44 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
45 print lbl, "(", loops, "loops ):", (time.time() - t)/loops, "s" |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
46 gc.enable() |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
47 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
48 ######################## |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
49 # get_settings_names() # |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
50 ######################## |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
51 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
52 print "Testing get_settings_names()" |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
53 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
54 names = mod.get_settings_names() |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
55 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
56 if type(names) is not list: |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
57 error("Must return a list") |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
58 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
59 if len(names) == 0: |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
60 error("Must return at least one element") |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
61 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
62 if not all(type(e) is str for e in names): |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
63 warn("The elements of the list should be strings") |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
64 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
65 ########################### |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
66 # regenerate_parameters() # |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
67 ########################### |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
68 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
69 print "Testing regenerate_parameters()" |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
70 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
71 params = mod.regenerate_parameters(0.2) |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
72 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
73 if type(params) is not list: |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
74 error("Must return a list") |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
75 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
76 if len(params) != len(names): |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
77 error("the returned parameter list must have the same length as the number of parameters") |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
78 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
79 params2 = mod.regenerate_parameters(0.2) |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
80 if params == params2: |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
81 error("the complexity parameter determines the distribution of the parameters, not their value") |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
82 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
83 mod.regenerate_parameters(0.0) |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
84 mod.regenerate_parameters(1.0) |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
85 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
86 mod.regenerate_parameters(0.5) |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
87 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
88 ##################### |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
89 # transform_image() # |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
90 ##################### |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
91 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
92 print "Testing transform_image()" |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
93 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
94 imgr = numpy.random.random_sample((32, 32)).astype(numpy.float32) |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
95 img1 = numpy.ones((32, 32), dtype=numpy.float32) |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
96 img0 = numpy.zeros((32, 32), dtype=numpy.float32) |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
97 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
98 resr = mod.transform_image(imgr) |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
99 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
100 if type(resr) is not numpy.ndarray: |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
101 error("Must return an ndarray") |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
102 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
103 if resr.shape != (32, 32): |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
104 error("Must return 32x32 array") |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
105 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
106 if resr.dtype != numpy.float32: |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
107 error("Must return float32 array") |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
108 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
109 res1 = mod.transform_image(img1) |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
110 res0 = mod.transform_image(img0) |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
111 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
112 if res1.max() > 1.0 or res0.max() > 1.0: |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
113 error("Must keep array values between 0 and 1") |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
114 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
115 if res1.min() < 0.0 or res0.min() < 0.0: |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
116 error("Must keep array values between 0 and 1") |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
117 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
118 mod.regenerate_parameters(0.0) |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
119 mod.transform_image(imgr) |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
120 mod.regenerate_parameters(1.0) |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
121 mod.transform_image(imgr) |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
122 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
123 print "Bonus Stage: timings" |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
124 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
125 timeit(lambda: None, "empty") |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
126 timeit(lambda: mod.regenerate_parameters(0.5), "regenerate_parameters()") |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
127 timeit(lambda: mod.transform_image(imgr), "tranform_image()") |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
128 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
129 def f(): |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
130 mod.regenerate_parameters(0.2) |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
131 mod.transform_image(imgr) |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
132 |
368f1907ad5a
Script to test modules for conformance to the interface.
Arnaud Bergeron <abergeron@gmail.com>
parents:
diff
changeset
|
133 timeit(f, "regen and transform") |